unit U_CKProductBCPOutListHZ;

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
  TfrmCKProductBCPOutListHZ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    Label4: TLabel;
    MPRTCodeName: TEdit;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    cxgrdPRTColor: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label5: TLabel;
    orderNo: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    v1Column4: TcxGridDBColumn;
    Label10: TLabel;
    CkOrdNo: TEdit;
    cxgrdganghao: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    DS_JuanPRT: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label11: TLabel;
    v1Column15: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    conNo: TEdit;
    Label15: TLabel;
    Label17: TLabel;
    apid: TEdit;
    cxgrdAPID: TcxGridDBColumn;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    RM3: TRMGridReport;
    v1Column24: TcxGridDBColumn;
    RMDBPRT: TRMDBDataSet;
    CDS_PRT: TClientDataSet;
    Label22: TLabel;
    KHNmae: TEdit;
    Label24: TLabel;
    PRTColor: TEdit;
    ganghao: TEdit;
    v1Column13: TcxGridDBColumn;
    Label1: TLabel;
    CKType: TComboBox;
    Label6: TLabel;
    lidan: TEdit;
    Label7: TLabel;
    ywy: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    chkColor: TCheckBox;
    chkConno: TCheckBox;
    ToolButton1: TToolButton;
    CheckBox1: TCheckBox;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    PS: TEdit;
    NETKGQty: TEdit;
    KGQty: TEdit;
    Qty: TEdit;
    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 CKTypeChange(Sender: TObject);
    procedure edtSMNOKeyPress(Sender: TObject; var Key: Char);
    procedure FormDestroy(Sender: TObject);
    procedure chkColorClick(Sender: TObject);
    procedure chkConnoClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
  private
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

var
  frmCKProductBCPOutListHZ: TfrmCKProductBCPOutListHZ;

implementation

uses
  U_DataLink, U_Fun, U_SysLogHelp,U_RTFun;

{$R *.dfm}

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

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

