unit U_CKProductBCPInList_Other;

interface

uses
  Windows, Messages, strutils,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,
  RM_BarCode;

type
  TfrmCKProductBCPInList_other = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    C_CodeName: TEdit;
    C_Color: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Label5: TLabel;
    conNo: TEdit;
    Label6: TLabel;
    MJID: TEdit;
    Label8: TLabel;
    KZ: TEdit;
    Label9: TLabel;
    MF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v2Column1: 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;
    BCGangNo: TEdit;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    TBRKCX: TToolButton;
    v1Column4: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1RollNum: TcxGridDBColumn;
    TBADD: TToolButton;
    v1C_Color: TcxGridDBColumn;
    v1gangNO: TcxGridDBColumn;
    v1kcKw: TcxGridDBColumn;
    v1CRType: TcxGridDBColumn;
    TBEdit: TToolButton;
    ToolButton2: TToolButton;
    CRTYPE: TComboBox;
    Label12: TLabel;
    v1ThCust: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    Label11: TLabel;
    factoryNoName: TEdit;
    Label13: TLabel;
    ThCust: TEdit;
    CheckBox1: TCheckBox;
    ADOPrint1: TADOQuery;
    ADOPrint: TADOQuery;
    RMDB_MD: TRMDBDataSet;
    RMDB_Sub: TRMDBDataSet;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton1: TToolButton;
    ToolButton6: TToolButton;
    RMGridReport1: TRMGridReport;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    v1Column16: TcxGridDBColumn;
    ToolButton7: TToolButton;
    v1Column17: TcxGridDBColumn;
    ToolButton8: TToolButton;
    Panetime: TPanel;
    RMBarCodeObject1: TRMBarCodeObject;
    ToolButton9: TToolButton;
    procedure FormDestroy(Sender: TObject);
    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 TBFindClick(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure conNoChange(Sender: TObject);
    procedure conNoKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBRKCXClick(Sender: TObject);
    procedure TBADDClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
  private
    TblCprk:string;
    procedure InitGrid();
    procedure DropTable();
    procedure CreateTable();
    { Private declarations }
  public
    fDParameters2:string;
    { Public declarations }
  end;

var
  frmCKProductBCPInList_other: TfrmCKProductBCPInList_other;

implementation
uses
  U_DataLink,U_Fun, U_Printckmd, U_CPRK,U_CKProductBCPOutth, U_NoteList,
  U_YGList, U_RKSMEdit, U_CPRK_Other, U_RKSMEdit_Other,U_JYOrderCDOne_Other;

{$R *.dfm}

procedure TfrmCKProductBCPInList_other.DropTable();
var
  strSQL:string;
begin
 // TblCprk := '[##CP_INOut123' + trim(DCode) + IntToStr(Handle)+formatdatetime('hhnnsszzz',time) + ']';
	//创建临时表
  strSQL := 'IF OBJECT_ID(''tempdb..' + TblCprk + ''') IS NOT NULL DROP TABLE ' + TblCprk;
	with ADOQueryCmd do
	begin
		Close;
		SQL.Clear;
		SQL.Add(strSQL);
		ExecSQL;
	end;
end;
procedure TfrmCKProductBCPInList_other.CreateTable();
var
  strSQL:string;
begin
  TblCprk := '[##CP_RK10' + trim(DCode) + IntToStr(Handle)+formatdatetime('hhnnsszzz',time) + ']';
	//创建临时表
  strSQL := 'IF OBJECT_ID(''tempdb..' + TblCprk + ''') IS NOT NULL DROP TABLE ' + TblCprk + #13
			    + 'CREATE TABLE ' + TblCprk + ' ('
			    + 'MainID VARCHAR(20),'
          + 'subID  VARCHAR(20),'
          + 'BCID   Varchar(20))'#13;
	with ADOQueryCmd do
	begin
		Close;
		SQL.Clear;
		SQL.Add(strSQL);
		ExecSQL;
	end;
end;

procedure TfrmCKProductBCPInList_other.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPInList_other:=nil;
end;

procedure TfrmCKProductBCPInList_other.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  writeCxGrid(self.Caption,Tv1,'成品仓库');
  Action:=caFree;
end;

