unit U_CPCKHZLD;

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, RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System,
  RM_GridReport, cxCheckBox, Menus, MovePanel, RM_E_llPDF, cxCalendar, Math,
  Clipbrd, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmCPCKHZLD = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    MPRTCodeName: TEdit;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Column15: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    conNo: TEdit;
    Label15: TLabel;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    Label1: TLabel;
    Label6: TLabel;
    lidan: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    cxGrid1: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    MovePanel2: TMovePanel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource2: TDataSource;
    CDS_Sub: TClientDataSet;
    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 FormDestroy(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure conNoChange(Sender: TObject);
  private
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

var
  frmCPCKHZLD: TfrmCPCKHZLD;

implementation

uses
  U_DataLink, U_Fun, U_SysLogHelp,U_RTFun;

{$R *.dfm}

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

procedure TfrmCPCKHZLD.FormCreate(Sender: TObject);
begin
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 3;
end;

procedure TfrmCPCKHZLD.InitGrid();
var
  fwhere, Pwhere: string;
begin
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
 { Pwhere := SGetFilters(Panel1, 1, 2);

  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' and ' + trim(Pwhere); }

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select ConNo,MPRTCodeName,lidan , sum(PS) PS,sum(KGQty) KGQty,sum(MQty) MQty,sum(YQty) YQty');
      sql.Add('From(select A.*,Case when isnull(QtyUnit,'''')=''M'' then Qty else Null End As MQty ');
      sql.Add(',Case when isnull(QtyUnit,'''')=''Y'' then Qty else Null End As YQty ');
      sql.Add('from(select ConNo,MPRTCodeName,lidan , sum(PS) PS,sum(NETKGQty) KGQty,sum(Qty) Qty ,QtyUnit  ');
      sql.Add(' from P_FinishCK     ');
      sql.Add(' where   CKDate>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
      sql.Add(' and  CKDate<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
      sql.Add(' and isnull(CKType,'''')=''销售出库'' ');
      sql.Add(' group by   QtyUnit ,MPRTCodeName,ConNo,lidan )A )AA Group by MPRTCodeName,ConNo,lidan ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;

end;

procedure TfrmCPCKHZLD.TBRafreshClick(Sender: TObject);
begin

  InitGrid();
end;

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

procedure TfrmCPCKHZLD.TBCloseClick(Sender: TObject);
begin
  if Trim(DName)='ADMIN' then
  begin
    WriteCxGridAll(trim(self.Caption), Tv1,ADOQueryCmd,ADOQueryTemp);
    WriteCxGridAll(trim(self.Caption)+'1', Tv2,ADOQueryCmd,ADOQueryTemp);
  end;

  Close;
end;

procedure TfrmCPCKHZLD.FormShow(Sender: TObject);
begin
  ReadCxGridAll(trim(self.Caption), Tv1);
  ReadCxGridAll(trim(self.Caption)+'1', Tv2);
end;

procedure TfrmCPCKHZLD.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  TcxGridToExcel(Self.Caption,cxGrid2);
end;

procedure TfrmCPCKHZLD.FormDestroy(Sender: TObject);
begin
  frmCPCKHZLD := nil;
end;

procedure TfrmCPCKHZLD.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Main.IsEmpty then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select PRTColor , sum(PS) PS,sum(KGQty) KGQty,sum(MQty) MQty,sum(YQty) YQty');
    sql.Add('From(select A.*,Case when isnull(QtyUnit,'''')=''M'' then Qty else Null End As MQty ');
    sql.Add(',Case when isnull(QtyUnit,'''')=''Y'' then Qty else Null End As YQty ');
    sql.Add('from(select PRTColor ,sum(PS) PS,sum(NETKGQty) KGQty,sum(Qty) Qty ,QtyUnit  ');
    sql.Add(' from P_FinishCK     ');
    sql.Add(' where   CKDate>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
    sql.Add(' and  CKDate<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
    sql.Add(' and isnull(CKType,'''')=''销售出库'' ');
    sql.Add(' and ConNo='''+Trim(CDS_Main.fieldbyname('ConNo').AsString)+'''');
    sql.Add(' and lidan='''+Trim(CDS_Main.fieldbyname('lidan').AsString)+'''');
    sql.Add(' group by   QtyUnit,PRTColor)A  )AA Group by PRTColor ');
    Open;
  end;
  SCreateCDS20(ADOQueryCmd,CDS_Sub);
  SInitCDSData20(ADOQueryCmd,CDS_Sub);
end;

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

end.

