unit U_PieceSampleRklist;

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, cxContainer,
  cxTextEdit, cxCurrencyEdit, cxCheckBox, RM_System, RM_Dataset, RM_Common,
  RM_Class, RM_GridReport, cxPC, Menus, U_SLT, jpeg, ComObj, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, RM_BarCode, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  FdDy = record
    inc: integer; //客户端套接字句柄
    FDdys: string[32]; //客户端套接字
    FdDysName: string[32]; //客户端套接字
  end;

  TfrmPieceSampleRklist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRType: TcxGridDBColumn;
    v1CRTime: TcxGridDBColumn;
    v1Filler: TcxGridDBColumn;
    v1FillTime: TcxGridDBColumn;
    Label2: TLabel;
    Label3: TLabel;
    P_CodeName: TEdit;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ckName: TComboBox;
    Label6: TLabel;
    v1P_Code: TcxGridDBColumn;
    v1INoutNo: TcxGridDBColumn;
    Label7: TLabel;
    inoutNo: TEdit;
    kuwei: TEdit;
    Label9: TLabel;
    gangNo: TEdit;
    Label10: TLabel;
    v1Column3: TcxGridDBColumn;
    RMDB_Main: TRMDBDataSet;
    v1Column4: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    OrderNo: TEdit;
    Label14: TLabel;
    P_Code: TEdit;
    Label15: TLabel;
    Label17: TLabel;
    P_Color: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column1: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    ToolButton1: TToolButton;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    adoqueryPicture: TADOQuery;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    ToolButton2: TToolButton;
    OpenDialog1: TOpenDialog;
    Label8: TLabel;
    Label11: TLabel;
    P_KZ: TEdit;
    P_MF: TEdit;
    Label4: TLabel;
    Label12: TLabel;
    P_SPEC: TEdit;
    P_CF: TEdit;
    ToolButton3: TToolButton;
    cv1Column11: TcxGridDBColumn;
    Edit1: TEdit;
    cxstylrpstry1: TcxStyleRepository;
    dazi1: TcxStyle;
    btn1: TToolButton;
    pnl1: TPanel;
    lbl2: TLabel;
    CY1: TEdit;
    btn2: TButton;
    RMBarCodeObject1: TRMBarCodeObject;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Label5: TLabel;
    PINGZHI: TEdit;
    Label13: TLabel;
    yptype: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TBAddClick(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 TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
  private
    procedure InitGrid();
    procedure SetStatus();
    procedure InitImage();
    { Private declarations }
  public
    dFdDy: array[0..30] of FdDy; //客户端连接数组
    fCKName, canshu1, canshu2: string;
    fCRType: string;
    { Public declarations }
  end;

//var
//  frmPieceSampleRklist: TfrmPieceSampleRklist;
var
  Mach: array of TfrmSlt;

implementation

uses
  U_DataLink, U_Fun10, U_Fun, U_ZDYHelp, U_FileUp, U_PieceSampleRk;

{$R *.dfm}
procedure TfrmPieceSampleRklist.InitImage();
var
  i, j: integer;
  jpg: TJpegImage;
  myStream: TADOBlobStream;
begin

  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  if CDS_Main.IsEmpty then
    exit;
  try
    with adoqueryPicture do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,B.FileName from TP_File A,XD_File B  where A.TFID =B.XFID ');
      if CDS_Main.IsEmpty = False then
      begin
        sql.Add('and A.WBID=' + quotedstr(trim(CDS_Main.fieldbyname('MJID').AsString)));
      end
      else
      begin
        sql.add(' and 1=2');
      end;
      open;
    end;
    j := adoqueryPicture.RecordCount;
    if j < 1 then
      exit;
    adoqueryPicture.DisableControls;
    adoqueryPicture.First;
    SetLength(Mach, j);
    jpg := TJpegImage.Create();
    for i := 0 to j - 1 do
    begin
      if triM(adoqueryPicture.fieldbyname('FilesOther').AsString) <> '' then
      begin
        myStream := tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('FilesOther')), bmread);
        jpg.LoadFromStream(myStream);
        Mach[i] := TfrmSlt.Create(Self);
        Mach[i].Name := trim(adoqueryPicture.fieldbyname('TFID').AsString);
        Mach[i].Parent := ScrollBox1;
        Mach[i].Left := 0 + i * 165;
        Mach[i].Init(adoqueryPicture.fieldbyname('TFID').AsString, adoqueryPicture.fieldbyname('FileName').AsString, jpg);
      end;
      adoqueryPicture.Next;
    end;
    adoqueryPicture.EnableControls;
  finally
    jpg.free;
    application.ProcessMessages;
  end;
