unit U_CKProductBCPInList;

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, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, MovePanel, Math, Clipbrd,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxContainer, dxCore,
  cxDateUtils, cxTextEdit, cxMaskEdit, dxSkinsCore, 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, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, dxSkinscxPCPainter;

type
  TfrmCKProductBCPInList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    SPNAME: TEdit;
    Label2: TLabel;
    CDS_Main: TClientDataSet;
    Label6: TLabel;
    MJID: TEdit;
    Label8: TLabel;
    SPKZ: TEdit;
    Label9: TLabel;
    SPMF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label10: TLabel;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    TBRKCX: TToolButton;
    v1Column4: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    APID: TEdit;
    Label16: TLabel;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    RMDBPRT: TRMDBDataSet;
    MovePanel2: TMovePanel;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    IsJYTime: TCheckBox;
    SPColor: TEdit;
    MJSTR4: TEdit;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ADOQueryPrint: TADOQuery;
    RM2: TRMGridReport;
    Label18: TLabel;
    ComboBox1: TComboBox;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ComboBox2: TComboBox;
    CDS_HZ: TClientDataSet;
    RM3: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    BegDate: TcxDateEdit;
    EndDate: TcxDateEdit;
    Label1: TLabel;
    rkordid: TEdit;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Label5: TLabel;
    Filler: TEdit;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: 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 orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBRKCXClick(Sender: TObject);
    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 ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    canshu2: string;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
  
    { Private declarations }
  public
    canshu1: string;
    FLeft, FTop: Integer;
    { Public declarations }
  end;

//var
//  frmCKProductBCPInList: TfrmCKProductBCPInList;

implementation

uses
  U_DataLink, U_Fun, U_SysLogHelp;

{$R *.dfm}

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

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

  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 TfrmCKProductBCPInList.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(BegDate.Text);
    fwhere := fwhere + ' and crTime<=' + quotedstr(endDate.Text);
    if trim(Pwhere) <> '' then
      fwhere := fwhere + ' and ' + trim(Pwhere);
    if Self.Caption = '拆匹入库管理' then
    begin
      fwhere := fwhere + ' and crtype in (''拆匹入库'',''国外拆匹入库'')';
    end
    else
    begin
      fwhere := fwhere + ' and crtype <>''拆匹入库'' and crtype<>''国外拆匹入库''  ';
    end;
  end
  else
  begin
    if trim(Pwhere) <> '' then
      fwhere := fwhere + ' where ' + trim(Pwhere);

    if Self.Caption = '拆匹入库管理' then
    begin
      fwhere := fwhere + ' and crtype in (''拆匹入库'',''国外拆匹入库'')';
    end
    else
    begin
      fwhere := fwhere + ' and crtype <>''拆匹入库'' and crtype<>''国外拆匹入库''  ';
    end;
  end;
  if SPColor.Text <> '' then
  begin
    fwhere := fwhere + 'and spcolor like ' + QuotedStr('%' + spcolor.Text + '%');
  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec P_Page_FinishClothRK ');
      sql.Add('@pageIndex=' + inttostr(CurrentPage));
      sql.Add(',@pageSize=' + inttostr(RecordsNumber));
      sql.Add(',@criteria= ' + quotedstr(fwhere));
//      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));
    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;

end;

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

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

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

procedure TfrmCKProductBCPInList.FormShow(Sender: TObject);
begin

  ReadCxGrid(trim(self.Caption), Tv1, '成品仓库');
  CurrentPage := 1;
  RecordsNumber := 500;
  canshu2 := Trim(DParameters2);
  if canshu1 = '查询' then
  begin
    TBRKCX.Visible := False;
  end
  else

  begin
    TBRKCX.Visible := true;
  end;

  if Self.Caption = '拆匹入库管理' then
  begin
    ToolButton3.Visible := True;
  end
  else
  begin
    ToolButton3.Visible := False;
  end;

end;

procedure TfrmCKProductBCPInList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('入库列表', cxGrid2);
end;

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

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

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

