unit U_CPAdd;

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


type
  TfrmCPAdd = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1CYNo: TcxGridDBColumn;
    v1CYName: TcxGridDBColumn;
    v1CYEName: TcxGridDBColumn;
    v1CYSpec: TcxGridDBColumn;
    v1CYCF: TcxGridDBColumn;
    v1CYECF: TcxGridDBColumn;
    v1CYMF: TcxGridDBColumn;
    v1CYKZ: TcxGridDBColumn;
    v1CYPrice: TcxGridDBColumn;
    v1CYNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    CDS_Sub: TClientDataSet;
    ADOQueryMain: TADOQuery;
    DataSource2: TDataSource;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1DefStr2: TcxGridDBColumn;
    v1CYHX: TcxGridDBColumn;
    v1DefStr7: TcxGridDBColumn;
    v1DefStr6: TcxGridDBColumn;
    v1CYPrice3: TcxGridDBColumn;
    v1CYPrice5: TcxGridDBColumn;
    v1DefStr10: TcxGridDBColumn;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    v1CYPrice15: TcxGridDBColumn;
    v1CYPrice12: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    cxGridPopupMenu4: TcxGridPopupMenu;
    Order_ML: TClientDataSet;
    DataSource3: TDataSource;
    Order_JG: TClientDataSet;
    DataSource4: TDataSource;
    v1CYColor: TcxGridDBColumn;
    v1CYSeHao: TcxGridDBColumn;
    Ord_BY: TClientDataSet;
    cxGrid5: TcxGrid;
    tv5: 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;
    cxGridLevel4: TcxGridLevel;
    DataSource5: TDataSource;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    Ord_ER: TClientDataSet;
    DataSource6: TDataSource;
    cxGridPopupMenu5: TcxGridPopupMenu;
    Ord_JGER: TClientDataSet;
    DataSource8: TDataSource;
    ADOQuery5: TADOQuery;
    cxGridPopupMenu7: TcxGridPopupMenu;
    Panel8: TPanel;
    Panel2: TPanel;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    v4ColorName: TcxGridDBColumn;
    v4ColorNo: TcxGridDBColumn;
    v4Note: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    ToolBar3: TToolBar;
    ToolButton9: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Ord_San: TClientDataSet;
    DataSource7: TDataSource;
    cxGridPopupMenu6: TcxGridPopupMenu;
    Ord_Si: TClientDataSet;
    DataSource9: TDataSource;
    cxGridPopupMenu8: TcxGridPopupMenu;
    Ord_JGSan: TClientDataSet;
    DataSource10: TDataSource;
    cxGridPopupMenu9: TcxGridPopupMenu;
    Ord_JGSi: TClientDataSet;
    DataSource11: TDataSource;
    cxGridPopupMenu10: TcxGridPopupMenu;
    ClientDataSet1: TClientDataSet;
    DataSource12: TDataSource;
    cxGridPopupMenu11: TcxGridPopupMenu;
    Ord_GYS: TClientDataSet;
    DataSource13: TDataSource;
    cxGridPopupMenu12: TcxGridPopupMenu;
    cxGridPopupMenu13: TcxGridPopupMenu;
    DataSource14: TDataSource;
    ClientDataSet3: TClientDataSet;
    cxGridPopupMenu14: TcxGridPopupMenu;
    DataSource15: TDataSource;
    ClientDataSet4: TClientDataSet;
    Panel1: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2CengJi: TcxGridDBColumn;
    v2GYSName: TcxGridDBColumn;
    v2MLName: TcxGridDBColumn;
    v2SZSpec: TcxGridDBColumn;
    v2JWMiDu: TcxGridDBColumn;
    v2MLMF: TcxGridDBColumn;
    v2MLKZ: TcxGridDBColumn;
    v2MLColor: TcxGridDBColumn;
    v2MLSeHao: TcxGridDBColumn;
    v2MLPrice: TcxGridDBColumn;
    v2MLSunHao: TcxGridDBColumn;
    v2ZPrice: TcxGridDBColumn;
    v2JGZPrice: TcxGridDBColumn;
    v2MLNote: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar2: TToolBar;
    TMXSave: TToolButton;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    v3JGCengJi: TcxGridDBColumn;
    v3JGFactoryName: TcxGridDBColumn;
    v3GYName: TcxGridDBColumn;
    v3HXNo: TcxGridDBColumn;
    v3MLKZ: TcxGridDBColumn;
    v3MLMF: TcxGridDBColumn;
    v3SZSpec: TcxGridDBColumn;
    v3JWMiDu: TcxGridDBColumn;
    v3MLColor: TcxGridDBColumn;
    v3MLSeHao: TcxGridDBColumn;
    v3Price: TcxGridDBColumn;
    v3MLSunHao: TcxGridDBColumn;
    v3JGPrice: TcxGridDBColumn;
    v3JGNote: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar4: TToolBar;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    cxTabControl1: TcxTabControl;
    cxSplitter1: TcxSplitter;
    cxSplitter3: TcxSplitter;
    v3XJPrice: TcxGridDBColumn;
    cxGrid7: TcxGrid;
    TV11: TcxGridDBTableView;
    cxGridLevel6: TcxGridLevel;
    CJ_Mianliao: TClientDataSet;
    DataSource16: TDataSource;
    V11CengJi: TcxGridDBColumn;
    V11MLName: TcxGridDBColumn;
    cxGridPopupMenu15: TcxGridPopupMenu;
    cxSplitter5: TcxSplitter;
    v3GYDM: TcxGridDBColumn;
    TDel: TToolButton;
    TAdd: TToolButton;
    v2LXRen: TcxGridDBColumn;
    v2LXTel: TcxGridDBColumn;
    v2IFSel: TcxGridDBColumn;
    v2MLCF: TcxGridDBColumn;
    v3IFZY: TcxGridDBColumn;
    v4CYNo: TcxGridDBColumn;
    V1GYDM: TcxGridDBColumn;
    v2MLCode: TcxGridDBColumn;
    v3JGCode: TcxGridDBColumn;
    cxSplitter4: TcxSplitter;
    V11HXNo: TcxGridDBColumn;
    v1OldCYNo: TcxGridDBColumn;
    cxGrid6: TcxGrid;
    TV6: TcxGridDBTableView;
    v2CYPrice1: TcxGridDBColumn;
    v2DefStr4: TcxGridDBColumn;
    v2CYPrice4: TcxGridDBColumn;
    v2CYPrice6: TcxGridDBColumn;
    v2DefStr5: TcxGridDBColumn;
    v2CYPrice7: TcxGridDBColumn;
    v2DefStr8: TcxGridDBColumn;
    v2CYPrice8: TcxGridDBColumn;
    v2DefStr9: TcxGridDBColumn;
    v2CYPrice9: TcxGridDBColumn;
    v2CYPrice5: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    cxGrid8: TcxGrid;
    TV7: TcxGridDBTableView;
    v3ZJCeng: TcxGridDBColumn;
    v3CYPrice10: TcxGridDBColumn;
    v3defstr3: TcxGridDBColumn;
    v3CYPrice11: TcxGridDBColumn;
    v3LiLiao: TcxGridDBColumn;
    v3CYPrice12: TcxGridDBColumn;
    v3CYPrice13: TcxGridDBColumn;
    v3CYPrice14: TcxGridDBColumn;
    v3CYPrice15: TcxGridDBColumn;
    v3CYPrice: TcxGridDBColumn;
    cxGridLevel7: TcxGridLevel;
    v1JCCYNo: TcxGridDBColumn;
    v3HXName: TcxGridDBColumn;
    Panel3: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    V1MLSeHao: TcxGridDBColumn;
    v1PRTDM: TcxGridDBColumn;
    Tooladd: TToolButton;
    ToPrint: TToolButton;
    Panel4: TPanel;
    Label3: TLabel;
    ComboBox1: TComboBox;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    v4Ssel: TcxGridDBColumn;
    v4CLRID: TcxGridDBColumn;
    v1XTNo: TcxGridDBColumn;
    v2MLCYNo: TcxGridDBColumn;
    Label7: TLabel;
    ColorName: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TYSXBQ: TToolButton;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    V1MLCode: TcxGridDBColumn;
    V11MLCYNo: TcxGridDBColumn;
    adoqueryPicture: TADOQuery;
    ODPat: TOpenDialog;
    SaveDialog1: TSaveDialog;
    IdFTP1: TIdFTP;
    RMBarCodeObject1: TRMBarCodeObject;
    v4Tupian: TcxGridDBColumn;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure v1Column8PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column26PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column27PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column14PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1CYPrice1PropertiesEditValueChanged(Sender: TObject);
    procedure v1DefStr10PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure v2CYPrice1PropertiesEditValueChanged(Sender: TObject);
    procedure v1CPNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2DefStr4PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2DefStr5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TMXSaveClick(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure v1CYColorPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1CYMFPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1CYKZPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1DefStr6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1CYSpecPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2MLColorPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v4CYColorPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v3JGGXPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v3JGFactoryNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1DefStr7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2GYSNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v3GXDMPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TClick(Sender: TObject);
    procedure cxTabControl1Changing(Sender: TObject;
      var AllowChange: Boolean);
    procedure v2MLPricePropertiesEditValueChanged(Sender: TObject);
    procedure v3MLSunHaoPropertiesEditValueChanged(Sender: TObject);
    procedure v3GYDMPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TDelClick(Sender: TObject);
    procedure TAddClick(Sender: TObject);
    procedure v2MLCFPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v4ColorNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2MLNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v3HXNoPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TooladdClick(Sender: TObject);
    procedure ToPrintClick(Sender: TObject);
    procedure ColorNameChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TYSXBQClick(Sender: TObject);
    procedure Tv4FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    Fint:Integer;
    procedure InitSubGrid();
    function  SaveData():Boolean;
    function  getmaxNo(FsCPID,FsCPNO:string):string;
    function MLSavedata(Order_ML:TClientDataSet):Boolean;
    function JGSavedata(Order_JG:TClientDataSet;Order_ML:TClientDataSet):Boolean;
    procedure COLSaveData();
    function ColMLSavedata(FMCYID:string):boolean;
    function ColJGSaveData(FMXCYID:string):Boolean;
    procedure Appenddata();
    procedure XJGenXin();
    { Private declarations }
  public
    FCYID,FCYCode,FCPID,FCPNO,FCPName:String;
    CopyInt,Finteger:Integer;

    { Public declarations }
  end;

var
  frmCPAdd: TfrmCPAdd;
  Mach: array of TfrmSlt;

implementation
uses
  U_DataLink,U_Fun,U_iniParam,U_ZDYHelp, U_ZdyAttachCP_HX_sel,U_CPManage_YPCXSel,
  U_ZdyAttachCP_Type_sel,U_ZdyAttachGYS_CX,U_CPManage_YPCX,U_CPManage_ColCX,
  U_FileUp;

{$R *.dfm}
procedure TfrmCPAdd.XJGenXin();
var FXJ:double;
begin
  Order_JG.DisableConstraints;
  with Order_JG do
  begin
    First;
    FXJ:=0;
    while not eof do
    begin
      if FieldByName('IFZY').AsBoolean=true then
      begin
        if FXJ=0 then
        FXJ:=Order_ML.fieldbyname('MLMF').AsFloat*Order_ML.fieldbyname('MLKZ').AsFloat*Order_ML.fieldbyname('MLPrice').AsFloat
             *(1+fieldbyname('MLSunHao').AsFloat)+fieldbyname('JGPrice').AsFloat
        else
        FXJ:=FXJ*(1+fieldbyname('MLSunHao').AsFloat)+fieldbyname('JGPrice').AsFloat;
        edit;
        FieldByName('XJPrice').Value:=FXJ;
      end;
      next;
    end;
  end;
  Order_JG.EnableConstraints;
  Order_ML.DisableConstraints;
  with Order_ML do
  begin
    First;
    while not eof do
    begin
      edit;
      FieldByName('ZPrice').Value:=FXJ;
      Next;
    end;
  end;
  Order_ML.EnableConstraints;
end;

procedure TfrmCPAdd.Appenddata();
begin
  if Order_ML.IsEmpty then
  begin
    with Order_ML do
    begin
      append;
      //fieldbyname('CengJi').Value:=1;
      Post;
    end;
  end;
  if Order_JG.IsEmpty then
  begin
    with Order_JG do
    begin
      append;
      FieldByName('JGCengji').Value:=1;
      Post;
    end;
  end;
end;

function TfrmCPAdd.ColJGSaveData(FMXCYID:string):Boolean;
var
  maxId:String;
begin
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CP_YDang_JG ');
      sql.Add('where CYID='''+trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
      open;
    end;
    with ADOQueryCmd do
    begin
      first;
      while not eof do
      begin
        if GetLSNo(ADOQuery2,maxId,'','CP_YDang_JG',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！','提示',0);
          Exit;
        end;
        with ADOQuery2 do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from CP_YDang_JG where JGID='''+Trim(maxId)+'''');
          Open;
        end;
        with ADOQuery2 do
        begin
          Append;
          FieldByName('JGID').Value:=Trim(maxid);
          FieldByName('CYID').Value:=Trim(FMXCYID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('Filltime').Value:=SGetServerDate(ADOQueryTemp);
          FieldByName('IFZY').Value:=ADOQueryCmd.fieldbyname('IFZY').AsBoolean;
          FieldByName('JGCode').Value:=ADOQueryCmd.fieldbyname('JGCode').AsString;
          FieldByName('JGCengJi').Value:=ADOQueryCmd.fieldbyname('JGCengJi').AsFloat;
          FieldByName('JGGX').Value:=ADOQueryCmd.fieldbyname('JGGX').AsString;
          FieldByName('GXDM').Value:=ADOQueryCmd.fieldbyname('GXDM').AsString;
          FieldByName('JGFactoryName').Value:=ADOQueryCmd.fieldbyname('JGFactoryName').AsString;
          FieldByName('GYName').Value:=ADOQueryCmd.fieldbyname('GYName').AsString;
          FieldByName('GYDM').Value:=ADOQueryCmd.fieldbyname('GYDM').AsString;
          FieldByName('HXNo').Value:=ADOQueryCmd.fieldbyname('HXNo').AsString;
          FieldByName('MLKZ').Value:=ADOQueryCmd.fieldbyname('MLKZ').asfloat;
          FieldByName('MLMF').Value:=ADOQueryCmd.fieldbyname('MLMF').asfloat;
          FieldByName('SZSpec').Value:=ADOQueryCmd.fieldbyname('SZSpec').AsString;
          FieldByName('JWMiDu').Value:=ADOQueryCmd.fieldbyname('JWMiDu').AsString;
          FieldByName('MLColor').Value:=ADOQueryCmd.fieldbyname('MLColor').AsString;
          FieldByName('MLSeHao').Value:=ADOQueryCmd.fieldbyname('MLSeHao').AsString;
          FieldByName('JGNote').Value:=ADOQueryCmd.fieldbyname('JGNote').AsString;
          FieldByName('Price').Value:=ADOQueryCmd.fieldbyname('Price').AsFloat;
          FieldByName('MLSunHao').Value:=ADOQueryCmd.fieldbyname('MLSunHao').AsFloat;
          FieldByName('JGPrice').Value:=ADOQueryCmd.fieldbyname('JGPrice').AsFloat;
          FieldByName('XJPrice').Value:=ADOQueryCmd.fieldbyname('XJPrice').AsFloat;
          FieldByName('CengJi').Value:=ADOQueryCmd.fieldbyname('CengJi').AsFloat;
          Post;
        end;
        next;
      end;
    end;
  except;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

function TfrmCPAdd.ColMLSavedata(FMCYID:string):boolean;
var
  maxId:String;
begin
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CP_YDang_ML ');
      sql.Add('where CYID='''+trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
      open;
    end;
    with ADOQueryCmd do
    begin
      first;
      while not eof do
      begin
        if GetLSNo(ADOQuery2,maxId,'','CP_YDang_ML',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！','提示',0);
          Exit;
        end;
        with ADOQuery2 do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from CP_YDang_ML where MLID='''+Trim(maxId)+'''');
          Open;
        end;
        with ADOQuery2 do
        begin
          Append;
          FieldByName('MLID').Value:=Trim(maxId);
          FieldByName('CYID').Value:=Trim(FMCYID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('Filltime').Value:=SGetServerDate(ADOQueryTemp);
          FieldByName('CengJi').Value:=ADOQueryCmd.fieldbyname('CengJi').AsFloat;
          FieldByName('IFSel').Value:=ADOQueryCmd.fieldbyname('IFSel').AsBoolean;
          FieldByName('MLCode').Value:=ADOQueryCmd.fieldbyname('MLCode').AsString;
          FieldByName('GYSName').Value:=ADOQueryCmd.fieldbyname('GYSName').AsString;
          FieldByName('MLName').Value:=ADOQueryCmd.fieldbyname('MLName').AsString;
          FieldByName('SZSpec').Value:=ADOQueryCmd.fieldbyname('SZSpec').AsString;
          FieldByName('JWMiDu').Value:=ADOQueryCmd.fieldbyname('JWMiDu').AsString;
          FieldByName('MLMF').Value:=ADOQueryCmd.fieldbyname('MLMF').AsFloat;
          FieldByName('MLKZ').Value:=ADOQueryCmd.fieldbyname('MLKZ').AsFloat;
          FieldByName('LXRen').Value:=ADOQueryCmd.fieldbyname('LXRen').AsString;
          FieldByName('LXTel').Value:=ADOQueryCmd.fieldbyname('LXTel').AsString;
          FieldByName('MLCF').Value:=ADOQueryCmd.fieldbyname('MLCF').AsString;
          FieldByName('MLNote').Value:=ADOQueryCmd.fieldbyname('MLNote').AsString;
          FieldByName('MLPrice').Value:=ADOQueryCmd.fieldbyname('MLPrice').AsFloat;
          FieldByName('MLSunHao').Value:=ADOQueryCmd.fieldbyname('MLSunHao').AsFloat;
          FieldByName('ZPrice').Value:=ADOQueryCmd.fieldbyname('ZPrice').AsFloat;
          FieldByName('JGZPrice').Value:=ADOQueryCmd.fieldbyname('JGZPrice').AsFloat;
          Post;
        end;
        next;
      end;
    end;
  except;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmCPAdd.ColSaveData();
var
  maxId:String;
begin
  try
    with Order_Sub do
    begin
      first;
      while not Eof do
      begin
        if FieldByName('CLRID').AsString='' then
        begin
          if GetLSNo(ADOQueryCmd,maxId,'CX','CP_YDang_Color',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败！','提示',0);
            Exit;
          end;
        end
        else
        Maxid:=Trim(FieldByName('CLRID').AsString);
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from CP_YDang_Color where CLRID='''+Trim(maxId)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if IsEmpty then Append else edit;
          FieldByName('CLRID').Value:=Trim(maxId);
          fieldbyname('CYNo').Value:=Trim(CDS_Sub.Fieldbyname('CYNo').AsString)+Trim(Order_Sub.Fieldbyname('ColorNo').AsString);
          FieldByName('CYID').Value:=Trim(CDS_Sub.Fieldbyname('CYID').AsString);
          SSetSaveDataCDSNew(ADOQueryCmd,Tv4,Order_Sub,'CP_YDang_Color',0);
          Post;
        end;
        with Order_Sub do
        begin
          edit;
          FieldByName('CYID').Value:=Trim(CDS_Sub.Fieldbyname('CYID').AsString);
          FieldByName('CLRID').value:=Trim(maxId);
        end;
        next;
      end;
    end;
  except;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

function TfrmCPAdd.MLSavedata(Order_ML:TClientDataSet):Boolean;
var
  maxId:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_ML do
    begin
      first;
      while not eof do
      begin
        if Trim(Order_ML.fieldbyname('MLID').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxId,'','CP_YDang_ML',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxId:=Trim(Order_ML.fieldbyname('MLID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from CP_YDang_ML where MLID='''+Trim(maxId)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if IsEmpty then
          begin
            Append;
          end else
          begin
            Edit;
          end;
          FieldByName('MLID').Value:=Trim(maxId);
          SSetSaveDataCDSNew(ADOQueryCmd,Tv2,Order_ML,'CP_YDang_ML',0);
          FieldByName('IFSel').Value:=Order_ML.fieldbyname('IFSel').AsBoolean;
          FieldByName('CYID').Value:=Trim(FCYID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('Filltime').Value:=SGetServerDate(ADOQueryTemp);
          FieldByName('MLPrice').Value:=Order_ML.fieldbyname('MLPrice').AsFloat;
          FieldByName('MLSunHao').Value:=Order_ML.fieldbyname('MLSunHao').AsFloat;
          FieldByName('ZPrice').Value:=Order_ML.fieldbyname('ZPrice').AsFloat;
          FieldByName('JGZPrice').Value:=Order_ML.fieldbyname('JGZPrice').AsFloat;
          FieldByName('cengji').Value:=cxTabControl1.TabIndex+1;
          Post;
        end;
        with Order_ML do
        begin
          edit;
          FieldByName('CYID').Value:=Trim(FCYID);
          FieldByName('MLID').Value:=Trim(maxid);
        end;
        next;
      end;
    end;
    //Application.MessageBox('保存成功!','提示',0);
    ADOQueryCmd.Connection.CommitTrans;
  except;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

function TfrmCPAdd.JGSavedata(Order_JG:TClientDataSet;Order_ML:TClientDataSet):Boolean;
var
  maxId:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_JG do
    begin
      first;
      while not eof do
      begin
        if Trim(Order_JG.fieldbyname('JGID').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxId,'','CP_YDang_JG',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxId:=Trim(Order_JG.fieldbyname('JGID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from CP_YDang_JG where JGID='''+Trim(maxId)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if IsEmpty then
          begin
            Append;
          end else
          begin
            Edit;
          end;
          FieldByName('JGID').Value:=Trim(maxid);
          //FieldByName('MLID').Value:=Trim(Order_ML.fieldbyname('MLID').AsString);
          SSetSaveDataCDSNew(ADOQueryCmd,Tv3,Order_JG,'CP_YDang_JG',0);
          FieldByName('CYID').Value:=Trim(FCYID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('Filltime').Value:=SGetServerDate(ADOQueryTemp);
          FieldByName('Price').Value:=Order_JG.fieldbyname('Price').AsFloat;
          FieldByName('IFZY').Value:=Order_JG.fieldbyname('IFZY').AsBoolean;
          FieldByName('MLSunHao').Value:=Order_JG.fieldbyname('MLSunHao').AsFloat;
          FieldByName('JGPrice').Value:=Order_JG.fieldbyname('JGPrice').AsFloat;
          FieldByName('CengJi').Value:=cxTabControl1.TabIndex+1;
          Post;
        end;
        with Order_JG do
        begin
          edit;
          FieldByName('CYID').Value:=Trim(FCYID);
          FieldByName('JGID').Value:=Trim(maxid);
          //FieldByName('MLID').Value:=Trim(Order_ML.fieldbyname('MLID').AsString);
        end;
        next;
      end;
    end;
    //Application.MessageBox('保存成功!','提示',0);
    ADOQueryCmd.Connection.CommitTrans;
  except;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

function  TfrmCPAdd.getmaxNo(FsCPID,FsCPNO:string):string;
var
  str1:string;
  i:integer;
begin
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select MAX(CYNo) as CYNo  from CP_YDang where CYType='''+trim(FsCPID)+''' ');
    open;
  end;
  IF trim(ADOQueryTemp.fieldbyname('CYNo').AsString)='' then
  begin
    str1:='000100';
  end
  else
  begin
    i:=10000+strtoint(leftstr(rightstr(trim(ADOQueryTemp.fieldbyname('CYNo').AsString),6),4))+1;
    str1:=rightstr(inttostr(i),4)+'00';
  end;

  result:=FsCPNO+str1;
end;

procedure TfrmCPAdd.InitSubGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from CP_YDang A left join CP_Type B on B.CPID=A.CYType where A.CYID='''+Trim(FCYID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Sub);
    SInitCDSData20(ADOQueryMain,CDS_Sub);
    if CopyInt<>0 then
    begin
      with CDS_Sub do
      begin
        edit;
        FieldByName('DefStr10').Value:='';
        FieldByName('PRTDM').Value:='';
      end;
    end;
    with ADOQuery1 do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select A.* ');
      sql.Add(',TuPian=(case when isnull((select Top 1 C.CYID from XD_File C where C.CYID=A.CYID and C.FileType=''YP''),'''')='''' then '''' else ''是'' end)');
      sql.Add(' from CP_YDang_Color A');
      if CopyInt=2 then
      //sql.Add('where CYID='''+trim(FCPID)+'''')
      sql.Add('where 1=2')
      else
      sql.Add('where CYID='''+trim(FCYID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQuery1,Order_Sub);
    SInitCDSData20(ADOQuery1,Order_Sub);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPAdd.FormShow(Sender: TObject);
var
  fsj,FFSj,FFQZSj,FFFSj,FFType:String;
  FFInt,i,j:Integer;
  ii:LongInt;
begin
  ReadCxGrid('样品编辑N',Tv1,'样品管理');
  ReadCxGrid('样品编辑N2'+inttostr(cxtabcontrol1.TabIndex),Tv2,'样品管理');
  ReadCxGrid('样品编辑N3'+inttostr(cxtabcontrol1.TabIndex),Tv3,'样品管理');
  ReadCxGrid('样品编辑N4',Tv4,'样品管理');
  ReadCxGrid('样品编辑N11',Tv11,'样品管理');
  InitSubGrid();
  IF trim(FCYID)='' then
  begin
    if CDS_Sub.IsEmpty then
    begin
      CDS_Sub.Append;
      CDS_Sub.FieldByName('CYNo').Value:='';
      CDS_Sub.FieldByName('XTNo').Value:='';
      CDS_Sub.FieldByName('CPName').Value:=Trim(FCPName);
      CDS_Sub.FieldByName('CYType').Value:=trim(FCPID);
      CDS_Sub.FieldByName('CYPrice6').Value:=1;
      CDS_Sub.FieldByName('CYPrice13').Value:=1;
      CDS_Sub.Post;
    end;
  end;
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from CP_YDang_ML ');
    SQL.Add('where CYID='''+trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    sql.Add(' and CengJi='''+trim(IntToStr(cxTabControl1.TabIndex+1))+'''');
    open;
  end;
  SCreateCDS20(ADOQuery1,Order_ML);
  SInitCDSData20(ADOQuery1,Order_ML);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.CengJI,A.MLName,A.MLSeHao,A.MLID,A.MLCYNo,A.MLCode ');
    sql.Add(',HXNo=(select Top 1 B.HXNo from CP_YDang_JG B where B.CengJi=A.CengJi and B.CYID=A.CYID and B.IFZY=1)');
    sql.Add(',GYDM=(select Top 1 B.GYDM from CP_YDang_JG B where B.CengJi=A.CengJi and B.CYID=A.CYID and B.IFZY=1)');
    sql.Add(' from CP_YDang_ML A ');
    SQL.Add('where A.CYID='''+trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    sql.Add(' and A.IFSel=1 ');
    sql.Add(' order by A.Cengji,A.MLID');
    open;
  end;
  SCreateCDS20(ADOQuery1,CJ_Mianliao);
  SInitCDSData20(ADOQuery1,CJ_Mianliao);
  with ADOQuery2 do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from CP_YDang_JG ');
    SQL.Add('where CYID='''+trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    sql.Add(' and CengJi='''+trim(IntToStr(cxTabControl1.TabIndex+1))+'''');
    open;
  end;
  SCreateCDS20(ADOQuery2,Order_JG);
  SInitCDSData20(ADOQuery2,Order_JG);
  if CopyInt<>0 then
  begin
    FCYID:='';
    CDS_Sub.edit;
    if CopyInt<>1 then
    begin
      CDS_Sub.FieldByName('JCCYNO').Value:=trim(CDS_Sub.fieldbyname('CYNO').AsString);
    end;
    CDS_Sub.FieldByName('XTNo').Value:='';
    //CDS_Sub.FieldByName('CYNo').Value:='';
    CDS_Sub.Post;
    with Order_ML do
    begin
      First;
      while not eof do
      begin
        Order_ML.edit;
        Order_ML.FieldByName('MLID').Value:='';
        Order_ML.FieldByName('CYID').Value:='';
        Order_ML.Post;
        next;
      end;
    end;
    with Order_JG do
    begin
      First;
      while not eof do
      begin
        Order_JG.edit;
        Order_JG.FieldByName('CYID').Value:='';
        Order_JG.FieldByName('MLID').Value:='';
        Order_JG.FieldByName('JGID').Value:='';
        Order_JG.Post;
        next;
      end;
    end;
    with Order_Sub do
    begin
      First;
      while not eof do
      begin
        edit;
        FieldByName('CYID').Value:='';
        FieldByName('CLRID').Value:='';
        Post;
        next;
      end;
    end;
  end;
  Appenddata();
end;

procedure TfrmCPAdd.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('样品编辑N',Tv1,'样品管理');
  WriteCxGrid('样品编辑N2'+inttostr(cxtabcontrol1.TabIndex),Tv2,'样品管理');
  WriteCxGrid('样品编辑N3'+inttostr(cxtabcontrol1.TabIndex),Tv3,'样品管理');
  WriteCxGrid('样品编辑N4',Tv4,'样品管理');
  WriteCxGrid('样品编辑N11',Tv11,'样品管理');
  Close;
end;

function TfrmCPAdd.SaveData():Boolean;
var
  maxId,FPRTDM,XTMaxID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    FPRTDM:=Trim(CDS_Sub.fieldbyname('PRTDM').AsString);
    if CDS_Sub.FieldByName('XTNo').AsString='' then
    begin
      if GetLSNo(ADOQueryCmd,XTMaxID,FPRTDM,'CP_YDang',5,0)=False then
      begin
        Result:=False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！','提示',0);
        Exit;
      end;
    end
    else
    XTMaxID:=Trim(CDS_Sub.fieldbyname('XTNo').AsString);
    if Trim(FCYID)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxId,'','CP_YDang',5,1)=False then
      begin
        Result:=False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！','提示',0);
        Exit;
      end;
    end else
    begin
      maxId:=Trim(FCYID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from CP_YDang where CYID='''+Trim(FCYID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FCYID)='' then
      begin
        Append;
      end else
      begin
        Edit;
      end;
      FieldByName('XTNo').Value:=Trim(XTMaxID);
      FieldByName('CYID').Value:=Trim(MaxID);
      FieldByName('CYType').Value:=Trim(FCPID);
      if Trim(FCYID)='' then FieldByName('DefFlt4').Value:=Fint;
      SSetSaveDataCDSNew(ADOQueryCmd,Tv1,CDS_Sub,'CP_YDang',3);
      FieldByName('CYPrice').Value:=CDS_Sub.fieldbyname('CYPrice').AsFloat;
      FieldByName('CYPrice1').Value:=CDS_Sub.fieldbyname('CYPrice1').AsFloat;
      FieldByName('CYPrice2').Value:=CDS_Sub.fieldbyname('CYPrice2').AsFloat;
      FieldByName('CYPrice3').Value:=CDS_Sub.fieldbyname('CYPrice3').AsFloat;
      FieldByName('CYPrice4').Value:=CDS_Sub.fieldbyname('CYPrice4').AsFloat;
      FieldByName('CYPrice5').Value:=CDS_Sub.fieldbyname('CYPrice5').AsFloat;
      FieldByName('CYPrice6').Value:=CDS_Sub.fieldbyname('CYPrice6').AsFloat;
      FieldByName('CYPrice7').Value:=CDS_Sub.fieldbyname('CYPrice7').AsFloat;
      FieldByName('CYPrice8').Value:=CDS_Sub.fieldbyname('CYPrice8').AsFloat;
      FieldByName('CYPrice9').Value:=CDS_Sub.fieldbyname('CYPrice9').AsFloat;
      FieldByName('CYPrice10').Value:=CDS_Sub.fieldbyname('CYPrice10').AsFloat;
      FieldByName('CYPrice11').Value:=CDS_Sub.fieldbyname('CYPrice11').AsFloat;
      FieldByName('CYPrice12').Value:=CDS_Sub.fieldbyname('CYPrice12').AsFloat;
      FieldByName('CYPrice13').Value:=CDS_Sub.fieldbyname('CYPrice13').AsFloat;
      FieldByName('CYPrice14').Value:=CDS_Sub.fieldbyname('CYPrice14').AsFloat;
      FieldByName('CYPrice15').Value:=CDS_Sub.fieldbyname('CYPrice15').AsFloat;
      if Trim(FCYID)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('Filltime').Value:=SGetServerDateTime(ADOQueryTemp);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      Post;
    end;
    if CopyInt=1 then
    begin
      //保存面料信息
      ColMLSavedata(Maxid);
      //保存加工信息
      ColJGSaveData(Maxid);
    end;
    if CopyInt=2 then
    begin
      //保存面料信息
      ColMLSavedata(Maxid);
      //保存加工信息
      ColJGSaveData(Maxid);
    end;
    with CDS_Sub do
    begin
      edit;
      FieldByName('CYID').Value:=Trim(maxid);
    end;
    FCYID:=Trim(maxId);
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except;
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmCPAdd.ToolButton1Click(Sender: TObject);
var FCYNo:string;
begin
  ToolBar1.SetFocus;
  if CDS_Sub.IsEmpty then exit;
  if CDS_Sub.Locate('PRTDM','',[])=True then
  begin
    Application.MessageBox('产品代码不能为空','提示');
    exit;
  end;
  if CDS_Sub.Locate('PRTDM',NULL,[])=True then
  begin
    Application.MessageBox('产品代码不能为空','提示');
    exit;
  end;
  if CDS_Sub.Locate('DefStr10','',[])=True then
  begin
    Application.MessageBox('产品属性不能为空','提示');
    exit;
  end;
  if CDS_Sub.Locate('DefStr10',NULL,[])=True then
  begin
    Application.MessageBox('产品属性不能为空','提示');
    exit;
  end;
  if Order_ML.Locate('IFSel',true,[])=false then
  begin
    Application.MessageBox('请选择面料的是否主要','提示',0);
    Exit;
  end;
  if Order_JG.Locate('IFZY',true,[])=false then
  begin
    Application.MessageBox('请选择工序的是否主要','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('ColSel',True,[])=False then
  begin
    //if Application.MessageBox('确定没有颜色吗','提示',1)=2 then exit;
  end
  else
  begin
    Order_Sub.Locate('ColSel',True,[]);
    with CDS_Sub do
    begin
      edit;
      FieldByName('CYColor').Value:=Trim(Order_Sub.fieldbyname('ColorName').AsString);
      FieldByName('CYSeHao').Value:=Trim(Order_Sub.fieldbyname('ColorNo').AsString);
    end;
  end;
  if CopyInt=1 then
  begin
    if SaveData() then
    begin
      with ADOQuery2 do
      begin
        Close;
        sql.Clear;
        sql.Add('delete from CP_YDang_ML where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
        sql.Add(' and CengJI='''+trim(inttostr(cxTabControl1.TabIndex+1))+''' ');
        ExecSQL;
      end;
      with ADOQuery2 do
      begin
        Close;
        sql.Clear;
        sql.Add('delete from CP_YDang_JG where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
        sql.Add(' and CengJI='''+trim(inttostr(cxTabControl1.TabIndex+1))+''' ');
        ExecSQL;
      end;
      if Order_ML.IsEmpty=false then
      begin
        //if Order_ML.Locate('MLName','',[])=False then
        begin
          MLSavedata(Order_ML);
        end;
      end;
      if Order_JG.IsEmpty=false then
      begin
        //if Order_JG.Locate('JGCengJi','',[])=False then
        begin
          JGSavedata(Order_JG,Order_ML);
        end;
      end;
    end;
  end
  else
  begin
    if SaveData() then
    begin
      if Order_ML.IsEmpty=false then
      begin
        //if Order_ML.Locate('MLName','',[])=False then
        begin
          MLSavedata(Order_ML);
        end;
      end;
      if Order_JG.IsEmpty=false then
      begin
        //if Order_JG.Locate('JGCengJi','',[])=False then
        begin
          JGSavedata(Order_JG,Order_ML);
        end;
      end;
    end;
  end;
  FCYNo:=Trim(CDS_Sub.fieldbyname('JCCYNo').AsString);//+'#'+Trim(CDS_Sub.fieldbyname('CYSeHao').AsString);
  with ADOQuery2 do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from CP_YDang_JG ');
    SQL.Add('where CYID='''+trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    //sql.Add(' and CengJi<>1');
    sql.Add(' and isnull(IFZY,'''')=''1''');
    sql.Add(' order by Cengji,JGCengJi');
    //ShowMessage(sql.Text);
    open;
  end;
  with ADOQuery2 do
  begin
    First;
    while not eof do
    begin
      FCYNO:=FCYNO+trim(fieldbyname('GYDM').AsString)+trim(fieldbyname('MLSeHao').AsString)+trim(fieldbyname('HXNo').AsString);
      next;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select CYID from CP_YDang where XTNo='''+Trim(CDS_Sub.fieldbyname('XTNo').AsString)+'''');
    //ShowMessage(sql.Text);
    Open;
    if ADOQueryCmd.RecordCount>1 then
    begin
      if CopyInt<>0 then
      begin
        with ADOQuery2 do
        begin
          close;
          sql.Clear;
          sql.Add('delete from CP_YDang where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          sql.Add('delete from CP_YDang_ML where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          sql.Add('delete from CP_YDang_JG where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          ExecSQL;
        end;
        FCYID:='';
      end
      else
      begin
        with ADOQuery2 do
        begin
          close;
          sql.Clear;
          sql.Add('update CP_YDang set CYNo='''+trim(CDS_Sub.fieldbyname('CYNo').AsString)+'''');
          sql.Add(' where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          ExecSQL;
        end;
      end;
      Application.MessageBox('公司编号重复！','提示',0);
      Exit;
    end;
  end;
  with ADOQuery2 do
  begin
    close;
    sql.Clear;
    sql.Add('update CP_YDang set CYNo='''+trim(FCYNo)+'''');
    sql.Add(' where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select CYID from CP_YDang where CYNo='''+Trim(FCYNo)+'''');
    //ShowMessage(sql.Text);
    Open;
    if ADOQueryCmd.RecordCount>1 then
    begin
      if CopyInt<>0 then
      begin
        with ADOQuery2 do
        begin
          close;
          sql.Clear;
          sql.Add('delete from CP_YDang where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          sql.Add('delete from CP_YDang_ML where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          sql.Add('delete from CP_YDang_JG where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          ExecSQL;
        end;
        FCYID:='';
      end
      else
      begin
        with ADOQuery2 do
        begin
          close;
          sql.Clear;
          sql.Add('update CP_YDang set CYNo='''+trim(CDS_Sub.fieldbyname('CYNo').AsString)+'''');
          sql.Add(' where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          ExecSQL;
        end;
      end;
      Application.MessageBox('编号重复！','提示',0);
      Exit;
    end;
  end;
  with CDS_Sub do
  begin
    edit;
    FieldByName('CYNo').AsString:=Trim(FCYNo);
  end;
  with ADOQuery2 do
  begin
    close;
    sql.Clear;
    sql.Add('update CP_YDang set CYHX=(select Top 1 HXName from CP_YDang_JG where CYID=CP_YDang.CYID and IFZY=1 and CengJi=6)');
    sql.Add(' where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    ExecSQL;
  end;
  COLSaveData();
  Application.MessageBox('保存成功！','提示',0);
  ModalResult:=1;
end;

procedure TfrmCPAdd.v1Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  if Trim(DParameters3)='' then
  begin
    mvalue:=Trim(TcxTextEdit(Sender).EditingText);
    with CDS_Sub do
    begin
      Edit;
      FieldByName('CYKZ').Value:=mvalue+'g/㎡';
      Post;
    end;
  end;

end;

procedure TfrmCPAdd.v1Column2PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YPName';
      flagname:='样品名称';
      if ShowModal=1 then
      begin
        with CDS_Sub do
        begin
          Edit;
          FieldByName('CYName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCPAdd.v1Column26PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
var
  fsj:string;
begin
  fsj:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GYStr';
      flagname:='工艺名称';
      if ShowModal=1 then
      begin
        with CDS_Sub do
        begin
          Edit;
          FieldByName(fsj).Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCPAdd.v1Column27PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
var
  fsj:string;
begin
  fsj:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YPJGFactory';
      flagname:='加工厂';
      if ShowModal=1 then
      begin
        with CDS_Sub do
        begin
          Edit;
          FieldByName(fsj).Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCPAdd.v1Column14PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZdyAttachCP_HX_Sel:=TfrmZdyAttachCP_HX_Sel.create(self);
  with frmZdyAttachCP_HX_Sel do
  begin
    IF showmodal=1 then
    begin
      CDS_Sub.Edit;
      CDS_Sub.FieldByName('CYHX').Value:=trim(cds_Hz.FieldByName('ZdyName').asstring);
      CDS_Sub.FieldByName('CYNo').Value:=trim(CDS_Sub.FieldByName('CYNo').AsString)+trim(cds_Hz.FieldByName('ZdyCode').asstring);
      CDS_Sub.Post;
    end;
    frmZdyAttachCP_HX_Sel.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCPAdd.v1CYPrice1PropertiesEditValueChanged(Sender: TObject);
var
  fieldname,mavlue:string;
begin
  fieldname:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  mavlue:=TcxTextEdit(Sender).EditingText;
  if mavlue='' then mavlue:='0';
  with CDS_Sub do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value:=mavlue;
    FieldByName('CYPrice').Value:=FieldByName('CYPrice12').AsFloat*(1+FieldByName('CYPrice13').AsFloat)+FieldByName('CYPrice14').AsFloat+FieldByName('CYPrice15').AsFloat;
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCPAdd.v1DefStr10PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
var
  fsj:string;
begin
  fsj:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YPSXingFlag';
      flagname:='产品属性';
      Fnote:=true;
      V1Note.Caption:='代码';
      if ShowModal=1 then
      begin
        with CDS_Sub do
        begin
          Edit;
          FieldByName(fsj).Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          FieldByName('PRTDM').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCPAdd.ToolButton2Click(Sender: TObject);
begin
  try
    frmFileUp:=TfrmFileUp.Create(Application);
    with frmFileUp do
    begin
      Code.Text:=Trim(Self.Order_Sub.fieldbyname('CYNO').AsString);
      CYID:=Trim(Self.Order_Sub.fieldbyname('CLRID').AsString);
      //FName:=Trim(Self.CDS_Sub.fieldbyname('CYNO').AsString)+Trim(Self.Order_Sub.fieldbyname('ColorNo').AsString);
      if ShowModal=1 then
      begin
        Self.Order_Sub.Locate('CLRID',CYID,[]);
      end;
    end;
  finally
    frmFileUp.Free;
  end;
end;

procedure TfrmCPAdd.v2CYPrice1PropertiesEditValueChanged(Sender: TObject);
var
  fieldname:string;
begin
  fieldname:=Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with CDS_Sub do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value:=TcxTextEdit(Sender).EditingText;
    FieldByName('CYPrice5').Value:=FieldByName('CYPrice1').AsFloat*FieldByName('CYPrice6').AsFloat+FieldByName('CYPrice7').AsFloat+
                                   FieldByName('CYPrice8').AsFloat+FieldByName('CYPrice9').AsFloat+FieldByName('CYPrice4').AsFloat;
    FieldByName('CYPrice').Value:=(FieldByName('CYPrice5').AsFloat+FieldByName('CYPrice10').AsFloat +FieldByName('CYPrice11').AsFloat+
                                   FieldByName('CYPrice12').AsFloat)*FieldByName('CYPrice13').AsFloat+FieldByName('CYPrice14').AsFloat+FieldByName('CYPrice15').AsFloat;
    Post;
  end;
  Tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmCPAdd.v1CPNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZdyAttachCP_Type_sel:=TfrmZdyAttachCP_Type_sel.Create(self);
  with frmZdyAttachCP_Type_sel do
  begin
    IF showmodal=1 then
    begin
      CDS_Sub.Edit;
      CDS_Sub.FieldByName('CYType').Value:=trim(CDS_HZ.FieldByName('CPID').asstring);
      CDS_Sub.FieldByName('CPName').Value:=trim(CDS_HZ.FieldByName('CPName').asstring);
      CDS_Sub.Post;
    end;
    frmZdyAttachCP_Type_sel.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCPAdd.v2DefStr4PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
var
  fsj:string;
begin
  fsj:=Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='RanZGY';
      flagname:='染整工艺';
      if ShowModal=1 then
      begin
        with CDS_Sub do
        begin
          Edit;
          FieldByName(fsj).Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCPAdd.v2DefStr5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
var
  fsj:string;
begin
  fsj:=Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='JiaGongGYFlag';
      flagname:='加工工艺';
      if ShowModal=1 then
      begin
        with CDS_Sub do
        begin
          Edit;
          FieldByName(fsj).Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCPAdd.TMXSaveClick(Sender: TObject);
begin
  //ToolBar2.SetFocus;
  if CDS_Sub.IsEmpty then exit;
  if Order_ML.IsEmpty then exit;
  //if Order_ML.Locate('MLName','',[])=false then
  begin
    MLSavedata(Order_ML);
  end;
  //if Order_JG.Locate('JGGX','',[])=false then
  begin
    JGSavedata(Order_JG,Order_ML);
  end;
end;

procedure TfrmCPAdd.ToolButton7Click(Sender: TObject);
begin
  if Order_ML.IsEmpty then exit;
  {if Trim(Order_ML.fieldbyname('MLID').AsString)='' then
  begin
    Application.MessageBox('请先保存面料信息','提示',0);
    Exit;
  end;}
  with Order_JG do
  begin
    Append;
    FieldByName('JGCengJi').Value:=Order_JG.RecordCount+1;
    Post;
  end;
end;

procedure TfrmCPAdd.ToolButton8Click(Sender: TObject);
begin
  if Order_JG.IsEmpty then Exit;
  if Trim(Order_JG.fieldbyname('JGID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from CP_YDang_JG where JGID='''+Trim(Order_JG.fieldbyname('JGID').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_JG.Delete;
end;

procedure TfrmCPAdd.ToolButton6Click(Sender: TObject);
begin
  ToolBar4.SetFocus;
  if Order_ML.IsEmpty then exit;
  if Order_JG.IsEmpty then exit;
  //if Order_JG.Locate('JGGX','',[])=false then
  JGSavedata(Order_JG,Order_ML);
end;

procedure TfrmCPAdd.ToolButton4Click(Sender: TObject);
begin
  frmCPManage_ColCX:=TfrmCPManage_ColCX.create(self);
  with frmCPManage_ColCX do
  begin
    FYPCPID:=Trim(FCPID);
    if showmodal=1 then
    begin
      with frmCPManage_ColCX.CDS_Sub do
      begin
        first;
        while not eof do
        begin
          if FieldByName('Ssel').AsBoolean=True then
          begin
            with self.Order_Sub do
            begin
              Append;
              FieldByName('ColorName').Value:=Trim(frmCPManage_ColCX.CDS_Sub.fieldbyname('ColorName').AsString);
              FieldByName('ColorNo').Value:=Trim(frmCPManage_ColCX.CDS_Sub.fieldbyname('ColorNo').AsString);
              post;
            end;
          end;
          next;
        end;
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.ToolButton5Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('CLRID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CP_YDang_Color where CLRID='''+Trim(Order_Sub.fieldbyname('CLRID').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmCPAdd.ToolButton9Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then exit;
  if Order_Sub.IsEmpty then exit;
  if Order_Sub.Locate('CYColor',null,[])=True then
  begin
    Application.MessageBox('颜色不能为空！','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('CYSeHao',null,[])=True then
  begin
    Application.MessageBox('色号不能为空！','提示',0);
    Exit;
  end;
  ToolBar1.SetFocus;
  COLSaveData();
  Application.MessageBox('保存成功！','提示',0);
  //ModalResult:=1;
end;

procedure TfrmCPAdd.v1CYColorPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='CYColor';
    flagname:='颜色';
    MainType:=Trim(CDS_Sub.fieldbyname('CYNo').AsString);
    if ShowModal=1 then
    begin
      with CDS_Sub do
      begin
        edit;
        FieldByName('CYColor').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.v1CYMFPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='CYMF';
    flagname:='门幅';
    if ShowModal=1 then
    begin
      with CDS_Sub do
      begin
        edit;
        FieldByName('CYMF').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.v1CYKZPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='CYKZ';
    flagname:='克重';
    if ShowModal=1 then
    begin
      with CDS_Sub do
      begin
        edit;
        FieldByName('CYKZ').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.v1DefStr6PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='DefStr6';
    flagname:='经纬密度';
    if ShowModal=1 then
    begin
      with CDS_Sub do
      begin
        edit;
        FieldByName('DefStr6').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.v1CYSpecPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='CYSpec';
    flagname:='纱支规格';
    if ShowModal=1 then
    begin
      with CDS_Sub do
      begin
        edit;
        FieldByName('CYSpec').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.v2MLColorPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='CYColor';
    flagname:='颜色';
    MainType:=Trim(CDS_Sub.fieldbyname('CYNo').AsString);
    if ShowModal=1 then
    begin
      with Order_ML do
      begin
        edit;
        FieldByName('MLColor').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.v4CYColorPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='CYColor';
    flagname:='颜色';
    MainType:=Trim(CDS_Sub.fieldbyname('CYNo').AsString);
    if ShowModal=1 then
    begin
      with Order_ML do
      begin
        edit;
        FieldByName('CYColor').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.v3JGGXPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='JGGX';
    flagname:='工序名称';
    fnote:=True;
    V1Note.Caption:='工序代码';
    if ShowModal=1 then
    begin
      with Order_JG do
      begin
        edit;
        FieldByName('JGGX').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        FieldByName('GXDM').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.cxTabControl1Change(Sender: TObject);
begin                                  
  if CDS_Sub.IsEmpty then exit;
  label2.Caption:=cxTabControl1.Tabs.Strings[cxTabControl1.TabIndex];
  //TMXSave.Click;
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from CP_YDang_ML ');
    SQL.Add('where CYID='''+trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    sql.Add(' and CengJi='''+trim(IntToStr(cxTabControl1.TabIndex+1))+'''');
    open;
  end;
  SCreateCDS20(ADOQuery1,Order_ML);
  SInitCDSData20(ADOQuery1,Order_ML);
  with ADOQuery2 do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from CP_YDang_JG ');
    SQL.Add('where CYID='''+trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    sql.Add(' and CengJi='''+trim(IntToStr(cxTabControl1.TabIndex+1))+'''');
    //ShowMessage(sql.Text);
    open;
  end;
  SCreateCDS20(ADOQuery2,Order_JG);
  SInitCDSData20(ADOQuery2,Order_JG);
  {with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    SQL.Add('delete from CP_YDang_ML where isnull(MLName,'''')=''''');
    execsql;
  end;
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    SQL.Add('delete from CP_YDang_JG where isnull(JGGX,'''')=''''');
    execsql;
  end;}
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.CengJI,A.MLName,A.MLSeHao,A.MLID ');
    sql.Add(',HXNo=(select Top 1 B.HXNo from CP_YDang_JG B where B.CengJi=A.CengJi and B.CYID=A.CYID and B.IFZY=1)');
    sql.Add(',GYDM=(select Top 1 B.GYDM from CP_YDang_JG B where B.CengJi=A.CengJi and B.CYID=A.CYID and B.IFZY=1)');
    sql.Add(' from CP_YDang_ML A ');
    SQL.Add('where A.CYID='''+trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    sql.Add(' and A.IFSel=1 ');
    sql.Add(' order by A.Cengji,A.MLID');
    open;
  end;
  SCreateCDS20(ADOQuery1,CJ_Mianliao);
  SInitCDSData20(ADOQuery1,CJ_Mianliao);
  Appenddata();
  ReadCxGrid('样品编辑N2'+inttostr(cxtabcontrol1.TabIndex),Tv2,'样品管理');
  ReadCxGrid('样品编辑N3'+inttostr(cxtabcontrol1.TabIndex),Tv3,'样品管理');
end;

procedure TfrmCPAdd.v3JGFactoryNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZdyAttachGYS_CX:=TfrmZdyAttachGYS_CX.Create(self);
  with frmZdyAttachGYS_CX do
  begin
    if ShowModal=1 then
    begin
      with Order_JG do
      begin
        edit;
        FieldByName('JGFactoryName').Value:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.v1DefStr7PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZdyAttachGYS_CX:=TfrmZdyAttachGYS_CX.Create(self);
  with frmZdyAttachGYS_CX do
  begin
    if ShowModal=1 then
    begin
      with CDS_Sub do
      begin
        edit;
        FieldByName('DefStr7').Value:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.v2GYSNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZdyAttachGYS_CX:=TfrmZdyAttachGYS_CX.Create(self);
  with frmZdyAttachGYS_CX do
  begin
    if ShowModal=1 then
    begin
      with Order_ML do
      begin
        edit;
        FieldByName('GYSName').Value:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        fieldbyname('LXRen').Value:=Trim(CDS_HZ.fieldbyname('DEFstr4').AsString);
        if Trim(CDS_HZ.fieldbyname('DEFstr2').AsString)<>'' then
        fieldbyname('LXTel').Value:=Trim(CDS_HZ.fieldbyname('DEFstr2').AsString)
        else
        fieldbyname('LXTel').Value:=Trim(CDS_HZ.fieldbyname('DEFstr1').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.v3GXDMPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='JGGX';
    flagname:='工序名称';
    fnote:=True;
    V1Note.Caption:='工序代码';
    if ShowModal=1 then
    begin
      with Order_JG do
      begin
        edit;
        FieldByName('JGGX').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        FieldByName('GXDM').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.TClick(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete from CP_YDang_ML where MLID='''+Trim(Order_ML.fieldbyname('MLID').AsString)+'''');
    ExecSQL;
  end;
  Order_ML.Delete;
end;

procedure TfrmCPAdd.cxTabControl1Changing(Sender: TObject;
  var AllowChange: Boolean);
var FCYNO:string;
begin
  ToolBar1.SetFocus;
  if CDS_Sub.Locate('PRTDM','',[])=True then
  begin
    Application.MessageBox('产品代码不能为空','提示');
    exit;
  end;
  if CDS_Sub.Locate('PRTDM',NULL,[])=True then
  begin
    Application.MessageBox('产品代码不能为空','提示');
    exit;
  end;
  if Order_Sub.Locate('ColSel',True,[])=False then
  begin
    //if Application.MessageBox('确定没有颜色吗','提示',1)=2 then exit;
  end
  else
  begin
    Order_Sub.Locate('ColSel',True,[]);
    with CDS_Sub do
    begin
      edit;
      FieldByName('CYColor').Value:=Trim(Order_Sub.fieldbyname('ColorName').AsString);
      FieldByName('CYSeHao').Value:=Trim(Order_Sub.fieldbyname('ColorNo').AsString);
    end;
  end;
  if FCYID='' then
  begin
    if SaveData() then
    begin
      if CopyInt=1 then
      begin
        with ADOQuery2 do
        begin
          Close;
          sql.Clear;
          sql.Add('delete from CP_YDang_ML where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          sql.Add(' and CengJI='''+trim(inttostr(cxTabControl1.TabIndex+1))+''' ');
          ExecSQL;
        end;
        with ADOQuery2 do
        begin
          Close;
          sql.Clear;
          sql.Add('delete from CP_YDang_JG where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          sql.Add(' and CengJI='''+trim(inttostr(cxTabControl1.TabIndex+1))+''' ');
          ExecSQL;
        end;
      end;
      TMXSave.Click;
    end;
  end
  else
  TMXSave.Click;
  FCYNo:=Trim(CDS_Sub.fieldbyname('JCCYNo').AsString);//+'#'+trim(CDS_Sub.fieldbyname('CYSeHao').AsString);
  {with Order_Sub do
  begin
    first;
    while not Eof do
    begin
      if FieldByName('ColSel').AsBoolean=True then
      begin
        FCYNO:=FCYNO+'#'+trim(fieldbyname('ColorNo').AsString);
      end;
      next;
    end;
  end;}
  with ADOQuery2 do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from CP_YDang_JG ');
    SQL.Add('where CYID='''+trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    sql.Add(' and CengJi<>1');
    //sql.Add(' and isnull(IFZY,'''')=''1''');
    sql.Add(' order by Cengji,JGCengJi');
    //ShowMessage(sql.Text);
    open;
  end;
  with ADOQuery2 do
  begin
    First;
    while not eof do
    begin
      FCYNO:=FCYNO+trim(fieldbyname('GYDM').AsString)+trim(fieldbyname('MLSeHao').AsString)+trim(fieldbyname('HXNo').AsString);
      next;
    end;
  end;
  with ADOQuery2 do
  begin
    close;
    sql.Clear;
    sql.Add('update CP_YDang set CYNo='''+trim(FCYNo)+'''');
    sql.Add(' where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select CYID from CP_YDang where CYNo='''+Trim(FCYNo)+'''');
    //ShowMessage(sql.Text);
    Open;
    if ADOQueryCmd.RecordCount>1 then
    begin
      if CopyInt<>0 then
      begin
        {with ADOQuery2 do
        begin
          close;
          sql.Clear;
          sql.Add('delete from CP_YDang where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          sql.Add('delete from CP_YDang_ML where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          sql.Add('delete from CP_YDang_JG where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          ExecSQL;
        end;
        FCYID:='';}
      end
      else
      begin
        with ADOQuery2 do
        begin
          close;
          sql.Clear;
          sql.Add('update CP_YDang set CYNo='''+trim(CDS_Sub.fieldbyname('CYNo').AsString)+'''');
          sql.Add(' where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          ExecSQL;
        end;
        Application.MessageBox('编号重复！','提示',0);
        Exit;
      end;
    end;
  end;
  with CDS_Sub do
  begin
    edit;
    FieldByName('CYNo').AsString:=Trim(FCYNo);
  end;
  COLSaveData();
  if CopyInt<>0 then
  begin
    CopyInt:=0;
  end;
  WriteCxGrid('样品编辑N2'+inttostr(cxtabcontrol1.TabIndex),Tv2,'样品管理');
  WriteCxGrid('样品编辑N3'+inttostr(cxtabcontrol1.TabIndex),Tv3,'样品管理');
end;

procedure TfrmCPAdd.v2MLPricePropertiesEditValueChanged(Sender: TObject);
var mavlue,FFieldName:string;
    FXJ:double;
begin
  mavlue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if FFieldName<>'IFSel' then
  begin
    if mavlue='' then mavlue:='0';
  end;
  with Order_ML do
  begin
    edit;
    FieldByName(FFieldName).value:=mavlue;
  end;
  XJGenXin();
end;

procedure TfrmCPAdd.v3MLSunHaoPropertiesEditValueChanged(Sender: TObject);
var mavlue,FfieldName:string;
    FJGXJ:double;
begin
  mavlue:=TcxTextEdit(Sender).EditingText;
  FfieldName:=Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if FfieldName<>'IFZY' then
  begin
    if mavlue='' then mavlue:='0';
  end;
  with Order_JG do
  begin
    Edit;
    FieldByName(FfieldName).Value:=mavlue;
  end;
  XJGenXin();
end;

procedure TfrmCPAdd.v3GYDMPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='GYName';
    flagname:='工艺';
    fnote:=True;
    V1Note.Caption:='工艺代码';
    if ShowModal=1 then
    begin
      with Order_JG do
      begin
        edit;
        FieldByName('GYName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        FieldByName('GYDM').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.TDelClick(Sender: TObject);
begin
  if Order_ML.IsEmpty then Exit;
  if Trim(Order_ML.fieldbyname('MLID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from CP_YDang_ML where MLID='''+Trim(Order_ML.fieldbyname('MLID').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_ML.Delete;
end;

procedure TfrmCPAdd.TAddClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then exit;
  {if Trim(CDS_Sub.fieldbyname('CYID').AsString)='' then
  begin
    Application.MessageBox('请先保存产品信息','提示',0);
    Exit;
  end;}
  with Order_ML do
  begin
    Append;
    //FieldByName('JGCengJi').Value:=Order_JG.RecordCount+1;
    Post;
  end;
end;

procedure TfrmCPAdd.v2MLCFPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='MLCF';
    flagname:='成份';
    if ShowModal=1 then
    begin
      with Order_JG do
      begin
        edit;
        FieldByName('MLCF').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.v4ColorNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='ColorName';
    flagname:='颜色';
    MainType:=Trim(CDS_Sub.fieldbyname('CYNo').AsString);
    if ShowModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        FieldByName('ColorName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd.v2MLNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmCPManage_YPCXSel:=TfrmCPManage_YPCXSel.create(self);
  with frmCPManage_YPCXSel do
  begin
    if showmodal=1 then
    begin
      with self.Order_ML do
      begin
        edit;
        //FieldByName('GYSName').Value:=Trim(CDS_Main.fieldbyname('DefStr7').AsString);
        FieldByName('MLName').Value:=Trim(CDS_Main.fieldbyname('CYName').AsString);
        FieldByName('SZSpec').Value:=Trim(CDS_Main.fieldbyname('CYSpec').AsString);
        FieldByName('JWMiDu').Value:=Trim(CDS_Main.fieldbyname('DefStr6').AsString);
        FieldByName('MLCode').Value:=Trim(CDS_Main.fieldbyname('XTNo').AsString);
        FieldByName('MLCYNo').Value:=Trim(CDS_Main.fieldbyname('CYNo').AsString);
        if CDS_Main.fieldbyname('CYMF').AsString='' then
        FieldByName('MLMF').Value:=0
        else
        FieldByName('MLMF').Value:=CDS_Main.fieldbyname('CYMF').AsString;
        if CDS_Main.fieldbyname('CYKZ').AsString='' then
        FieldByName('MLKZ').Value:=0
        else
        FieldByName('MLKZ').Value:=CDS_Main.fieldbyname('CYKZ').AsString;
        FieldByName('MLCF').Value:=Trim(CDS_Main.fieldbyname('CYCF').AsString);
        FieldByName('MLSeHao').Value:=Trim(CDS_Sub.fieldbyname('ColorNo').AsString)+Trim(CDS_Sub.fieldbyname('ColorName').AsString);
        //post;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from CP_YDang_JG where CYID='''+Trim(CDS_Main.fieldbyname('CYID').AsString)+'''');
        sql.Add(' and IFZY=1');
        open;
      end;
      SInitCDSData20(ADOQueryCmd,Order_JG);
    end;
    free;
  end;
end;

procedure TfrmCPAdd.v3HXNoPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZdyAttachCP_HX_Sel:=TfrmZdyAttachCP_HX_Sel.create(self);
  with frmZdyAttachCP_HX_Sel do
  begin
    IF showmodal=1 then
    begin
      Order_JG.Edit;
      Order_JG.FieldByName('HXNo').Value:=trim(cds_Hz.FieldByName('zdyCode').asstring);
      Order_JG.FieldByName('HXName').Value:=trim(cds_Hz.FieldByName('zdyName').asstring);
      Order_JG.FieldByName('JGCode').Value:=trim(CDS_Sub.FieldByName('CYNo').AsString)+trim(cds_Hz.FieldByName('ZdyCode').asstring);
      Order_JG.Post;
    end;
    frmZdyAttachCP_HX_Sel.Free;
  end;
  tv3.Controller.EditingController.ShowEdit();
end;

procedure TfrmCPAdd.TooladdClick(Sender: TObject);
var i:string;
begin
  i:=IntToStr(Order_Sub.RecordCount+1);
  if Length(i)<2 then
  i:='0'+i;
  Order_Sub.DisableControls;
  with Order_Sub do
  begin
    first;
    while not eof do
    begin
      if Order_Sub.Locate('ColorNo',i,[])=True then
      begin
        First;
        i:=IntToStr(strtoint(i)+1);
      end;
      next;
    end;
  end;
  Order_Sub.EnableControls;
  if Length(i)<2 then
  i:='0'+i;
  with Order_Sub do
  begin
    Append;
    FieldByName('ColorNo').Value:=i;
    Post;
  end;
end;

procedure TfrmCPAdd.ToPrintClick(Sender: TObject);
var
  fPrintFile,FFCYID,FFCLRID:string;
  DPS,i,j:Integer;
  Txt,fImagePath,FCJ1,FCJ2,FCJ3,FCJ4,FCJ5,FCJ6,FCJ7,FCJ8,FHX,FHXName,FGHNo:string;
  Moudle: THandle;
  Makebar:TMakebar;
  Mixtext:TMixtext;
begin
  if Order_Sub.IsEmpty then Exit;
  if Order_Sub.Locate('Ssel',True,[])=false then
  begin
    Application.MessageBox('没有选择数据','提示');
    exit;
  end;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select GHNo from OA_YG_DangAn where YGName='''+trim(DName)+''' ');
    open;
  end;
  FGHNo:=trim(ADOQueryCmd.fieldbyname('GHNo').asstring);
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\子颜色标签.rmf';
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_YDang_JG where CYID='''+Trim(Order_Sub.fieldbyname('CYID').AsString)+'''');
    sql.Add(' and Cengji=6 and IFZY=1');
    open;
  end;
  FHX:=Trim(ADOQueryCmd.fieldbyname('HXNo').AsString);
  FHXName:=Trim(ADOQueryCmd.fieldbyname('HXName').AsString);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_YDang_ML where CYID='''+Trim(Order_Sub.fieldbyname('CYID').AsString)+'''');
    sql.Add(' and IFSel=1');
    open;
  end;
  with ADOQueryCmd do
  begin
    first;
    while not eof do
    begin
      if FieldByName('CengJi').AsInteger=1 then
      begin
        FCJ1:=Trim(FieldByName('MLName').AsString);
      end;
      if FieldByName('CengJi').AsInteger=2 then
      begin
        FCJ2:=Trim(FieldByName('MLName').AsString);
      end;
      if FieldByName('CengJi').AsInteger=3 then
      begin
        FCJ3:=Trim(FieldByName('MLName').AsString);
      end;
      if FieldByName('CengJi').AsInteger=4 then
      begin
        FCJ4:=Trim(FieldByName('MLName').AsString);
      end;
      if FieldByName('CengJi').AsInteger=5 then
      begin
        FCJ5:=Trim(FieldByName('MLName').AsString);
      end;
      if FieldByName('CengJi').AsInteger=6 then
      begin
        FCJ6:=Trim(FieldByName('MLName').AsString);
      end;
      if FieldByName('CengJi').AsInteger=7 then
      begin
        FCJ7:=Trim(FieldByName('MLName').AsString);
      end;
      if FieldByName('CengJi').AsInteger=8 then
      begin
        FCJ8:=Trim(FieldByName('MLName').AsString);
      end;
      next;
    end;
  end;
  DPS:=0;
  FFCYID:='';
  i:=1;
  if Trim(ComboBox1.Text)='' then
  begin
    j:=1;
  end else
  begin
    j:=StrToInt(ComboBox1.Text);
  end;
  for i:=1 to j do
  begin
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('SSel').AsBoolean=True then
        begin
          try
            Moudle:=LoadLibrary('MakeQRBarcode.dll');
            @Makebar:=GetProcAddress(Moudle,'Make');
            @Mixtext:=GetProcAddress(Moudle,'MixText');
            Txt:=Trim(Order_Sub.fieldbyname('CLRID').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;
            RMVariables['CYMF']:=Trim(CDS_Sub.fieldbyname('CYMF').AsString);
            RMVariables['CYKZ']:=Trim(CDS_Sub.fieldbyname('CYKZ').AsString);
            RMVariables['CYEName']:=Trim(CDS_Sub.fieldbyname('CYEName').AsString);
            RMVariables['CYCF']:=Trim(CDS_Sub.fieldbyname('CYCF').AsString);
            RMVariables['XTNo']:=Trim(CDS_Sub.fieldbyname('XTNo').AsString);
            RMVariables['CYECF']:=Trim(CDS_Sub.fieldbyname('CYECF').AsString);

            RMVariables['FCJ1']:=Trim(FCJ1);
            RMVariables['FCJ2']:=Trim(FCJ2);
            RMVariables['FCJ3']:=Trim(FCJ3);
            RMVariables['FCJ4']:=Trim(FCJ4);
            RMVariables['FCJ5']:=Trim(FCJ5);
            RMVariables['FCJ6']:=Trim(FCJ6);
            RMVariables['FCJ7']:=Trim(FCJ7);
            RMVariables['FCJ8']:=Trim(FCJ8);
            RMVariables['FHX']:=Trim(FHX);
            RMVariables['FHXName']:=Trim(FHXName);
            RMVariables['FCode']:=Trim(FGHNo);
            RMVariables['FName']:=Trim(FGHNo);
            RM1.LoadFromFile(fPrintFile);
            RM1.PrintReport;
          end else
          begin
            Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\子颜色标签.rmf'),'提示',0);
            Exit;
          end;
        end;
        Next;
      end;
    end;
    Order_Sub.EnableControls;
  end;
end;

procedure TfrmCPAdd.ColorNameChange(Sender: TObject);
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from CP_YDang_Color ');
    sql.Add('where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    if ColorName.Text<>'' then
    sql.Add(' and ColorName like '''+'%'+trim(ColorName.Text)+'%'+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
end;

procedure TfrmCPAdd.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Sub,true);
end;

procedure TfrmCPAdd.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Sub,false);
end;

procedure TfrmCPAdd.TYSXBQClick(Sender: TObject);
var
  fPrintFile,FFCYID,FFCLRID:string;
  DPS,i,j:Integer;
  Txt,fImagePath,FCJ1,FCJ2,FCJ3,FCJ4,FCJ5,FCJ6,FCJ7,FCJ8,FHX,FHXName,FGHNo:string;
  Moudle: THandle;
  Makebar:TMakebar;
  Mixtext:TMixtext;
begin
  if Order_Sub.IsEmpty then Exit;
  if Order_Sub.Locate('Ssel',True,[])=false then
  begin
    Application.MessageBox('没有选择数据','提示');
    exit;
  end;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select GHNo from OA_YG_DangAn where YGName='''+trim(DName)+''' ');
    open;
  end;
  FGHNo:=trim(ADOQueryCmd.fieldbyname('GHNo').asstring);
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\颜色小标签.rmf';
  DPS:=0;
  FFCYID:='';
  i:=1;
  if Trim(ComboBox1.Text)='' then
  begin
    j:=1;
  end else
  begin
    j:=StrToInt(ComboBox1.Text);
  end;
  for i:=1 to j do
  begin
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('SSel').AsBoolean=True then
        begin
          try
            Moudle:=LoadLibrary('MakeQRBarcode.dll');
            @Makebar:=GetProcAddress(Moudle,'Make');
            @Mixtext:=GetProcAddress(Moudle,'MixText');
            Txt:=Trim(Order_Sub.fieldbyname('CLRID').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;
            RMVariables['CYMF']:=Trim(CDS_Sub.fieldbyname('CYMF').AsString);
            RMVariables['CYKZ']:=Trim(CDS_Sub.fieldbyname('CYKZ').AsString);
            RMVariables['CYEName']:=Trim(CDS_Sub.fieldbyname('CYEName').AsString);
            RMVariables['CYCF']:=Trim(CDS_Sub.fieldbyname('CYCF').AsString);
            RMVariables['XTNo']:=Trim(CDS_Sub.fieldbyname('XTNo').AsString);
            RMVariables['CYECF']:=Trim(CDS_Sub.fieldbyname('CYECF').AsString);
            RMVariables['FHX']:=Trim(FHX);
            RMVariables['FHXName']:=Trim(FHXName);
            RMVariables['FCode']:=Trim(FGHNo);
            RMVariables['FName']:=Trim(FGHNo);
            RM1.LoadFromFile(fPrintFile);
            RM1.PrintReport;
          end else
          begin
            Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\颜色小标签.rmf'),'提示',0);
            Exit;
          end;
        end;
        Next;
      end;
    end;
    Order_Sub.EnableControls;
  end;
end;

procedure TfrmCPAdd.Tv4FocusedRecordChanged(Sender: TcxCustomGridTableView;
  APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
var
  i,j:integer;
  jpg:TJpegImage;
  myStream: TADOBlobStream;
begin
  IF Order_Sub.IsEmpty then exit;
  if Order_Sub.FieldByName('CLRID').AsString='' 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 B.CYID='+quotedstr(trim(Order_Sub.fieldbyname('CLRID').AsString)));
      //ShowMessage(SQL.Text);
      open;
    end;
    j:=length(Mach);
    if j>0 then
    begin
      for i:=0 to j-1 do
      begin
        Mach[i].free;
      end;
    end;
    SetLength(Mach, 0);
    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 TfrmCPAdd.ToolButton3Click(Sender: TObject);
var
  fHandle:THandle;
  FInt:Integer;
  FFName,FPath:String;
begin
  ToolBar1.SetFocus;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from XD_File where CYID='''+Trim(Order_sub.fieldbyname('CLRID').AsString)+'''');
    Open;
    if IsEmpty then
    begin
      Application.MessageBox('样品图片未上传！','提示',0);
      Exit;
    end;
  end;
  try
    ReadINIFile();
    server:=ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
    if Length(server)<6 then
    begin
      server:='127.0.0.1';
    end;
    IdFTP1.Host :=server;//PicSvr;
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;
  FPath:='D:\Right1209\';
  if not DirectoryExists(ExtractFileDir(FPath)) then
    CreateDir(ExtractFileDir(FPath));
  FFName:=Trim(ADOQueryTemp.fieldbyname('FileName').AsString);
  FFName:=FPath+FFName;
  if FileExists(FFName) then
  begin
    FInt:=1;
  end;
  if FInt<>1 then
    IdFTP1.Get(UserDataFlag+'YP\'+Trim(ADOQueryTemp.fieldbyname('FileName').AsString),
                   FPath+Trim(ADOQueryTemp.fieldbyname('FileName').AsString)
                    );
  if IdFTP1.Connected then IdFTP1.Quit;
  ShellExecute(Handle, 'open',PChar(FPath+Trim(ADOQueryTemp.fieldbyname('FileName').AsString)),'', '', SW_SHOWNORMAL);
end;

procedure TfrmCPAdd.FormDestroy(Sender: TObject);
var j,i: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);
  frmCPAdd:=nil;
end;

end.
