unit U_YFCRSaveQC;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP,ShellAPI,IniFiles, cxCheckBox, cxCalendar,
  cxButtonEdit, cxTextEdit, cxPC, cxDropDownEdit;

type
  TfrmYFCRSaveQC = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    FactoryName10: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2CRTime: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    VYB: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Note: TcxGridDBColumn;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FactoryName10Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure v2Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);

  private
    { Private declarations }

    procedure InitGrid();
    function  SaveData():Boolean;
  public
    fFlag:integer;
    { Public declarations }
    RKFlag,FCYID,fmanage:String;

  end;

var
  frmYFCRSaveQC: TfrmYFCRSaveQC;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_GYSListSelJJ,U_Fun;

{$R *.dfm}
procedure TfrmYFCRSaveQC.InitGrid();
begin
  ToolBar1.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered:=False;
      sql.Add(' select A.*,QtyStr=dbo.[F_Get_ShuZiStr](A.Qty),MoneyStr=dbo.[F_Get_ShuZiStr](A.Money) ');
      sql.Add(',FactoryName10=dbo.getPinYin(A.FactoryName)+RTrim(A.FactoryName)');
      sql.Add(',KHName=(select KHNameJC from KH_Main KM where KM.KHNO=A.FactoryNo)');
      sql.Add(' from YS_Money_CR A');
      sql.Add(' where A.CRType=''期初应付款登记''  ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;


procedure TfrmYFCRSaveQC.FormDestroy(Sender: TObject);
begin
  frmYFCRSaveQC:=nil;
end;

procedure TfrmYFCRSaveQC.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmYFCRSaveQC.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应付款登记YXQC',Tv2,'财务管理');
  Close;
end;
procedure TfrmYFCRSaveQC.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add(' insert into YS_Money_CR_DelLog ');
      sql.Add(' select * from YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      sql.Add(' Update YS_Money_CR_DelLog Set DelTime=getdate(),DelPerson='''+Trim(DName)+'''');
      sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      sql.Add(' delete YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_HZ.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除失败!','提示',0);
  end;
end;

procedure TfrmYFCRSaveQC.FormShow(Sender: TObject);
begin
  ReadCxGrid('应付款登记YXQC',Tv2,'财务管理');
  InitGrid();
end;


procedure TfrmYFCRSaveQC.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmYFCRSaveQC.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,CDS_HZ);
  SInitCDSData20(ADOQueryMain,CDS_HZ);
end;

procedure TfrmYFCRSaveQC.FactoryName10Change(Sender: TObject);
begin
  ToolButton2.Click;
end;

function TfrmYFCRSaveQC.SaveData():Boolean;
var
  maxId,CRID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'YF','YS_Money_CR',4,1)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_KC where FactoryName='''+Trim(CDS_HZ.fieldbyname('FactoryName').AsString)+''' ');
      sql.Add(' and KCType=''GYS'' ');
      Open;
    end;
    if ADOQueryCmd.IsEmpty=False then
    begin
      CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YS_Money_CRID set CRID=CRID+1');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
      CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YS_Money_KC where 1<>1');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('CRID').Value:=StrToInt(CRID);
        FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
        FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
        FieldByName('ZdyStr1').Value:='应付付';
        Post;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value:=Trim(maxId);
      FieldByName('CRID').Value:=StrToInt(CRID);
      RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YS_Money_CR',7);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('CRType').Value:='期初应付款登记';
      FieldByName('CRFlag').Value:='应付付';
      FieldByName('QtyFlag').Value:=1;
      FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
      FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
      FieldByName('CRTime').Value:=formatdateTIme('yyyy-MM-dd',CDS_HZ.fieldbyname('CRTime').AsDateTime);
      FieldByName('HSMoney').Value:=CDS_HZ.fieldbyname('Money').Value;
      FieldByName('JSMoney').Value:=CDS_HZ.fieldbyname('Money').Value;
      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('YFId').Value:=Trim(maxId);
      FieldByName('CRID').Value:=StrToInt(CRID);
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
end;

procedure TfrmYFCRSaveQC.ToolButton3Click(Sender: TObject);
begin
  try
    frmGYSListSelJJ:=TfrmGYSListSelJJ.Create(Application);
    with frmGYSListSelJJ do
    begin
      frmGYSListSelJJ.canshu2:='高权限';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Append;
          FieldByName('FactoryNo').Value:=Trim(frmGYSListSelJJ.CDS_HZ.fieldbyname('KHNO').AsString);
          FieldByName('FactoryName').Value:=Trim(frmGYSListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
          FieldByName('CRTime').Value:='2020-05-01';
          FieldByName('KPType').Value:='开票';
          Post;
        end;
        Self.SaveData();
      end;
    end;
  finally
    frmGYSListSelJJ.Free;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmYFCRSaveQC.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'期初应付款列表');
end;

procedure TfrmYFCRSaveQC.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmYFCRSaveQC.ToolButton5Click(Sender: TObject);
var
  FDate:TDateTime;
begin
  if CDS_HZ.IsEmpty then Exit;
  ToolBar1.SetFocus;
  if Application.MessageBox('确定要保存当前选中的数据吗？','提示',32+4)<>IDYES then Exit;
  try
    FDate:=SGetServerDateTime(ADOQueryTemp);
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YS_Money_CR',7);
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('EditTime').Value:=FDate ;
      FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
      FieldByName('ChkerP').Value:=Trim(DName);
      FieldByName('ChkTimeP').Value:=FDate;
      FieldByName('ChkStatusP').Value:='已审核';
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YS_Money_CR Set ');
      sql.Add('HSMoney=Money');
      sql.Add(',JSMoney=Money');
      sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmYFCRSaveQC.ToolButton6Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  ToolBar1.SetFocus;
  if Application.MessageBox('确定要批量保存当前页面的所有数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Edit;
          RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YS_Money_CR',7);
          FieldByName('Editer').Value:=Trim(DName);
          FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YS_Money_CR Set ');
          sql.Add('HSMoney=Money');
          sql.Add(',JSMoney=Money');
          sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
        Next;
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmYFCRSaveQC.v2Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGYSListSelJJ:=TfrmGYSListSelJJ.Create(Application);
    with frmGYSListSelJJ do
    begin
      frmGYSListSelJJ.canshu2:='高权限';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('FactoryNo').Value:=Trim(frmGYSListSelJJ.CDS_HZ.fieldbyname('KHNO').AsString);
          FieldByName('FactoryName').Value:=Trim(frmGYSListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
          FieldByName('KHName').Value:=Trim(frmGYSListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
          Post;
        end;
      end;
    end;
  finally
    frmGYSListSelJJ.Free;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

end.