procedure TfrmCKProductBCPInList.TBRKCXClick(Sender: TObject);
var
  FFMainId, FPrice, strsql, WSql, WLog: 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;
  WSql := '';
  WLog := '';
  MovePanel2.Visible := True;
  MovePanel2.Refresh;

  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    while CDS_Main.Locate('SSel', True, []) = True do
    begin
      begin

        if WSql = '' then
          WSql := quotedstr(Trim(CDS_Main.fieldbyname('mjid').AsString))
        else
          WSql := WSql + ',' + quotedstr(Trim(CDS_Main.fieldbyname('mjid').AsString));

        strsql := '卷ID：' + trim(CDS_Main.FieldByName('mjid').AsString) + ' 入库单号：' + trim(CDS_Main.FieldByName('rkordid').AsString);

        WLog := WLog + ' insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ';
        WLog := WLog + ' ' + quotedstr(trim(DName));
        WLog := WLog + ' ,getdate() ';
        WLog := WLog + ' ,' + quotedstr(trim(self.Caption));
        WLog := WLog + ' ,' + quotedstr(trim('入库撤销'));
        WLog := WLog + ' ,' + quotedstr(trim(strsql));
        WLog := WLog + ' ,' + quotedstr(trim('成功')) + ' )';

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

  if WSql = '' then
  begin
    MovePanel2.Visible := False;
    exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_BanCP_CR where mjid  in (' + WSql + ')');
    sql.Add('and CRFlag=''出库'' ');
    Open;
  end;
  if ADOQueryTemp.RecordCount >= 1 then
  begin

    MovePanel2.Visible := False;
    Application.MessageBox('已出库不能撤销入库!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(WLog);
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;

      sql.Add(' Update CK_BanCP_CR Set CRFlag=''待入库'',CRType='''',CRTime=NULL,RKOrdId='''',RKStage='''' ');
      sql.Add(' where MJID in (' + WSql + ')');
      sql.Add(' and CRFlag=''入库'' ');
      sql.Add(' Update WFB_MJJY Set MJStr2=''未入库'' where MJID in (' + WSql + ')');

      ExecSQL;
    end;

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

//  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;
//  BegDate.SetFocus;
//  MovePanel2.Visible := True;
//  MovePanel2.Refresh;
//  try
//    ADOQueryCmd.Connection.BeginTrans;
//    with CDS_Main do
//    begin
//      while Locate('SSel', True, []) do
//      begin
//        with ADOQueryTemp do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add('select * from CK_BanCP_CR where mjid=' + Trim(CDS_Main.fieldbyname('mjid').AsString));
//          sql.Add('and CRFlag=''出库'' ');
//          Open;
//        end;
//        if ADOQueryTemp.RecordCount >= 1 then
//        begin
//          ADOQueryCmd.Connection.RollbackTrans;
//          MovePanel2.Visible := False;
//          Application.MessageBox('已出库不能撤销入库!', '提示', 0);
//          Exit;
//        end;
//        with ADOQueryCmd do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add(' Update CK_BanCP_CR Set CRFlag=''待入库'',CRType='''',CRTime=NULL,RKOrdId='''',RKStage='''' ');
//          sql.Add(' where mjid=''' + Trim(CDS_Main.fieldbyname('mjid').AsString) + ''' ');
//          sql.Add(' and CRFlag=''入库'' ');
//          sql.Add(' Update WFB_MJJY Set MJStr2=''未入库'' where exists(select MJID from CK_BanCP_CR X where X.MJID=WFB_MJJY.MJID and X.CRFlag=''待入库'' and MJStr2<>''未入库'')');
//          ExecSQL;
//        end;
//
//        strsql := '卷ID：' + trim(CDS_Main.FieldByName('MJID').AsString) + ' 入库单号：' + trim(CDS_Main.FieldByName('RKOrdID').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;
//    ADOQueryCmd.Connection.CommitTrans;
//    MovePanel2.Visible := False;
//  except
//    ADOQueryCmd.Connection.RollbackTrans;
//    MovePanel2.Visible := False;
//    Application.MessageBox('撤销失败!', '提示', 0);
//  end;
end;

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

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

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

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

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

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

procedure TfrmCKProductBCPInList.ToolButton2Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if CDS_Main.FieldByName('SSel').AsBoolean = True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update WFB_MJJY Set PrtAgnFlag=1,PrtAgnDate=getdate(),PrtAgnPerson=''' + Trim(DName) + '''');
          sql.Add(' where MJID=''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + '''');
          ExecSQL;
        end;
        with ADOQueryPrint do
        begin
          Close;
          SQL.Clear;
          sql.Add(' EXEC P_Print_JYLab ');
          SQL.Add(' @MJID=''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + '''');
          Open;
        end;
        try
          Moudle := LoadLibrary('MakeQRBarcode.dll');
          @Makebar := GetProcAddress(Moudle, 'Make');
          @Mixtext := GetProcAddress(Moudle, 'MixText');
          Txt := Trim(ADOQueryPrint.fieldbyname('MJID').AsString);
          fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
          if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
            CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
          if FileExists(fImagePath) then
            DeleteFile(fImagePath);
          Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
        except
          application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
          exit;
        end;

        if Trim(ADOQueryPrint.fieldbyname('LbName').AsString) <> '' then
        begin
          ExportFtErpFile(Trim(ADOQueryPrint.fieldbyname('LbName').AsString), ADOQueryTemp);
          fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ADOQueryPrint.fieldbyname('LbName').AsString)
        end
        else
        begin
          ExportFtErpFile('通用标签.rmf', ADOQueryTemp);
          fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\通用标签.rmf';
        end;

        if FileExists(fPrintFile) then
        begin
          RMVariables['QRBARCODE'] := fImagePath;
          RM2.LoadFromFile(fPrintFile);
          Rm2.DefaultCopies := strtointdef(trim(ComboBox1.Text), 1);
          RM2.PrintReport;
        end
        else
        begin
          CDS_Main.EnableControls;
          Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
end;

procedure TfrmCKProductBCPInList.ToolButton3Click(Sender: TObject);
var
  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;
  BegDate.SetFocus;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      while Locate('SSel', True, []) do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_BanCP_CR where mjid=' + Trim(CDS_Main.fieldbyname('mjid').AsString));
          sql.Add('and CRFlag=''出库'' ');
          Open;
        end;
        if ADOQueryTemp.RecordCount >= 1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          MovePanel2.Visible := False;
          Application.MessageBox('已出库不能撤销入库!', '提示', 0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;

          sql.Add('exec  P_Do_CXCP    @MJID=''' + Trim(CDS_Main.fieldbyname('MJID').AsString) + '''');

          ExecSQL;
        end;

        strsql := '卷ID：' + trim(CDS_Main.FieldByName('MJID').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;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible := False;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    MovePanel2.Visible := False;
    Application.MessageBox('撤销失败!', '提示', 0);
  end;
end;

procedure TfrmCKProductBCPInList.ToolButton4Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID, FKHName, FFPNo: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  if ComboBox2.Text = '' then
  begin
    Application.MessageBox('没有选择打印格式', '提示', 0);
    Exit;
  end;
  ExportFtErpFile(trim(ComboBox2.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 (ComboBox2.Text = '入库码单(米数)') then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_RKMD1 ');
      sql.add(' @DName=' + quotedstr(Trim(DCode)));

      Open;
    end;

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

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

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

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

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

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

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

  end;

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

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

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

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

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

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

  end;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox2.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.

