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,
  cxLookAndFeels, cxLookAndFeelPainters, cxTLdxBarBuiltInMenu, cxNavigator,
  dxBarBuiltInMenu, cxPC, Math, RM_BarCode, Clipbrd, cxTextEdit, BtnEdit,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter, 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, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue;

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;
    v1Column5: 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;
    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;
    Label6: TLabel;
    CYCF: TEdit;
    Label7: TLabel;
    ZCYNote: TEdit;
    TBImport: TToolButton;
    v1Column7: TcxGridDBColumn;
    OpenDialog1: TOpenDialog;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label9: TLabel;
    CYKZ: TEdit;
    Label8: TLabel;
    CYMF: TEdit;
    v1Column13: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column11: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    cxgrdCYCNHS: TcxGridDBColumn;
    Panel7: TPanel;
    Panel8: TPanel;
    cxTabControl1: TcxTabControl;
    Panel4: TPanel;
    Label14: TLabel;
    LBCPAP1: TLabel;
    Button1: TButton;
    Button2: TButton;
    TCBNOR1: TComboBox;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    RMBarCodeObject1: TRMBarCodeObject;
    Label5: TLabel;
    Label10: TLabel;
    XX1: TEdit;
    ShaZhi: TEdit;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CheckBox1: TCheckBox;
    N3: TMenuItem;
    BSBJ1: TcxGridDBColumn;
    CheckBox2: TCheckBox;
    bsbj: TEdit;
    N4: TMenuItem;
    N5: TMenuItem;
    Tv1Column8: TcxGridDBColumn;
    Label15: TLabel;
    khcpcode2: TEdit;
    Label16: TLabel;
    CYConNo: TEdit;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    CDS2: TClientDataSet;
    DataSource3: TDataSource;
    ToolButton2: TToolButton;
    Label17: TLabel;
    xgbegdate: TDateTimePicker;
    xgenddate: TDateTimePicker;
    CheckBox3: TCheckBox;
    Panel6: TPanel;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridDBColumn32: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton6: TToolButton;
    ToolButton12: TToolButton;
    CDSCB: TClientDataSet;
    DataSource4: TDataSource;
    TV3Column1: TcxGridDBColumn;
    Panel9: TPanel;
    Label18: TLabel;
    Edit2: TEdit;
    Label19: TLabel;
    Edit3: TEdit;
    Button3: TButton;
    Button4: TButton;
    ADOQuery1: TADOQuery;
    Label20: TLabel;
    CustomerNoName: TBtnEditC;
    TV3Column2: TcxGridDBColumn;
    ToolButton3: TToolButton;
    CBID: TEdit;
    CPTYPE: TComboBox;
    Label21: TLabel;
    gongyi: TComboBox;
    Label22: TLabel;
    TV3Column3: TcxGridDBColumn;
    TV3Column4: TcxGridDBColumn;
    Label23: TLabel;
    CDS_ZG: TClientDataSet;
    DataSource5: TDataSource;
    Panel11: TPanel;
    Label24: TLabel;
    Label25: TLabel;
    Label29: TLabel;
    MSGL: TEdit;
    Edit5: TEdit;
    Button5: TButton;
    Button6: TButton;
    zgid: TEdit;
    Label27: TLabel;
    ZGL2: TEdit;
    Label28: TLabel;
    ZGL4: TEdit;
    Panel10: TPanel;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn35: TcxGridDBColumn;
    cxGridDBColumn34: TcxGridDBColumn;
    cxGridDBColumn36: TcxGridDBColumn;
    cxGridDBColumn37: TcxGridDBColumn;
    cxGridDBColumn39: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    ToolBar3: TToolBar;
    ToolButton5: TToolButton;
    ToolButton7: TToolButton;
    ToolButton9: 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 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 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 TCBNOR1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure N3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox3Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton12Click(Sender: TObject);
    procedure CustomerNoNameBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
  private
    { Private declarations }
    CurrentPage, RecordsNumber: Integer;
    CPID: string;
    PState: Integer;
    FCPID, FTopID: string;
    FJurisdiction: string;
    procedure SetJurisdiction();
    procedure InitTree();
    procedure InitGrid();
    procedure Initcb();
    procedure ReadINIFile();
    procedure InitImage();
    procedure LookImage(FileName: string);
  public
    dFdDy: array[0..20] of FdDy; //客户端连接数组
    { Public declarations }
  end;

var
  Mach: array of TfrmSlt;

implementation

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

{$R *.dfm}

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
    application.ProcessMessages;
    try
