unit U_SKCRCYZZ;

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, cxDropDownEdit;

type
  TfrmSKCRCYZZ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    YFName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    FactoryName: TEdit;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    Label4: TLabel;
    OrderNo: TEdit;
    ToolButton5: TToolButton;
    Label8: TLabel;
    HZFactoryName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    VDate: TcxGridDBColumn;
    VFTYName: TcxGridDBColumn;
    VYFName: TcxGridDBColumn;
    VPayMent: TcxGridDBColumn;
    VYB: TcxGridDBColumn;
    VNote: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    Label9: TLabel;
    ComTaiTou: TEdit;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column13: 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 FactoryNameChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure VPayMentPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column9PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    canshu1:String;
    procedure InitGrid();

  public
    { Public declarations }
    RKFlag,FCYID:String;

  end;

var
  frmSKCRCYZZ: TfrmSKCRCYZZ;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp,U_ZHKHListNewCX;

{$R *.dfm}

procedure TfrmSKCRCYZZ.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select A.* ');
      sql.Add('  from YF_Money_CR A');
      sql.Add('where A.CRTime>=:begdate and A.CRTime<:Enddate and A.CRType=''收款登记'' ');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',begdate.DateTime));
      Parameters.ParamByName('Enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1));
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmSKCRCYZZ.FormDestroy(Sender: TObject);
begin
  frmSKCRCYZZ:=nil;
end;

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

procedure TfrmSKCRCYZZ.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('收款登记1',Tv2,'财务管理');
  Close;
end;
procedure TfrmSKCRCYZZ.TBDelClick(Sender: TObject);
var
  FBBMoney,CRID:string;
begin
  if CDS_HZ.IsEmpty then Exit;
  if CDS_HZ.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  {CDS_HZ.DisableControls;
  with CDS_HZ do
  begin
    First;
    while not Eof  do
    begin
      if FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
          sql.Add(' and isnull(Money,0)<>isnull(WFPMoney,0) ');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          Application.MessageBox('收款已被分配不能删除数据!','提示',0);
          Exit;
        end;
      end;
    end;
  end;
  CDS_HZ.EnableControls;}
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while Locate('SSel',True,[]) do
      begin
        if Trim(CDS_HZ.fieldbyname('YFID').AsString)<>'' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
            sql.Add(' and isnull(Money,0)<>isnull(WFPMoney,0) ');
            Open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            CDS_HZ.EnableControls;
            Application.MessageBox('收款已被分配不能删除数据!','提示',0);
            Exit;
          end;
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('delete YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
            ExecSQL;
          end;
        end;
        CDS_HZ.Delete;
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除异常！','提示',0);
  end;
end;

procedure TfrmSKCRCYZZ.FormShow(Sender: TObject);
begin
  ReadCxGrid('收款登记1',Tv2,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=SGetServerDateMBeg(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmSKCRCYZZ.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmSKCRCYZZ.FactoryNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;
procedure TfrmSKCRCYZZ.ToolButton3Click(Sender: TObject);
begin
  try
    frmZHKHListNewCX:=TfrmZHKHListNewCX.Create(Application);
    with frmZHKHListNewCX do
    begin
     
      DParameters1:='高权限';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Append;
          FieldByName('FactoryNo').Value:=Trim(Order_Main.fieldbyname('ZKID').AsString);
          FieldByName('FactoryName').Value:=Trim(Order_Main.fieldbyname('KHName').AsString);
          FieldByName('CRTime').Value:=SGetServerDate(ADOQueryTemp);
          Post;
        end;
        //Self.SaveData();
      end;
    end;
  finally
    frmZHKHListNewCX.Free;
  end;
end;

procedure TfrmSKCRCYZZ.cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SKYFName';
      flagname:='项目及摘要';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('YFCode').Value:=Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString);
          FieldByName('YFName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmSKCRCYZZ.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('收款列表',cxGrid2);
end;

procedure TfrmSKCRCYZZ.ToolButton5Click(Sender: TObject);
var
  maxId,FYFID:String;
begin
  if CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if CDS_HZ.Locate('SSel;Money',VarArrayOf([True,Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('客户实付金额不能为空!','提示',0);
    Exit;
  end;
  if CDS_HZ.Locate('SSel;BZType',VarArrayOf([True,Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('币种不能为空!','提示',0);
    Exit;
  end;
  if CDS_HZ.Locate('SSel;ComTaiTou',VarArrayOf([True,Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('公司抬头不能为空!','提示',0);
    Exit;
  end;
  if CDS_HZ.Locate('SSel;CRTime',VarArrayOf([True,Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('日期不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  ToolBar1.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while Locate('SSel',True,[])  do
      begin
        with ADOQueryTemp do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from YF_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
          Open;
        end;
        FYFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
        if Trim(FYFID)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxId,'SK','YF_Money_CR',4,1)=False then
          begin
            CDS_HZ.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取收款最大号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxId:=Trim(FYFID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from YF_Money_CR where YFID='''+Trim(maxId)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FYFID)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
          end;
          FieldByName('YFID').Value:=Trim(maxId);
          RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YF_Money_CR',3);
          FieldByName('YFType').Value:='手工录入';
          FieldByName('CRType').Value:='收款登记';
          FieldByName('CRFlag').Value:='应收收';
          FieldByName('QtyFlag').Value:=-1;
          FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
          Post;
        end;
        with CDS_HZ do
        begin
          Edit;
          FieldByName('YFId').Value:=Trim(maxId);
          FieldByName('SSel').Value:=False;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_CR Set WFPMoney=isnull(Money,0)-isnull(FPDJMoney,0)-isnull(FPYKMoney,0)');
          sql.Add('-isnull(FPQKMoney,0)-isnull(FPYJMoney,0)-isnull(FPKKMoney,0)');
          sql.Add('where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;

procedure TfrmSKCRCYZZ.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
end;

procedure TfrmSKCRCYZZ.VPayMentPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SKPayment';
      flagname:='支付方式';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('PayMent').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmSKCRCYZZ.v2Column5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GSTT';
      flagname:='公司抬头';
      V1ZdyStr1.Visible:=True;
      V1ZdyStr2.Visible:=True;
      V1ZdyStr3.Visible:=True;
      V1ZdyStr4.Visible:=True;
      V1ZdyStr1.Caption:='简称';
      V1ZdyStr2.Caption:='电话';
      V1ZdyStr3.Caption:='传真';
      V1ZdyStr4.Caption:='地址';
      frmZDYHelp.Align:=alClient;
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('ComTaiTou').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmSKCRCYZZ.v2Column9PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FName,RZMoney,BankFee,Money:string;
begin
  FName:=Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FName).Value:=mvalue;
    Post;
  end;
  if Trim(CDS_HZ.fieldbyname('RZMoney').AsString)='' then  RZMoney:='0'
  else  RZMoney:=CDS_HZ.fieldbyname('RZMoney').AsString;
  if Trim(CDS_HZ.fieldbyname('BankFee').AsString)='' then  BankFee:='0'
  else  BankFee:=CDS_HZ.fieldbyname('BankFee').AsString;
  if Trim(CDS_HZ.fieldbyname('Money').AsString)='' then  Money:='0'
  else  Money:=CDS_HZ.fieldbyname('Money').AsString;
  if FName<>'Money' then
  begin
    with CDS_HZ do
    begin
      Edit;
      FieldByName('Money').Value:=StrToFloat(RZMoney)+StrToFloat(BankFee);
      Post;
    end;
  end else
  begin
    if RZMoney<>'0' then
    begin
      with CDS_HZ do
      begin
        Edit;
        FieldByName('BankFee').Value:=StrToFloat(Money)-StrToFloat(RZMoney);
        Post;
      end;
      Exit;
    end;
    if BankFee<>'0' then
    begin
      with CDS_HZ do
      begin
        Edit;
        FieldByName('RZMoney').Value:=StrToFloat(Money)-StrToFloat(BankFee);
        Post;
      end;
    end;
  end;

end;

end.
