unit U_FDINVOICE_XMX;

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, cxPC, RM_e_Xls, Menus,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmFDINVOICE_XMX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    Tfind: TToolButton;
    Label1: TLabel;
    P_CodeName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_FP: TDataSource;
    CDS_FP: TClientDataSet;
    v1ToFactoryName: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    Label4: TLabel;
    P_Spec: TEdit;
    Label5: TLabel;
    ToFactoryName: TEdit;
    cxTabControl1: TcxTabControl;
    TSel: TToolButton;
    v1Column1: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    cxgrdbclmnv1Column2: 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 TfindClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure InvoiceNoChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TSelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure YFGSTypeChange(Sender: TObject);
  private
    { Private declarations }
    canshu1,canshu2:string;
    procedure InitGrid();
    procedure SetStatus();
  public
    fInvoiceFlag:string;
    { Public declarations }
  end;

var
  frmFDINVOICE_XMX: TfrmFDINVOICE_XMX;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_FDINVOICE_Sub_X, U_FjList10,U_XXFPHXList,U_XXFPHx;

{$R *.dfm}

procedure TfrmFDINVOICE_XMX.SetStatus();
begin
  
  IF fInvoiceFlag='' then
  fInvoiceFlag:='1';
end;

procedure TfrmFDINVOICE_XMX.InitGrid();
begin
  try

    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.ToFactoryName,B.P_CodeName,B.P_Spec,B.QtyUnit,sum(B.Qty) Qty,sum(B.Money) Money,sum(B.taxMoney) taxMoney,sum(B.taxMoney-B.Money) tax');

      sql.Add(' from FD_Invoice A inner join FD_Invoicesub B on A.InvoiceNo=B.InvoiceNo');
      sql.add(' where A.InvoiceDate>='''+formatdatetime('yyyy-MM-dd',begdate.datetime)+''' ');
      sql.Add(' and A.InvoiceDate<'''+formatdateTime('yyyy-MM-dd',enddate.DateTime+1)+''' ');
      sql.Add(' and InvoiceFlag='''+fInvoiceFlag+''' ');
      if cxTabControl1.TabIndex=0 then
      sql.add(' and A.Chkstatus=''0''');
      if cxTabControl1.TabIndex=1 then
      sql.add(' and A.Chkstatus=''1''');
      sql.add(' group by A.ToFactoryName,B.P_CodeName,B.P_Spec,B.QtyUnit');
      Open;
    end;
 
    SCreateCDS20(ADOQueryMain,CDS_FP);
    SInitCDSData20(ADOQueryMain,CDS_FP);
  finally
 
  end;
  Tfind.Click;
end;


procedure TfrmFDINVOICE_XMX.FormDestroy(Sender: TObject);
begin
  frmFDINVOICE_XMX:=nil;
end;

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

procedure TfrmFDINVOICE_XMX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv1,'财务管理');
  Close;
end;
procedure TfrmFDINVOICE_XMX.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption,Tv1,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=Enddate.DateTime-30;
  
  SetStatus();
  InitGrid();
end;

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

procedure TfrmFDINVOICE_XMX.TfindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_FP);
    SInitCDSData20(ADOQueryMain,CDS_FP);
  end;
end;

procedure TfrmFDINVOICE_XMX.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(self.Caption,cxgrid1);
end;

procedure TfrmFDINVOICE_XMX.InvoiceNoChange(Sender: TObject);
begin
  Tfind.Click;
end;

procedure TfrmFDINVOICE_XMX.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  initGrid();
end;

procedure TfrmFDINVOICE_XMX.TSelClick(Sender: TObject);
begin
  frmInvoice_Sub_X:=TfrmInvoice_Sub_X.Create(self);
  with frmInvoice_Sub_X do
  begin
    TSave.Visible:=False;
    ToolBar2.Enabled:=False;
    FInvoiceNo:=trim(CDS_FP.fieldbyname('InvoiceNo').asstring);
    fInvoiceFlag:=self.fInvoiceFlag;
    canshu1:=self.canshu1;
    if showmodal=1 then
    begin
      
    end;
  end;
end;

procedure TfrmFDINVOICE_XMX.FormCreate(Sender: TObject);
begin
  canshu1:=trim(DParameters1);
end;

procedure TfrmFDINVOICE_XMX.N1Click(Sender: TObject);
begin
  CDS_FP.DisableControls;
  while CDS_FP.Locate('SSel',False,[]) do
  begin
      CDS_FP.Edit;
      CDS_FP.FieldByName('SSel').Value:=True;
      CDS_FP.Post;
  end;
  CDS_FP.EnableControls
end;

procedure TfrmFDINVOICE_XMX.N2Click(Sender: TObject);
begin
  CDS_FP.DisableControls;
  while CDS_FP.Locate('SSel',True,[]) do
  begin
      CDS_FP.Edit;
      CDS_FP.FieldByName('SSel').Value:=False;
      CDS_FP.Post;
  end;
  CDS_FP.EnableControls
end;

procedure TfrmFDINVOICE_XMX.YFGSTypeChange(Sender: TObject);
begin
  Tfind.Click;
end;

end.