//      ShowMessage(PChar(Trim('D:\' + Trim(FileName))));
      IdFTP1.Get(Trim(UserDataFlag + 'YP\' + FileName), sFieldName, true, false);
    except
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
end;

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;

  if trim(self.Caption) = '产品信息查询(低权限)' then
  begin
    cxgrdCYCNHS.Visible := False;
    cxgrdCYCNHS.Hidden := 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.Initcb();
begin

  with ADOQuery1 do
  begin
    Close;

    sql.Clear;
    sql.Add('select * from cp_cbprice where cyid=' + QuotedStr(Trim(CDS_Main.fieldbyname('cyid').AsString)));
    SQL.Add('order by filltime desc');
    Open;
  end;
  SCreateCDS20(ADOQuery1, CDSCB);
  SInitCDSData20(ADOQuery1, CDSCB);

  with ADOQuery1 do
  begin
    Close;

    sql.Clear;
    sql.Add('select * from cp_ZGUI where cyid=' + QuotedStr(Trim(CDS_Main.fieldbyname('cyid').AsString)));
    SQL.Add('order by filltime desc');

    Open;
  end;
  SCreateCDS20(ADOQuery1, CDS_ZG);
  SInitCDSData20(ADOQuery1, CDS_ZG);

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;
  fwhere := 'where 1=1 ';
  if CheckBox1.Checked then
  begin
    fwhere := fwhere + ' and   LaYangDate>=' + QuotedStr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
    fwhere := fwhere + 'and   LaYangDate<' + QuotedStr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
  end;
  if CheckBox3.Checked then
  begin
    fwhere := fwhere + ' and   edittime>=' + QuotedStr(FormatDateTime('yyyy-MM-dd', xgBegDate.DateTime));
    fwhere := fwhere + 'and   edittime<' + QuotedStr(FormatDateTime('yyyy-MM-dd', xgenddate.DateTime + 1));
  end;

  Pwhere := SGetFilters(Panel1, 1, 2);

  if trim(Pwhere) <> '' then
  begin
    if fwhere <> '' then
    begin
      fwhere := fwhere + ' and ' + trim(Pwhere);

    end
    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;
//  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;

  ReadCxGrid(self.Caption, Tv1, '样品管理');
  if Self.Caption <> '产品信息管理' then
  begin
    CheckBox2.Visible := False;
    n4.Visible := False;
    n5.Visible := False;
  end;
  RecordsNumber := 100;
  CurrentPage := 1;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 60;

  xgEndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  xgbegdate.DateTime := EndDate.DateTime - 60;
  InitTree();
  SetJurisdiction();
  InitGrid();

//  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(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 := 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;
  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);

  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.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)));
      SQL.Add('ORDER BY XHINT2,XFID');
      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 FCPID = '0' then
      begin
        with ADOQueryTemp do
        begin
          close;
          sql.Clear;
          sql.Add(' select * from  CP_Type  ');
          sql.add('where  CPID=' + quotedstr(trim(CDS_Main.fieldbyname('CYTYPE').AsString)));
          open;
        end;
        FCPID := Trim(ADOQueryTemp.fieldbyname('CPID').AsString);
        FCPNo := Trim(ADOQueryTemp.fieldbyname('CPNo').AsString);
      end;
      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
    CurrentPage := 1;
    InitGrid();
  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(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;
      CYNO.Enabled := False;
      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();
        CDS_Main.locate('CYID', FCYID, []);
      end;
    end;
  finally
    frmCPAddPN.Free;
  end;
end;

procedure TfrmCPManage.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin

  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select * from  XD_File  ');
    sql.add('where   CYID =' + quotedstr(trim(CDS_Main.fieldbyname('CYID').AsString)));
    SQL.Add('ORDER BY XHINT2');
    open;
  end;
  if not ADOQueryTemp.IsEmpty then
    LookImage(Trim(Self.ADOQueryTemp.fieldbyname('FileName').AsString));


//  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.TCBNOR1Change(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR1.Text);
  CurrentPage := 1;
  cyno.SetFocus;
  InitGrid();
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.ToolButton1Click(Sender: TObject);
begin
  try
    frmCPAddPN := TfrmCPAddPN.Create(Application);
    with frmCPAddPN do
    begin
      CopyInt := 0;
      ToolButton1.Visible := False;
      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.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  FCYID: string;
begin
  if CDS_Main.IsEmpty then
    Exit;

  InitImage();
  if Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName) <> 'Ssel' then
  begin
    FCYID := CDS_Main.FieldByName('CYID').AsString;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('SSel').Value := False;
        Post;
        Next;
      end;
    end;
    CDS_Main.locate('CYID', FCYID, []);
    with CDS_Main do
    begin
      Edit;
      FieldByName('SSel').Value := True;
      Post;
    end;
    CDS_Main.EnableControls;
  end;

