unit U_YSSKList_KP;

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;

type
  TfrmYSSKList_KP = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    V1FactoryName: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    FactoryName: TComboBox;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column2: TcxGridDBColumn;
    v2YFName: TcxGridDBColumn;
    v2factoryName: TcxGridDBColumn;
    v2ComTaiTou: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2P_Spec: TcxGridDBColumn;
    v2FKType: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2KPMoney: TcxGridDBColumn;
    v2Money: TcxGridDBColumn;
    v2QtyUnit: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2FKTaitou: TcxGridDBColumn;
    YForder_Sub: TClientDataSet;
    DataSource1: TDataSource;
    V1FKTaitou: TcxGridDBColumn;
    Label7: TLabel;
    FKTaitou: TEdit;
    V1RMBSQMoney: TcxGridDBColumn;
    Panel3: TPanel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    ComTaiTou: TEdit;
    YFName: TEdit;
    P_CodeName: TEdit;
    P_Spec: 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 ToolButton2Click(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TV1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure YFNameChange(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    fDParameters1:string;
    { Public declarations }
    RKFlag,FCYID,FFactoryName:String;

  end;

var
  frmYSSKList_KP: TfrmYSSKList_KP;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}


procedure TfrmYSSKList_KP.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.add('select AA.*,RMBKPWSK=(isnull(RMBSQKP,0)+isnull(RMBBQKP,0)-isnull(RMBFKMoney,0)-isnull(RMBSQMoney,0)) ');
      sql.add('From (select A.factoryName,A.FKTaitou');
      sql.add(',RMBSQKP=(select isnull(Sum(KPMoney),0) from YF_Money_CR B ');
      sql.add('where B.CRTime<'''+trim(formatdatetime('yyyy-MM-dd',begdate.DateTime))+'''');
      sql.add('and B.BZType<>''USD'' and B.BZType<>''$'' and B.factoryName=A.factoryName ');
      sql.add('and B.CRFlag=''应收收'' and CRType=''应收款发票登记'' and isnull(B.FKTaiTou,'''')=isnull(A.FKTaitou,''''))');
      sql.add(',RMBBQKP=(select isnull(Sum(KPMoney),0) from YF_Money_CR B ');
      sql.add('where B.CRTime>='''+trim(formatdatetime('yyyy-MM-dd',begdate.DateTime))+''' ');
      sql.add(' and B.CRTime<'''+trim(formatdatetime('yyyy-MM-dd',Enddate.DateTime))+''' ');
      sql.add('and B.factoryName=A.factoryName and isnull(B.FKTaiTou,'''')=isnull(A.FKTaitou,'''') and B.BZType<>''USD'' and B.BZType<>''$'' ');
      sql.add('and B.CRFlag=''应收收'' and CRType=''应收款发票登记'')');
      sql.add(',RMBFKMoney=(select isnull(Sum(Money),0)+isnull(Sum(SXMoney),0) from YF_Money_CR B ');
      sql.add('where B.CRTime>='''+trim(formatdatetime('yyyy-MM-dd',begdate.DateTime))+''' ');
      sql.add(' and B.CRTime<'''+trim(formatdatetime('yyyy-MM-dd',Enddate.DateTime))+''' ');
      sql.add(' and B.CRType=''收款登记'' and B.Status=''2'' and B.BZType<>''USD'' and B.BZType<>''$'' ');
      sql.add(' and B.factoryName=A.factoryName and isnull(B.FKTaiTou,'''')=isnull(A.FKTaitou,'''') and B.CRFlag=''应收收'' and B.YFName<>''坯布虚拟费'') ');
      sql.add(',RMBSQMoney=(select isnull(Sum(Money),0)+isnull(Sum(SXMoney),0) from YF_Money_CR B ');
      sql.add('where B.CRTime<'''+trim(formatdatetime('yyyy-MM-dd',begdate.DateTime))+''' ');
      sql.add(' and B.CRType=''收款登记'' and B.Status=''2'' and B.BZType<>''USD'' and B.BZType<>''$'' ');
      sql.add(' and B.factoryName=A.factoryName and isnull(B.FKTaiTou,'''')=isnull(A.FKTaitou,'''') and B.CRFlag=''应收收'' and B.YFName<>''坯布虚拟费'') ');
      sql.add('from (select distinct factoryName,FKTaitou from YF_Money_CR where CRFlag=''应收收'' and CRType=''应收款发票登记'')A )AA ');
      sql.add('where 1=1');
      if factoryName.Text<>'' then
      sql.add(' and AA.factoryName='''+Trim(factoryName.Text)+'''');
      if FKTaitou.Text<>'' then
      sql.add(' and AA.FKTaitou='''+trim(FKTaitou.Text)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmYSSKList_KP.FormDestroy(Sender: TObject);
begin
  frmYSSKList_KP:=nil;
end;

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

procedure TfrmYSSKList_KP.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应收收款统计',Tv2,'财务管理');
  WriteCxGrid('应收收款统计1',Tv2,'财务管理');
  Close;
end;
procedure TfrmYSSKList_KP.FormShow(Sender: TObject);
var fsj:string;
begin
  ReadCxGrid('应收收款统计',Tv2,'财务管理');
  ReadCxGrid('应收收款统计1',Tv2,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=Enddate.DateTime-7;
  fsj:='select distinct (A.FactoryName) name from YF_Money_CR A where A.CRType=''应付款登记'' ';
  SInitComBoxBySql(ADOQueryCmd,FactoryName,False,fsj);
  FactoryName.Text:=Trim(FFactoryName);
  InitGrid();
end;

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

procedure TfrmYSSKList_KP.ToolButton2Click(Sender: TObject);
var
  fsj:string;
begin
  initgrid();
end;

procedure TfrmYSSKList_KP.FactoryNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYSSKList_KP.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'应收收款统计列表');
end;

procedure TfrmYSSKList_KP.TV1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_HZ.IsEmpty then exit;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select AA.* ');
      sql.add(' from (select A.*');
      sql.Add(' from YF_Money_CR A');
      sql.Add(' where A.CRTime>='''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
      sql.Add(' and A.CRTime<'''+FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1)+''' ');
      sql.Add(' and A.CRType=''应收款发票登记'' ');
      sql.add(' union all');
      sql.Add(' select  A.*');
      sql.Add(' from YF_Money_CR A');
      sql.Add(' where A.CRTime>='''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
      sql.Add(' and A.CRTime<'''+FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1)+''' ');
      sql.Add(' and A.CRType=''收款登记'' ');
      sql.Add(' and A.status=''2'' ) AA');
      sql.add(' where AA.FactoryName='''+trim(CDS_HZ.fieldbyname('FactoryName').AsString)+'''');
      sql.add(' and isnull(AA.FKTaitou,'''')='''+trim(CDS_HZ.fieldbyname('FKTaitou').AsString)+'''');
      sql.add(' order by AA.CRtime');
      Open;
    end;
    SCreateCDS20(ADOQueryCmd,YForder_Sub);
    SInitCDSData20(ADOQueryCmd,YForder_Sub);
end;

procedure TfrmYSSKList_KP.YFNameChange(Sender: TObject);
begin
  //if YForder_Sub.IsEmpty then exit;
  SDofilter(ADOQueryCmd,Trim(SGetFilters(Panel3,1,2)));
  SCreateCDS20(ADOQueryCmd,YForder_Sub);
  SInitCDSData20(ADOQueryCmd,YForder_Sub);
end;

end.
