unit U_YSZDList_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, BtnEdit, RM_e_Xls, cxPC;

type
  TfrmYSZDList_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;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Label1: TLabel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2CXDate: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2ComTaiTou: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2P_Spec: TcxGridDBColumn;
    v2YFName: TcxGridDBColumn;
    v2ps: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2FeeType: TcxGridDBColumn;
    Label4: TLabel;
    YFName: TEdit;
    Label5: TLabel;
    FeeType: TEdit;
    v2ConNo: TcxGridDBColumn;
    v2OrderNO: TcxGridDBColumn;
    v2FKType: TcxGridDBColumn;
    Label7: TLabel;
    ComTaiTou: TEdit;
    Label8: TLabel;
    ConNo: TEdit;
    Label6: TLabel;
    P_CodeName: TEdit;
    Label10: TLabel;
    Label12: TLabel;
    v2Ssel: TcxGridDBColumn;
    Label9: TLabel;
    Label11: TLabel;
    v2USDyfk: TcxGridDBColumn;
    v2USDfk: TcxGridDBColumn;
    v2USDyfkye: TcxGridDBColumn;
    TPrint: TToolButton;
    Order_Print: TClientDataSet;
    Panel2: TPanel;
    Label13: TLabel;
    Label14: TLabel;
    BankNo: TEdit;
    BankName: TBtnEditA;
    RMXLSExport1: TRMXLSExport;
    FactoryName: TComboBox;
    Label15: TLabel;
    HName: TBtnEditA;
    v2QtyUnit: TcxGridDBColumn;
    v2KPNO: TcxGridDBColumn;
    Label16: TLabel;
    Label17: TLabel;
    v2BZType: TcxGridDBColumn;
    Label18: TLabel;
    KPNO: TEdit;
    cxTabControl1: TcxTabControl;
    v2KPStatusH: TcxGridDBColumn;
    Label19: TLabel;
    KPStatusH: TComboBox;
    Label23: TLabel;
    Note: 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 FormCreate(Sender: TObject);
    procedure v2SselPropertiesChange(Sender: TObject);
    procedure TPrintClick(Sender: TObject);
    procedure BankNameBtnClick(Sender: TObject);
    procedure HNameBtnClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    { Public declarations }
    RKFlag,FCYID:String;

  end;

var
  frmYSZDList_MX: TfrmYSZDList_MX;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}

