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, StrUtils,
  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,
  cxContainer, cxImage, cxDBEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxTLdxBarBuiltInMenu, cxNavigator, cxTextEdit, cxButtonEdit, 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
  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;
    Label2: TLabel;
    CYID: TEdit;
    TBFilter: TToolButton;
    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;
    CDS_CYNO: TClientDataSet;
    ADOQueryTree: TClientDataSet;
    Panel5: TPanel;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    Label11: TLabel;
    Label12: TLabel;
    Panel3: TPanel;
    GroupBox1: TGroupBox;
    adoqueryPicture: TADOQuery;
    ToolButton8: TToolButton;
    v1Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label4: TLabel;
    CYName: TEdit;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBMLEdit: TToolButton;
    Edit1: TEdit;
    Label5: TLabel;
    CYCF: TEdit;
    TBImport: TToolButton;
    OpenDialog1: TOpenDialog;
    v1Column10: TcxGridDBColumn;
    Label9: TLabel;
    Label10: TLabel;
    CYKZ: TEdit;
    CarType: TEdit;
    Label8: TLabel;
    CYMF: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column11: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    cxgrdbclmnv1Column17: TcxGridDBColumn;
    cccc: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    ADOQueryImage: TADOQuery;
    DataSource3: TDataSource;
    Picture4: TcxDBImage;
    cxButtonEdit1: TcxButtonEdit;
    Tv1Column1: TcxGridDBColumn;
    Label1: TLabel;
    CYGC: TEdit;
    ToolButton1: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    RMDBMain: TRMDBDataSet;
    RM2: TRMGridReport;
    CDS_SJ: TClientDataSet;
    RMDBSJ: TRMDBDataSet;
    RMDBPZ: TRMDBDataSet;
    CDS_PZ: TClientDataSet;
    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 TBFilterClick(Sender: TObject);
    procedure CYIDKeyPress(Sender: TObject; var Key: Char);
    procedure CYNoChange(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 CYNOKeyPress(Sender: TObject; var Key: Char);
    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 Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure Picture4DblClick(Sender: TObject);
    procedure cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
    CPID: string;
    PState: Integer;
    FCPID, FTopID: string;
    FJurisdiction: string;
    procedure SetJurisdiction();
    procedure LookImage(FileName: string);
    procedure InitTree();
    procedure InitGrid();
    procedure ReadINIFile();
    procedure InitImage();
  public
    dFdDy: array[0..20] of FdDy; //客户端连接数组
    fFlileFlag: 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, getpic, U_ZDYHelp;

{$R *.dfm}
procedure TfrmCPManage.SetJurisdiction();
begin
  if trim(self.Caption) = '产品信息录入' then
  begin
    FJurisdiction := '登记';
  end
  else if trim(self.Caption) = '产品信息管理' then
  begin
    FJurisdiction := '管理';
  end
  else
  begin
    FJurisdiction := '查询';
  end;
  TBAdd.Visible := false;
  TBCopy.Visible := false;
  TBEdit.Visible := false;
  TBMLEdit.Visible := false;
  TBDel.Visible := false;
  TBImport.Visible := false;
  TBUP.Visible := false;
  TBExport.Visible := false;
  if FJurisdiction = '管理' then
  begin
    TBAdd.Visible := true;
    TBCopy.Visible := true;
    TBEdit.Visible := true;
    TBMLEdit.Visible := true;
    TBDel.Visible := true;
    TBImport.Visible := true;
    TBUP.Visible := true;
    TBExport.Visible := true;
  end
  else if FJurisdiction = '登记' then
  begin
    TBAdd.Visible := true;
    TBCopy.Visible := true;
    TBEdit.Visible := true;
    TBMLEdit.Visible := true;
    TBDel.Visible := true;
    TBImport.Visible := true;
    TBUP.Visible := true;
    TBExport.Visible := true;
  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();
begin
  Panel2.Visible := True;
  Panel2.Refresh;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      SQL.Add('exec P_Select_CP_YDang :Code,:PState,:CYType');
      Parameters.ParamByName('Code').Value := '';
      Parameters.ParamByName('PState').Value := 0;
      Parameters.ParamByName('CYType').Value := Trim(ADOQueryTree.fieldbyname('CPID').AsString);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  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;
  if FJurisdiction = '登记' then
  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) + '''');
        ExecSQL;
      end;
    end;
    CDS_Main.Delete;
  end;
  if FJurisdiction = '管理' then
  begin
    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) + '''');
          ExecSQL;
        end;
      end;
      CDS_Main.Delete;
    end;
  end;

end;

