unit U_ZZYSMXlist;

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, cxDropDownEdit, RM_e_Xls;

type
  TfrmZZYSMXlist = 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;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxCRTime: TcxGridDBColumn;
    cxMoneyYe: TcxGridDBColumn;
    cxQty: TcxGridDBColumn;
    cxQtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2Price: TcxGridDBColumn;
    VMoney: TcxGridDBColumn;
    VOrderNo: TcxGridDBColumn;
    v2BBMoney: TcxGridDBColumn;
    v2MPRTCodeName: TcxGridDBColumn;
    Label4: TLabel;
    OrdDefNote3: TComboBox;
    v2factoryName: TcxGridDBColumn;
    v2MRPTspec: TcxGridDBColumn;
    v2YFName: TcxGridDBColumn;
    Label5: TLabel;
    ToolButton3: TToolButton;
    RMXLSExport1: TRMXLSExport;
    v2Column1: TcxGridDBColumn;
    v2note: TcxGridDBColumn;
    Panetime: TPanel;
    factoryName: TComboBox;
    v2Column2: 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 ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    procedure InitGYS();

  public
    { Public declarations }
    RKFlag,FCYID:String;
    fType,FMX:string;

  end;

var
  frmZZYSMXlist: TfrmZZYSMXlist;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_ProductOrderNewList,U_RTFun;

{$R *.dfm}

procedure TfrmZZYSMXlist.InitGYS();
var
  fsj:string;
begin
  fsj:=' select distinct(factoryName) Name,Code=''''  from YF_Money_CR where CRFlag=''应收收'' and isnull(factoryName,'''')<>'''' ';
  SInitComBoxBySql(ADOQueryTemp,factoryName,False,fsj);
end;


procedure TfrmZZYSMXlist.InitGrid();
begin
  IF trim(factoryName.Text)='' then
  begin
    application.MessageBox('请输入客户！','提示信息',MB_ICONERROR);
    exit;
  end;
  screen.Cursor:=crsqlwait;
  Panetime.Visible:=true;
  Panetime.Left:=(Width-Panetime.Width) div 2;
  Panetime.top:=(Height-Panetime.Height-200) div 2;
  application.ProcessMessages;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' exec  P_Qry_Ysk_mx :begdate,:enddate,:Type,:factoryName');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',begdate.DateTime));
      Parameters.ParamByName('Enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1));
      Parameters.ParamByName('Type').Value:=Trim(fType);
      Parameters.ParamByName('factoryName').Value:=Trim(factoryName.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
    Panetime.Visible:=false;
    screen.Cursor:=crdefault;
  end;
end;


procedure TfrmZZYSMXlist.FormDestroy(Sender: TObject);
begin
  frmZZYSMXlist:=nil;
end;

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

procedure TfrmZZYSMXlist.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv2,'财务管理');
  Close;
end;
procedure TfrmZZYSMXlist.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption,Tv2,'财务管理');
  if Trim(FMX)='' then
  begin
    InitGYS();
    Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
    begdate.DateTime:=SGetServerDateMBeg(ADOQueryTemp);
  end else
  begin
    InitGrid();
  end;



end;

procedure TfrmZZYSMXlist.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

procedure TfrmZZYSMXlist.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,self.Caption);
end;

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

procedure TfrmZZYSMXlist.ToolButton3Click(Sender: TObject);
var
  filepath:string;
  mBegdate,mEnddate:string;
begin
  mbegdate:=FormatDateTime('yyyy-MM-dd',begdate.Date);
  menddate:=FormatDateTime('yyyy-MM-dd',enddate.Date);
  if CDS_HZ.IsEmpty then exit;
  try
    filepath:=ExtractFilePath(Application.ExeName) + 'report\应收款对账单.rmf';
    if not FileExists(Pchar(filepath))  then
    begin
      application.MessageBox(pchar('文件['+filepath+']不存在！'),'提示信息',MB_IConError);
      exit;
    end;
    RMVariables['dtxz'] :=FormatDateTime('yyyy-mm-dd',date());
    RMVariables['zdr'] :=trim(DName);
    RmVariables['begDate'] :=FormatDateTime('yymmdd',begDate.Date)+'-'+FormatDateTime('yymmdd',enddate.Date);
    CDS_HZ.First;
    RmVariables['QCNum'] :=CDS_HZ.fieldbyname('MoneyYe').AsFloat - CDS_HZ.fieldbyname('Money').AsFloat + CDS_HZ.fieldbyname('BBMoney').AsFloat + CDS_HZ.fieldbyname('T_money').AsFloat + CDS_HZ.fieldbyname('DerateMoney').AsFloat;
    CDS_HZ.Last;
    RmVariables['endNum'] :=CDS_HZ.fieldbyname('MoneyYe').AsString;
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;

  finally

  end;

end;

end.
