unit U_BpRklist;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxButtonEdit, cxDropDownEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset,
  RM_Common, RM_Class, RM_e_Xls, RM_System, RM_GridReport, cxPC, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, cxCheckBox, cxTextEdit,
  Menus, 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
  TfrmBpRklist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    v1P_MF: TcxGridDBColumn;
    v1P_KZ: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRType: TcxGridDBColumn;
    v1CRTime: TcxGridDBColumn;
    v1Filler: TcxGridDBColumn;
    v1FillTime: TcxGridDBColumn;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    P_CodeName: TEdit;
    P_Color: TEdit;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1CPType: TcxGridDBColumn;
    ckName: TComboBox;
    Label6: TLabel;
    v1P_Color: TcxGridDBColumn;
    RMGridReport1: TRMGridReport;
    RMXLSExport2: TRMXLSExport;
    ADOPrint: TADOQuery;
    RMDB_MD: TRMDBDataSet;
    ADOPrint1: TADOQuery;
    RMDB_Sub: TRMDBDataSet;
    Label7: TLabel;
    cust: TEdit;
    v1cust: TcxGridDBColumn;
    v1packNO: TcxGridDBColumn;
    packNo: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    defstr2: TComboBox;
    v1defstr2: TcxGridDBColumn;
    v1gangNo: TcxGridDBColumn;
    v1defNote1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column1: TcxGridDBColumn;
    Label10: TLabel;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    factoryName: TEdit;
    Label11: TLabel;
    cxTabControl1: TcxTabControl;
    tbcdel: TToolButton;
    v1Column4: TcxGridDBColumn;
    orderNo: TEdit;
    Label12: TLabel;
    ToolButton2: TToolButton;
    cxGrid2: TcxGrid;
    TvMX: TcxGridDBTableView;
    TvMXColumn1: TcxGridDBColumn;
    vMXColumn3: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    vMXColumn2: TcxGridDBColumn;
    vMXColumn4: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    CDS_Sub: TClientDataSet;
    ToolButton3: TToolButton;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    RM1: TRMGridReport;
    Tv1Column1: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure P_CodeNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure tbcdelClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    TblCprk: string;
    procedure InitGrid();
    procedure SetComboBox();
    procedure CreateTable();
    procedure DropTable();
    procedure DoRkNum();
    procedure Setstatus();
    procedure PrtData(FJYID: string);
    procedure PrtRKData(FJYID: string);
    { Private declarations }
  public
    fCKName, fManage: string;
    { Public declarations }
  end;

var
  frmBpRklist: TfrmBpRklist;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_BpRk, U_CPCKMXLR, U_Fun;
{$R *.dfm}

procedure TfrmBpRklist.SetStatus();
begin
  TBedit.Visible := false;
  Tbdel.Visible := false;
  tbcdel.Visible := false;
  case cxTabControl1.TabIndex of
    0:
      begin
        TBedit.Visible := true;
        Tbdel.Visible := true;
      end;
    1:
      begin
        tbcdel.Visible := true;
      end;
  end;
end;

procedure TfrmBpRklist.DoRkNum();
var
  RKNo: Tstringlist;
begin
  RKNo := Tstringlist.Create;
  RKNo.Clear;
  try
    with CDS_Main do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if RKNo.IndexOf(trim(fieldbyname('CRNO').AsString)) < 0 then
        begin
          RKNo.Add(trim(fieldbyname('CRNO').AsString));
        end;
        next;
      end;
      EnableControls;
      First;
    end;
    Label10.Caption := '入库单数：' + inttostr(RKNo.Count);
  finally
    RKNo.Free;
  end;

end;

procedure TfrmBpRklist.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 TfrmBpRklist.CreateTable();
var
  strSQL: string;
begin
  TblCprk := '[##BP_INoutPPack' + 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 TfrmBpRklist.SetComboBox();
