unit U_CKDaBaoList;

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, cxPC, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu;

type
  TfrmCKDaBaoList = 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;
    v2Column3: TcxGridDBColumn;
    cxgrdMJGW: TcxGridDBColumn;
    cxgrdMJLen: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    v1Column4: TcxGridDBColumn;
    Label10: TLabel;
    CKOrdNo: TEdit;
    TBCKCX: TToolButton;
    v1Column11: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    DS_JuanPRT: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxgrdMJNW: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    conNo: TEdit;
    Label15: TLabel;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    RM3: TRMGridReport;
    RMDBPRT: TRMDBDataSet;
    CDS_PRT: TClientDataSet;
    PRTColor: TEdit;
    ComboBox1: TComboBox;
    ToolButton1: TToolButton;
    VC_baono: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    Panel2: TPanel;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    cxTabControl1: TcxTabControl;
    cxgrdPS: TcxGridDBColumn;
    Label1: TLabel;
    baoid: TEdit;
    Label5: TLabel;
    VC_BaoQty: TcxGridDBColumn;
    Label6: TLabel;
    SmNO: 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 TBCKCXClick(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 CRTypeChange(Sender: TObject);
    procedure edtSMNOKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure SmNOKeyPress(Sender: TObject; var Key: Char);
  private
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

//var
//  frmCKProductBCPOutList: TfrmCKProductBCPOutList;

implementation

uses
  U_DataLink, U_Fun, U_SysLogHelp,U_RTFun;

{$R *.dfm}
procedure TfrmCKDaBaoList.SetStatus();
var
  i: Integer;
begin

  case cxTabControl1.TabIndex of
    0:
      begin
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := false;
        end;
        ClearOrHideControls(Panel1, '0', True, True);
        ClearOrHideControls(Panel1, '1', True, False);
        IsVisibleTV(Panel1, TV1, '0', True);
        VC_BaoQty.Visible := true;
      end;
    1:
      begin
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := false;
        end;
        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', False, True);
        IsVisibleTV(Panel1, TV1, '0', True);
        IsVisibleTV(Panel1, TV1, '1', True);
        VC_baono.Visible := true;
      end;
  end;
  cxgrdPS.Visible := true;
  cxgrdMJGW.Visible := true;
  cxgrdMJNW.Visible := true;
  cxgrdMJLen.Visible := true;
end;

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

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

procedure TfrmCKDaBaoList.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 + ' where ' + trim(Pwhere);

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      case cxTabControl1.TabIndex of

        0:   //////////////////////////订单/////////////////////////////
          begin
            sql.Add(' select AAA.* from ( ');
            sql.Add(' select AA.CKOrdNo,BB.conNo,BB.MPRTCodeName,CC.PRTColor,COUNT(BaoID) BaoQty,sum(PIQty) PIQty,sum(Qty) Qty, sum(NetKGQty) NetKGQty,sum(KGQty) KGQty from ( ');
            sql.Add(' select CKOrdNo,MainID,SubID,BaoID,BaoNo,count(MJID) PIQty,sum(Qty) Qty, sum(NetKGQty) NetKGQty,sum(KGQty) KGQty   ');
            sql.Add(' from CK_BanCP_CR A ');
            sql.Add(' where isnull(A.BaoID,'''')<>'''' and A.CRFlag in (''待出库'',''出库'') ');
            sql.Add(' and exists(select FSId from FHSQ_Main_CP X where X.FSID=A.CKOrdNo  ');
            sql.Add(' and X.filltime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
            sql.Add(' and X.filltime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
            sql.Add('    ) ');
            sql.Add('GROUP BY CKOrdNo,MainID,SubID,BaoID,BaoNo ');
            sql.Add(') AA  inner join JYOrder_Main BB on AA.MainID=BB.MainId inner join JYOrder_Sub CC on AA.SubID=CC.SubId  ');
            sql.Add('GROUP BY AA.CKOrdNo,BB.conNo,BB.MPRTCodeName,CC.PRTColor ');
            sql.Add(' ) AAA ' + fwhere);
          end;
        1:   /////////////////////////包//////////////////////////////
          begin
            sql.Add(' select AAA.* from ( ');
            sql.Add(' select AA.CKOrdNo,BB.conNo,BB.MPRTCodeName,CC.PRTColor,AA.BaoID,AA.BaoNo,PIQty,Qty, NetKGQty,KGQty from ( ');
            sql.Add(' select CKOrdNo,MainID,SubID,BaoID,BaoNo,count(MJID) PIQty,sum(Qty) Qty, sum(NetKGQty) NetKGQty,sum(KGQty) KGQty   ');
            sql.Add(' from CK_BanCP_CR A ');
            sql.Add(' where isnull(A.BaoID,'''')<>'''' and A.CRFlag in (''待出库'',''出库'') ');
            sql.Add(' and exists(select FSId from FHSQ_Main_CP X where X.FSID=A.CKOrdNo ');
            sql.Add(' and X.filltime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
            sql.Add(' and X.filltime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
            sql.Add('    ) ');
            sql.Add('GROUP BY CKOrdNo,MainID,SubID,BaoID,BaoNo ');
            sql.Add(') AA  inner join JYOrder_Main BB on AA.MainID=BB.MainId inner join JYOrder_Sub CC on AA.SubID=CC.SubId  ');
            sql.Add(' ) AAA ' + fwhere);
          end;
      end;
//      ShowMessage(SQL.Text);
      Open;
    end;

    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);

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

end;

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

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

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

  Close;
end;

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

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

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

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

procedure TfrmCKDaBaoList.TBCKCXClick(Sender: TObject);
var
  FFMainId, FPrice, strsql: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要撤销数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      while CDS_Main.Locate('SSel', True, []) = True do
      begin
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_BanCP_CR Set CRFlag=''待出库'',CRType='''',CRTime=NULL,CRNote='''',CKOrdNo='''' ');
            sql.Add(' where MJID=''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add(' and CRFlag=''出库''');

            sql.Add(' UPdate CK_BanCP_KC Set KCValid=''Y'' ,CKDate=0  ');
            SQL.Add(' where MJID=''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + '''');

            sql.Add(' Update WFB_MJJY Set MJStr2=''已入库'' where MJID=''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + ''' ');

            ExecSQL;
          end;

          strsql := '卷ID：' + trim(CDS_Main.FieldByName('MJID').AsString) + ' 出库单号：' + trim(CDS_Main.FieldByName('CKOrdNo').AsString);

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
            sql.Add(' ' + quotedstr(trim(DName)));
            sql.Add(',getdate() ');
            sql.Add(',' + quotedstr(trim(self.Caption)));
            sql.Add(',' + quotedstr(trim('出库撤销')));
            sql.Add(',' + quotedstr(trim(strsql)));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;

          CDS_Main.Delete;
        end;
      end;
    end;
    CDS_Main.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible := False;
    Exit;
  except
    MovePanel2.Visible := False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！', '提示', 0);
  end;

end;

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

procedure TfrmCKDaBaoList.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 TfrmCKDaBaoList.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

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

procedure TfrmCKDaBaoList.CRTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

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

  end;
end;

procedure TfrmCKDaBaoList.ToolButton1Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID, FKHName, FFPNo: string;
begin
  if CDS_Main.IsEmpty then
    Exit;

  if trim(ComboBox1.Text) = '仓库打包码单' then
  begin
    with ADOQueryTemp do
    begin

      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD1 ');
      sql.add('@CKOrdNo=' + quotedstr(Trim(Trim(CDS_Main.fieldbyname('CKOrdNo').AsString))));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_PRT);
    SInitCDSData20(ADOQueryTemp, CDS_PRT);
  end;
  if trim(ComboBox1.Text) = '单证打包码单' then
  begin
    with ADOQueryTemp do
    begin

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

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD3 ');
      sql.add('@CKOrdNo=' + quotedstr(Trim(Trim(CDS_Main.fieldbyname('CKOrdNo').AsString))));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);
  end;
  RMXLSExport1:= TRMXLSExport.Create(RMXLSExport1);
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox1.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM3.LoadFromFile(fPrintFile);
    RMVariables['ZDR'] := trim(DName);
    RM3.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmCKDaBaoList.Tv1DblClick(Sender: TObject);
var
  Tabint: integer;
begin
  Tabint := cxTabControl1.TabIndex;
  case Tabint of
    0:
      begin
        AssignmentControls(Panel1, CDS_Main, '0');
        cxTabControl1.TabIndex := 1;
      end;
  end;
end;

procedure TfrmCKDaBaoList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

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

    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        case cxTabControl1.TabIndex of

          0:   //////////////////////////订单/////////////////////////////
            begin
              sql.Add(' select AAA.* from ( ');
              sql.Add(' select AA.CKOrdNo,BB.conNo,BB.MPRTCodeName,CC.PRTColor,COUNT(BaoID) BaoQty,sum(PIQty) PIQty,sum(Qty) Qty, sum(NetKGQty) NetKGQty,sum(KGQty) KGQty from ( ');
              sql.Add(' select CKOrdNo,MainID,SubID,BaoID,BaoNo,count(MJID) PIQty,sum(Qty) Qty, sum(NetKGQty) NetKGQty,sum(KGQty) KGQty   ');
              sql.Add(' from CK_BanCP_CR A ');
              sql.Add(' where isnull(A.BaoID,'''')<>'''' and A.CRFlag in (''待出库'',''出库'') ');
              sql.Add(' and A.CKOrdNo =  ' + quotedstr(Trim(SmNO.Text)));
              sql.Add('GROUP BY CKOrdNo,MainID,SubID,BaoID,BaoNo ');
              sql.Add(') AA  inner join JYOrder_Main BB on AA.MainID=BB.MainId inner join JYOrder_Sub CC on AA.SubID=CC.SubId  ');
              sql.Add('GROUP BY AA.CKOrdNo,BB.conNo,BB.MPRTCodeName,CC.PRTColor ');
              sql.Add(' ) AAA ');
            end;
          1:   /////////////////////////包//////////////////////////////
            begin
              sql.Add(' select AAA.* from ( ');
              sql.Add(' select AA.CKOrdNo,BB.conNo,BB.MPRTCodeName,CC.PRTColor,AA.BaoID,AA.BaoNo,PIQty,Qty, NetKGQty,KGQty from ( ');
              sql.Add(' select CKOrdNo,MainID,SubID,BaoID,BaoNo,count(MJID) PIQty,sum(Qty) Qty, sum(NetKGQty) NetKGQty,sum(KGQty) KGQty   ');
              sql.Add(' from CK_BanCP_CR A ');
              sql.Add(' where isnull(A.BaoID,'''')<>'''' and A.CRFlag in (''待出库'',''出库'') ');
              sql.Add(' and A.CKOrdNo =  ' + quotedstr(Trim(SmNO.Text)));
              sql.Add('GROUP BY CKOrdNo,MainID,SubID,BaoID,BaoNo ');
              sql.Add(') AA  inner join JYOrder_Main BB on AA.MainID=BB.MainId inner join JYOrder_Sub CC on AA.SubID=CC.SubId  ');
              sql.Add(' ) AAA ');
            end;
        end;
//      ShowMessage(SQL.Text);
        Open;
      end;

      SCreateCDS20(ADOQueryMain, CDS_Main);
      SInitCDSData20(ADOQueryMain, CDS_Main);

      MovePanel2.Visible := false;
    finally
      ADOQueryMain.EnableControls;
      MovePanel2.Visible := false;
    end;
    SmNO.Text := '';
    SmNO.SetFocus;
  end;
end;

end.

