unit U_YWYTopKHMX;

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, cxDBLookupComboBox, cxContainer, cxDropDownEdit, cxPC, Menus,
  TeEngine, Series, TeeProcs, Chart, DbChart, GanttCh;

type
  TfrmYWYTopKHMX = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Panel2: TPanel;
    Panel3: TPanel;
    SH1: TPanel;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    DataSource3: TDataSource;
    ClientDataSet3: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column12: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel1: TPanel;
    KH1: TPanel;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Panel4: TPanel;
    Panel5: TPanel;
    Panel6: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Tv2Column1: TcxGridDBColumn;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Tv3Column1: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    Tv3Column4: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ClientDataSet2: TClientDataSet;
    DataSource2: TDataSource;
    cxGridPopupMenu3: TcxGridPopupMenu;
    v1Column6: TcxGridDBColumn;
    Label1: TLabel;
    Panel7: TPanel;
    Panel8: TPanel;
    Panel9: TPanel;
    v1Column4: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Panel7Click(Sender: TObject);
    procedure Panel8Click(Sender: TObject);
    procedure Panel9Click(Sender: TObject);
  private

    { Private declarations }
    procedure InitGrid();
  public
    FBegdate,FEnddate:TDate;
    FKHName,FKHNO:String;

  end;

var
  frmYWYTopKHMX: TfrmYWYTopKHMX;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}


procedure TfrmYWYTopKHMX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('exec P_Get_KHTop_MX :begdate,:enddate,:Type,:KHNO');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FBegdate));
      Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FEnddate));
      Parameters.ParamByName('Type').Value:='销售';
      Parameters.ParamByName('KHNO').Value:=FKHNO;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, ClientDataSet1);
    SInitCDSData20(ADOQueryMain, ClientDataSet1);
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('exec P_Get_KHTop_MX :begdate,:enddate,:Type,:KHNO');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FBegdate));
      Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FEnddate));
      Parameters.ParamByName('Type').Value:='收款';
      Parameters.ParamByName('KHNO').Value:=FKHNO;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, ClientDataSet2);
    SInitCDSData20(ADOQueryMain, ClientDataSet2);

    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('exec P_Get_KHTop_MX :begdate,:enddate,:Type,:KHNO');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FBegdate));
      Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FEnddate));
      Parameters.ParamByName('Type').Value:='费用';
      Parameters.ParamByName('KHNO').Value:=FKHNO;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, ClientDataSet3);
    SInitCDSData20(ADOQueryMain, ClientDataSet3);

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYWYTopKHMX.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  WriteCxGrid('YWYTopXSKH1', Tv1, '排行榜');
  WriteCxGrid('YWYTopSKKH1', Tv2, '排行榜');
  WriteCxGrid('YWYTopFYKH1', Tv3, '排行榜');
  Action := caHide;
end;

procedure TfrmYWYTopKHMX.FormShow(Sender: TObject);
begin
  ReadCxGrid('YWYTopXSKH1', Tv1, '排行榜');
  ReadCxGrid('YWYTopSKKH1', Tv2, '排行榜');
  ReadCxGrid('YWYTopFYKH1', Tv3, '排行榜');
  Label1.Caption:=Trim(FKHName);
  InitGrid();
end;

procedure TfrmYWYTopKHMX.FormDestroy(Sender: TObject);
begin
  frmYWYTopKHMX:=nil;
end;

procedure TfrmYWYTopKHMX.Panel7Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  TcxGridToExcel('销售数据'+FKHName,cxGrid1);
end;

procedure TfrmYWYTopKHMX.Panel8Click(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then Exit;
  TcxGridToExcel('收款数据'+FKHName,cxGrid2);
end;

procedure TfrmYWYTopKHMX.Panel9Click(Sender: TObject);
begin
  if ClientDataSet3.IsEmpty then Exit;
  TcxGridToExcel('费用数据'+FKHName,cxGrid3);
end;

end.

