unit U_CKProductBCPOutList;

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, dxBarBuiltInMenu,
  cxPC, cxContainer, dxCore, cxDateUtils, cxTextEdit, cxMaskEdit, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmCKProductBCPOutList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    Label4: TLabel;
    spname: TEdit;
    Label2: TLabel;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label6: TLabel;
    MJID: TEdit;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label8: TLabel;
    spkz: TEdit;
    Label7: TLabel;
    v1Column3: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    v1Column4: TcxGridDBColumn;
    Label10: TLabel;
    CkOrdNo: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    TBCKCX: TToolButton;
    v1Column11: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    DS_JuanPRT: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label17: TLabel;
    apid: TEdit;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RMDBprt: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    RM3: TRMGridReport;
    v1Column24: TcxGridDBColumn;
    Label22: TLabel;
    CUSTNAME: TEdit;
    CPType: TComboBox;
    IsJYTime: TCheckBox;
    spColor: TEdit;
    v1Column13: TcxGridDBColumn;
    ComboBox1: TComboBox;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ADOQuery1: TADOQuery;
    RMDB_HZ: TRMDBDataSet;
    RMGridReport1: TRMGridReport;
    Panel2: TPanel;
    Panel3: TPanel;
    cxTabControl1: TcxTabControl;
    Panel6: TPanel;
    Label1: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    Tv1Column1: TcxGridDBColumn;
    BegDate: TcxDateEdit;
    EndDate: TcxDateEdit;
    Tv1Column2: TcxGridDBColumn;
    ComboBox2: TComboBox;
    Label5: TLabel;
    ToolButton3: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    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 BTNPClick(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure CPTypeChange(Sender: TObject);
    procedure CRTypeChange(Sender: TObject);
    procedure edtSMNOKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  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;

{$R *.dfm}
procedure TfrmCKProductBCPOutList.SetStatus();
var
  i: Integer;
begin
//  TBManage.Visible := False;
//  TBZF.Visible := False;
//  v1Column15.Options.Focusing := False;
//  ToolButton1.Visible := False;
//  ToolButton8.Visible := False;
//  ToolButton2.Visible := False;
//
//  if Trim(canshu1) = '管理' then
//  begin
//
//    case cxTabControl1.TabIndex of
//      0:
//        begin
//
//        end;
//      1:
//        begin
//          TBManage.Visible := True;
//          TBZF.Visible := True;
//          v1Column15.Options.Focusing := True;
//          ToolButton1.Visible := true;
//          ToolButton8.Visible := true;
//          ToolButton2.Visible := true;
//
//        end;
//      2:
//        begin
//
//        end;
//    end;
//  end;

  case cxTabControl1.TabIndex of
    0:
      begin
        ClearOrHideControls(Panel1, '0', True, True);
        ClearOrHideControls(Panel1, '1', True, False);

        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := false;
        end;
        IsVisibleTV(Panel1, TV1, '0', True);
        v2Column1.Visible := True;
        v2Column3.Visible := True;
        v1Column8.Visible := True;
        v1Column9.Visible := True;
        v2Column5.Visible := True;
        v1Column21.Visible := True;
        v2Column6.Visible := True;
        v1Column4.Visible := True;
        Tv1Column1.Visible := True;
        v1Column13.Visible := True;
      end;

    1:
      begin
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := True;
        end;

        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', False, True);
        Tv1Column1.Visible := FALSE;
      end;
  end;

end;

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

procedure TfrmCKProductBCPOutList.FormCreate(Sender: TObject);
begin
  EndDate.Text := FormatDateTime('yyyy-MM-dd', SGetServerDate10(ADOQueryTemp) + 1) + ' 00:00:00';
  BegDate.Text := FormatDateTime('yyyy-MM-dd', SGetServerDate10(ADOQueryTemp) - 1) + ' 00:00:00';
end;

procedure TfrmCKProductBCPOutList.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 IsJYTime.Checked then
//  begin
//    fwhere := ' where CRTime>' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
//    fwhere := fwhere + ' and CRTime<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
//    if trim(Pwhere) <> '' then
//      fwhere := fwhere + ' and ' + trim(Pwhere);
//  end
//  else


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

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;

      case cxTabControl1.TabIndex of
        0:   /////////////////////////订单汇总//////////////////////////////
          begin

            sql.Add(' select B.SPName,B.SPMF,SPKZ,SPCOLOR ,CUSTNAME,apid,CKOrdNo,sum(f.kcQty)kcQty,sum(f.kckgqty)kckgqty,count(*)ps   ');

            sql.Add(' from CK_BanCP_CR A  inner join CK_SXPB_CR B ON A.APID=B.SPID ');
            sql.Add('inner join CK_BanCP_KC f on f.mjid=a.mjid');
            sql.Add(' where  A.CRFlag=''出库''  AND isnull(ctype,'''')='''' ');

            if Self.Caption = '拆匹出库管理' then
            begin
              sql.Add(' AND    A.crtype in (''拆匹出库'',''国外拆匹出库'')');
            end
            else
            begin
              sql.Add(' AND     A.crtype <>''拆匹出库'' and A.crtype<>''国外拆匹出库''    ');
            end;

            if IsJYTime.Checked then
            begin
              sql.Add(' and   A.CRTime>' + quotedstr(BegDate.text));
              sql.Add(' and A.CRTime<=' + quotedstr(EndDate.Text));
            end;

            sql.Add(fwhere);
            sql.Add('group by B.SPName,B.SPMF,SPKZ,SPCOLOR ,apid,CKOrdNo,CUSTNAME');
            //ShowMessage(SQL.Text);
          end;
        1:      /////////////////////////明细//////////////////////////////
          begin
            sql.Add(' select top ' + IntToStr(RecordsNumber) + ' *  from  ');
            sql.Add('(select row_number() over(order by AA.filltime desc) as rownumber');
            sql.Add(',COUNT(1) OVER() AS TotalCount');
            sql.Add(',* from (select A.*,B.SPName,B.SPMF,SPKZ,SPCOLOR ,kcqty=(select kcqty from CK_BanCP_KC F WHERE F.MJID=A.MJID)   ');
            SQL.Add(',ISDY=cast((CASE WHEN A.PrintCount>0 THEN 1 ELSE 0 END )as bit)');
            sql.Add(' ,kckgqty=(select kckgqty from CK_BanCP_KC F WHERE F.MJID=A.MJID)  from CK_BanCP_CR A  inner join CK_SXPB_CR B ON A.APID=B.SPID  ');

            sql.Add(' where  A.CRFlag=''出库''  AND isnull(ctype,'''')='''' ');

            if Self.Caption = '拆匹出库管理' then
            begin
              sql.Add(' AND    A.crtype in (''拆匹出库'',''国外拆匹出库'')');
            end
            else
            begin
              sql.Add(' AND     A.crtype <>''拆匹出库'' and A.crtype<>''国外拆匹出库''');
            end;

            if IsJYTime.Checked then
            begin
              sql.Add(' and   A.CRTime>' + quotedstr(BegDate.Text));
              sql.Add(' and A.CRTime<=' + quotedstr(endDate.Text));
            end;
            sql.Add(fwhere);
            sql.Add('  ) AA');
            if ComboBox2.Text = '已打印' then
            begin
              sql.Add('where  ISDY=1');
            end;
            if ComboBox2.Text = '未打印' then
            begin
              sql.Add('where  ISDY=0');
            end;

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

      Open;
    end;
//    with ADOQueryMain do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('exec P_Page_FinishClothCK ');
//      sql.Add('@pageIndex=' + inttostr(CurrentPage));
//      sql.Add(',@pageSize=' + inttostr(RecordsNumber));
//      sql.Add(',@criteria= ' + quotedstr(fwhere));
//      Open;
//    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    if cxTabControl1.TabIndex = 1 then
    begin
      LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
    end;

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

end;

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

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

procedure TfrmCKProductBCPOutList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '成品仓库');
  Close;