procedure TfrmCPManage.FormShow(Sender: TObject);
begin
  Panel3.Align := alclient;
  InitTree();
  ReadCxGrid(self.Caption, Tv1, '样品管理');
  SetJurisdiction();
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select CYID='''' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_CYNO);
  SInitCDSData20(ADOQueryTemp, CDS_CYNO);
  with ADOQueryMain do
  begin
    Filtered := False;
    Close;
    SQL.Clear;
    SQL.Add('exec P_Select_CP_YDang :Code,:PState,:CYType');
    Parameters.ParamByName('Code').Value := '';
    Parameters.ParamByName('PState').Value := 0;
    Parameters.ParamByName('CYType').Value := 'XXXXXXX';
    Open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_Main);
  SInitCDSData20(ADOQueryMain, CDS_Main);
  CDS_CYNO.Delete;

end;

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

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

procedure TfrmCPManage.TBFilterClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;

end;

procedure TfrmCPManage.CYIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Trim(CYID.Text) = '' then
      Exit;
    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add('exec P_Select_CP_YDang :Code,1,'''' ');
      Parameters.ParamByName('Code').Value := Trim(CYID.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    CYID.Text := '';
    ADOQueryTree.Locate('CPID', CPID, []);
  end;
end;

procedure TfrmCPManage.CYNoChange(Sender: TObject);
begin
  //if Length(Trim(TEdit(Sender).Text))<4 then Exit;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;

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
  CYID.SetFocus;
  if CDS_Main.IsEmpty then
    Exit;

  if trim(cxButtonEdit1.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(cxButtonEdit1.Text) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + trim(cxButtonEdit1.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 := False;
            RM1.LoadFromFile(fPrintFile);
            RM1.PrintReport;
          //  RM1.ShowReport;
          end
          else
          begin
            Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 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.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;

  FormGetPic := TFormGetPic.create(self);
  with FormGetPic do
  begin
    fFlileFlag := self.fFlileFlag;
    fkeyNo := trim(CDS_Main.fieldbyname('cyid').AsString);
    pat1 := 'FileName';
    pic1 := 'FilesOther';
    FTFType := 'YPP';
    if showmodal = 1 then
    begin
    end;
    free;
  end;
  InitImage();
end;

procedure TfrmCPManage.TBImportClick(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName: string;
begin
  FCPID := Trim(Self.ADOQueryTree.fieldbyname('CPID').AsString);
  FCPName := Trim(Self.ADOQueryTree.fieldbyname('CPName').AsString);

  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 50 do
    begin
      if trim(excelApp.Cells[1, i].value) = '' then
        continue;
      //遍历界面绑定界面中显示的字段和名称
      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;

  try
    ADOQueryCmd.Connection.BeginTrans;
    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;

      ADOQueryCmd.Append;
      for j := 0 to k - 1 do
      begin
        if trim(dFdDy[j].FDdys) = '' then
          continue;
        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;
    ADOQueryCmd.Connection.CommitTrans;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
//    InitGrid();
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    ADOQueryCmd.Connection.CommitTrans;
    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();
begin
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID=' + quotedstr(Trim(CDS_Main.fieldbyname('cyid').AsString)));
    open;
  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.CYNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Trim(CYNO.Text) = '' then
      Exit;
    if Length(Trim(CYNO.Text)) < 4 then
      Exit;
    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add('exec P_Select_CP_YDang :Code,2,'''' ');
      Parameters.ParamByName('Code').Value := '%' + Trim(CYNO.Text) + '%';
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    CYID.Text := '';
    ADOQueryTree.Locate('CPID', CPID, []);
  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
  CYID.SetFocus;
  if CDS_Main.IsEmpty then
    Exit;

  if trim(cxButtonEdit1.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(cxButtonEdit1.Text) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + trim(cxButtonEdit1.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;
      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);
      if FJurisdiction = '查询' then
      begin
        ToolButton1.Visible := False;
      end;
      if trim(self.Caption) = '产品信息查询(低权限)' then
      begin
//        lbl2.Visible := False;
//        CYCNHS.Visible := False;
      end;
      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.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('BaoHuDate').Index] = null then
    Exit;
  if (AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('BaoHuDate').Index]) < Now() then
  begin
    ACanvas.Brush.Color := $ADDEFF;
  end;

end;

procedure TfrmCPManage.Picture4DblClick(Sender: TObject);
begin
  if TcxDBImage(Sender).Picture.Height = 0 then
    exit;

  LookImage(ADOQueryImage.fieldbyname(TcxDBImage(Sender).Hint).asstring);
end;

procedure TfrmCPManage.LookImage(FileName: string);
var
  sFieldName: string;
 //	ff: TADOBlobStream;
 // FJStream : TMemoryStream;
begin

  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);

  sFieldName := sFieldName + '\' + trim(FileName);

  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin

    //  Panel2.Caption:='正在下载数据，请稍等...';
    //  Panel2.Visible:=true;
    //  application.ProcessMessages;
    try
      IdFTP1.Get(fFlileFlag + '\' + Trim(FileName), sFieldName, true, false);
    except
   //   Panel2.Visible:=false;
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
 //   Panel2.Visible:=false;
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
//  Panel2.Visible:=false;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
end;

procedure TfrmCPManage.cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);

      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCPManage.ToolButton1Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  OrderKg: Double;
begin
  if CDS_Main.IsEmpty then
    Exit;


//  LBName := ComboBox1.Text + '采购单';
//  ExportFtErpFile(Trim(LBName) + '.rmf', ADOQueryTemp);

  LBName := '工艺单';
  ExportFtErpFile('工艺单.rmf', ADOQueryTemp);

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM CP_YDang');
    sql.add('WHERE CYNO=' + quotedstr(trim(CDS_Main.fieldbyname('CYNO').AsString)));
    Open;
  end;
  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select SPZ1=PZ1,SPZ2=PZ2,SPZ3=PZ3,SPZ4=PZ4,SPZ5=PZ5,SPZ6=PZ6,SPZ7=PZ7,SPZ8=PZ8,SPZ9=PZ9,SPZ10=PZ10, ');
    sql.Add(' SPZ11=PZ11,SPZ12=PZ12,SPZ13=PZ13,SPZ14=PZ14,SPZ15=PZ15,SPZ16=PZ16,SPZ17=PZ17,SPZ18=PZ18,SPZ19=PZ19,SPZ20=PZ20, ');
    sql.Add(' SPZ21=PZ21,SPZ22=PZ22,SPZ23=PZ23,SPZ24=PZ24,SPZ25=PZ25,SPZ26=PZ26,SPZ27=PZ27,SPZ28=PZ28,SPZ29=PZ29,SPZ30=PZ30, ');
    sql.Add(' SPZ31=PZ31,SPZ32=PZ32,SPZ33=PZ33,SPZ34=PZ34,SPZ35=PZ35,SPZ36=PZ36 ');
    sql.Add(' from Cloth_PaiZhen B INNER JOIN CP_YDang A on A.CYID=B.MainId  where A.CYNO=''' + Trim(CDS_Main.fieldbyname('CYNo').AsString) + '''');
    sql.Add(' and RTrim(PZType)=''单面三角'' ');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PZ);
  SInitCDSData20(ADOQueryTemp, CDS_PZ);
  if CDS_PZ.IsEmpty then
  begin
    CDS_PZ.Append;
    CDS_PZ.Post;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select SPZ1=PZ1,SPZ2=PZ2,SPZ3=PZ3,SPZ4=PZ4,SPZ5=PZ5,SPZ6=PZ6,SPZ7=PZ7,SPZ8=PZ8,SPZ9=PZ9,SPZ10=PZ10, ');
    sql.Add(' SPZ11=PZ11,SPZ12=PZ12,SPZ13=PZ13,SPZ14=PZ14,SPZ15=PZ15,SPZ16=PZ16,SPZ17=PZ17,SPZ18=PZ18,SPZ19=PZ19,SPZ20=PZ20, ');
    sql.Add(' SPZ21=PZ21,SPZ22=PZ22,SPZ23=PZ23,SPZ24=PZ24,SPZ25=PZ25,SPZ26=PZ26,SPZ27=PZ27,SPZ28=PZ28,SPZ29=PZ29,SPZ30=PZ30, ');
    sql.Add(' SPZ31=PZ31,SPZ32=PZ32,SPZ33=PZ33,SPZ34=PZ34,SPZ35=PZ35,SPZ36=PZ36 ');
    sql.Add(' from Cloth_PaiZhen B INNER JOIN CP_YDang A on A.CYID=B.MainId   where A.CYNO=''' + Trim(CDS_Main.fieldbyname('CYNo').AsString) + '''');
    sql.Add(' and RTrim(PZType)=''双面三角'' ');
//   ShowMessage(sql.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_SJ);
  SInitCDSData20(ADOQueryTemp, CDS_SJ);
  if CDS_SJ.IsEmpty then
  begin
    CDS_SJ.Append;
    CDS_SJ.Post;
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM2.PreviewButtons := [pbZoom, pbLoad, pbSave, pbPrint, pbFind, pbPageSetup, pbExit, pbSaveToXLS, pbExport, pbNavigator];
    RM2.LoadFromFile(fPrintFile);
    RMVariables['SYRName'] := Trim(SYRName);
    RM2.ShowPrintDialog := True;
    RM2.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf'), '提示', 0);
  end;
  RM2.ShowPrintDialog := False;
end;

end.

