unit U_KHSKList;

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, BtnEdit, Menus, Buttons,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmKHSKList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    TBFind: TToolButton;
    Label1: TLabel;
    YFName: TEdit;
    ADOQueryMain: TADOQuery;
    TBExport: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    TBAdd: TToolButton;
    VYB: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    Label5: TLabel;
    PayMent: TEdit;
    v2Column2: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    TBEdit: TToolButton;
    Label6: TLabel;
    BZType: TComboBox;
    v2Column5: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    Label7: TLabel;
    ConNo: TEdit;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    Label8: TLabel;
    GSNO: TEdit;
    Label9: TLabel;
    BKNO: TEdit;
    v2Column14: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    Panel2: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    Image2: TImage;
    Label10: TLabel;
    Label11: TLabel;
    v2Column16: TcxGridDBColumn;
    v2Column17: TcxGridDBColumn;
    v2Column18: TcxGridDBColumn;
    v2Column19: TcxGridDBColumn;
    FactoryNameHZ: TEdit;
    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 TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Image2Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure v2Column10CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure FactoryNameHZChange(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();

  public
    { Public declarations }

  end;

var
  frmKHSKList: TfrmKHSKList;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_KHListSelJJ,U_RTFun,U_KHSKInPut,U_FjList;

{$R *.dfm}



procedure TfrmKHSKList.InitGrid();
var
  fsj:string;
begin
  fsj:=SGetWhereFinds(Panel1,1,2);
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add('SELECT AA.* FROM(');
      sql.Add(' select CRTime=Convert(varchar(7),A.CRTime,120),A.SKDate,A.FactoryNo,A.FactoryNoSub,A.BZType,A.YFName,A.Payment,A.Note,A.YFID,A.Money,A.ConNo ');
      sql.Add(',FactoryName=(select Top 1 KHNameJC from KH_Main KM where KM.KHNO=A.FactoryNo)');
      sql.Add(',HuMing=(select Top 1 HuMing from KH_Sub_Bank KSB where KSB.KHNOSub=A.FactoryNoSub)');
      sql.Add(',FactoryNameHZ=dbo.getpinyin((select Top 1 KHName from KH_Main KM where KM.KHNO=A.FactoryNo))+(select Top 1KHNameJC from KH_Main KM where KM.KHNO=A.FactoryNo)+ ');
      sql.Add('isnull((select TOp 1 HuMing from KH_Sub_Bank KSB where KSB.KHNOSub=A.FactoryNoSub),'''')');
      sql.Add(' +A.FactoryNo+isnull(FactoryNoSub,'''')+dbo.getpinyin(isnull((select Top 1 HuMing from KH_Sub_Bank KSB where KSB.KHNOSub=A.FactoryNoSub),''''))');
      sql.Add(',SDQty=(select Count(*) from TP_File TF where TF.WBID=A.YFID and TF.TFType=''收款管理''  )');
      sql.Add(',Case when isnull(BZType,'''')=''￥'' then ShiShouMoney else Null end as MoneyRMB ');
      sql.Add(',Case when isnull(BZType,'''')=''$'' then ShiShouMoney else Null end as MoneyMJ ');
      sql.Add(',B.BankNo,B.BankName,B.HuMingJC,A.GSNO,A.BKNO,A.ShuidanMoney,A.ShouXuMoney,A.JSMoney,A.Filler,A.FillTime');
      sql.Add('  from YS_Money_CR A');
      sql.Add(' left join KH_Sub_Bank B on A.BKNO=B.BKNO');
      sql.Add(' left join KH_Main C on B.KHMainId=C.KHMainId');
      if CheckBox1.Checked then
      begin
        sql.Add('where A.SKDate>='''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
        sql.Add(' and SKDate<'''+FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1)+''' ');
      end else
      begin
        sql.Add('where A.SKDate>=''2000-01-01'' ');
        sql.Add(' and SKDate<''2500-01-01'' ');
      end;
      sql.Add(' and A.CRType=''收款登记'')AA where 1=1 ');
      SQL.Add(fsj);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;


procedure TfrmKHSKList.FormDestroy(Sender: TObject);
begin
  frmKHSKList:=nil;
end;

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

procedure TfrmKHSKList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('客户收款列表',Tv2,'财务管理');
  Close;
end;
procedure TfrmKHSKList.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 TfrmKHSKList.FormShow(Sender: TObject);
begin
  ReadCxGrid('客户收款列表',Tv2,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=StrToDate(FormatDateTime('yyyy',Enddate.Date)+'-01-01');
  InitGrid();
end;

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

procedure TfrmKHSKList.TBFindClick(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 TfrmKHSKList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'收款列表');
end;

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

procedure TfrmKHSKList.TBAddClick(Sender: TObject);
begin
  Panel2.Visible:=True;
  
end;

procedure TfrmKHSKList.TBEditClick(Sender: TObject);
begin
  try
    frmKHSKInPut:=TfrmKHSKInPut.Create(Application);
    with frmKHSKInPut do
    begin
      FMainId:=Trim(Self.CDS_HZ.fieldbyname('YFID').AsString);
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmKHSKInPut.Free;
  end;
end;

procedure TfrmKHSKList.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  FName:string;
begin
  FName:=Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if Trim(FName)='SDQty' then
  begin
    if CDS_HZ.IsEmpty then Exit;
    try
      frmFjList:=TfrmFjList.Create(Application);
      with frmFjList do
      begin
        fkeyNO:=Trim(CDS_HZ.fieldbyname('YFID').AsString);
        fType:='收款管理';
        fstatus:=0;
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmFjList.Free;
    end;
  end;
end;

procedure TfrmKHSKList.Image2Click(Sender: TObject);
begin
  Panel2.Visible:=False;
end;

procedure TfrmKHSKList.SpeedButton1Click(Sender: TObject);
begin
  Panel2.Visible:=False;
  try
    frmKHSKInPut:=TfrmKHSKInPut.Create(Application);
    with frmKHSKInPut do
    begin
      FMainId:='';
      BZType.ItemIndex:=BZType.Items.IndexOf(TSpeedButton(Sender).Caption);
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmKHSKInPut.Free;
  end;  
end;

procedure TfrmKHSKList.v2Column10CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id,id10:Integer;
begin
  Id:=Tv2.GetColumnByFieldName('ShuidanMoney').Index;
  id10:=Tv2.GetColumnByFieldName('MoneyMJ').Index;
  if AViewInfo.GridRecord.Values[Id]>0 then
  begin
    if AViewInfo.GridRecord.Values[Id10]=Null then
    begin
      ACanvas.Brush.Color:=clRed;
    end;
  end;
end;

procedure TfrmKHSKList.FactoryNameHZChange(Sender: TObject);
begin
  TBFind.Click;
end;

end.