procedure TfrmCKProductBCPInList_other.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCKProductBCPInList_other.InitGrid();
begin
  screen.Cursor:=crsqlwait;
  Panetime.Visible:=true;
  Panetime.Left:=(Width-Panetime.Width) div 2;
  Panetime.top:=(Height-Panetime.Height-200) div 2;
  application.ProcessMessages;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      Filtered:=False;
      SQL.Add('select A.defstr1,A.defstr2,A.defstr3,A.Filler,A.FillTime,A.note,A.ThCust,A.mainID,A.subID,A.MJID,A.BCID,A.CRTime,A.CRType,A.CRID,A.Qty,A.RollNum,A.QtyUnit,A.CPType,A.CKOrdNo,A.C_CodeName,A.C_Color,A.RCgangNo,A.BCgangNO,a.kcKw,');
      sql.Add('conNo=(select top 1 ConNo from WFB_MJJY X where X.MJID=A.MJID),');
      sql.Add('MF.defstr7 as PrtMF,MF.defstr8 as Prtkz, ');
      sql.Add('factoryNoName=(select X.Aorddefstr4 from JYOrder_Sub_AnPai X where X.APID=A.APID) ');
      sql.add('from CK_BanCP_CR1 A ');
      Sql.add(' LEFT join KH_Zdy_Attachment MF on MF.zdyName=A.C_CodeName and MF.defstr3=A.C_Color and MF.Type=''CP''');
      sql.add('where A.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''' ');
      sql.add('and A.CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1))+''' ');
      sql.add('and A.C_CodeName like '+quotedstr('%'+trim(C_CodeName.Text)+'%'));
      sql.add('and A.C_Color like '+quotedstr('%'+trim(C_Color.Text)+'%'));
      sql.add('and A.BCGangNo like '+quotedstr('%'+trim(BCGangNo.Text)+'%'));
      SQL.Add('and A.CRFlag=''入库'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
    Panetime.Visible:=false;
    screen.Cursor:=crdefault;
  end;

end;

procedure TfrmCKProductBCPInList_other.TBRafreshClick(Sender: TObject);
begin
//  BegDate.SetFocus;
  InitGrid();
end;

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

procedure TfrmCKProductBCPInList_other.TBCloseClick(Sender: TObject);
begin
//ReadCxGrid(self.Caption,Tv1,'成品仓库');
  Close;
end;

procedure TfrmCKProductBCPInList_other.FormShow(Sender: TObject);
begin

  ReadCxGrid(self.Caption,Tv1,'成品仓库');
  if Trim(fDParameters2)='管理' then
  begin
    TBRKCX.Visible:=True;
  end;

  if Trim(fDParameters2)='查询' then
  begin
    TBADD.Visible:=false;
    TBEdit.Visible:=false;
    TBRKCX.Visible:=false;
    ToolButton3.Visible:=false;
    ToolButton4.Visible:=false;
    ToolButton5.Visible:=false;
  end;
  //InitGrid();
end;

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

procedure TfrmCKProductBCPInList_other.TBFindClick(Sender: TObject);
var
  strwhere:string;
begin

  if ADOQueryMain.Active then
  begin
    strwhere:=  SGetFilters(Panel1,1,2);
    SDofilter(ADOQueryMain,strwhere);
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCKProductBCPInList_other.C_CodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPInList_other.TBPrintClick(Sender: TObject);
var
  filepath:string;
begin
  if CDS_Main.IsEmpty then exit;
 if CDS_Main.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  CreateTable();


  with CDS_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      IF Fieldbyname('ssel').AsBoolean then
      begin
        with adoqueryCmd do
        begin
          Close;
	   	    SQL.Clear;
	   	    SQL.Add(' insert into '+TblCprk+'(BCID) values( ');
          SQL.Add(' '+quotedstr(trim(CDS_Main.fieldbyname('BCID').AsString)));
          sql.add(')');
	   	    execsql;
        end;
      end;
      next;
    end;
    First;
    EnableControls;
  end;

  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Get_CprkList ');
    sql.Add('@mainID='+quotedstr(trim('')));
    sql.Add(',@TblCprk='+quotedstr(trim(TblCprk)));
    sql.Add(',@flag='+quotedstr(trim('1')));
    open;
  end;

   with AdoPrint1 do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Get_CprkList ');
    sql.Add('@mainID='+quotedstr(trim('')));
    sql.Add(',@TblCprk='+quotedstr(trim(TblCprk)));
    sql.Add(',@flag='+quotedstr(trim('10')));
    open;
  end;

  try
    filepath:=ExtractFilePath(Application.ExeName) + 'report\成品入库单.rmf';
    if not FileExists(Pchar(filepath))  then
    begin
      application.MessageBox(pchar('文件['+filepath+']不存在！'),'提示信息',MB_IConError);
      exit;
    end;
    RMVariables['begdate'] :=FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+' - '+FormatDateTime('yyyy-MM-dd',enddate.DateTime);
    RMVariables['dtxz'] :=FormatDateTime('yyyy-mm-dd',date());
    RMVariables['zdr'] :=trim(DName);
 //   RMVariables['Filler'] :=trim(CDS_Main.fieldbyname('Filler').AsString);
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;
  finally

  end;

  DropTable();

end;

procedure TfrmCKProductBCPInList_other.conNoChange(Sender: TObject);
begin
//  if Length(Trim(conNo.Text))<4 then Exit;
  TBFind.Click;
end;

procedure TfrmCKProductBCPInList_other.conNoKeyPress(Sender: TObject;
  var Key: Char);
begin
{  if Key=#13 then
  begin
    if Length(Trim(conNo.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        SQL.Add('select A.*,B.ConNo,C.C_Color,C.MF,C.KZ,D.MJXH');
        sql.Add(',isnull(factoryNoName,B.ConNo) KHName');
        sql.Add(',E.AOrdDefStr1');
        sql.add('from CK_BanCP_CR A ');
        Sql.add(' left join Contract_Main B on A.MainId=B.MainId');
        Sql.add(' left join Contract_sub C on A.SubId=C.SubId');
        Sql.add(' left join WFB_MJJY D on A.MJId=D.MJId');
        sql.Add(' left join JYOrder_Sub_AnPai E on A.APID=E.APID');
        sql.add('where A.orderNo
        SQL.Add(' and CRFlag=''入库'' ');
        Parameters.ParamByName('orderNo').Value:='%'+Trim(conNo.Text)+'%';

        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;  }
end;

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

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

procedure TfrmCKProductBCPInList_other.TBRKCXClick(Sender: TObject);
var
  YFID,CRID: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;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      while Locate('SSel',True,[]) do
      begin

        IF (trim(CDS_Main.fieldbyname('CRType').AsString)='退货入库') or
           (trim(CDS_Main.fieldbyname('CRType').AsString)='采购入库') then
        begin

          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YF_Money_CR ');
            sql.Add(' where MainID='''+Trim(CDS_Main.fieldbyname('ckordNo').AsString)+'''');
            sql.Add(' and P_COdeName='''+Trim(CDS_Main.fieldbyname('C_COdeName').AsString)+'''');
            sql.Add(' and P_Color='''+Trim(CDS_Main.fieldbyname('C_COlor').AsString)+'''');
            sql.Add(' and YFName IN(''退货入库'',''采购入库'') ');
            sql.Add(' and status>=''1'' ');
            Open;
            if not IsEmpty then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('应收款或者应付款没有撤销审核，不能撤销入库!','提示',0);
              Exit;
            end;
          end;

          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from YF_Money_CR ');
            sql.Add(' where MainID='''+Trim(CDS_Main.fieldbyname('ckordNo').AsString)+'''');
            sql.Add(' and P_COdeName='''+Trim(CDS_Main.fieldbyname('C_COdeName').AsString)+'''');
            sql.Add(' and P_Color='''+Trim(CDS_Main.fieldbyname('C_COlor').AsString)+'''');
            sql.Add(' and YFName='''+Trim(CDS_Main.fieldbyname('CRType').AsString)+'''');
            Open;
            if not IsEmpty then
            begin
             YFID:=Fieldbyname('YFID').AsString;
             CRID:=Fieldbyname('CRID').AsString;
            end;
          end;
        end;

        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_BanCP_CR1 where CRID='+Trim(CDS_Main.fieldbyname('CRID').AsString));
          Open;
        end;
        if ADOQueryTemp.RecordCount>1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('已出库不能撤销入库!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;

          sql.Add('delete CK_BanCP_CR1 where BCID='''+Trim(CDS_Main.fieldbyname('BCID').AsString)+'''');
          sql.Add('delete CK_BanCP_KC1 where CRID='+Trim(CDS_Main.fieldbyname('CRID').AsString));
          sql.Add('Update WFB_MJJY Set MJStr2=''未入库'' where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');

          IF   trim(CDS_Main.fieldbyname('CRType').AsString)='退货入库' then
          begin
          sql.Add('update YF_Money_CR Set Qty=(select  -1*isnull(Sum(Qty),0) from CK_BanCP_CR1 A  ');
          sql.Add(' where A.ckordNo=YF_Money_CR.mainID and  A.C_COdeName=YF_Money_CR.P_CodeName and A.C_Color=YF_Money_CR.P_Color and A.CRType=''退货入库''  )');
          sql.Add(',PS=(select -1* isnull(count(*),0) from CK_BanCP_CR1 A  ');
          sql.Add('  where A.ckordNo=YF_Money_CR.mainID and  A.C_COdeName=YF_Money_CR.P_CodeName and A.C_Color=YF_Money_CR.P_Color and A.CRType=''退货入库''  )');
          sql.Add(' where YFID='''+Trim(YFID)+'''');
          end;
           IF   trim(CDS_Main.fieldbyname('CRType').AsString)='采购入库' then
          begin
          sql.Add('update YF_Money_CR Set Qty=(select  isnull(Sum(Qty),0) from CK_BanCP_CR1 A  ');
          sql.Add(' where A.ckordNo=YF_Money_CR.mainID and  A.C_COdeName=YF_Money_CR.P_CodeName and A.C_Color=YF_Money_CR.P_Color and A.CRType=''退货入库''  )');
          sql.Add(',PS=(select  isnull(count(*),0) from CK_BanCP_CR1 A  ');
          sql.Add('  where A.ckordNo=YF_Money_CR.mainID and  A.C_COdeName=YF_Money_CR.P_CodeName and A.C_Color=YF_Money_CR.P_Color and A.CRType=''退货入库''  )');
          sql.Add(' where YFID='''+Trim(YFID)+'''');
          end;


            sql.Add('delete from YF_Money_CR ');
            sql.Add('where YFID='''+Trim(YFID)+''' and Qty=0 ');

            sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
            sql.Add(' where YFID='''+Trim(YFID)+'''');

            sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
            sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
            sql.Add(' where  CRID='''+Trim(CRID)+'''');
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;

  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销失败!','提示',0);
  end;
end;

procedure TfrmCKProductBCPInList_other.TBADDClick(Sender: TObject);
begin
  frmCPRK_Other:=TfrmCPRK_Other.create(self);
  with frmCPRK_Other do
  begin
    if Trim(DParameters2)<>'管理' then
    begin
      CRType.Items.Clear;
      CRType.Items.Add('退货入库');
    //  CRType.Items.Add('采购入库');
      CRType.ItemIndex:=0;
    end;
    fFlag:=0;
    fkeyNo:='';
    fBCID:='';
    if showmodal=1 then
    begin
      TBRafresh.Click;
    end;
    free;
  end;
end;

procedure TfrmCKProductBCPInList_other.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  if (trim(CDS_Main.fieldbyname('CRType').AsString)<>'期初入库') and
  (trim(CDS_Main.fieldbyname('CRType').AsString)<>'退货入库') then
  begin
    application.MessageBox('不是期初入库或超期退货，不能修改','提示信息',0);
    exit;
  end;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select A.* from CK_BanCP_KC1 A    ');
    sql.Add('inner join CK_BanCP_CR1 B on B.crid=A.crid and B.MJID=A.MJID  ');
    sql.Add('where  (A.KCQty<>B.Qty or A.kcRollNum <> B.RollNum or isnull(A.KCStatus,''0'')>''0'')   ');
    sql.Add('and B.CKOrdNo='+quotedstr(trim(CDS_Main.fieldbyname('ckordNo').AsString)));
    sql.Add('and B.BCID='+quotedstr(trim(CDS_Main.fieldbyname('BCID').AsString)));
    open;
  end;
  if not ADOQueryCmd.IsEmpty then
  begin
    application.MessageBox('此入库数据已出库，不能修改','提示信息',0);
    exit;
  end;
  frmCPRK_Other:=TfrmCPRK_Other.create(self);
  with frmCPRK_Other do
  begin
    fFlag:=1;
    fkeyNo:=self.CDS_Main.fieldbyname('ckordNo').AsString;
    fBCID:=self.CDS_Main.fieldbyname('BCID').AsString;
    if showmodal=1 then
    begin
     self.InitGrid();
    end;
    free;
  end;

end;

procedure TfrmCKProductBCPInList_other.ToolButton2Click(Sender: TObject);
begin
  frmCKProductBCPOutth:=TfrmCKProductBCPOutth.create(self);
  with frmCKProductBCPOutth do
  begin
    if showmodal=1 then
    begin
      self.TBRafresh.Click;
    end;
    Release;
  end;
end;

procedure TfrmCKProductBCPInList_other.CheckBox1Click(Sender: TObject);
begin
 IF  CDS_Main.IsEmpty then exit;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=checkbox1.Checked;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmCKProductBCPInList_other.ToolButton3Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  frmYGlist:=TfrmYGlist.create(self);
  with frmYGlist do
  begin
    frmYGlist.Caption:='添加打卷人';
    frmYGlist.Label1.Caption:='打卷人';
    if showmodal=1 then
    begin
     // Fnote:=trim(Memo1.Text);

      with CDS_Main do
      begin
        DisableControls;
        first;
        while not eof do
        begin
          IF Fieldbyname('ssel').AsBoolean then
          begin
            with ADOQueryCmd do
            begin
              close;
              sql.Clear;
              sql.Add('update CK_Bancp_CR SET defstr1='+quotedstr(trim(Memo1.Text)));
              sql.Add('where BCID='+quotedstr(trim(CDS_Main.fieldbyname('BCID').AsString)));
              execsql;
            end;
          end;
          next;
        end;
        First;
        EnableControls;
      end;
      TBRafresh.Click;
    end;
    close;
  end;

end;

procedure TfrmCKProductBCPInList_other.ToolButton4Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  frmYGlist:=TfrmYGlist.create(self);
  with frmYGlist do
  begin
    frmYGlist.Caption:='添加抄单人';
    frmYGlist.Label1.Caption:='抄单人';
    if showmodal=1 then
    begin
     // Fnote:=trim(Memo1.Text);

      with CDS_Main do
      begin
        DisableControls;
        first;
        while not eof do
        begin
          IF Fieldbyname('ssel').AsBoolean then
          begin
            with ADOQueryCmd do
            begin
              close;
              sql.Clear;
              sql.Add('update CK_Bancp_CR SET defstr2='+quotedstr(trim(Memo1.Text)));
              sql.Add('where BCID='+quotedstr(trim(CDS_Main.fieldbyname('BCID').AsString)));
              execsql;
            end;
          end;
          next;
        end;
        First;
        EnableControls;
      end;
      TBRafresh.Click;
    end;
    close;
  end;
end;

procedure TfrmCKProductBCPInList_other.ToolButton5Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  frmYGlist:=TfrmYGlist.create(self);
  with frmYGlist do
  begin
    frmYGlist.Caption:='添加上架人';
    frmYGlist.Label1.Caption:='上架人';
    if showmodal=1 then
    begin
     // Fnote:=trim(Memo1.Text);

      with CDS_Main do
      begin
        DisableControls;
        first;
        while not eof do
        begin
          IF Fieldbyname('ssel').AsBoolean then
          begin
            with ADOQueryCmd do
            begin
              close;
              sql.Clear;
              sql.Add('update CK_Bancp_CR SET defstr3='+quotedstr(trim(Memo1.Text)));
              sql.Add('where BCID='+quotedstr(trim(CDS_Main.fieldbyname('BCID').AsString)));
              execsql;
            end;
          end;
          next;
        end;
        First;
        EnableControls;
      end;
      TBRafresh.Click;
    end;
    close;
  end;
end;

procedure TfrmCKProductBCPInList_other.ToolButton1Click(Sender: TObject);
begin
    try
    frmRKSMEdit_other:=TfrmRKSMEdit_other.Create(Application);
    with frmRKSMEdit_other do
    begin

      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmRKSMEdit_other.Free;
  end;
end;

procedure TfrmCKProductBCPInList_other.ToolButton6Click(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
        try
          Moudle:=LoadLibrary('MakeQRBarcode.dll');
          @Makebar:=GetProcAddress(Moudle,'Make');
          @Mixtext:=GetProcAddress(Moudle,'MixText');
          Txt:=Trim(CDS_Main.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);
          CDS_Main.EnableControls;
          exit;
        end;
        with ADOQueryPrint do     //英文名称不要在样品表中读取，要在客户自定义表中读取。note字段。
        begin
          Close;
          SQL.Clear;
          sql.Add('select A.*,Qty as mjlen,QtyUnit as MJTypeother ');
          sql.Add(' from CK_BanCP_CR1 A') ;
          SQL.Add(' where A.MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
          SQL.Add(' and Qty>0 and CRFlag=''入库'' ');
          Open;
        end;
          fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\通用标签.rmf' ;
        if FileExists(fPrintFile) then
        begin
          RMVariables['QRBARCODE']:=fImagePath;
          RMGridReport1.LoadFromFile(fPrintFile);
         // RMGridReport1.ShowReport;
          RMGridReport1.PrintReport;
        end else
        begin
          CDS_Main.EnableControls;
          Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\卷标签.rmf'),'提示',0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
end;

procedure TfrmCKProductBCPInList_other.ToolButton7Click(Sender: TObject);
var
  fPrintFile:string;
  Txt,fImagePath:string;
  Moudle: THandle;
  Makebar:TMakebar;
  Mixtext:TMixtext;
begin
  if CDS_Main.IsEmpty then Exit;
  IF trim(CDS_Main.FieldByName('CRType').AsString)<>'退货入库' then
  begin
    application.MessageBox('不是退货入库信息，不能打印！','提示信息',0);
    exit;
  end;
{  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select * from CK_BanCp_CR1  ');
    sql.Add('where CKordNo='+quotedstr(trim(CDS_Main.fieldbyname('CKordNo').AsString)));
    open;
  end;   }



  CreateTable();
  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Do_PrintMd10 ');
    sql.Add('@mainID='+quotedstr(trim(CDS_Main.fieldbyname('CKordNo').AsString)));
    sql.Add(',@TblCprk='+quotedstr(trim(TblCprk)));
    sql.Add(',@flag='+quotedstr(trim('退货入库2')));
    open;
  end;
  DropTable();

     {   try
          Moudle:=LoadLibrary('MakeQRBarcode.dll');
          @Makebar:=GetProcAddress(Moudle,'Make');
          @Mixtext:=GetProcAddress(Moudle,'MixText');
          Txt:=Trim(AdoPrint.fieldbyname('CKordNo').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);
          CDS_Main.EnableControls;
          exit;
        end;
    }
  try
    fPrintFile:=ExtractFilePath(Application.ExeName) + 'report\成品入库退货单2.rmf';
    if not FileExists(Pchar(fPrintFile))  then
    begin
      application.MessageBox(pchar('文件['+fPrintFile+']不存在！'),'提示信息',MB_IConError);
      exit;
    end;
    RMVariables['QRBARCODE']:=fImagePath;
  //  RMVariables['begdate'] :=FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+' - '+FormatDateTime('yyyy-MM-dd',enddate.DateTime);
    RMVariables['dtxz'] :=FormatDateTime('yyyy-mm-dd',date());
    RMVariables['zdr'] :=trim(DName);
 //   RMVariables['Filler'] :=trim(CDS_Main.fieldbyname('Filler').AsString);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  finally

  end;
end;

procedure TfrmCKProductBCPInList_other.ToolButton8Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  frmYGlist:=TfrmYGlist.create(self);
  with frmYGlist do
  begin
    frmYGlist.Caption:='添加铺布人';
    frmYGlist.Label1.Caption:='铺布人';
    if showmodal=1 then
    begin
     // Fnote:=trim(Memo1.Text);

      with CDS_Main do
      begin
        DisableControls;
        first;
        while not eof do
        begin
          IF Fieldbyname('ssel').AsBoolean then
          begin
            with ADOQueryCmd do
            begin
              close;
              sql.Clear;
              sql.Add('update CK_Bancp_CR SET defstr4='+quotedstr(trim(Memo1.Text)));
              sql.Add('where BCID='+quotedstr(trim(CDS_Main.fieldbyname('BCID').AsString)));
              execsql;
            end;
          end;
          next;
        end;
        First;
        EnableControls;
      end;
      TBRafresh.Click;
    end;
    close;
  end;
end;

procedure TfrmCKProductBCPInList_other.ToolButton9Click(Sender: TObject);
begin
  frmJYOrderCDOne_other:=TfrmJYOrderCDOne_other.Create(self);
  with frmJYOrderCDOne_other do
  begin

    trk.Visible:=true;
    ToolButton1.Visible:=false;
    MJStr2.ItemIndex:=MJStr2.Items.IndexOf('未入库');
    MJStr2.Enabled:=false;
    if showmodal=1 then
    begin
    end;
    free;

  end;
end;

end.