end;

procedure TfrmCKProductBCPOutList.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid(trim(self.Caption), Tv1, '成品仓库');

  CurrentPage := 1;
  RecordsNumber := 500;
  if Trim(canshu1) = '管理' then
  begin
    TBCKCX.Visible := True;
  end
  else
  begin
    TBCKCX.Visible := false;
  end;
  SETSTATUS();
end;

procedure TfrmCKProductBCPOutList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  if cxTabControl1.TabIndex = 0 then
  begin
    TcxGridToExcel('成品出库汇总列表', cxGrid2);
  end
  else
  begin
    TcxGridToExcel('成品出库明细列表', cxGrid2);
  end;

//  SelExportData(Tv1, ADOQueryMain, '');
end;

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

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

procedure TfrmCKProductBCPOutList.TBCKCXClick(Sender: TObject);
var
  FFMainId, FPrice, strsql: string;
begin
  if cxTabControl1.TabIndex <> 1 then
    Exit;
  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='''',ctype='''' ');
            sql.Add(' where MJID=''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add(' and CRFlag=''出库''');

            sql.Add('Update CK_BanCP_CR Set  ctype=''''  ');
            sql.Add(' where MJID=''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add(' and CRFlag=''入库''');

            sql.Add(' UPdate CK_BanCP_KC Set KCValid=''Y'' ');
            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 TfrmCKProductBCPOutList.orderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

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

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

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

procedure TfrmCKProductBCPOutList.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid();
end;

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

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

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

  end;
end;

procedure TfrmCKProductBCPOutList.ToolButton1Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID, FKHName, FFPNo: string;
begin
  if cxTabControl1.TabIndex <> 1 then
    Exit;
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  if ComboBox1.Text = '' then
  begin
    Application.MessageBox('没有选择打印格式', '提示', 0);
    Exit;
  end;
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  CDS_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin

          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value := Trim(CDS_Main.fieldbyname('MJID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_Main.EnableControls;
  if (ComboBox1.Text = '出库码单(米数)') then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));

      Open;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHZ ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_hz);
    SInitCDSData20(ADOQueryTemp, CDS_hz);
  end;
  if (ComboBox1.Text = '出库码单(重量)') then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD11 ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHZ ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_hz);
    SInitCDSData20(ADOQueryTemp, CDS_hz);
  end;
  if (ComboBox1.Text = '出库码单(米数A5)') then
  begin

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD2 ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHZ ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_hz);
    SInitCDSData20(ADOQueryTemp, CDS_hz);

  end;
  if (ComboBox1.Text = '出库码单(重量A5)') then
  begin

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD21 ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHZ ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_hz);
    SInitCDSData20(ADOQueryTemp, CDS_hz);

  end;

  if (ComboBox1.Text = '出库码单(米数A5备注)') then
  begin

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD2_note ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHZ ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_hz);
    SInitCDSData20(ADOQueryTemp, CDS_hz);

  end;
  if (ComboBox1.Text = '出库码单(重量A5备注)') then
  begin

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD21_note ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHZ ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_hz);
    SInitCDSData20(ADOQueryTemp, CDS_hz);

  end;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox1.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM3.LoadFromFile(fPrintFile);
    RMVariables['ZDR'] := trim(DName);
    RMVariables['FHdate'] := trim(CDS_Main.fieldbyname('CRTime').AsString);

    RM3.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmCKProductBCPOutList.ToolButton2Click(Sender: TObject);
begin
  frmSysLogHelp := TfrmSysLogHelp.create(self);
  with frmSysLogHelp do
  begin
    fModel := self.caption;
  //  facction:='生产指示单删除';
    showmodal;
    free;
  end;
end;

procedure TfrmCKProductBCPOutList.cxTabControl1Change(Sender: TObject);
begin
  SETSTATUS();
  initgrid();
end;

procedure TfrmCKProductBCPOutList.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 TfrmCKProductBCPOutList.ComboBox2Change(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCKProductBCPOutList.ToolButton3Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID, FKHName, FFPNo: string;
begin
  if cxTabControl1.TabIndex <> 1 then
    Exit;
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  if ComboBox1.Text = '' then
  begin
    Application.MessageBox('没有选择打印格式', '提示', 0);
    Exit;
  end;
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  CDS_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin

          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value := Trim(CDS_Main.fieldbyname('MJID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_Main.EnableControls;
  if (ComboBox1.Text = '出库码单(米数)') then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));

      Open;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHZ ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_hz);
    SInitCDSData20(ADOQueryTemp, CDS_hz);
  end;
  if (ComboBox1.Text = '出库码单(重量)') then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD11 ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHZ ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_hz);
    SInitCDSData20(ADOQueryTemp, CDS_hz);
  end;
  if (ComboBox1.Text = '出库码单(米数A5)') then
  begin

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD2 ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHZ ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_hz);
    SInitCDSData20(ADOQueryTemp, CDS_hz);

  end;
  if (ComboBox1.Text = '出库码单(重量A5)') then
  begin

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD21 ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHZ ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_hz);
    SInitCDSData20(ADOQueryTemp, CDS_hz);

  end;

  if (ComboBox1.Text = '出库码单(米数A5备注)') then
  begin

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD2_note ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHZ ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_hz);
    SInitCDSData20(ADOQueryTemp, CDS_hz);

  end;
  if (ComboBox1.Text = '出库码单(重量A5备注)') then
  begin

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD21_note ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMDHZ ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_hz);
    SInitCDSData20(ADOQueryTemp, CDS_hz);

  end;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox1.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM3.LoadFromFile(fPrintFile);
    RMVariables['ZDR'] := trim(DName);
    RMVariables['FHdate'] := trim(CDS_Main.fieldbyname('CRTime').AsString);

    RM3.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + fPrintFile), '提示', 0);
  end;
end;

end.

