unit U_CustStatement;

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, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, cxPC, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  U_BaseList, Vcl.Menus, Vcl.Clipbrd, DateUtils, dxSkinWXI,
  dxScrollbarAnnotations;

type
  TfrmCustStatement = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    OppCoName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    Label1: TLabel;
    ComTaiTou: TEdit;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ADOQueryPrint: TADOQuery;
    cxTabControl1: TcxTabControl;
    ToolButton1: TToolButton;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    Tv2Column4: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure OppCoNameChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    fmanage, FZKType: string;
    { Public declarations }
    RKFlag, FCYID: string;
  end;

implementation

uses
  U_DataLink, U_RtFun, U_ZDYHelp, U_SingleCustStatement;


{$R *.dfm}

procedure TfrmCustStatement.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' exec  P_Fin_CustStatement  ');
      sql.Add('  @begdate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
      sql.Add(' ,@Enddate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' ,@Currency=''USD'' ');
          end;
        1:
          begin
            sql.Add(' ,@Currency=''CNY'' ');
          end;
//        2:
//          begin
//            sql.Add(' ,@Currency=''€'' ');
//          end;
      end;
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    TV2.DataController.Filter.Clear;
  end;
  ToolButton2.Click;
end;

procedure TfrmCustStatement.N1Click(Sender: TObject);
begin

  Clipboard.SetTextBuf(PChar(Trim(CDS_HZ.fieldbyname(TV2.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));

end;

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

procedure TfrmCustStatement.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmCustStatement.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption), Tv2, '财务管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
//  begdate.DateTime := StartOfTheMonth(Enddate.DateTime);
  begdate.DateTime := EndDate.DateTime - 365;
  InitGrid();
end;

procedure TfrmCustStatement.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCustStatement.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv2, '财务管理');
end;

procedure TfrmCustStatement.ToolButton2Click(Sender: TObject);
var
  fsj: string;
begin
  if ADOQueryMain.Active = False then
    Exit;

  SDofilter(ADOQueryMain, Trim(SGetFilters(Panel1, 1, 2)));
  SCreateCDS(ADOQueryMain, CDS_HZ);
  SInitCDSData(ADOQueryMain, CDS_HZ);
end;

procedure TfrmCustStatement.OppCoNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmCustStatement.ToolButton3Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
//  frmSingleCustStatement := TfrmSingleCustStatement.Create(Application);
//  with frmSingleCustStatement do
//  begin
//    begdate.DateTime := self.begdate.DateTime;
//    enddate.DateTime := self.Enddate.DateTime;
//    OppCoName.Text := self.CDS_HZ.fieldbyname('OppCoName').AsString;
//    FOppCoNo := self.CDS_HZ.fieldbyname('OppCoNo').AsString;
//    FZKType := Self.FZKType;
//    case cxTabControl1.TabIndex of
//      0:
//        begin
//          FCurrency := 'USD';
//        end;
//      1:
//        begin
//          FCurrency := 'CNY';
//        end;
//    end;
//    show;
//  end;

  try
    frmSingleCustStatement := TfrmSingleCustStatement.Create(Application);
    with frmSingleCustStatement do
    begin
      begdate.DateTime := self.begdate.DateTime;
      enddate.DateTime := self.Enddate.DateTime;
      OppCoName.Text := self.CDS_HZ.fieldbyname('OppCoName').AsString;
      FOppCoNo := self.CDS_HZ.fieldbyname('OppCoNo').AsString;
      FOurCoNo := self.CDS_HZ.fieldbyname('OurCoNo').AsString;
      FZKType := Self.FZKType;
      case cxTabControl1.TabIndex of
        0:
          begin
            FCurrency := 'USD';
          end;
        1:
          begin
            FCurrency := 'CNY';
          end;
      end;
      if ShowModal = 1 then
      begin
//        InitGrid();
      end;
    end;
  finally
    frmSingleCustStatement.Free;
  end;
end;

procedure TfrmCustStatement.ToolButton4Click(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;
    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
      sql.Add(' exec P_YSSK_List @begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))) + ',@Enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))) + ',@flag=''已审核''');
      Open;
    end;
    Open;
  end;

  try
    filepath := ExtractFilePath(Application.ExeName) + 'report\应收账款.rmf';
    if not FileExists(Pchar(filepath)) then
    begin
      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
      exit;
    end;

    RmVariables['begDate'] := FormatDateTime('yyyy-mm-dd', begDate.Date);
    RmVariables['enddate'] := FormatDateTime('yyyy-mm-dd', enddate.Date);
    RMVariables['ZDR'] := trim(DName);
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;
  finally

  end;

end;

procedure TfrmCustStatement.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

end.

