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;

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;
    Label3: TLabel;
    InvoiceNo: TEdit;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_FP: TDataSource;
    CDS_FP: TClientDataSet;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label4: TLabel;
    P_Spec: TEdit;
    Label5: TLabel;
    FactoryName: TEdit;
    cxTabControl1: TcxTabControl;
    TSel: TToolButton;
    ToolButton3: TToolButton;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    Label9: TLabel;
    ToFactoryName: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column3: TcxGridDBColumn;
    v1InvoiceNo: TcxGridDBColumn;
    v1InvoiceDate: TcxGridDBColumn;
    v1InvoiceType: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1ToFactoryName: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v1Column4: 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 ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(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;

{$R *.dfm}

procedure TfrmFDINVOICE_XMX.SetStatus();
begin

end;

procedure TfrmFDINVOICE_XMX.InitGrid();
begin
  try

    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*,B.*,cast((case when qty<>0 then cast((money/qty)as decimal(18,9))else 0 end) as varchar(30))as price1,shuie=cast(cast((isnull(tax,0)*isnull(money,0)/100) as decimal(18,2)) as varchar(30))');
      sql.Add(' from FD_Invoice A left 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''');
      if cxTabControl1.TabIndex = 2 then
        sql.add(' and A.Chkstatus <>''2''');
      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.ToolButton3Click(Sender: TObject);
begin
  if CDS_FP.IsEmpty then
    Exit;
  try
    frmFjList10 := TfrmFjList10.Create(Application);
    with frmFjList10 do
    begin
    //  PState:=1;
   //   if Trim(canshu1)<>'高权限'  then cxButton1.Visible:=false;
      fkeyNO := Trim(Self.CDS_FP.fieldbyname('InvoiceNo').AsString);
      fType := '发票';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmFjList10.Free;
  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;

end.

