unit U_CPTopList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridBandedTableView, cxGridDBBandedTableView,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGridDBTableView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxDropDownEdit, MovePanel, Menus,
  cxCheckBox, RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport,
  RM_e_Xls, cxPC;

type
  TfrmCPTopList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    Label1: TLabel;
    P_Code: TEdit;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    MovePanel2: TMovePanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBBandedTableView;
    cxGrid1Level1: TcxGridLevel;
    v1Column1: TcxGridDBBandedColumn;
    v1Column2: TcxGridDBBandedColumn;
    v1Column3: TcxGridDBBandedColumn;
    v1Column4: TcxGridDBBandedColumn;
    v1Column5: TcxGridDBBandedColumn;
    v1Column6: TcxGridDBBandedColumn;
    v1Column7: TcxGridDBBandedColumn;
    v1Column8: TcxGridDBBandedColumn;
    v1Column9: TcxGridDBBandedColumn;
    v1Column10: TcxGridDBBandedColumn;
    v1Column11: TcxGridDBBandedColumn;
    v1Column12: TcxGridDBBandedColumn;
    v1Column13: TcxGridDBBandedColumn;
    v1Column14: TcxGridDBBandedColumn;
    v1Column15: TcxGridDBBandedColumn;
    v1Column16: TcxGridDBBandedColumn;
    v1Column17: TcxGridDBBandedColumn;
    v1Column18: TcxGridDBBandedColumn;
    v1Column19: TcxGridDBBandedColumn;
    v1Column20: TcxGridDBBandedColumn;
    v1Column21: TcxGridDBBandedColumn;
    v1Column22: TcxGridDBBandedColumn;
    v1Column23: TcxGridDBBandedColumn;
    v1Column24: TcxGridDBBandedColumn;
    v1Column25: TcxGridDBBandedColumn;
    v1Column26: TcxGridDBBandedColumn;
    v1Column27: TcxGridDBBandedColumn;
    v1Column28: TcxGridDBBandedColumn;
    v1Column29: TcxGridDBBandedColumn;
    v1Column30: TcxGridDBBandedColumn;
    v1Column31: TcxGridDBBandedColumn;
    v1Column32: TcxGridDBBandedColumn;
    v1Column33: TcxGridDBBandedColumn;
    v1Column34: TcxGridDBBandedColumn;
    v1Column35: TcxGridDBBandedColumn;
    v1Column36: TcxGridDBBandedColumn;
    v1Column37: TcxGridDBBandedColumn;
    v1Column38: TcxGridDBBandedColumn;
    v1Column39: TcxGridDBBandedColumn;
    v1Column40: TcxGridDBBandedColumn;
    v1Column41: TcxGridDBBandedColumn;
    v1Column42: TcxGridDBBandedColumn;
    v1Column43: TcxGridDBBandedColumn;
    v1Column44: TcxGridDBBandedColumn;
    v1Column45: TcxGridDBBandedColumn;
    v1Column46: TcxGridDBBandedColumn;
    v1Column47: TcxGridDBBandedColumn;
    v1Column48: TcxGridDBBandedColumn;
    v1Column49: TcxGridDBBandedColumn;
    v1Column50: TcxGridDBBandedColumn;
    v1Column51: TcxGridDBBandedColumn;
    v1Column52: TcxGridDBBandedColumn;
    v1Column53: TcxGridDBBandedColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBBandedTableView;
    cxGridDBBandedColumn1: TcxGridDBBandedColumn;
    cxGridDBBandedColumn2: TcxGridDBBandedColumn;
    cxGridDBBandedColumn3: TcxGridDBBandedColumn;
    cxGridDBBandedColumn4: TcxGridDBBandedColumn;
    cxGridDBBandedColumn5: TcxGridDBBandedColumn;
    cxGridDBBandedColumn6: TcxGridDBBandedColumn;
    cxGridDBBandedColumn7: TcxGridDBBandedColumn;
    cxGridDBBandedColumn8: TcxGridDBBandedColumn;
    cxGridDBBandedColumn9: TcxGridDBBandedColumn;
    cxGridDBBandedColumn10: TcxGridDBBandedColumn;
    cxGridDBBandedColumn11: TcxGridDBBandedColumn;
    cxGridDBBandedColumn12: TcxGridDBBandedColumn;
    cxGridDBBandedColumn13: TcxGridDBBandedColumn;
    cxGridDBBandedColumn14: TcxGridDBBandedColumn;
    cxGridDBBandedColumn15: TcxGridDBBandedColumn;
    cxGridDBBandedColumn16: TcxGridDBBandedColumn;
    cxGridDBBandedColumn17: TcxGridDBBandedColumn;
    cxGridDBBandedColumn50: TcxGridDBBandedColumn;
    cxGridDBBandedColumn51: TcxGridDBBandedColumn;
    cxGridDBBandedColumn52: TcxGridDBBandedColumn;
    cxGridDBBandedColumn53: TcxGridDBBandedColumn;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    RMXLSExport1: TRMXLSExport;
    RM1: TRMGridReport;
    ToolButton2: TToolButton;
    CDS_Prt: TClientDataSet;
    RMDBDataSet1: TRMDBDataSet;
    ToolButton3: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure P_CodeChange(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    FLeft,FTop:Integer;
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
    FMainid,FSubId,FConNo,FColor,FCodeName:String;

  end;

var
  frmCPTopList: TfrmCPTopList;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmCPTopList.FormDestroy(Sender: TObject);
begin
  frmCPTopList:=nil;
end;

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

procedure TfrmCPTopList.InitGrid();
begin
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('exec P_View_CPTop :FYear,:CXState');
      Parameters.ParamByName('FYear').Value:=Trim(FormatDateTime('yyyy',BegDate.Date));
      if cxPageControl1.ActivePageIndex=0 then
      begin
        Parameters.ParamByName('CXState').Value:='月';
      end else
      begin
        Parameters.ParamByName('CXState').Value:='季度';
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);   
  finally
    ADOQueryMain.EnableControls;
  end;
  MovePanel2.Visible:=False;
