unit U_YFZDList_MX;

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;

type
  TfrmYFZDList_MX = 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;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2ps: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2YFName: TcxGridDBColumn;
    v2P_Spec: TcxGridDBColumn;
    v2ComTaiTou: TcxGridDBColumn;
    Label4: TLabel;
    ComTaiTou: TEdit;
    Label5: TLabel;
    P_CodeName: TEdit;
    Label7: TLabel;
    FKTaitou: TEdit;
    v2FKTaitou: TcxGridDBColumn;
    Label8: TLabel;
    KHType: TComboBox;
    v2KHType: TcxGridDBColumn;
    FactoryName: TComboBox;
    v2ConNoHZ: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Label6: TLabel;
    ConNoHZ: TEdit;
    Label9: TLabel;
    Label10: TLabel;
    KPStatusH: TComboBox;
    KPType: TEdit;
    Label1: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    v2Ssel: TcxGridDBColumn;
    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 FormCreate(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v2SselPropertiesChange(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    { Public declarations }
    RKFlag,FCYID:String;

  end;

var
  frmYFZDList_MX: TfrmYFZDList_MX;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}



procedure TfrmYFZDList_MX.InitGrid();
var FMoney:double;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select Sum(Money*QtyFlag) yfkye ');
      sql.add('from YF_Money_CR A ');
      sql.add('where CRTime<'''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+'''');
      sql.add(' and CRtype in (''应付款登记'',''付款登记'') ');
      if FactoryName.Text<>'' then
      sql.add(' and FactoryName like '''+'%'+trim(FactoryName.Text)+'%'+'''');
      if ComTaiTou.Text<>'' then
      sql.add(' and ComTaiTou like '''+'%'+trim(ComTaiTou.Text)+'%'+'''');
      if P_CodeName.Text<>'' then
      sql.add(' and P_CodeName like '''+'%'+trim(P_CodeName.Text)+'%'+'''');
      if FKTaitou.Text<>'' then
      sql.add(' and FKTaitou like '''+'%'+trim(FKTaitou.Text)+'%'+'''');

      if ConNoHZ.Text<>'' then
      sql.add(' and [dbo].[F_Get_Order_SubStr]((select B.Mainid from JYOrder_Main B where B.OrderNo=A.OrderNo),''ConNoHZ'') like '''+'%'+trim(ConNoHZ.Text)+'%'+'''');
      if KPType.Text<>'' then
      sql.add(' and KPType like '''+'%'+trim(KPType.Text)+'%'+'''');
      if KPStatusH.Text<>'' then
      sql.add(' and (case when isnull(KPstatus,'''')='''' then ''未开票'' else ''开票完成'' end) like '''+'%'+trim(KPStatusH.Text)+'%'+'''');
      if KHType.Text<>'' then
      begin
        SQL.Add(' and (select Top 1 C.DEFstr5 from KH_Zdy_Attachment C where C.Type=''GYS'' and C.ZdyName=A.FactoryName) like '''+'%'+trim(KHType.Text)+'%'+'''');
      end;
      if cxTabControl1.TabIndex=0 then
      sql.Add(' and isnull(status,''0'')<>''0'' ');
      if cxTabControl1.TabIndex=1 then
      sql.Add(' and isnull(status,''0'')=''2'' ');
      open;
    end;
    FMoney:=ADOQueryTemp.fieldbyname('yfkye').asfloat;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select AA.* ');
      sql.add(' ,KHType=(select Top 1 C.DEFstr5 from KH_Zdy_Attachment C where C.Type=''GYS'' and C.ZdyName=AA.FactoryName)');
      sql.add(' from (select A.*,cast(Money as decimal(18,2)) yfk,cast(0 as decimal(18,2)) fk,cast(0 as decimal(18,2)) yfkye');
      sql.Add(',ConNoHZ=[dbo].[F_Get_Order_SubStr]((select B.Mainid from JYOrder_Main B where B.OrderNo=A.OrderNo),''ConNoHZ'')');
      SQL.ADD(' ,KPStatusH=(case when isnull(KPstatus,'''')='''' then ''未开票'' else ''开票完成'' end)');
      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=''应付款登记'' ');
      if cxTabControl1.TabIndex=0 then
      sql.Add(' and isnull(A.status,''0'')<>''0'' ');
      if cxTabControl1.TabIndex=1 then
      sql.Add(' and isnull(A.status,''0'')=''2'' ');
      sql.add(' union all');
      sql.Add(' select  A.*,cast(0 as decimal(18,2)) yfk,cast(Money as decimal(18,2)) fk,cast(0 as decimal(18,2)) yfkye');
      sql.Add(' ,ConNoHZ=cast('''' as varchar(50))');
      SQL.ADD(' ,KPStatusH=cast('''' as varchar(50))');
      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=''付款登记'' ');
      if cxTabControl1.TabIndex=0 then
      sql.Add(' and isnull(status,''0'')<>''0'' ');
      if cxTabControl1.TabIndex=1 then
      sql.Add(' and isnull(status,''0'')=''2'' ');
      sql.Add(' ) AA');
      sql.add(' where 1=1');
      if FactoryName.Text<>'' then
      sql.add(' and AA.FactoryName like '''+'%'+trim(FactoryName.Text)+'%'+'''');
      if ComTaiTou.Text<>'' then
      sql.add(' and AA.ComTaiTou like '''+'%'+trim(ComTaiTou.Text)+'%'+'''');
      if P_CodeName.Text<>'' then
      sql.add(' and AA.P_CodeName like '''+'%'+trim(P_CodeName.Text)+'%'+'''');
      if FKTaitou.Text<>'' then
      sql.add(' and AA.FKTaitou like '''+'%'+trim(FKTaitou.Text)+'%'+'''');
      if ConNoHZ.Text<>'' then
      sql.add(' and AA.ConNoHZ like '''+'%'+trim(ConNoHZ.Text)+'%'+'''');
      if KPType.Text<>'' then
      sql.add(' and AA.KPType like '''+'%'+trim(KPType.Text)+'%'+'''');
      if KPStatusH.Text<>'' then
      sql.add(' and AA.KPStatusH like '''+'%'+trim(KPStatusH.Text)+'%'+'''');
      if KHType.Text<>'' then
      begin
        SQL.Add(' and (select Top 1 C.DEFstr5 from KH_Zdy_Attachment C where C.Type=''GYS'' and C.ZdyName=AA.FactoryName) like '''+'%'+trim(KHType.Text)+'%'+'''');
      end;
      sql.Add('order by AA.CRTime');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    with CDS_HZ do
    begin
      append;
      fieldbyname('CRTime').Value:=begdate.DateTime;
      fieldbyname('yfkye').Value:=FMoney;
      post;
    end;
    CDS_HZ.DisableControls;
    with ADOQueryMain do
    begin
      first;
      while not eof do
      begin
        with CDS_HZ do
        begin
          append;
          fieldbyname('CRTime').Value:=ADOQueryMain.fieldbyname('CRTime').AsDateTime;
          fieldbyname('factoryName').Value:=ADOQueryMain.fieldbyname('factoryName').AsString;
          fieldbyname('FKTaitou').Value:=ADOQueryMain.fieldbyname('FKTaitou').AsString;
          fieldbyname('P_CodeName').Value:=ADOQueryMain.fieldbyname('P_CodeName').AsString;
          fieldbyname('KPStatusH').Value:=ADOQueryMain.fieldbyname('KPStatusH').AsString;
          fieldbyname('P_Color').Value:=ADOQueryMain.fieldbyname('P_Color').AsString;
          fieldbyname('P_Spec').Value:=ADOQueryMain.fieldbyname('P_Spec').AsString;
          fieldbyname('YFName').Value:=ADOQueryMain.fieldbyname('YFName').AsString;
          fieldbyname('KHType').Value:=ADOQueryMain.fieldbyname('KHType').AsString;
          FieldByName('ConNoHZ').Value:=ADOQueryMain.fieldbyname('ConNoHZ').AsString;
          FieldByName('KPType').Value:=ADOQueryMain.fieldbyname('KPType').AsString;
          FieldByName('KPStatus').Value:=ADOQueryMain.fieldbyname('KPStatus').AsString;
          fieldbyname('ps').Value:=ADOQueryMain.fieldbyname('ps').AsFloat;
          fieldbyname('Qty').Value:=ADOQueryMain.fieldbyname('Qty').AsFloat;
          fieldbyname('Price').Value:=ADOQueryMain.fieldbyname('Price').AsFloat;
          fieldbyname('yfk').Value:=ADOQueryMain.fieldbyname('yfk').AsFloat;
          fieldbyname('fk').Value:=ADOQueryMain.fieldbyname('fk').AsFloat;
          FMoney:=FMoney+ADOQueryMain.fieldbyname('Money').AsFloat*ADOQueryMain.fieldbyname('QtyFlag').AsFloat;
          fieldbyname('yfkye').Value:=FMoney;
          fieldbyname('note').Value:=ADOQueryMain.fieldbyname('note').AsString;
          fieldbyname('ComTaiTou').Value:=ADOQueryMain.fieldbyname('ComTaiTou').AsString;
          post;
        end;
        next;
      end;
    end;
    CDS_HZ.EnableControls;
    Label12.Caption:='0';
    Label13.Caption:='0';
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmYFZDList_MX.FormDestroy(Sender: TObject);
begin
  frmYFZDList_MX:=nil;
end;

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

procedure TfrmYFZDList_MX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv2,'财务管理1');
  Close;
end;
procedure TfrmYFZDList_MX.FormShow(Sender: TObject);
var fsj:string;
begin
  ReadCxGrid(self.Caption,Tv2,'财务管理1');
  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);
  //InitGrid();
end;

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

procedure TfrmYFZDList_MX.ToolButton2Click(Sender: TObject);
begin
  initgrid();
end;

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

procedure TfrmYFZDList_MX.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(self.Caption,cxgrid2);
//  SelExportData(Tv2,ADOQueryMain,'应付款付款统计列表');
end;

procedure TfrmYFZDList_MX.FormCreate(Sender: TObject);
begin
  cxGrid2.Align:=AlClient;
end;

procedure TfrmYFZDList_MX.cxTabControl1Change(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmYFZDList_MX.v2SselPropertiesChange(Sender: TObject);
begin
  toolbar1.SetFocus;
  HJ(CDS_HZ,Label12,CDS_HZ.fieldbyname('Qty').AsCurrency);
  HJ(CDS_HZ,Label13,CDS_HZ.fieldbyname('yfk').AsCurrency);
end;

end.