procedure TfrmYSZDList_MX.InitGrid();
var FMoney,FUSDMoney:double;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select Sum(yfkye) yfkye,sum(USDyfkye) USDyfkye ');
      sql.add('from (select yfkye=(case when BZType<>''USD'' and BZType<>''$'' then Sum((Money+isnull(SXMoney,0))*QtyFlag) else 0 end) ');
      sql.add(',USDyfkye=(case when BZType<>''RMB'' and BZType<>''￥'' then Sum((Money+isnull(SXMoney,0))*QtyFlag) else 0 end) ');
      sql.add('from YF_Money_CR ');
      sql.add('where isnull(SJFHDate,CRTime)<'''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+'''');
      sql.add(' and CRtype in (''应收款登记'',''收款登记'') and isnull(FeeType,'''')<>''虚拟费用''');
      if FactoryName.Text<>'' then
      sql.add(' and FactoryName='''+trim(FactoryName.Text)+'''');
      if YFName.Text<>'' then
      sql.add(' and YFName like '''+'%'+trim(YFName.Text)+'%'+'''');
      if FeeType.Text<>'' then
      sql.add(' and FeeType like '''+'%'+trim(FeeType.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 ConNO.Text<>'' then
      sql.add(' and ConNO like '''+'%'+trim(ConNO.Text)+'%'+'''');
      if KPNO.Text<>'' then
      sql.add(' and KPNO like '''+'%'+trim(KPNO.Text)+'%'+'''');
      if Note.Text<>'' then
      sql.add(' and Note like '''+'%'+trim(Note.Text)+'%'+'''');
      if KPstatusH.Text<>'' then
      sql.add(' and (case when isnull(KPstatus,'''')='''' then ''未开票'' else ''开票完成'' end) like '''+'%'+trim(KPstatusH.Text)+'%'+'''');
      if cxTabControl1.TabIndex=0 then
      sql.Add(' and isnull(status,''0'')<>''0''')
      else
      sql.add(' and isnull(status,'''')=''2''');
      sql.add(' group by BZType) AA');
      open;
    end;
    FMoney:=ADOQueryTemp.fieldbyname('yfkye').asfloat;
    FUSDMoney:=ADOQueryTemp.fieldbyname('USDyfkye').asfloat;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select P_CodeName=isnull((select Top 1 E.PrtCodeName from JYOrderCon_Sub E where E.Subid=AA.ConID),AA.P_CodeName)');
      SQL.ADD(' ,KPStatusH=(case when isnull(KPstatus,'''')='''' then ''未开票'' else ''开票完成'' end)');
      sql.add(' ,P_Color=isnull(AA.P_Color,(select Top 1 isnull(E.PRTColor,E.SOrdDefStr4) from JYOrderCon_Sub E where E.Subid=AA.ConID))');
      sql.add(' ,P_Spec=Isnull((select Top 1 E.prtspec from JYOrderCon_Sub E where E.Subid=AA.ConID),P_Spec)');
      sql.add(' ,ConNo=isnull((select Top 1 F.ConNo from JYOrderCon_Sub E inner join JYOrderCon_Main F on E.Mainid=F.Mainid where E.Subid=AA.ConID),AA.ConNO)');
      sql.add(' ,OrderNO=isnull(AA.OrderNo,(select Top 1 E.OrderNo from JYOrder_Main E where E.Mainid=AA.Mainid))');
      sql.add(' ,AA.* ');
      sql.add(' from (select A.*,yfk=(case when A.BZType<>''USD'' and A.BZType<>''$'' then cast(Money as decimal(18,2)) else 0 end)');
      sql.add(',USDyfk=(case when A.BZType<>''RMB'' and A.BZType<>''￥'' then cast(Money as decimal(18,2)) else 0 end)');
      sql.add(',fk=cast(0 as decimal(18,2)),USDfk=cast(0 as decimal(18,2)),isnull(A.SJFHDate,A.CRTime) CXDate');
      sql.add(',cast(0 as decimal(18,2)) yfkye,cast(0 as decimal(18,2)) USDyfkye');
      sql.add(',RMBMoney=(case when A.BZType<>''USD'' and A.BZType<>''$'' then cast(Money as decimal(18,2)) else 0 end)');
      sql.add(',USDMoney=(case when A.BZType<>''RMB'' and A.BZType<>''￥'' then cast(Money as decimal(18,2)) else 0 end)');
      sql.Add(' from YF_Money_CR A');
      sql.Add(' where isnull(A.SJFHDate,A.CRTime)>='''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
      sql.Add(' and isnull(A.SJFHDate,A.CRTime)<'''+FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1)+''' ');
      sql.Add(' and A.CRType=''应收款登记'' ');
      sql.Add(' and isnull(YFName,'''')<>''坯布虚拟费''');
      sql.add(' union all');
      sql.Add(' select A.*,cast(0 as decimal(18,2)) yfk,cast(0 as decimal(18,2)) USDyfk ');
      sql.add(',fk=(case when A.BZType<>''USD'' and A.BZType<>''$'' then cast(KHDKMoney as decimal(18,2)) else 0 end)');
      sql.add(',USDfk=(case when A.BZType<>''RMB'' and A.BZType<>''￥'' then cast(KHDKMoney as decimal(18,2)) else 0 end)');
      sql.add(',A.CRTime CXDate,cast(0 as decimal(18,2)) yfkye,cast(0 as decimal(18,2)) USDyfkye');
      sql.add(',RMBMoney=(case when A.BZType<>''USD'' and A.BZType<>''$'' then cast(KHDKMoney as decimal(18,2)) else 0 end)');
      sql.add(',USDMoney=(case when A.BZType<>''RMB'' and A.BZType<>''￥'' then cast(KHDKMoney as decimal(18,2)) else 0 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=''收款登记'' ) AA');
      sql.add(' where 1=1');
      if FactoryName.Text<>'' then
      sql.add(' and AA.FactoryName='''+trim(FactoryName.Text)+'''');
      if YFName.Text<>'' then
      sql.add(' and AA.YFName like '''+'%'+trim(YFName.Text)+'%'+'''');
      if FeeType.Text<>'' then
      sql.add(' and AA.FeeType like '''+'%'+trim(FeeType.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 ConNO.Text<>'' then
      sql.add(' and AA.ConNO like '''+'%'+trim(ConNO.Text)+'%'+'''');
      if Note.Text<>'' then
      sql.add(' and Note like '''+'%'+trim(Note.Text)+'%'+'''');
      if KPNO.Text<>'' then
      sql.add(' and AA.KPNO like '''+'%'+trim(KPNO.Text)+'%'+'''');
      if KPstatusH.Text<>'' then
      sql.add(' and (case when isnull(AA.KPstatus,'''')='''' then ''未开票'' else ''开票完成'' end) like '''+'%'+trim(KPstatusH.Text)+'%'+'''');
      if cxTabControl1.TabIndex=0 then
      sql.Add(' and isnull(status,''0'')<>''0''')
      else
      sql.add(' and AA.status=''2''');
      sql.Add('order by isnull(AA.SJFHDate,AA.CRTime)');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    with CDS_HZ do
    begin
      append;
      fieldbyname('Ssel').AsBoolean:=false;
      fieldbyname('CXDate').Value:=begdate.DateTime;
      fieldbyname('yfkye').Value:=FMoney;
      fieldbyname('USDyfkye').Value:=FUSDMoney;
      post;
    end;
    CDS_HZ.DisableControls;
    with ADOQueryMain do
    begin
      first;
      while not eof do
      begin
        with CDS_HZ do
        begin
          append;
          fieldbyname('Ssel').AsBoolean:=false;
          fieldbyname('CXDate').Value:=ADOQueryMain.fieldbyname('CXDate').AsDateTime;
          fieldbyname('factoryName').Value:=ADOQueryMain.fieldbyname('factoryName').AsString;
          fieldbyname('ConNO').Value:=ADOQueryMain.fieldbyname('ConNO').AsString;
          if ADOQueryMain.fieldbyname('P_CodeName').AsString<>'' then
          fieldbyname('P_CodeName').Value:=ADOQueryMain.fieldbyname('P_CodeName').AsString
          else
          fieldbyname('P_CodeName').Value:=ADOQueryMain.fieldbyname('FKTaiTou').AsString;
          fieldbyname('OrderNO').Value:=ADOQueryMain.fieldbyname('OrderNO').AsString;
          fieldbyname('KPNO').Value:=ADOQueryMain.fieldbyname('KPNO').AsString;
          fieldbyname('KPStatusH').Value:=ADOQueryMain.fieldbyname('KPStatusH').AsString;
          fieldbyname('P_Color').Value:=ADOQueryMain.fieldbyname('P_Color').AsString;
          fieldbyname('BZType').Value:=ADOQueryMain.fieldbyname('BZType').AsString;
          fieldbyname('Money').Value:=ADOQueryMain.fieldbyname('Money').AsCurrency;
          fieldbyname('QtyFlag').Value:=ADOQueryMain.fieldbyname('QtyFlag').AsCurrency;
          fieldbyname('P_Spec').Value:=ADOQueryMain.fieldbyname('P_Spec').AsString;
          fieldbyname('YFName').Value:=ADOQueryMain.fieldbyname('YFName').AsString;
          fieldbyname('Qtyunit').Value:=ADOQueryMain.fieldbyname('Qtyunit').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;
          fieldbyname('USDyfk').Value:=ADOQueryMain.fieldbyname('USDyfk').AsFloat;
          fieldbyname('USDfk').Value:=ADOQueryMain.fieldbyname('USDfk').AsFloat;
          FMoney:=FMoney+ADOQueryMain.fieldbyname('RMBMoney').AsFloat*ADOQueryMain.fieldbyname('QtyFlag').AsFloat;
          FUSDMoney:=FUSDMoney+ADOQueryMain.fieldbyname('USDMoney').AsFloat*ADOQueryMain.fieldbyname('QtyFlag').AsFloat;
          fieldbyname('yfkye').Value:=FMoney;
          fieldbyname('USDyfkye').Value:=FUSDMoney;
          fieldbyname('note').Value:=ADOQueryMain.fieldbyname('note').AsString;
          fieldbyname('FKType').Value:=ADOQueryMain.fieldbyname('FKType').AsString;
          fieldbyname('YFName').Value:=ADOQueryMain.fieldbyname('YFName').AsString;
          fieldbyname('FeeType').Value:=ADOQueryMain.fieldbyname('FeeType').AsString;
          fieldbyname('ComTaiTou').Value:=trim(ADOQueryMain.fieldbyname('ComTaiTou').AsString);
          post;
        end;
        next;
      end;
    end;
    CDS_HZ.EnableControls;
  finally;
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmYSZDList_MX.FormDestroy(Sender: TObject);
begin
  frmYSZDList_MX:=nil;
end;

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

procedure TfrmYSZDList_MX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv2,'财务管理2');
  Close;
end;
procedure TfrmYSZDList_MX.FormShow(Sender: TObject);
var fsj:string;
begin
  ReadCxGrid(self.Caption,Tv2,'财务管理2');
  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 TfrmYSZDList_MX.TBRafreshClick(Sender: TObject);
begin
  Toolbar1.SetFocus;
  Label12.Caption:='0';
  Label11.Caption:='0';
  Label17.Caption:='0';
  InitGrid();
end;

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

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

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

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

procedure TfrmYSZDList_MX.v2SselPropertiesChange(Sender: TObject);
begin
  toolBar1.SetFocus;
  HJ(CDS_HZ,Label12,CDS_HZ.fieldbyname('ps').AsCurrency);
  HJ(CDS_HZ,Label11,CDS_HZ.fieldbyname('Qty').AsCurrency);
  HJ(CDS_HZ,Label17,CDS_HZ.fieldbyname('Money').AsCurrency*CDS_HZ.fieldbyname('QtyFlag').AsCurrency);
end;

procedure TfrmYSZDList_MX.TPrintClick(Sender: TObject);
var fPrintFile,FFactoryName,FComTaiTou,FSQNo:string;
    FSQMoney:double;
    FMoney,FUSDMoney:double;
begin
  if CDS_HZ.IsEmpty then exit;
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select Sum(yfkye) yfkye,sum(USDyfkye) USDyfkye ');
      sql.add('from (select yfkye=(case when BZType<>''USD'' and BZType<>''$'' then Sum(Money*QtyFlag) else 0 end) ');
      sql.add(',USDyfkye=(case when BZType<>''RMB'' and BZType<>''￥'' then Sum(Money*QtyFlag) else 0 end) ');
      sql.add('from YF_Money_CR ');
      sql.add('where CRTime<'''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+'''');
      sql.add(' and CRtype in (''应收款登记'',''收款登记'') and isnull(FeeType,'''')<>''虚拟费用''');
      if FactoryName.Text<>'' then
      sql.add(' and FactoryName like '''+'%'+trim(FactoryName.Text)+'%'+'''');
      if YFName.Text<>'' then
      sql.add(' and YFName like '''+'%'+trim(YFName.Text)+'%'+'''');
      if FeeType.Text<>'' then
      sql.add(' and FeeType like '''+'%'+trim(FeeType.Text)+'%'+'''');
      if ComTaiTou.Text<>'' then
      sql.add(' and ComTaiTou like '''+'%'+trim(ComTaiTou.Text)+'%'+'''');
      sql.add(' and status=''2''');
      sql.add(' group by BZType) AA');
      open;
    end;
    FMoney:=ADOQueryTemp.fieldbyname('yfkye').asfloat;
    FUSDMoney:=ADOQueryTemp.fieldbyname('USDyfkye').asfloat;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select P_CodeName=isnull((select Top 1 E.PrtCodeName from JYOrderCon_Sub E where E.Subid=AA.ConID),AA.P_CodeName)');
      sql.add(' ,P_Color=(select Top 1 isnull(E.PRTColor,E.SOrdDefStr4) from JYOrderCon_Sub E where E.Subid=AA.ConID)');
      sql.add(' ,P_Spec=Isnull((select Top 1 E.prtspec from JYOrderCon_Sub E where E.Subid=AA.ConID),P_Spec)');
      sql.add(' ,ConNo=isnull((select Top 1 F.ConNo from JYOrderCon_Sub E inner join JYOrderCon_Main F on E.Mainid=F.Mainid where E.Subid=AA.ConID),AA.ConNO)');
      sql.add(' ,OrderNO=isnull(AA.OrderNo,(select Top 1 E.OrderNo from JYOrder_Main E where E.Mainid=AA.Mainid))');
      sql.add(' ,AA.* ');
      sql.add(' from (select A.*,yfk=(case when A.BZType<>''USD'' and A.BZType<>''$'' then cast(Money as decimal(18,2)) else 0 end)');
      sql.add(',USDyfk=(case when A.BZType<>''RMB'' and A.BZType<>''￥'' then cast(Money as decimal(18,2)) else 0 end)');
      sql.add(',fk=cast(0 as decimal(18,2)),USDfk=cast(0 as decimal(18,2))');
      sql.add(',cast(0 as decimal(18,2)) yfkye,cast(0 as decimal(18,2)) USDyfkye');
      sql.add(',RMBMoney=(case when A.BZType<>''USD'' and A.BZType<>''$'' then cast(Money as decimal(18,2)) else 0 end)');
      sql.add(',USDMoney=(case when A.BZType<>''RMB'' and A.BZType<>''￥'' then cast(Money as decimal(18,2)) else 0 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=''应收款登记'' ');
      sql.Add(' and isnull(YFName,'''')<>''坯布虚拟费''');
      sql.add(' union all');
      sql.Add(' select A.*,cast(0 as decimal(18,2)) yfk,cast(0 as decimal(18,2)) USDyfk ');
      sql.add(',fk=(case when A.BZType<>''USD'' and A.BZType<>''$'' then cast(Money as decimal(18,2)) else 0 end)');
      sql.add(',USDfk=(case when A.BZType<>''RMB'' and A.BZType<>''￥'' then cast(Money as decimal(18,2)) else 0 end)');
      sql.add(',cast(0 as decimal(18,2)) yfkye,cast(0 as decimal(18,2)) USDyfkye');
      sql.add(',RMBMoney=(case when A.BZType<>''USD'' and A.BZType<>''$'' then cast(Money as decimal(18,2)) else 0 end)');
      sql.add(',USDMoney=(case when A.BZType<>''RMB'' and A.BZType<>''￥'' then cast(Money as decimal(18,2)) else 0 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=''收款登记'' ) AA');
      sql.add(' where 1=1');
      if FactoryName.Text<>'' then
      sql.add(' and AA.FactoryName like '''+'%'+trim(FactoryName.Text)+'%'+'''');
      if YFName.Text<>'' then
      sql.add(' and AA.YFName like '''+'%'+trim(YFName.Text)+'%'+'''');
      if FeeType.Text<>'' then
      sql.add(' and AA.FeeType like '''+'%'+trim(FeeType.Text)+'%'+'''');
      if ComTaiTou.Text<>'' then
      sql.add(' and AA.ComTaiTou like '''+'%'+trim(ComTaiTou.Text)+'%'+'''');
      sql.add(' and AA.status=''2''');
      sql.Add('order by AA.CRTime');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Print);
    with Order_Print do
    begin
      append;
      fieldbyname('Ssel').AsBoolean:=false;
      fieldbyname('CRTime').Value:=begdate.DateTime;
      fieldbyname('yfkye').Value:=FMoney;
      fieldbyname('USDyfkye').Value:=FUSDMoney;
      post;
    end;
    with ADOQueryMain do
    begin
      first;
      while not eof do
      begin
        with Order_Print do
        begin
          append;
          fieldbyname('Ssel').AsBoolean:=false;
          fieldbyname('CRTime').Value:=ADOQueryMain.fieldbyname('CRTime').AsDateTime;
          fieldbyname('factoryName').Value:=ADOQueryMain.fieldbyname('factoryName').AsString;
          fieldbyname('ConNO').Value:=ADOQueryMain.fieldbyname('ConNO').AsString;
          fieldbyname('P_CodeName').Value:=ADOQueryMain.fieldbyname('P_CodeName').AsString;
          fieldbyname('OrderNO').Value:=ADOQueryMain.fieldbyname('OrderNO').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('QtyUnit').Value:=ADOQueryMain.fieldbyname('QtyUnit').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;
          fieldbyname('USDyfk').Value:=ADOQueryMain.fieldbyname('USDyfk').AsFloat;
          fieldbyname('USDfk').Value:=ADOQueryMain.fieldbyname('USDfk').AsFloat;
          FMoney:=FMoney+ADOQueryMain.fieldbyname('RMBMoney').Value*ADOQueryMain.fieldbyname('QtyFlag').Value;
          FUSDMoney:=FUSDMoney+ADOQueryMain.fieldbyname('USDMoney').Value*ADOQueryMain.fieldbyname('QtyFlag').Value;
          fieldbyname('yfkye').Value:=FMoney;
          fieldbyname('USDyfkye').Value:=FUSDMoney;
          fieldbyname('note').Value:=ADOQueryMain.fieldbyname('note').AsString;
          fieldbyname('FKType').Value:=ADOQueryMain.fieldbyname('FKType').AsString;
          fieldbyname('YFName').Value:=ADOQueryMain.fieldbyname('YFName').AsString;
          fieldbyname('FeeType').Value:=ADOQueryMain.fieldbyname('FeeType').AsString;
          fieldbyname('ComTaiTou').Value:=trim(ADOQueryMain.fieldbyname('ComTaiTou').AsString);
          post;
        end;
        next;
      end;
    end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\应收款对账单.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['FBegDate']:=BegDate.Date;
    RMVariables['FEndDate']:=EndDate.Date;
    RMVariables['FRMBMoney']:=FMoney;
    RMVariables['FUSDMoney']:=FUSDMoney;
    RMVariables['FBankName']:=trim(BankName.Text);
    RMVariables['FBankNo']:=trim(BankNo.Text);
    RMVariables['FComTaiTou']:=trim(Order_Print.fieldbyname('ComTaiTou').AsString);
    RMVariables['FKHName']:=trim(Order_Print.fieldbyname('factoryName').AsString);
    RMVariables['FHName']:=trim(HName.Text);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\应收款对账单.rmf'),'提示',0);
  end;
end;

procedure TfrmYSZDList_MX.BankNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='BankName3';
      flagname:='银行名称';
      fnote:=true;
      V1Note.Caption:='银行账号';
      if ShowModal=1 then
      begin
        BankName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        BankNO.Text:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSZDList_MX.HNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='HName';
      flagname:='户名';
      fnote:=true;
      V1Note.Caption:='银行账号';
      if ShowModal=1 then
      begin
        HName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

end.
