unit U_KHTop;

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, MovePanel, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmKHTop = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Panel2: TPanel;
    Panel3: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column12: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    SH1: TPanel;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    v1Column1: TcxGridDBColumn;
    DataSource3: TDataSource;
    ClientDataSet3: TClientDataSet;
    Panel1: TPanel;
    Label1: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    C_Code: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Panel10: TPanel;
    ToolButton1: TToolButton;
    ComboBox1: TComboBox;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Panel4: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel5: TPanel;
    Timer1: TTimer;
    MovePanel3: TMovePanel;
    Label13: TLabel;
    Button9: TButton;
    Button10: TButton;
    Password: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure Tv2DblClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
  private

    { Private declarations }
    procedure InitGrid();
    procedure InitGrid10();
  public
  end;

var
  frmKHTop: TfrmKHTop;

implementation

uses
  U_DataLink, U_RTFun, U_KHCPTopMX, U_YWYCPTopMX, U_KHCPColorTopMX;

{$R *.dfm}

procedure TfrmKHTop.InitGrid();
begin
  Panel10.Visible := True;
  Panel10.Refresh;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('exec P_BI_KHAll :begdate,:enddate,:Type');
      Parameters.ParamByName('begdate').Value := Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date));
      Parameters.ParamByName('enddate').Value := Trim(FormatDateTime('yyyy-MM-dd', enddate.Date + 1));
      Parameters.ParamByName('Type').Value := '客户';
      Open;
    end;
    SCreateCDS20(ADOQueryMain, ClientDataSet1);
    SInitCDSData20(ADOQueryMain, ClientDataSet1);
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('exec P_BI_KHAll :begdate,:enddate,:Type');
      Parameters.ParamByName('begdate').Value := Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date));
      Parameters.ParamByName('enddate').Value := Trim(FormatDateTime('yyyy-MM-dd', enddate.Date + 1));
      Parameters.ParamByName('Type').Value := '业务员';
      Open;
    end;
    SCreateCDS20(ADOQueryMain, ClientDataSet3);
    SInitCDSData20(ADOQueryMain, ClientDataSet3);
  finally
    ADOQueryMain.EnableControls;
  end;
  Panel10.Visible := False;
end;

procedure TfrmKHTop.InitGrid10();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('exec P_BI_KHAll :begdate,:enddate,:Type');
      Parameters.ParamByName('begdate').Value := '2000-01-04';
      Parameters.ParamByName('enddate').Value := '2000-01-02';
      Parameters.ParamByName('Type').Value := '客户';
      Open;
    end;
    SCreateCDS20(ADOQueryMain, ClientDataSet1);
    SInitCDSData20(ADOQueryMain, ClientDataSet1);
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('exec P_BI_KHAll :begdate,:enddate,:Type');
      Parameters.ParamByName('begdate').Value := '2000-01-04';
      Parameters.ParamByName('enddate').Value := '2000-01-02';
      Parameters.ParamByName('Type').Value := '业务员';
      Open;
    end;
    SCreateCDS20(ADOQueryMain, ClientDataSet3);
    SInitCDSData20(ADOQueryMain, ClientDataSet3);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmKHTop.FormDestroy(Sender: TObject);
begin
  frmKHTop := nil;
end;

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

procedure TfrmKHTop.TBCloseClick(Sender: TObject);
begin
  //WriteCxGrid('收货', Tv1, '前十大产品排行榜');
  Close;
end;

procedure TfrmKHTop.FormShow(Sender: TObject);
begin
  //ReadCxGrid('收货', Tv1, '前十大产品排行榜');
end;

procedure TfrmKHTop.TBRafreshClick(Sender: TObject);
begin
  MovePanel3.Visible := True;
end;

procedure TfrmKHTop.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, ClientDataSet1);
    SInitCDSData20(ADOQueryMain, ClientDataSet1);
  end;
end;

procedure TfrmKHTop.C_CodeNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmKHTop.FormCreate(Sender: TObject);
var
  FYear: string;
  FMonth: string;
  FDate: TDate;
begin
  FDate := SGetServerDate10(ADOQueryTemp);
  FYear := FormatDateTime('yyyy', FDate);
  FMonth := FormatDateTime('MM', FDate);
  if StrToInt(FMonth) < 2 then
  begin
    BegDate.Date := StrToDate(IntToStr(strtoint(FYear) - 1) + '-02-01');
    EndDate.Date := StrToDate(FYear + '-01-31');
  end
  else
  begin
    BegDate.Date := StrToDate(FYear + '-02-01');
    EndDate.Date := StrToDate(IntToStr(strtoint(FYear) + 1) + '-01-31');
  end;
end;

procedure TfrmKHTop.ToolButton1Click(Sender: TObject);
begin
  if ComboBox1.Text = '' then
    Exit;
  if ComboBox1.ItemIndex = 0 then
  begin
    TcxGridToExcel(ComboBox1.Text, cxGrid1);
  end
  else if ComboBox1.ItemIndex = 1 then
  begin
    TcxGridToExcel(ComboBox1.Text, cxGrid2);
  end;
end;

procedure TfrmKHTop.Tv1DblClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then
    Exit;
  try
    frmKHCPTopMX := TfrmKHCPTopMX.Create(Application);
    with frmKHCPTopMX do
    begin
      FBegdate := Self.BegDate.Date;
      FEnddate := Self.EndDate.Date;
      FKHName := Trim(Self.ClientDataSet1.fieldbyname('KHName').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmKHCPTopMX.Free;
  end;
end;

procedure TfrmKHTop.Tv2DblClick(Sender: TObject);
begin
  if ClientDataSet3.IsEmpty then
    Exit;
  try
    frmYWYCPTopMX := TfrmYWYCPTopMX.Create(Application);
    with frmYWYCPTopMX do
    begin
      FBegdate := Self.BegDate.Date;
      FEnddate := Self.EndDate.Date;
      FKHName := Trim(Self.ClientDataSet3.fieldbyname('KHName').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmYWYCPTopMX.Free;
  end;
end;

procedure TfrmKHTop.Timer1Timer(Sender: TObject);
begin
  InitGrid10();
  Timer1.Enabled := False;
  MovePanel3.Visible := True;
  TBRafresh.Visible := False;
  if frmKHCPTopMX <> nil then
  begin
    frmKHCPTopMX.Close;
  end;
  if frmYWYCPTopMX <> nil then
  begin
    frmYWYCPTopMX.Close;
  end;
  if frmKHCPColorTopMX <> nil then
  begin
    frmKHCPColorTopMX.Close;
  end;
end;

procedure TfrmKHTop.Button10Click(Sender: TObject);
begin
  MovePanel3.Visible := False;
end;

procedure TfrmKHTop.Button9Click(Sender: TObject);
var
  mm: string;
begin
  with ADOQueryTemp do
  begin
    sql.Clear;
    sql.add('SELECT pw FROM SY_User WHERE userid=' + '''' + trim(DCode) + '''');
    Open;
  end;
  mm := Trim(ADOQueryTemp.fieldbyname('pw').AsString);
  if mm <> Trim(Password.Text) then
  begin
    Application.MessageBox('密码错误!', '提示', 0);
    Exit;
  end;
  Password.Text := '';
  MovePanel3.Visible := False;
  InitGrid();
  Timer1.Enabled := True;
end;

end.

