unit U_CP_XL;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, ToolWin, DBClient, DB, ADODB,
  cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxControls,
  cxGridCustomView, cxClasses, cxGridLevel, cxGrid;

type
  TfrmCP_XL = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    C_CodeName: TEdit;
    ConNoM: TEdit;
    Label2: TLabel;
    ADOQueryMain: TADOQuery;
    DS_Main: TDataSource;
    CDS_main: TClientDataSet;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBBandedTableView;
    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;
    ToolButton1: TToolButton;
    TBexcel: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBexcelClick(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    canshu1:string;
    { Public declarations }
  end;

var
  frmCP_XL: TfrmCP_XL;

implementation
uses
  U_DataLink,U_Fun10,U_CP_COL_XL;
{$R *.dfm}
procedure TfrmCP_XL.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      Filtered:=False;
      sql.Clear;
      sql.Add(' exec P_Get_CP_Xl ');
      SQL.Add(' @begdate='''+FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+'''');
      SQL.Add(',@enddate='''+FormatDateTime('yyyy-MM-dd',enddate.DateTime+1)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  TBfind.Click;
end;

procedure TfrmCP_XL.FormDestroy(Sender: TObject);
begin
  frmCP_XL:=nil;
end;

procedure TfrmCP_XL.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=cafree;
end;

procedure TfrmCP_XL.FormCreate(Sender: TObject);
begin
  enddate.DateTime:=now();
  begdate.DateTime:=enddate.DateTime;
  cxGrid1.Align:=alclient;
  canshu1:=trim(DParameters1);
end;

procedure TfrmCP_XL.TBCloseClick(Sender: TObject);
begin
  close;
end;

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

procedure TfrmCP_XL.C_CodeNameChange(Sender: TObject);
begin
  TBfind.Click;
end;

procedure TfrmCP_XL.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

procedure TfrmCP_XL.FormShow(Sender: TObject);
begin
  IF trim(canshu1)='桂华纺织'then
  begin
    tv1.Bands[2].Visible:=false;
    tv1.Bands[3].Visible:=false;
    tv1.Bands[4].Visible:=false;
  end;
  IF trim(canshu1)='尚美特'then
  begin
    tv1.Bands[1].Visible:=false;
    tv1.Bands[3].Visible:=false;
    tv1.Bands[4].Visible:=false;
  end;
  IF trim(canshu1)='金鼎丰门市部'then
  begin
    tv1.Bands[1].Visible:=false;
    tv1.Bands[2].Visible:=false;
    tv1.Bands[4].Visible:=false;
  end;
  InitGrid();
end;

procedure TfrmCP_XL.ToolButton1Click(Sender: TObject);
begin
  IF cds_main.IsEmpty then exit;
  frmCP_COL_XL:=TfrmCP_COL_XL.create(self);
  with frmCP_COL_XL do
  begin
    begdate.Date:=self.BegDate.Date;
    enddate.Date:=self.EndDate.date;
    C_Codename.Text:=self.cds_main.fieldbyname('C_CodeName').asstring;
    show;
  end;
end;

procedure TfrmCP_XL.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  ToolButton1.Click;
end;

procedure TfrmCP_XL.TBexcelClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel(self.Caption,cxGrid1);
end;

end.