end;

procedure TfrmCPManage.N3Click(Sender: TObject);
var
  myclipbrd: TClipboard;
begin
  myclipbrd := TClipboard.Create();
  myclipbrd.AsText := cds_main.FieldByName('CYNO').ASSTRING;
end;

procedure TfrmCPManage.ToolButton2Click(Sender: TObject);
begin
  with ADOQueryTemp do
  begin
    Close;
    Filtered := False;
    sql.Clear;
    sql.Add(' exec P_Page_YP  ');
    sql.Add('@CYType=0');
    sql.Add(',@pageIndex=1');
    sql.Add(',@pageSize=100000');
    sql.Add(',@criteria= ''''');
//      ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS2);
  SInitCDSData20(ADOQueryTemp, CDS2);
  TcxGridToExcel('产品档案', cxGrid2);

end;

procedure TfrmCPManage.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[BSBJ1.Index] <> null then
  begin
    if AViewInfo.GridRecord.Values[BSBJ1.Index] = '1' then
      ACanvas.FONT.Color := clRed;
  end;
end;

procedure TfrmCPManage.ToolButton3Click(Sender: TObject);
begin
  if CDSCB.IsEmpty then
    Exit;
  Panel9.Visible := True;
  Edit2.Text := CDSCB.fieldbyname('price').AsString;
  CustomerNoName.Text := CDSCB.fieldbyname('CustomerNoName').AsString;
  Edit3.Text := CDSCB.fieldbyname('note').AsString;
  CBID.Text := CDSCB.fieldbyname('CBID').AsString;
end;

procedure TfrmCPManage.CheckBox2Click(Sender: TObject);
begin
  if CheckBox2.Checked = True then
  begin
    bsbj.Text := '1';
  end
  else
  begin
    bsbj.Text := '';
  end;
  InitGrid();
end;

procedure TfrmCPManage.N4Click(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;
      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 BSBJ=''1'' where CYID=''' + 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.N5Click(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;
      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 BSBJ=NULL where CYID=''' + 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.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmCPManage.ToolButton6Click(Sender: TObject);
begin
  panel9.Visible := True;
end;

procedure TfrmCPManage.Button4Click(Sender: TObject);
begin
  Panel9.Visible := False;
  Edit2.Text := '';
  CustomerNoName.Text := '';
  Edit3.Text := '';
  CBID.Text := '';
end;

procedure TfrmCPManage.Button3Click(Sender: TObject);
var
  maxId: string;
  freal: Double;
begin
  if Edit2.Text = '' then
  begin
    Application.MessageBox('成本价不能为空！', '提示', 0);
    Exit;
  end;
  if TryStrToFloat(Edit2.Text, freal) = False then
  begin
    Application.MessageBox('成本价格式不正确！', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if CBID.Text = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'Y', 'CP_cbprice', 4, 1) = False then
      begin

        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from CP_cbprice where CBID=' + QuotedStr(Trim(CBID.Text)));
      Open;
    end;
    if CBID.Text = '' then
    begin
      with ADOQueryCmd do
      begin

        Append;
        FieldByName('cbid').Value := Trim(maxId);
        FieldByName('CYID').Value := Trim(CDS_Main.FIELDBYNAME('CYID').AsString);
        FieldByName('FILLER').Value := Trim(DName);
        FieldByName('FILLTIME').Value := SGetServerDateTime(ADOQueryTemp);
        FieldByName('CustomerNoName').Value := CustomerNoName.Text;

        FieldByName('gongyi').Value := gongyi.Text;
        FieldByName('CPTYPE').Value := CPTYPE.Text;
        FieldByName('price').Value := StrToFloatdef(Edit2.Text, 0);
        FieldByName('note').Value := Edit3.Text;
        Post;
      end;
    end
    else
    begin
      with ADOQueryCmd do
      begin

        Edit;
//        FieldByName('cbid').Value := Trim(CBID.Text);
        FieldByName('CYID').Value := Trim(CDS_Main.FIELDBYNAME('CYID').AsString);
        FieldByName('EDITER').Value := Trim(DName);
        FieldByName('EDITTIME').Value := SGetServerDateTime(ADOQueryTemp);
        FieldByName('gongyi').Value := gongyi.Text;
        FieldByName('CPTYPE').Value := CPTYPE.Text;
        FieldByName('CustomerNoName').Value := CustomerNoName.Text;
        FieldByName('price').Value := StrToFloatdef(Edit2.Text, 0);
        FieldByName('note').Value := Edit3.Text;
        Post;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!', '提示', 0);
    button4.click();
    Initcb();
  except

    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmCPManage.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  Initcb();
end;

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

  if Trim(CDSCB.fieldbyname('FILLER').AsString) <> Trim(DNAME) then
  begin

    Application.MessageBox('非录入人不能删除！', '提示', 0);
    Exit;

  end
  else
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete CP_CBPRICE where CBID=''' + Trim(CDSCB.fieldbyname('CBID').AsString) + '''');
      ExecSQL;
    end;
    Application.MessageBox('删除成功！', '提示', 0);
  end;
  CDSCB.Delete;

end;

procedure TfrmCPManage.CustomerNoNameBtnUpClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
 
//      IF self.canshu1<>'高权限' then canshu1:='业务员';
      if ShowModal = 1 then
      begin
        CustomerNoName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);

      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmCPManage.CustomerNoNameBtnDnClick(Sender: TObject);
begin
  CustomerNoName.Text := '';
end;

procedure TfrmCPManage.ToolButton5Click(Sender: TObject);
begin
  panel11.Visible := True;
end;

procedure TfrmCPManage.ToolButton7Click(Sender: TObject);
begin
  if CDS_ZG.IsEmpty then
    Exit;
  Panel11.Visible := True;
  MSGL.Text := CDS_ZG.fieldbyname('MSGL').AsString;
  ZGL2.Text := CDS_ZG.fieldbyname('20ZGL').AsString;
  ZGL4.Text := CDS_ZG.fieldbyname('40ZGL').AsString;
  Edit5.Text := CDS_ZG.fieldbyname('note').AsString;
  zgID.Text := CDS_ZG.fieldbyname('zgID').AsString;
end;

procedure TfrmCPManage.Button5Click(Sender: TObject);
var
  maxId: string;
  freal: Double;
begin

  try
    ADOQueryCmd.Connection.BeginTrans;
    if zgid.Text = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'Z', 'CP_zgui', 4, 1) = False then
      begin

        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from CP_zgui where ZGID=' + QuotedStr(Trim(ZGID.Text)));
      Open;
    end;
    if ZGID.Text = '' then
    begin
      with ADOQueryCmd do
      begin

        Append;
        FieldByName('ZGid').Value := Trim(maxId);
        FieldByName('CYID').Value := Trim(CDS_Main.FIELDBYNAME('CYID').AsString);
        FieldByName('FILLER').Value := Trim(DName);
        FieldByName('FILLTIME').Value := SGetServerDateTime(ADOQueryTemp);
        FieldByName('MSGL').Value := MSGL.Text;

        FieldByName('20ZGL').Value := ZGL2.Text;
        FieldByName('40ZGL').Value := ZGL4.Text;

        FieldByName('note').Value := Edit5.Text;
        Post;
      end;
    end
    else
    begin
      with ADOQueryCmd do
      begin

        Edit;
//        FieldByName('cbid').Value := Trim(CBID.Text);
        FieldByName('CYID').Value := Trim(CDS_Main.FIELDBYNAME('CYID').AsString);
        FieldByName('EDITER').Value := Trim(DName);
        FieldByName('EDITTIME').Value := SGetServerDateTime(ADOQueryTemp);
        FieldByName('MSGL').Value := MSGL.Text;

        FieldByName('20ZGL').Value := ZGL2.Text;
        FieldByName('40ZGL').Value := ZGL4.Text;

        FieldByName('note').Value := Edit5.Text;
        Post;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!', '提示', 0);
    button6.click();
    Initcb();
  except

    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmCPManage.Button6Click(Sender: TObject);
begin
  Panel11.Visible := False;
  MSGL.Text := '';
  ZGL2.Text := '';
  ZGL4.Text := '';
  edit5.Text := '';
  zgID.Text := '';
end;

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

  if Trim(CDS_ZG.fieldbyname('FILLER').AsString) <> Trim(DNAME) then
  begin

    Application.MessageBox('非录入人不能删除！', '提示', 0);
    Exit;

  end
  else
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete CP_zgui where zgID=''' + Trim(CDS_ZG.fieldbyname('zgID').AsString) + '''');
      ExecSQL;
    end;
    Application.MessageBox('删除成功！', '提示', 0);
  end;
  CDS_zg.Delete;

end;

end.