end;

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

procedure TfrmCPTopList.TBCloseClick(Sender: TObject);
begin
  WriteCxBandedGrid('Tv1',Tv1,'产品排行');
  WriteCxBandedGrid('Tv2',Tv2,'产品排行');
  Close;
end;

procedure TfrmCPTopList.FormShow(Sender: TObject);
begin
  BegDate.Date:=SGetServerDate(ADOQueryTemp);
  ReadCxBandedGrid('Tv1',Tv1,'产品排行');
  ReadCxBandedGrid('Tv2',Tv2,'产品排行');
end;

procedure TfrmCPTopList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPTopList.P_CodeChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCPTopList.cxPageControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCPTopList.ToolButton1Click(Sender: TObject);
var
  fPrintFile:string;
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('exec P_Print_PBCK '+quotedstr(Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date)))+' ,''年'' ');
    Open;
//    ShowMessage(SQL.Text);
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Prt);
  SInitCDSData20(ADOQueryTemp,CDS_Prt);
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\年发货报表.rmf' ;
  if FileExists(fPrintFile) then
  begin
//    RMVariables['ConNo']:=Order_Main.fieldbyname('ConNo').Value;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\年发货报表.rmf'),'提示',0);
  end;
end;

procedure TfrmCPTopList.ToolButton2Click(Sender: TObject);
var
  fPrintFile:string;
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('exec P_Print_PBCK '+quotedstr(Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date)))+' ,''月'' ');
    Open;
//    ShowMessage(SQL.Text);
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Prt);
  SInitCDSData20(ADOQueryTemp,CDS_Prt);
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\月发货报表.rmf' ;
  if FileExists(fPrintFile) then
  begin
//    RMVariables['ConNo']:=Order_Main.fieldbyname('ConNo').Value;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\月发货报表.rmf'),'提示',0);
  end;
end;

procedure TfrmCPTopList.ToolButton3Click(Sender: TObject);
var
  fPrintFile:string;
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('exec P_Print_PBCK '+quotedstr(Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date)))+' ,''年客户'' ');
    Open;
//    ShowMessage(SQL.Text);
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Prt);
  SInitCDSData20(ADOQueryTemp,CDS_Prt);
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\客户排列年报表.rmf' ;
  if FileExists(fPrintFile) then
  begin
//    RMVariables['ConNo']:=Order_Main.fieldbyname('ConNo').Value;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\客户排列年报表.rmf'),'提示',0);
  end;
end;

end.