begin
  ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''BPCK'' ');
    if trim(fCKName) <> '' then
      sql.Add('and zdyName=' + quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      ckName.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if ckName.Items.Count > 0 then
    ckName.ItemIndex := 0;

  CRType.Items.Clear;
  CRType.Items.Add('');
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''BPRKTYPE''');
    if trim(fCKName) <> '' then
      sql.Add('and note=' + quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      CRType.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if CRType.Items.Count > 0 then
    CRType.ItemIndex := 0;

  if fCKName = '广东仓库' then
    ToolButton1.Visible := true;
end;

procedure TfrmBpRklist.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*,');
      sql.Add('orderNo=(select Top 1 orderNo from JYOrder_Main X where X.MainID=A.MainID )');
      sql.Add('from BP_InOut A ');
      sql.Add('where CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.Add('and CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      sql.Add('and CRFlag=''入库'' ');
      if trim(FckName) <> '' then
        sql.Add('and ckName=' + quotedstr(trim(fckName)));
      if cxTabControl1.TabIndex = 0 then
        sql.Add('and valid=''Y'' ');
      if cxTabControl1.TabIndex = 1 then
        sql.Add('and valid=''N'' ');
//        ShowMessage(sql.text);
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
    DoRkNum();
  finally
  end;
end;

procedure TfrmBpRklist.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(self.Caption, Tv1, '布匹仓库');
  Action := cafree;
end;

procedure TfrmBpRklist.FormDestroy(Sender: TObject);
begin
  frmBpRklist := nil;
end;

procedure TfrmBpRklist.TBAddClick(Sender: TObject);
begin
  frmbpRk := TfrmbpRk.create(self);
  with frmbpRk do
  begin
    fkeyNo := '';
    fType := 0;
    fckName := self.fckName;                         
    if showmodal = 1 then
    begin                                        
      TBRafresh.Click;            
      self.CDS_Main.Locate('crNo', trim(fkeyNo), []);
    end;
    free;
  end;
end;

procedure TfrmBpRklist.P_CodeNameChange(Sender: TObject);
begin
  if ADOQueryTmp.Active = False then
    Exit;
  SDofilter(ADOQueryTmp, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryTmp, CDS_Main);
  SInitCDSData20(ADOQueryTmp, CDS_Main);
  DoRkNum();
end;

procedure TfrmBpRklist.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption, Tv1, '布匹仓库');
  SetComboBox();
  SetStatus();
  InitGrid();
  if fckName = '广东仓库' then
  begin
    if fmanage = '高权限' then
      TBDel.Visible := true
    else
      TBDel.Visible := false;
  end;
end;

procedure TfrmBpRklist.FormCreate(Sender: TObject);
begin
  BegDate.Date := DServerDate;
  EndDate.Date := DServerDate;
  cxGrid1.Align := alclient;

end;

procedure TfrmBpRklist.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmBpRklist.TBEditClick(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;
  if pos('调拨', Trim(CDS_Main.fieldbyname('CRType').AsString)) > 0 then
  begin
    Application.MessageBox('调拨入库不允许删除!', '提示', 0);
    Exit;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BP_InOut A');
    SQL.Add('where exists(select CRNO from BP_InOut X where X.KCID=A.KCID and X.CRFlag=''入库'' and X.CRNO=''' + trim(cds_main.fieldbyname('CRNO').AsString) + ''') ');
    sQL.Add('and A.CRFlag=''出库''');
    Open;
  end;
  if not ADOQueryCmd.IsEmpty then
  begin
    Application.MessageBox('已产生出库数据，不能修改！', '提示', 0);
    Exit;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from YF_Money_CR ');
    SQL.Add(' where  mainID=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
    sql.Add(' and status>=''1'' ');
    Open;
    if not IsEmpty then
    begin
      Application.MessageBox('应付款没有撤销审核，不能修改!', '提示', 0);
      exit;
    end;
  end;

  frmbpRk := TfrmbpRk.create(self);
  with frmbpRk do
  begin
    fkeyNo := self.cds_main.fieldbyname('CRNO').AsString;
    fType := 1;
    fckName := self.fckName;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('crNo', trim(fkeyNo), []);
    end;
    free;
  end;
end;

procedure TfrmBpRklist.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
 { if Trim(CDS_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end; }

  if pos('调拨', Trim(CDS_Main.fieldbyname('CRType').AsString)) > 0 then
  begin
    Application.MessageBox('调拨入库不允许删除!', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BP_InOut A');
    SQL.Add('where exists(select CRNO from BP_InOut X where X.KCID=A.KCID and X.CRFlag=''入库'' and X.CRNO=''' + trim(cds_main.fieldbyname('CRNO').AsString) + ''') ');
    sQL.Add('and A.CRFlag=''出库''');
    Open;
  end;
  if not ADOQueryCmd.IsEmpty then
  begin
    Application.MessageBox('已产生出库数据，不能作废！', '提示', 0);
    Exit;
  end;
  if not ADOQueryCmd.IsEmpty then
  begin
    Application.MessageBox('已产生出库数据，不能删除！', '提示', 0);
    Exit;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from YF_Money_CR ');
    SQL.Add(' where  mainID=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
    sql.Add(' and status>=''1'' ');
    Open;
    if not IsEmpty then
    begin
      Application.MessageBox('应付款没有撤销审核，不能作废!', '提示', 0);
      exit;
    end;
  end;

  if Application.MessageBox('确定要做作废数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete  from YF_Money_CR ');
      SQL.Add('where  maiNID=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');

      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 YFDefFlag1=1 ');


      sql.Add('delete  from BP_kc ');
      SQL.Add('where  CRNO=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
      sql.Add('update  BP_InOut SET valid=''N'' ');
      SQL.Add('where  CRNO=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
      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('入库单号：' + trim(CDS_Main.FieldByName('CRNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    TBRafresh.Click;
  except
    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

procedure TfrmBpRklist.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

procedure TfrmBpRklist.TBPrintClick(Sender: TObject);
var
  filepath: string;
begin
  if CDS_Main.IsEmpty then
    exit;

  CreateTable();

  with CDS_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      with adoqueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add(' insert into ' + TblCprk + '(MainID,subID) values( ');
        SQL.Add(' ' + quotedstr(trim(CDS_Main.fieldbyname('CRNO').AsString)));
        SQL.Add(',' + quotedstr(trim(CDS_Main.fieldbyname('CRID').AsString)));
        sql.add(')');
        execsql;
      end;
      next;
    end;
    First;
    EnableControls;
  end;

  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Get_BPRkpackList ');
    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_BPRkpackList ');
    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);
    RMGridReport1.LoadFromFile(filepath);
    RMGridReport1.ShowReport;
  finally

  end;

  DropTable();

end;

procedure TfrmBpRklist.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmBpRklist.ToolButton1Click(Sender: TObject);
var
  filepath: string;
begin
  if CDS_Main.IsEmpty then
    exit;

  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Do_PrintMd ');
    sql.Add('@mainID=' + quotedstr(trim(CDS_Main.fieldbyname('CRNO').AsString)));
    sql.Add(',@flag=' + quotedstr(trim('广东出库')));
    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['dtxz'] := FormatDateTime('yyyy-mm-dd', date());
    RMVariables['zdr'] := trim(DName);
    RMVariables['Filler'] := trim(CDS_Main.fieldbyname('Filler').AsString);
    RMGridReport1.LoadFromFile(filepath);
    RMGridReport1.ShowReport;
  finally

  end;
end;

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

procedure TfrmBpRklist.tbcdelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    with ADOQueryCmd do      
    begin
      Close;
      sql.Clear;
      sql.Add('delete from BP_InOut where CRNO=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
      sql.Add('delete from CK_SXPB_CRMX where CRNO=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
      execsql;
    end;
    TBRafresh.Click;
  except
    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

procedure TfrmBpRklist.ToolButton2Click(Sender: TObject);
begin
  if cds_main.IsEmpty = true then
  begin
    exit;
  end;
//  if CDS_Main.fieldbyname('ssel').value <> true then
//  begin
//    Application.MessageBox('没有选择数据或勾选数据不准确!', '提示', 0);
//    Exit;
//  end;
  frmCPCKMXLR := TfrmCPCKMXLR.create(self);
  with frmCPCKMXLR do
  begin
    fkeyNo := self.cds_main.fieldbyname('CRNO').AsString;
    fkeyNoS := self.cds_main.fieldbyname('CRNOS').AsString;
    fkeyID := self.cds_main.fieldbyname('CRID').AsString;
    FMXGangHao := self.cds_main.fieldbyname('Gangno').AsString;
    Edit3.Text := self.cds_main.fieldbyname('qtyunit').AsString;
//    Edit2.Text := self.cds_main.fieldbyname('FZSPID').AsString;

    FCRTYPE := '入库';

    fType := 0;
    if showmodal = 1 then
    begin

//      self.InitGrid();
//      self.CDS_Main.Locate('spid', fkeyNo, [])
    end;
    free;
  end;
end;

procedure TfrmBpRklist.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  try
    with ADOQuerySub do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CRMX A ');
      sql.Add(' right JOIN BP_InOut B on B.CRID=A.CRID  and B.crno=A.crno');
      sql.Add(' where A.CRNO=' + quotedstr(trim(CDS_Main.fieldbyname('CRNO').AsString)));
      sql.Add(' and A.CRID=' + quotedstr(trim(CDS_Main.fieldbyname('CRID').AsString)));
      SQL.Add(' and crzt=''入库''');
//      SQL.Add('AND NOT EXISTS(SELECT MXID FROM CK_SXPB_CRMX C WHERE C.FZMXID=A.MXID)');
      sql.Add(' order by MXID');
//      ShowMessage(SQL.text);
      open;
    end;
    SCreateCDS20(ADOQuerySub, CDS_Sub);
    SInitCDSData20(ADOQuerySub, CDS_Sub);
  finally
  end;
end;

procedure TfrmBpRklist.ToolButton3Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Sub.IsEmpty then
    Exit;
  if CDS_Sub.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  CDS_Sub.DisableControls;
  with CDS_Sub do
  begin
    First;
    while not Eof do
    begin
      if CDS_Sub.FieldByName('SSel').AsBoolean = True then
      begin
        PrtData(CDS_Sub.FieldByName('MXID').AsString);
      end;
      Next;
    end;
  end;
  CDS_Sub.EnableControls;
end;

procedure TfrmBpRklist.PrtData(FJYID: string);
var
  fPrintFile: string;
  Txt, fImagePath, Txt2, fImagePath2: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin

  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' EXEC P_Print_JYLab ');
    SQL.Add(' @mjID=''' + Trim(FJYID) + '''');
    Open;
  end;
  if ADOQueryPrint.IsEmpty then
  begin
    application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
    exit;
  end;

  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(ADOQueryPrint.fieldbyname('MXID').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_Sub.EnableControls;
    exit;
  end;

  ExportFtErpFile('布匹标签.rmf', ADOQueryTmp);

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\布匹标签.rmf';

  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM1.LoadFromFile(fPrintFile);

    RM1.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
    exit;
  end;
end;

procedure TfrmBpRklist.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Sub, True);
end;

procedure TfrmBpRklist.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Sub, False);
end;

procedure TfrmBpRklist.ToolButton4Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
//  if CDS_Sub.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
        PrtRKData(CDS_Main.FieldByName('CRNOS').AsString);
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
end;

procedure TfrmBpRklist.PrtRKData(FJYID: string);
var
  fPrintFile: string;
  Txt, fImagePath, Txt2, fImagePath2: string;
  Moudle: THandle;
  Makebar: TMakebar;                    
  Mixtext: TMixtext;
begin

  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' EXEC P_Print_BPrkLab ');
    SQL.Add(' @CRNO=''' + Trim(FJYID) + '''');
    Open;
  end;
  if ADOQueryPrint.IsEmpty then           
  begin
    application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
    exit;
  end;                                     

  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
//    Txt := Trim(ADOQueryPrint.fieldbyname('CRNO').AsString);
       Txt := Trim(ADOQueryPrint.fieldbyname('CRNOS').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_Sub.EnableControls;
    exit;
  end;

  ExportFtErpFile('布匹入库标签.rmf', ADOQueryTmp);

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\布匹入库标签.rmf';

  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
//    RM1.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
    exit;
  end;
end;

end.

