unit U_CKProductBCPKCHZList;

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;

type
  TfrmCKProductBCPKCHZList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    C_CodeName: TEdit;
    C_Color: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    Label5: TLabel;
    conNO: TEdit;
    Label6: TLabel;
    MJID: TEdit;
    v1Column7: TcxGridDBColumn;
    Label8: TLabel;
    KZ: TEdit;
    Label9: TLabel;
    MF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    MovePanel2: TMovePanel;
    v1KCRollNum: TcxGridDBColumn;
    Label10: TLabel;
    Edit1: TEdit;
    v1Column1: TcxGridDBColumn;
    C_CodeName_ZW: TEdit;
    Label11: TLabel;
    v1Column2: TcxGridDBColumn;
    BCKZ1: TEdit;
    Label12: TLabel;
    batchNo: TEdit;
    Label13: TLabel;
    TBMX: TToolButton;
    v1Column3: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBMXClick(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
  private
    procedure InitGrid();
    { Private declarations }
  public
    fckName:string;
    { Public declarations }
  end;

var
  frmCKProductBCPKCHZList: TfrmCKProductBCPKCHZList;

implementation
uses
  U_DataLink,U_Fun,U_CKProductBCPKCList;

{$R *.dfm}

procedure TfrmCKProductBCPKCHZList.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPKCHZList:=nil;
end;

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

procedure TfrmCKProductBCPKCHZList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCKProductBCPKCHZList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin

      Close;
      Filtered:=False;
      sql.Clear;
      sql.Add('select C_CodeName,C_Color,C_CodeName_ZW,BCKZ,cast(BCKZ as varchar) as BCKZ1,sum(KCQty) KCQty,Sum(KCKgQty) KCKgQty,Sum(KCRollNum) KCRollNum');
      sql.Add(',XSPs=(select COUNT(MJID) from CK_BanCP_CR X where DATEDIFF(DAY, FillTime,GETDATE())<=90 and CRType=''配货出库'' and X.C_CodeName=AA.C_CodeName and X.C_Color=AA.C_Color)');
      sql.Add('from( ');
      sql.Add(' select A.C_CodeName,A.C_Color,A.batchNo,A.KCQty,A.KCKgQty,A.KCRollNum,B.BCKZ, ');
      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 ');
      sql.Add(' inner join CK_BanCP_CR B on B.BCID=A.BCID ');
      sql.Add(' where A.KCQty>0');
   //   IF trim(fCKName)<>'' then
   //   sql.Add(' and A.CKName='''+trim(fckName)+''' ');
      sql.Add(' )AA ');
      sql.Add(' group by C_CodeName,C_Color,C_CodeName_ZW,BCKZ,cast(BCKZ as varchar)  order by C_CodeName,dbo.getNum(C_Color)');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmCKProductBCPKCHZList.TBRafreshClick(Sender: TObject);
begin
  //BegDate.SetFocus;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible:=False;
end;

procedure TfrmCKProductBCPKCHZList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  end;
end;

procedure TfrmCKProductBCPKCHZList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('仓库库存汇总列表2',Tv1,'成品仓库');
  Close;
end;

procedure TfrmCKProductBCPKCHZList.FormShow(Sender: TObject);
begin

  ReadCxGrid('仓库库存汇总列表2',Tv1,'成品仓库');

  InitGrid();
end;

procedure TfrmCKProductBCPKCHZList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('库存汇总列表',cxGrid2);
end;

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

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

procedure TfrmCKProductBCPKCHZList.TBMXClick(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  frmCKProductBCPKCList:=TfrmCKProductBCPKCList.create(self);
  with frmCKProductBCPKCList do
  begin
    C_CodeName.Text:=self.CDS_Main.fieldbyname('C_CodeName').AsString;
    C_CodeName.Tag:=1;
    C_Color.Text:=self.CDS_Main.fieldbyname('C_Color').AsString;
    C_Color.Tag:=1;
    BCKZ1.text:=self.CDS_Main.fieldbyname('BCKZ1').AsString;
    TBRafresh.Click;
    show;
  end;
end;

procedure TfrmCKProductBCPKCHZList.Tv1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  IF AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('xsps').Index]<60 then
  ACanvas.Brush.Color:=clred;
end;

end.
