unit U_YWYTopKH;

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
  TfrmYWYTopKH = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Panel2: TPanel;
    Panel3: TPanel;
    SH1: TPanel;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Panel7: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column12: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel1: TPanel;
    Panel4: TPanel;
    Panel5: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Panel7Click(Sender: TObject);
    procedure Panel5Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure Tv2DblClick(Sender: TObject);
  private

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

  end;

var
  frmYWYTopKH: TfrmYWYTopKH;

implementation

uses
  U_DataLink, U_RTFun,U_YWYTopKHMX;

{$R *.dfm}


procedure TfrmYWYTopKH.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('exec P_Get_YWYLiDanKHTop :begdate,:enddate,:BZType,:Type,:YGName');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FBegdate));
      Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FEnddate));
      Parameters.ParamByName('BZType').Value:='$';
      Parameters.ParamByName('Type').Value:=FType;
      Parameters.ParamByName('YGName').Value:=FMXName;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, ClientDataSet1);
    SInitCDSData20(ADOQueryMain, ClientDataSet1);
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('exec P_Get_YWYLiDanKHTop :begdate,:enddate,:BZType,:Type,:YGName');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FBegdate));
      Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FEnddate));
      Parameters.ParamByName('BZType').Value:='￥';
      Parameters.ParamByName('Type').Value:=FType;
      Parameters.ParamByName('YGName').Value:=FMXName;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, ClientDataSet2);
    SInitCDSData20(ADOQueryMain, ClientDataSet2);

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYWYTopKH.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  WriteCxGrid('YWYTopKHR1', Tv1, '排行榜');
  WriteCxGrid('YWYTopKHM1', Tv2, '排行榜');
  Action := caHide;
end;

procedure TfrmYWYTopKH.FormShow(Sender: TObject);
begin
  ReadCxGrid('YWYTopKHR1', Tv1, '排行榜');
  ReadCxGrid('YWYTopKHM1', Tv2, '排行榜');
  Label1.Caption:=Trim(FType)+':'+Trim(FMXName);
  InitGrid();
end;

procedure TfrmYWYTopKH.FormDestroy(Sender: TObject);
begin
  frmYWYTopKH:=nil;
end;

procedure TfrmYWYTopKH.Panel7Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  TcxGridToExcel(FType+'客户数据'+FMXName+'($)',cxGrid1);
end;

procedure TfrmYWYTopKH.Panel5Click(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then Exit;
  TcxGridToExcel(FType+'客户数据'+FMXName+'(￥)',cxGrid2);
end;

procedure TfrmYWYTopKH.Tv1DblClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  try
    frmYWYTopKHMX:=TfrmYWYTopKHMX.Create(Application);
    with frmYWYTopKHMX do
    begin
      FBegdate:=Self.FBegdate;
      FEnddate:=Self.FEnddate;
      FKHName:=Trim(Self.ClientDataSet1.fieldbyname('KHNameJC').AsString);
      FKHNO:=Trim(Self.ClientDataSet1.fieldbyname('FactoryNo').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmYWYTopKHMX.Free;
  end;
end;

procedure TfrmYWYTopKH.Tv2DblClick(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then Exit;
  try
    frmYWYTopKHMX:=TfrmYWYTopKHMX.Create(Application);
    with frmYWYTopKHMX do
    begin
      FBegdate:=Self.FBegdate;
      FEnddate:=Self.FEnddate;
      FKHName:=Trim(Self.ClientDataSet2.fieldbyname('KHNameJC').AsString);
      FKHNO:=Trim(Self.ClientDataSet2.fieldbyname('FactoryNo').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmYWYTopKHMX.Free;
  end;
end;

end.