procedure TfrmCKProductBCPOutListHZ.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;
      if (chkColor.Checked = False) and (chkConno.Checked = False) then
      begin
        cxgrdganghao.Visible := True;
        cxgrdPRTColor.Visible := True;
        cxgrdAPID.Visible := True;
        sql.Add('select top '+inttostr(RecordsNumber)+' *  from ');
        sql.Add('(select rownumber=Cast((row_number() over(order by  CKDate desc)) as int)');
        sql.Add(',COUNT(1) OVER() AS TotalCount,* from (');

        sql.Add(' select  * 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(fwhere);

        sql.Add(') temp_row)AA');
        sql.Add(' where rownumber> '+inttostr((CurrentPage-1)*RecordsNumber) );
      end;
      if chkColor.Checked then
      begin
        cxgrdganghao.Visible := False;
        cxgrdPRTColor.Visible := True;
        cxgrdAPID.Visible := False;
        sql.Add('select top '+inttostr(RecordsNumber)+' *  from ');
        sql.Add('(select rownumber=Cast((row_number() over(order by  CKDate desc)) as int)');
        sql.Add(',COUNT(1) OVER() AS TotalCount,* from (');

        sql.Add('select  sum(PS) PS,sum(NETKGQty) NETKGQty,sum(KGQty) KGQty,sum(Qty) Qty   ');
        sql.Add(' ,CKOrdNo,MainId,SubId,QtyUnit,APID='''',ganghao='''',KuanHao,KHNmae,CKDate,CKType,OrderNo  ');
        sql.Add(',MPRTCodeName,MPRTMF,MPRTKZ,ConNo,KHConno,lidan,YWY,PRTColor,PRTEColor   ');
        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(fwhere);
        sql.Add(' group by   CKOrdNo,MainId,SubId,QtyUnit,KuanHao,KHNmae,CKDate,CKType ');
        sql.Add(' ,OrderNo,MPRTCodeName,MPRTMF,MPRTKZ,ConNo,KHConno,lidan,YWY,PRTColor,PRTEColor  ');

        sql.Add(') temp_row)AA');
        sql.Add(' where rownumber> '+inttostr((CurrentPage-1)*RecordsNumber) );

      end;
      if chkConno.Checked then
      begin
        cxgrdganghao.Visible := False;
        cxgrdPRTColor.Visible := False;
        cxgrdAPID.Visible := False;
        sql.Add('select top '+inttostr(RecordsNumber)+' *  from ');
        sql.Add('(select rownumber=Cast((row_number() over(order by  CKDate desc)) as int)');
        sql.Add(',COUNT(1) OVER() AS TotalCount,* from (');

        sql.Add('select  sum(PS) PS,sum(NETKGQty) NETKGQty,sum(KGQty) KGQty,sum(Qty) Qty   ');
        sql.Add(' ,CKOrdNo,MainId,QtyUnit,APID='''',ganghao='''',KuanHao,KHNmae,CKDate,CKType,OrderNo  ');
        sql.Add(',MPRTCodeName,MPRTMF,MPRTKZ,ConNo,KHConno,lidan,YWY,PRTColor='''',PRTEColor=''''   ');
        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(fwhere);
        sql.Add(' group by   CKOrdNo,MainId,QtyUnit,KuanHao,KHNmae,CKDate,CKType ');
        sql.Add(' ,OrderNo,MPRTCodeName,MPRTMF,MPRTKZ,ConNo,KHConno,lidan,YWY  ');

        sql.Add(') temp_row)AA');
        sql.Add(' where rownumber> '+inttostr((CurrentPage-1)*RecordsNumber) );

      end;
      //ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select sum(PS) PS,sum(NETKGQty) NETKGQty,sum(KGQty) KGQty,sum(Qty) Qty ');
      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(fwhere);
      Open;
    end;
    PS.Text:=Trim(ADOQueryTemp.fieldbyname('PS').AsString);
    NETKGQty.Text:=Trim(ADOQueryTemp.fieldbyname('NETKGQty').AsString);
    KGQty.Text:=Trim(ADOQueryTemp.fieldbyname('KGQty').AsString);
    Qty.Text:=Trim(ADOQueryTemp.fieldbyname('Qty').AsString);
    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;

end;

procedure TfrmCKProductBCPOutListHZ.TBRafreshClick(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

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

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

  Close;
end;

procedure TfrmCKProductBCPOutListHZ.FormShow(Sender: TObject);
begin
  ReadCxGridAll(trim(self.Caption), Tv1);
  CurrentPage := 1;
  RecordsNumber := 100;
end;

procedure TfrmCKProductBCPOutListHZ.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  SelExportData(Tv1, ADOQueryMain, '');
end;

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

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

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

procedure TfrmCKProductBCPOutListHZ.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

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

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

procedure TfrmCKProductBCPOutListHZ.CKTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

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

  end;
end;

procedure TfrmCKProductBCPOutListHZ.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPOutListHZ := nil;
end;

procedure TfrmCKProductBCPOutListHZ.chkColorClick(Sender: TObject);
begin
  if chkColor.Checked = True then
    chkConno.Checked := False;
  InitGrid();
end;

procedure TfrmCKProductBCPOutListHZ.chkConnoClick(Sender: TObject);
begin
  if chkConno.Checked = True then
    chkColor.Checked := False;
  InitGrid();
end;

procedure TfrmCKProductBCPOutListHZ.ToolButton1Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID, FKHName, FFPNo: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  ExportFtErpFile('次品出库单.rmf', ADOQueryTemp);
  with ADOQueryTemp do
  begin

    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMDCPCK ');
    sql.add('@CKOrdNo=' + quotedstr(Trim(Trim(CDS_Main.fieldbyname('CKOrdNo').AsString))));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PRT);
  SInitCDSData20(ADOQueryTemp, CDS_PRT);

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\次品出库单.rmf';
  if FileExists(fPrintFile) then
  begin
    RM3.LoadFromFile(fPrintFile);
    RMVariables['Filler'] := trim(DName);
    RM3.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + fPrintFile), '提示', 0);
  end;

end;

procedure TfrmCKProductBCPOutListHZ.CheckBox1Click(Sender: TObject);
var
  i:Integer;
begin
  with Panel1 do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if CheckBox1.Checked=True then
        Controls[i].Tag:=1
      else
        Controls[i].Tag:=2;
    end;
  end;

end;

procedure TfrmCKProductBCPOutListHZ.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmCKProductBCPOutListHZ.BTNPClick(Sender: TObject);
begin
  if CurrentPage < CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

end.

