unit U_CPManage;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, jpeg, U_SLT, ComObj, Menus,
  cxPC, Math;

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

  TfrmCPManage = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    DataSource1: TDataSource;
    ADOQueryTree20: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxSplitter1: TcxSplitter;
    Panel1: TPanel;
    v1CYNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    DataSource2: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_Main: TClientDataSet;
    v1Column12: TcxGridDBColumn;
    TBExport: TToolButton;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    TBUP: TToolButton;
    Label3: TLabel;
    CYNO: TEdit;
    TBCopy: TToolButton;
    Panel2: TPanel;
    v1Column19: TcxGridDBColumn;
    DSCYNO: TDataSource;
    ADOQueryTree: TClientDataSet;
    Panel5: TPanel;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    Label11: TLabel;
    Label12: TLabel;
    ComboBox1: TComboBox;
    Panel3: TPanel;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    adoqueryPicture: TADOQuery;
    ToolButton8: TToolButton;
    v1Column39: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label4: TLabel;
    CYName: TEdit;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBMLEdit: TToolButton;
    Edit1: TEdit;
    v1Column6: TcxGridDBColumn;
    Label1: TLabel;
    CYColor: TEdit;
    Label5: TLabel;
    CYSpec: TEdit;
    Label6: TLabel;
    CYCF: TEdit;
    Label7: TLabel;
    CYNote: TEdit;
    TBImport: TToolButton;
    OpenDialog1: TOpenDialog;
    v1Column10: TcxGridDBColumn;
    Label9: TLabel;
    Label10: TLabel;
    CYKZ: TEdit;
    CarType: TEdit;
    Label8: TLabel;
    CYMF: TEdit;
    Label13: TLabel;
    ZhenXing: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column11: TcxGridDBColumn;
    Panel7: TPanel;
    Panel8: TPanel;
    cxTabControl1: TcxTabControl;
    Panel4: TPanel;
    Label14: TLabel;
    LBCPAP1: TLabel;
    Button1: TButton;
    Button2: TButton;
    TCBNOR1: TComboBox;
    tbChk: TToolButton;
    tbChkNo: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TBUPClick(Sender: TObject);
    procedure TBImportClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure TBCopyClick(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure TBMLEditClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure tbChkClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TCBNOR1Change(Sender: TObject);
    procedure tbChkNoClick(Sender: TObject);
    procedure CYNOKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
  private
    { Private declarations }
    CurrentPage, RecordsNumber: Integer;
    CPID: string;
    PState: Integer;
    FCPID, FTopID: string;
    procedure SetJurisdiction();
    procedure InitTree();
    procedure InitGrid();
    procedure ReadINIFile();
    procedure InitImage();
  public
    dFdDy: array[0..20] of FdDy; //客户端连接数组
    canshu1: string;
    { Public declarations }
  end;

var
//  frmCPManage: TfrmCPManage;
  Mach: array of TfrmSlt;

implementation

uses
  U_DataLink, U_Fun, U_CPAdd, U_FileUp, U_YPCR, U_CPAddPN, U_YPTypeHelp,
  U_YPGL_YGDJ;

{$R *.dfm}
procedure TfrmCPManage.SetJurisdiction();
begin

  TBAdd.Visible := false;
  TBCopy.Visible := false;
  TBEdit.Visible := false;
  TBMLEdit.Visible := false;
  TBDel.Visible := false;
  tbChkNo.Visible := false;
  tbChk.Visible := false;
  TBImport.Visible := false;
  TBUP.Visible := false;
  TBExport.Visible := false;

  if canshu1 = '管理' then
  begin
    TBAdd.Visible := true;
    TBCopy.Visible := true;
    TBImport.Visible := true;
    TBUP.Visible := true;
    TBExport.Visible := true;
    case cxTabControl1.TabIndex of
      0:
        begin
          TBEdit.Visible := true;
          TBMLEdit.Visible := true;
          TBDel.Visible := true;
          tbChk.Visible := true;
        end;
      1:
        begin
          tbChkNo.Visible := true;
        end;
    end;

  end;
  if canshu1 = '录入' then
  begin
    TBAdd.Visible := true;
    TBCopy.Visible := true;
    TBImport.Visible := true;
    TBUP.Visible := true;
    TBExport.Visible := true;
    case cxTabControl1.TabIndex of
      0:
        begin
          TBEdit.Visible := true;
          TBMLEdit.Visible := true;
          TBDel.Visible := true;
        end;
      1:
        begin

        end;
    end;
  end;

end;

procedure TfrmCPManage.InitTree();
var
  i: Integer;
begin
  with ADOQueryTree20 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from CP_Type order by CPlevel,CPOrder,CPName');
    Open;
  end;
  SCreateCDS20(ADOQueryTree20, ADOQueryTree);
  SInitCDSData20(ADOQueryTree20, ADOQueryTree);
  cxDBTreeList1.Items[0].Expand(false);
  //cxDBTreeList1.Items[1].Expand(False);
end;

procedure TfrmCPManage.InitGrid();
var
  fwhere, Pwhere: string;
begin
  Panel2.Visible := True;
  Panel2.Refresh;

  case cxTabControl1.TabIndex of
    0:
      begin
        fwhere := 'where status=''0'' ';
      end;
    1:
      begin
        fwhere := 'where status=''1'' ';
      end;
  end;
  Pwhere := SGetFilters(Panel1, 1, 2);
  if CYNo.TexT <> '' then
  begin
    if POS('*', CYNo.TexT) > 0 then
    begin
      CYNO.Tag := 9;
      if Pwhere = '' then
        Pwhere := ' CYNo  like ' + quotedstr(StringReplace(CYNo.TexT, '*', '%', [rfReplaceAll]));
    end
    else
    begin
      CYNO.Tag := 2;
      Pwhere := SGetFilters(Panel1, 1, 2);
    end;

  end;

  if trim(Pwhere) <> '' then
  begin
    if fwhere <> '' then
      fwhere := fwhere + ' and ' + trim(Pwhere)
    else
      fwhere := ' where ' + trim(Pwhere);
  end;
  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add(' exec P_Page_YP  ');
      sql.Add('@CYType=' + quotedstr(Trim(ADOQueryTree.fieldbyname('CPID').AsString)));
      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);

    LBCPAP1.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));

  finally
    ADOQueryMain.EnableControls;
  end;
  Panel2.Visible := False;
