unit U_CPCKHZ;

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
  TfrmCPCKHZ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column6: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    MovePanel2: TMovePanel;
    DS_JuanPRT: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM3: TRMGridReport;
    Label22: TLabel;
    CustomerNoName: TEdit;
    Label24: TLabel;
    Label1: TLabel;
    JGTYPE: TComboBox;
    CDS_Label: TClientDataSet;
    v1Column14: TcxGridDBColumn;
    Label9: TLabel;
    MPRTCode: TEdit;
    ADOQuery1: TADOQuery;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    cxGrid1: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DataSource2: TDataSource;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure CPTypeChange(Sender: TObject);
    procedure JGTYPEChange(Sender: TObject);
    procedure edtSMNOKeyPress(Sender: TObject; var Key: Char);
    procedure FormDestroy(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);

  private
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();

    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

var
  frmCPCKHZ: TfrmCPCKHZ;

implementation

uses
  U_DataLink, U_Fun, U_SysLogHelp, U_CKProductBCPOutListHZFS;

{$R *.dfm}

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

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

procedure TfrmCPCKHZ.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   sum(qty)qty,MPRTCode from CK_BanCP_CR  a inner join JYOrder_Main b on a.MainId=b.mainid  ');
      sql.Add(' WHERE  A.CRFlag=''出库'' AND  A.CRTYPE=''销售出库''  AND    A.CRTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
      sql.Add(' and  A.CRTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
      sql.Add(fwhere);

      SQL.Add('group by   MPRTCode');

      SQL.Add('ORDER BY    sum(qty) DESC ');
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);

    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;
//  SQL.Add('select  jgtype,sum(qty)qty,MPRTCode from CK_BanCP_CR  a inner join JYOrder_Main b on a.MainId=b.mainid  ');
//  sql.Add(' WHERE  A.CRFlag=''出库'' AND  A.CRTYPE=''销售出库''  AND    A.CRTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
//  sql.Add(' and  A.CRTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
//  sql.Add(fwhere);
//
//  SQL.Add('group by jgtype,MPRTCode');
//
//  SQL.Add('ORDER BY    sum(qty) DESC ');
end;

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

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

procedure TfrmCPCKHZ.TBCloseClick(Sender: TObject);
begin

  WriteCxGrid(trim(self.Caption), Tv1, '成品仓库');
  WriteCxGrid(trim(self.Caption) + 'TV2', Tv2, '成品仓库');
  Close;
end;

procedure TfrmCPCKHZ.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid(trim(self.Caption), Tv1, '成品仓库');
  ReadCxGrid(trim(self.Caption) + 'TV2', Tv2, '成品仓库');
  fsj := 'select distinct(JGTYPE) NAME from JYOrder_Main    ';
  SInitComBoxBySql(ADOQueryCmd, JGTYPE, true, fsj);

end;

procedure TfrmCPCKHZ.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  SelExportData(Tv1, ADOQueryMain, '客户产品出库汇总');
end;

procedure TfrmCPCKHZ.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmCPCKHZ.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmCPCKHZ.orderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmCPCKHZ.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  try
    frmCKProductBCPOutListHZFS := TfrmCKProductBCPOutListHZFS.Create(Application);
    with frmCKProductBCPOutListHZFS do
    begin
      FCKOrdNo := Self.CDS_Main.fieldbyname('CKOrdNo').AsString;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmCKProductBCPOutListHZFS.Free;
  end;
end;

procedure TfrmCPCKHZ.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

procedure TfrmCPCKHZ.CPTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCPCKHZ.JGTYPEChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCPCKHZ.edtSMNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin

  end;
end;

procedure TfrmCPCKHZ.FormDestroy(Sender: TObject);
begin
  frmCPCKHZ := nil;
end;

procedure TfrmCPCKHZ.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
var
  fwhere, Pwhere: string;
begin

  Pwhere := SGetFilters(Panel1, 1, 2);

  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' AND  ' + trim(Pwhere);
  try
    ADOQueryCmd.DisableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;

      SQL.Add('select   jgtype,CustomerNoName,sum(qty)qty,MPRTCode from CK_BanCP_CR  a inner join JYOrder_Main b on a.MainId=b.mainid  ');
      sql.Add(' WHERE  A.CRFlag=''出库'' AND  A.CRTYPE=''销售出库''  AND    A.CRTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
      sql.Add(' and  A.CRTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
      sql.Add('and MPRTCode=' + QuotedStr(Trim(CDS_Main.fieldbyname('MPRTCode').AsString)));
      sql.Add(fwhere);

      SQL.Add('group by   MPRTCode,jgtype,CustomerNoName');

      SQL.Add('ORDER BY    sum(qty) DESC ');
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryCmd, CDS_HZ);
    SInitCDSData20(ADOQueryCmd, CDS_HZ);

    MovePanel2.Visible := false;
  finally
    ADOQueryCmd.EnableControls;
    MovePanel2.Visible := false;
  end;
end;

end.