end;

procedure TfrmPieceSampleRklist.SetStatus();
begin
  TBEdit.Visible := False;
  TBDel.Visible := False;
  if trim(canshu1) = '仓库' then
  begin
    TBadd.Visible := true;
    TBDel.Visible := False;
    ToolButton1.Visible := False;
    ToolButton2.Visible := False;
    TBEdit.Visible := true;
    TBDel.Visible := true;
    btn1.Visible := True;
  end;
  if Trim(canshu1) = '高权限' then
  begin
    TBEdit.Visible := true;
    TBDel.Visible := true;
  end
  else
  begin

  end;
end;

procedure TfrmPieceSampleRklist.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*,fromFactoryName2=(case when CRType=''客户退样'' then '''' else fromFactoryName end) from CK_Sample_CR A ');
      sql.Add(' where A.CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.Add(' and  A.CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      sql.Add(' and A.CRFlag=''入库''  and isnull(VisType,'''')<>''寄样''  and Chkstatus= ''1''  and A.Valid=''Y'' and CKName=' + quotedstr(trim(fCKName)));

      if SGetFilters(Panel1, 1, 2) <> '' then
        sql.Add(' and ' + SGetFilters(Panel1, 1, 2));
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
  finally

  end;
end;

procedure TfrmPieceSampleRklist.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cafree;
end;

procedure TfrmPieceSampleRklist.FormDestroy(Sender: TObject);
begin
//  frmPieceSampleRklist:=nil;
end;

procedure TfrmPieceSampleRklist.TBAddClick(Sender: TObject);
begin
  frmPieceSampleRk := TfrmPieceSampleRk.create(self);
  with frmPieceSampleRk do
  begin
    fkeyNo := '';
    fType := 0;
    fCKName := trim(self.fCKName);
    canshu1 := self.canshu1;
    if canshu2 = '染色' then
    begin
      yptype.Text := '染色米样';
    end
    else
    begin
      yptype.Text := '印花米样';
    end;

    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('inoutno', trim(fkeyNo), []);
      if Application.MessageBox('入库完成,是否打印？', '提示', 32 + 4) = IDYES then
      begin

        with Self.CDS_Main do
        begin
          First;
          while not Eof do
          begin
            if Self.CDS_Main.fieldbyname('inoutno').AsString = Trim(fkeyNO) then
            begin
              Edit;
              Self.CDS_Main.FieldByName('ssel').Value := True;

            end;
            Next;
          end;
        end;
        Self.ToolButton3.Click();
      end;

    end;
    free;
  end;
end;

procedure TfrmPieceSampleRklist.FormShow(Sender: TObject);
begin
  readCxGrid(trim(self.Caption), Tv1, '样品仓库');
  SetStatus();
  yptype.Text := Self.canshu2;
//  InitGrid();
end;

procedure TfrmPieceSampleRklist.FormCreate(Sender: TObject);
begin
  EndDate.Date := SGetServerDate10(ADOQueryTmp);
  BegDate.Date := EndDate.Date - 7;
end;

procedure TfrmPieceSampleRklist.TBCloseClick(Sender: TObject);
begin
  writeCxGrid(trim(self.Caption), Tv1, '样品仓库');
  close;
end;