end;

procedure TfrmCPManage.FormDestroy(Sender: TObject);
begin
  //frmCPManage:=nil;
end;

procedure TfrmCPManage.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i, j: integer;
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);
//  writeCxGrid(self.Caption+tv1.Name,tv1);
 // Application := mainApplication;
  Action := cafree;
end;

procedure TfrmCPManage.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '样品管理');
  if DirectoryExists(ExtractFileDir('D:\Right1209')) then
    winexec('cmd /c rd /s /q D:\Right1209', sw_hide);
  Close;
end;

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

  while CDS_Main.Locate('SSel', true, []) do
  begin
    if Trim(CDS_Main.fieldbyname('CYID').AsString) <> '' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('delete CP_YDang where CYID=''' + Trim(CDS_Main.fieldbyname('CYID').AsString) + '''');
        sql.Add('delete CP_Plan_Yarn where Y_CYID=''' + Trim(CDS_Main.fieldbyname('CYID').AsString) + '''');
        ExecSQL;
      end;
    end;
    CDS_Main.Delete;
  end;

end;

procedure TfrmCPManage.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption, Tv1, '样品管理');
  RecordsNumber := 100;
  Panel3.Align := alclient;
  CurrentPage := 1;
  InitTree();

  SetJurisdiction();

  InitGrid();
end;

procedure TfrmCPManage.cxDBTreeList1DblClick(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmCPManage.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel('产品档案', cxGrid1);
end;

procedure TfrmCPManage.ToolButton4Click(Sender: TObject);
var
  fPrintFile, FFCYID: string;
  DPS, i, j: Integer;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  CYNO.SetFocus;
  if CDS_Main.IsEmpty then
    Exit;

  if trim(ComboBox1.Text) = '' then
  begin
    Application.MessageBox('样品标签不能为空!', '提示', 0);
    exit;
  end;
  if Trim(Edit1.Text) <> '' then
  begin
    if TryStrToInt(Edit1.Text, i) = False then
    begin
      Application.MessageBox('份数录入错误!', '提示', 0);
      exit;
    end;
  end;
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + trim(ComboBox1.Text) + '.rmf';
  CDS_Main.DisableControls;
  DPS := 0;
  FFCYID := '';

  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('CYID').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 FileExists(fPrintFile) then
        begin
          RMVariables['QRBARCODE'] := fImagePath;
          RM1.ShowPrintDialog := False;
          RM1.LoadFromFile(fPrintFile);
          RM1.PrintReport;
          RM1.DefaultCopies := StrToIntdef(Edit1.Text, 0);
        end
        else
        begin
          Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
          Exit;
        end;

        if DPS = 0 then
        begin
          FFCYID := Trim(CDS_Main.fieldbyname('CYID').AsString);
        end;
      end;

      Next;
    end;
  end;
  CDS_Main.EnableControls;
  CDS_Main.Locate('CYID', FFCYID, []);
  Edit1.Text := '1';

end;

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

procedure TfrmCPManage.TBImportClick(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName, t1, t2, t3: string;
begin
  FCPID := Trim(Self.ADOQueryTree.fieldbyname('CPID').AsString);
  FCPName := Trim(Self.ADOQueryTree.fieldbyname('CPName').AsString);
  ShowMessage(FCPID+'-'+FCPName);
  if GetLSNo(ADOQueryCmd, maxId, 'Y', 'CP_YDang', 2, 1) = False then
  begin
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;

  if trim(FCPID) = '' then
    exit;
  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 20 do
    begin
      if trim(excelApp.Cells[1, i].value) = '' then
        break;
      if trim(excelApp.Cells[1, i].value) = '类型' then
      begin
        dFdDy[k].inc := i;
        LX := i;
        dFdDy[k].FDdys := 'CPName';
        dFdDy[k].FdDysName := '类型';
      end;
      if trim(excelApp.Cells[1, i].value) = '产品编号' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYNo';
        dFdDy[k].FdDysName := '产品编号';
      end;
      if trim(excelApp.Cells[1, i].value) = '产品名称' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYName';
        dFdDy[k].FdDysName := '产品名称';
      end;
      if trim(excelApp.Cells[1, i].value) = '原料成分' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYSpec';
        dFdDy[k].FdDysName := '原料成分';
      end;
      if trim(excelApp.Cells[1, i].value) = '成分' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYCF';
        dFdDy[k].FdDysName := '成分';
      end;
      if trim(excelApp.Cells[1, i].value) = '门幅' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYMF';
        dFdDy[k].FdDysName := '门幅';
      end;
      if trim(excelApp.Cells[1, i].value) = '克重' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYKZ';
        dFdDy[k].FdDysName := '克重';
      end;
      if trim(excelApp.Cells[1, i].value) = '花型' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYHX';
        dFdDy[k].FdDysName := '花型';
      end;

      if trim(excelApp.Cells[1, i].value) = '颜色' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYColor';
        dFdDy[k].FdDysName := '颜色';
      end;

      if trim(excelApp.Cells[1, i].value) = '间距' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'ZhenXing';
        dFdDy[k].FdDysName := '间距';
      end;

      if trim(excelApp.Cells[1, i].value) = '机型' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CarType';
        dFdDy[k].FdDysName := '机型';
      end;
      if trim(excelApp.Cells[1, i].value) = '产量' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYCL';
        dFdDy[k].FdDysName := '产量';
      end;

      if trim(excelApp.Cells[1, i].value) = '纱长' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'ShaChang';
        dFdDy[k].FdDysName := '纱长';
      end;

      if trim(excelApp.Cells[1, i].value) = '备注' then
      begin
        dFdDy[k].inc := i;
        dFdDy[k].FDdys := 'CYNote';
        dFdDy[k].FdDysName := '备注';
      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 CP_YDang where 1=2');
        open;
      end;
      if trim(excelApp.Cells[i, LX].value) <> trim(FCPName) then
        continue;
      ADOQueryCmd.Append;
      for j := 0 to k - 1 do
      begin
        if trim(dFdDy[j].FDdys) = '' then
          continue;
        t1 := dFdDy[j].FDdys;
        t2 := inttostr(dFdDy[j].inc);
        t3 := excelApp.Cells[i, dFdDy[j].inc].Value;
        if trim(dFdDy[j].FDdys) <> 'CPName' then
          ADOQueryCmd.fieldbyname(dFdDy[j].FDdys).Value := excelApp.Cells[i, dFdDy[j].inc].Value;
      end;
      ADOQueryCmd.FieldByName('CYValid').Value := 'Y';
      ADOQueryCmd.FieldByName('CYType').Value := Trim(FCPID);
      ADOQueryCmd.FieldByName('CYID').Value := maxId + inttostr(i);
      ADOQueryCmd.Post;

    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('保存数据错误！', '错误信息', MB_ICONERROR);
    exit;
  end;
end;

procedure TfrmCPManage.ReadINIFile();
var
  programIni: Tinifile;  //配置文件名
  FileName: string;
begin
  FileName := ExtractFilePath(Paramstr(0)) + 'SYSTEMSET.INI';
  programIni := Tinifile.create(FileName);
  server := programIni.ReadString('SERVER', '服务器地址', '127.0.0.1');
  programIni.Free;
end;

procedure TfrmCPManage.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Main.IsEmpty then
    Exit;
{  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_YDang where CYID='''+Trim(CDS_Main.fieldbyname('CYID').asstring)+'''');
    Open;
  end;
  CPID:=Trim(ADOQueryTemp.fieldbyname('CYType').AsString);
  ADOQueryTree.Locate('CPID',CPID,[]);  }
  InitImage();
end;

procedure TfrmCPManage.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 ');
      sql.add('where A.TFID =B.XFID and A.WBID=' + quotedstr(trim(CDS_Main.fieldbyname('CYID').AsString)));
      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 TfrmCPManage.TBCopyClick(Sender: TObject);
begin
  try
    frmCPAddPN := TfrmCPAddPN.Create(Application);
    with frmCPAddPN do
    begin
      CopyInt := 1;
      FCYID := Trim(Self.CDS_Main.fieldbyname('CYID').AsString);
      FCPID := Trim(Self.ADOQueryTree.fieldbyname('CPID').AsString);
      FCPNo := Trim(Self.ADOQueryTree.fieldbyname('CPNo').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmCPAddPN.Free;
  end;
end;

procedure TfrmCPManage.ToolButton8Click(Sender: TObject);
var
  fPrintFile, FFCYID: string;
  DPS, i, j: Integer;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  CYNO.SetFocus;
  if CDS_Main.IsEmpty then
    Exit;

  if trim(ComboBox1.Text) = '' then
  begin
    Application.MessageBox('样品标签不能为空!', '提示', 0);
    exit;
  end;
  //if CDS_CYNO.IsEmpty then Exit;
  if Trim(Edit1.Text) <> '' then
  begin
    if TryStrToInt(Edit1.Text, i) = False then
    begin
      Application.MessageBox('份数录入错误!', '提示', 0);
      exit;
    end;
  end;
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + trim(ComboBox1.Text) + '.rmf';
  CDS_Main.DisableControls;
  DPS := 0;
  FFCYID := '';
  i := 1;
  if Trim(Edit1.Text) = '' then
  begin
    j := 1;
  end
  else
  begin
    j := StrToInt(Edit1.Text);
  end;
  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('CYID').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;

        for i := 1 to j do
        begin
          if FileExists(fPrintFile) then
          begin
            RM1.ShowPrintDialog := True;
            RM1.LoadFromFile(fPrintFile);
          //  RM1.PrintReport;
            RM1.ShowReport;
          end
          else
          begin
            Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\样品标签.rmf'), '提示', 0);
            Exit;
          end;
        end;

        if DPS = 0 then
        begin
          FFCYID := Trim(CDS_Main.fieldbyname('CYID').AsString);
        end;
      end;
      //CDS_Main.Locate('CYID',Trim(CDS_CYNO.fieldbyname('CYID').AsString),[]);

      Next;
    end;
  end;
  CDS_Main.EnableControls;
  CDS_Main.Locate('CYID', FFCYID, []);
  Edit1.Text := '1';

end;

procedure TfrmCPManage.TBAddClick(Sender: TObject);
var
  i: Integer;
  FieldName: string;
begin
  try
    frmCPAddPN := TfrmCPAddPN.Create(Application);
    with frmCPAddPN do
    begin
      CopyInt := 0;
      FCYID := '';
      FCPID := Trim(Self.ADOQueryTree.fieldbyname('CPID').AsString);
      FCPNo := Trim(Self.ADOQueryTree.fieldbyname('CPNo').AsString);
      FCPName := Trim(Self.ADOQueryTree.fieldbyname('CPName').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmCPAddPN.Free;
  end;
end;

procedure TfrmCPManage.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  try
    frmCPAddPN := TfrmCPAddPN.Create(Application);
    with frmCPAddPN do
    begin
      CopyInt := 0;
      PState :=1;
      FCYID := Trim(Self.CDS_Main.fieldbyname('CYID').AsString);
      FCPID := Trim(Self.ADOQueryTree.fieldbyname('CPID').AsString);
      FCPNo := Trim(Self.ADOQueryTree.fieldbyname('CPNo').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmCPAddPN.Free;
  end;
end;

procedure TfrmCPManage.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  try
    frmCPAddPN := TfrmCPAddPN.Create(Application);
    with frmCPAddPN do
    begin
      CopyInt := 0;
      FCYID := Trim(Self.CDS_Main.fieldbyname('CYID').AsString);
      FCPID := Trim(Self.ADOQueryTree.fieldbyname('CPID').AsString);
      FCPNo := Trim(Self.ADOQueryTree.fieldbyname('CPNo').AsString);
      FCPName := Trim(Self.ADOQueryTree.fieldbyname('CPName').AsString);
      ToolButton1.Visible := False;
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        CDS_Main.FieldByName('CPName').Value := FCPName;
        CDS_Main.FieldByName('CYID').Value := FCYID;
        CDS_Main.FieldByName('CYType').Value := FCPID;
        CDS_Main.FieldByName('CYNo').Value := CYNo.Text;
        CDS_Main.FieldByName('CYName').Value := CYName.Text;

        Self.CDS_Main.Post;
      end;
    end;
  finally
    frmCPAddPN.Free;
  end;
end;

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

  frmYPTypeHelp := TfrmYPTypeHelp.create(self);
  with frmYPTypeHelp do
  begin
    if showmodal = 1 then
    begin
      fYPType := trim(ADOQueryHelp.fieldbyname('CPID').asstring);
    end;
    free;
  end;

  if trim(fYPType) = '' then
    exit;

  try
    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  CP_YDang SET CYType=''' + trim(fYPType) + ''' ');
            sql.Add('where CYID=' + quotedstr(trim(CDS_Main.fieldbyname('CYID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    application.MessageBox('操作成功！', '提示信息');
    initgrid();
  except
    CDS_Main.EnableControls;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

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

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

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

  try
    with CDS_Main do
    begin
      DisableControls;
      while CDS_Main.Locate('SSel', true, []) do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('update  CP_YDang SET status=''1'',Chktime=getdate(),Chker= ' + quotedstr(trim(DName)));
          sql.Add('where CYID=' + quotedstr(trim(CDS_Main.fieldbyname('CYID').AsString)));
          execsql;
        end;
        CDS_Main.Delete;
      end;
      EnableControls;
    end;
    application.MessageBox('审核成功！', '提示信息');

  except
    CDS_Main.EnableControls;
    application.MessageBox('审核失败！', '提示信息', 0);
  end;
end;

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

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

procedure TfrmCPManage.TCBNOR1Change(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR1.Text);
  CurrentPage := 1;
  InitGrid();
end;

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

  try
    with CDS_Main do
    begin
      DisableControls;
      while CDS_Main.Locate('SSel', true, []) do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('update  CP_YDang SET status=''0'',Chktime=null,Chker= null');
          sql.Add('where CYID=' + quotedstr(trim(CDS_Main.fieldbyname('CYID').AsString)));
          execsql;
        end;

        CDS_Main.Delete;
      end;
      EnableControls;
    end;
    application.MessageBox('撤销审核成功！', '提示信息');
  except
    CDS_Main.EnableControls;
    application.MessageBox('撤销审核失败！', '提示信息', 0);
  end;
end;

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

procedure TfrmCPManage.cxTabControl1Change(Sender: TObject);
begin
  SetJurisdiction();
  CurrentPage := 1;
  InitGrid();
end;

end.

