unit U_YSMXlist;

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
  TfrmYSMXlist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    MPRTCodeName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    Label3: TLabel;
    OrderNo: TEdit;
    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;
    factoryName: TEdit;
    v2Column1: TcxGridDBColumn;
    v2ps: TcxGridDBColumn;
    ToolButton3: TToolButton;
    RMGridReport1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    v2note: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    v2DerateMoney: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    Panetime: TPanel;
    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 P_OrderNo_YSK(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();

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

  end;

var
  frmYSMXlist: TfrmYSMXlist;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_ProductOrderNewList;

{$R *.dfm}



procedure TfrmYSMXlist.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
      Close;
      SQL.Clear;
      Filtered:=False;
      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 TfrmYSMXlist.FormDestroy(Sender: TObject);
begin
  frmYSMXlist:=nil;
end;

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

procedure TfrmYSMXlist.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv2,'财务管理');
  Close;
end;
procedure TfrmYSMXlist.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption,Tv2,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=Enddate.DateTime;
//  InitGrid();
 { if fType='1' then
  begin
    VOrderNo.Caption:='销售单号';
    Label3.Caption:='销售单号';
  end;  }
end;

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

procedure TfrmYSMXlist.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  end;
end;

procedure TfrmYSMXlist.P_OrderNo_YSK(Sender: TObject);
begin
  ToolButton2.Click;
end;

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

procedure TfrmYSMXlist.OrderNoChange(Sender: TObject);
begin
  if Length(Trim(OrderNo.Text))<3 then Exit;
  ToolButton2.Click;
end;

procedure TfrmYSMXlist.MPRTCodeNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

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

procedure TfrmYSMXlist.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;


    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      IF fType='1' THEN
      sql.Add(' exec  P_Qry_Ysk_mx_dz_Gd ')
      else
      sql.Add(' exec  P_Qry_Ysk_mx_dz ');
      sql.Add(' @begdate='''+Trim(FormatDateTime('yyyy-MM-dd',begdate.DateTime))+''' ');
      sql.Add(',@enddate='''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+''' ');
      sql.Add(',@Type='''+Trim(fType)+''' ');
      sql.Add(',@factoryName='''+Trim(CDS_HZ.fieldbyname('factoryName').AsString)+''' ');
      Open;
    end;

  try
    IF fType='2' THEN
    filepath:=ExtractFilePath(Application.ExeName) + 'report\湖州应收款对账单.rmf'
    ELSE
    IF fType='1' THEN
    filepath:=ExtractFilePath(Application.ExeName) + 'report\广东应收款对账单.rmf'
    ELSE
    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['begDate'] :=FormatDateTime('yyyy-mm-dd',begDate.Date);
    RmVariables['enddate'] :=FormatDateTime('yyyy-mm-dd',enddate.Date);
    RMVariables['zdr'] :=trim(DName);
    RmVariables['begDate'] :=FormatDateTime('yymmdd',begDate.Date)+'-'+FormatDateTime('yymmdd',enddate.Date);
    ADOQueryPrint.First;
    RmVariables['QCNum'] :=ADOQueryPrint.fieldbyname('MoneyYe').AsFloat - ADOQueryPrint.fieldbyname('Money').AsFloat + ADOQueryPrint.fieldbyname('BBMoney').AsFloat +  ADOQueryPrint.fieldbyname('DerateMoney').AsFloat ;
    ADOQueryPrint.Last;
    RmVariables['endNum'] :=ADOQueryPrint.fieldbyname('MoneyYe').AsString;
    RMGridReport1.LoadFromFile(filepath);
   // RMGridReport1.PrintReport;
    RMGridReport1.ShowReport;

  finally

  end;

end;


end.