procedure TfrmPieceSampleRklist.TBEditClick(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* from CK_Sample_CR A');
    SQL.Add('where A.inoutno=''' + trim(cds_main.fieldbyname('inoutno').AsString) + ''' ');
    SQL.Add('and A.CRFlag=''出库'' and A.valid=''Y'' ');
    Open;
  end;
  if not ADOQueryCmd.IsEmpty then
  begin
    Application.MessageBox('此单已经产生出库，不能修改！', '提示', 0);
    Exit;
  end;

  frmPieceSampleRk := TfrmPieceSampleRk.create(self);
  with frmPieceSampleRk do
  begin
    fkeyNo := self.cds_main.fieldbyname('inoutno').AsString;
    fType := 1;
    fCKName := trim(self.fCKName);
    canshu1 := Self.canshu1;
    if Self.cds_main.fieldbyname('crtype').AsString = '放样入库' then
    begin
      CRType.Items.Add('放样入库');
      frmPieceSampleRk.CRType.Text := '放样入库';
    end;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('crNo', trim(fkeyNo), []);
      if Application.MessageBox('入库完成,是否打印？', '提示', 32 + 4) = IDYES then
      begin
//        ShowMessage(fkeyNo);
        with Self.CDS_Main do
        begin
          First;
          while not Eof do
          begin
            if Self.CDS_Main.fieldbyname('inoutno').AsString = Trim(fkeyNO) then
            begin
              Edit;
              Self.CDS_Main.FieldByName('ssel').Value := True;

            end;
            Next;
          end;
        end;
        Self.ToolButton3.Click();
      end;
    end;
    free;
  end;
end;

procedure TfrmPieceSampleRklist.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.IsEmpty then
    Exit;
//  if Trim(CDS_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
//  begin
//    Application.MessageBox('不能操作他人数据!', '提示', 0);
//    Exit;
//  end;
  if Trim(CDS_Main.fieldbyname('crtype').AsString) = '放样入库' then
  begin
    Application.MessageBox('自动生成的记录不能删除!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox(PChar('确定要删除编号为' + Trim(CDS_Main.FieldByName('P_Code').AsString) + '的数据吗'), '提示', 32 + 4) <> IDYES then
    Exit;
  try
    if Trim(CDS_Main.FieldByName('CRNO').AsString) <> '' then
    begin
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('update CK_Sample_CR set Valid=''N'' where CRNO=' + quotedstr(Trim(CDS_Main.FieldByName('CRNO').AsString)));
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('exec P_Up_SampleKc ' + quotedstr(Trim(CDS_Main.FieldByName('MJID').AsString)));
        Open;
      end;
      if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('出库数量超出入库数量!', '提示', 0);
        Exit;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Main.Delete;
    end;
  except
    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

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

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

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

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

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

procedure TfrmPieceSampleRklist.ToolButton1Click(Sender: TObject);
begin
  try
    frmFileUp := TfrmFileUp.Create(Application);
    with frmFileUp do
    begin
      Code.Text := Trim(Self.CDS_Main.fieldbyname('MJID').AsString);
      CYID := Trim(Self.CDS_Main.fieldbyname('MJID').AsString);
      if ShowModal = 1 then
      begin
        //Self.InitGrid();
        //Self.CDS_Main.Locate('SPID',CYID,[]);
      end;
    end;
  finally
    frmFileUp.Free;
  end;
end;

procedure TfrmPieceSampleRklist.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
//  InitImage();
end;

procedure TfrmPieceSampleRklist.ToolButton2Click(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, ExcelRowCount: integer;
  maxId: string;
begin

  if GetLSNo(ADOQueryCmd, maxId, 'SP', 'CK_Sample_CR', 4, 1) = False then
  begin
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;

  try
    excelApp := CreateOleObject('Excel.Application');
    openDialog1.Filter := '*.CSV;*.xls';
    if opendialog1.Execute then
    begin
      WorkBook := excelApp.WorkBooks.Open(OpenDialog1.FileName);
    end
    else
      exit;

    excelApp.Visible := false;
    ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
  except
    application.MessageBox('加载EXCEL错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    k := 0;
    for i := 1 to 30 do
    begin
      if trim(excelApp.Cells[1, i].value) = '' then
        break;
      
      //遍历界面绑定界面中显示的字段和名称
      for j := 0 to Tv1.ColumnCount - 1 do
      begin
        if trim(excelApp.Cells[1, i].value) = trim(Tv1.Columns[j].Caption) then
        begin
          dFdDy[k].inc := i;
          dFdDy[k].FDdys := trim(Tv1.Columns[j].DataBinding.FieldName);
          dFdDy[k].FdDysName := trim(Tv1.Columns[j].Caption);
        end;
      end;

      k := k + 1;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  ADOQueryCmd.Connection.BeginTrans;
  try
    for i := 2 to ExcelRowCount do
    begin

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from CK_Sample_CR where 1=2');
        open;
      end;

      ADOQueryCmd.Append;
      //添加第i条数据
      for j := 1 to k - 1 do
      begin
        if trim(dFdDy[j].FDdys) <> '' then
          ADOQueryCmd.fieldbyname(dFdDy[j].FDdys).Value := excelApp.Cells[i, dFdDy[j].inc].Value;
      end;

      ADOQueryCmd.FieldByName('Filler').Value := Trim(DName);
      ADOQueryCmd.FieldByName('CRNO').Value := Trim(maxId) + inttostr(i);
      ADOQueryCmd.FieldByName('INoutNo').Value := Trim(maxId) + inttostr(i);
      ADOQueryCmd.FieldByName('MJID').Value := Trim(maxId) + inttostr(i);
      ADOQueryCmd.FieldByName('CKName').Value := trim(fCKName);
//      ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', now);
      ADOQueryCmd.FieldByName('CRType').Value := '期初入库';
      ADOQueryCmd.FieldByName('CRFlag').Value := '入库';
      ADOQueryCmd.FieldByName('CRQtyFlag').Value := 1;
      ADOQueryCmd.Post;

    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    ADOQueryCmd.Connection.CommitTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_Up_SampleKcAll');
      execsql;
    end;
    InitGrid();
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('保存数据错误！', '错误信息', MB_ICONERROR);
    exit;
  end;
end;

procedure TfrmPieceSampleRklist.ToolButton3Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin

  if CDS_Main.IsEmpty then
    Exit;

  ExportFtErpFile('样品入库标签.rmf', ADOQueryTmp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\样品入库标签.rmf';
  while CDS_Main.Locate('SSel', True, []) do
  begin

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
//      sql.Add('select *  from CP_YDang A inner join CK_Sample_CR B on A.CYNo=B.P_Code and  A.CYColor=B.P_COLOR ');
      sql.Add('select *  from CK_Sample_CR A ');
      sql.Add('where CRNO=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + '''');
//      showmessage(sql.Text);
      Open;
    end;
    try
      Moudle := LoadLibrary('MakeQRBarcode.dll');
      @Makebar := GetProcAddress(Moudle, 'Make');
      @Mixtext := GetProcAddress(Moudle, 'MixText');
      Txt := Trim(ADOQueryPrint.fieldbyname('crno').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;
    RMVariables['QRBARCODE'] := fImagePath;

    if FileExists(fPrintFile) then
    begin
      RM1.ShowPrintDialog := False;
      Rm1.DefaultCopies := strtointdef(trim(Edit1.Text), 1);
      RM1.LoadFromFile(fPrintFile);
//      RM1.DefaultCopies := 1;
      RM1.PrintReport;
    end
    else
    begin
      Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
      Exit;
    end;
    CDS_Main.edit;
    CDS_Main.FieldByName('ssel').Value := False;
  end;
end;

procedure TfrmPieceSampleRklist.btn2Click(Sender: TObject);
var
  maxno, maxno2: string;
  F1: string;
  ZS: double;
begin
  if trim(cy1.Text) = '' then
    exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    F1 := CDS_Main.FieldByName('CRNO').AsString;
    while CDS_Main.Locate('SSel', true, []) do
    begin
      if not TryStrToFloat(Trim(CY1.Text), ZS) then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('您填写的非数字！', '提示', 0);
        Exit;
      end;
      if F1 <> trim(CDS_Main.FieldByName('CRNO').AsString) then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('不能选择多个库存！', '提示', 0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('UPDATE CK_Sample_CR SET  QTY=' + quotedstr(Trim(CY1.Text)));
        SQL.Add(' where crno= ' + quotedstr(trim(CDS_Main.FieldByName('CRNO').AsString)));
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('exec P_Up_SampleKc ' + quotedstr(trim(CDS_Main.fieldbyname('MJID').AsString)));
        Open;
      end;

      if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
      begin
        CDS_Main.EnableControls;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('出库数量超出库存数量!', '提示', 0);
        Exit;
      end;
      with CDS_Main do
      begin
        Edit;
        FieldByName('SSel').Value := false;

      end;

    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('修改成功!', '提示', 0);
    pnl1.Visible := False;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end
end;

procedure TfrmPieceSampleRklist.btn1Click(Sender: TObject);
begin
  if cds_main.IsEmpty then
  begin

    exit;
  end;
  if cds_main.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if pnl1.Visible = False then
  begin
    pnl1.Visible := True;

  end
  else
    pnl1.Visible := False;
end;

procedure TfrmPieceSampleRklist.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    InitGrid();
end;

end.

