unit U_CP_KC;

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, cxGridDBTableView;

type
  TfrmCP_KC = 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;
    ToolButton1: TToolButton;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    C_CodeName_ZW: TEdit;
    Label3: TLabel;
    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 ToolButton1Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure FormShow(Sender: TObject);
    procedure TBexcelClick(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    canshu1,fCKName:string;
    { Public declarations }
  end;

var
  frmCP_KC: TfrmCP_KC;

implementation
uses
  U_DataLink,U_Fun10,U_CKProductBCPKCHZList;
{$R *.dfm}
procedure TfrmCP_KC.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      Filtered:=False;
      sql.Clear;
      sql.Add(' select C_CodeName,sum(KCQty) KCQty,Sum(KCKgQty) KCKgQty,Sum(KCRollNum) KCRollNum, ');
      sql.Add(' C_CodeName_ZW=(select Top 1 defnote3 from KH_Zdy_Attachment X where X.ZdyName=A.C_CodeName and X.Type=''CP'') ');
      sql.Add(' from CK_BanCP_KC A ');
    //  IF trim(fCKName)<>'' then
     // sql.Add(' and A.CKName='''+trim(fckName)+''' ');
      sql.Add(' where A.KCQty>0');
      sql.Add(' group by C_CodeName  order by C_CodeName');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  TBfind.Click;
end;

procedure TfrmCP_KC.FormDestroy(Sender: TObject);
begin
  frmCP_KC:=nil;
end;

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

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

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

procedure TfrmCP_KC.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_KC.C_CodeNameChange(Sender: TObject);
begin
  TBfind.Click;
end;

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

procedure TfrmCP_KC.ToolButton1Click(Sender: TObject);
begin
  IF cds_main.IsEmpty then exit;
  frmCKProductBCPKCHZList:=TfrmCKProductBCPKCHZList.create(self);
  with frmCKProductBCPKCHZList do
  begin
    C_CodeName.Text:=self.cds_main.fieldbyname('C_CodeName').asstring;
    C_CodeName.Tag:=1;
    show;
  end;
end;

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

procedure TfrmCP_KC.FormShow(Sender: TObject);
begin
  InitGrid();
end;

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

end.
