unit U_KHSKChkList;

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

type
  TfrmKHSKChkList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    TBFind: TToolButton;
    Label1: TLabel;
    YFName: TEdit;
    ADOQueryMain: TADOQuery;
    TBExport: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    VYB: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    Label5: TLabel;
    PayMent: TEdit;
    v2Column2: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    Label6: TLabel;
    BZType: TComboBox;
    v2Column5: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    Label8: TLabel;
    GSNO: TEdit;
    Label9: TLabel;
    BKNO: TEdit;
    PopupMenu1: TPopupMenu;
    N6: TMenuItem;
    N7: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N1: TMenuItem;
    N2: TMenuItem;
    v2Column14: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    Label12: TLabel;
    ChkStatusP: TComboBox;
    v2Column17: TcxGridDBColumn;
    Panel2: TPanel;
    v2Column18: TcxGridDBColumn;
    v2Column19: TcxGridDBColumn;
    Panel3: TPanel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    v2Column20: TcxGridDBColumn;
    Label3: TLabel;
    Label7: TLabel;
    ConNo: TEdit;
    FactoryNameHZ: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure FactoryName10Change(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv2StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure N6Click(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure FactoryNameHZChange(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();

  public
    { Public declarations }

  end;

var
  frmKHSKChkList: TfrmKHSKChkList;

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

{$R *.dfm}



procedure TfrmKHSKChkList.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.BZType,A.YFName,A.Payment,A.Note,A.YFID,A.Money,A.ConNo ');
      sql.Add(',A.ChkerP,A.ChkTimeP,A.ChkStatusP');
      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 1 KHNameJC 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');
      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 isnull(ShiShouMoney,-99.999)<>-99.99');
      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 TfrmKHSKChkList.FormDestroy(Sender: TObject);
begin
  frmKHSKChkList:=nil;
end;

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

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

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

procedure TfrmKHSKChkList.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 TfrmKHSKChkList.FactoryName10Change(Sender: TObject);
begin
  TBFind.Click;
end;



procedure TfrmKHSKChkList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'收款审核');
end;

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

procedure TfrmKHSKChkList.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 TfrmKHSKChkList.Tv2StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id:Integer;
begin
  Id:=Tv2.GetColumnByFieldName('ChkStatusP').Index;
  if ARecord.Values[id]='已审核' then exit;
  AStyle:=DataLink_Money.SkyBlue;
end;

procedure TfrmKHSKChkList.N6Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ,True);
end;

procedure TfrmKHSKChkList.N7Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ,False);
end;

procedure TfrmKHSKChkList.N4Click(Sender: TObject);
var
  FDate:TDateTime;
begin
  if CDS_HZ.IsEmpty then Exit;
  if CDS_HZ.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  ToolBar1.SetFocus;
  try
    Panel2.Visible:=True;
    Panel2.Refresh;
    FDate:=SGetServerDateTime(ADOQueryTemp);
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while CDS_HZ.Locate('SSel',True,[]) do
      begin
        if Trim(CDS_HZ.fieldbyname('ChkStatusP').AsString)<>'已审核' then
        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('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; }
          with CDS_HZ do
          begin
            Edit;
            FieldByName('ChkStatusP').Value:='已审核';
            FieldByName('ChkerP').Value:=Trim(DName);
            FieldByName('ChkTimeP').Value:=FDate;
            FieldByName('SSel').Value:=False;
            Post;
          end;
        end else
        begin
          with CDS_HZ do
          begin
            Edit;
            FieldByName('SSel').Value:=False;
            Post;
          end;
        end;
      end;
    end;
    CDS_HZ.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    Panel2.Visible:=False;
  except
    Panel2.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmKHSKChkList.N5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if CDS_HZ.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  ToolBar1.SetFocus;
  try
    Panel2.Visible:=True;
    Panel2.Refresh;
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while CDS_HZ.Locate('SSel',True,[]) do
      begin
        if Trim(CDS_HZ.fieldbyname('ChkStatusP').AsString)<>'未审核' then
        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;
            FieldByName('ChkerP').Value:=Null;
            FieldByName('ChkTimeP').Value:=Null;
            FieldByName('ChkStatusP').Value:='未审核';
            Post;
          end;
          with CDS_HZ do
          begin
            Edit;
            FieldByName('ChkStatusP').Value:='未审核';
            FieldByName('ChkerP').Value:=Null;
            FieldByName('ChkTimeP').Value:=Null;
            FieldByName('SSel').Value:=False;
            Post;
          end;
        end else
        begin
          with CDS_HZ do
          begin
            Edit;
            FieldByName('SSel').Value:=False;
            Post;
          end;
        end;

      end;
    end;
    CDS_HZ.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    Panel2.Visible:=False;
  except
    Panel2.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmKHSKChkList.N1Click(Sender: TObject);
var
  FDate:TDateTime;
begin
  if CDS_HZ.IsEmpty then Exit;
  ToolBar1.SetFocus;
  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('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; }
    with CDS_HZ do
    begin
      Edit;
      FieldByName('ChkStatusP').Value:='已审核';
      FieldByName('ChkTimeP').Value:=FDate;
      FieldByName('ChkerP').Value:=Trim(DName);
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmKHSKChkList.N2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  ToolBar1.SetFocus;
  try
    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;
      FieldByName('ChkerP').Value:=Null;
      FieldByName('ChkTimeP').Value:=Null;
      FieldByName('ChkStatusP').Value:='未审核';
      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('ChkStatusP').Value:='未审核';
      FieldByName('ChkerP').Value:=Null;
      FieldByName('ChkTimeP').Value:=Null;
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

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

end.
