unit U_CPAdd_ZLR;

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, RM_Dataset, RM_System, RM_Common, RM_Class,
  RM_GridReport, cxCheckBox, Menus, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdFTP, RM_BarCode, ShellAPI, IniFiles, jpeg,
  U_SLT, Math, cxCalendar, cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore,
  dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee,
  dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue, dxSkinscxPCPainter, cxNavigator;

type
  TfrmCPAdd_ZLR = 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;
    cxGrid1Level1: TcxGridLevel;
    CDS_Sub: TClientDataSet;
    ADOQueryMain: TADOQuery;
    DataSource2: TDataSource;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1DefStr2: TcxGridDBColumn;
    v1DefStr6: TcxGridDBColumn;
    v1DefStr10: TcxGridDBColumn;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    v1CPName: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    cxGridPopupMenu4: TcxGridPopupMenu;
    Order_ML: TClientDataSet;
    DataSource3: TDataSource;
    Order_JG: TClientDataSet;
    DataSource4: TDataSource;
    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;
    Ord_Si: TClientDataSet;
    DataSource9: TDataSource;
    cxGridPopupMenu8: TcxGridPopupMenu;
    Ord_JGSi: TClientDataSet;
    DataSource11: TDataSource;
    cxGridPopupMenu10: TcxGridPopupMenu;
    ClientDataSet1: TClientDataSet;
    DataSource12: TDataSource;
    cxGridPopupMenu11: TcxGridPopupMenu;
    ClientDataSet2: TClientDataSet;
    DataSource13: TDataSource;
    cxGridPopupMenu12: TcxGridPopupMenu;
    cxGridPopupMenu13: TcxGridPopupMenu;
    DataSource14: TDataSource;
    ClientDataSet3: TClientDataSet;
    cxGridPopupMenu14: TcxGridPopupMenu;
    DataSource15: TDataSource;
    ClientDataSet4: TClientDataSet;
    CJ_Mianliao: TClientDataSet;
    DataSource16: TDataSource;
    cxGridPopupMenu15: TcxGridPopupMenu;
    v1COldCYNo: TcxGridDBColumn;
    cxSplitter4: TcxSplitter;
    Panel2: TPanel;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    v4CYNo: TcxGridDBColumn;
    v4ColorName: TcxGridDBColumn;
    v4ColorNo: TcxGridDBColumn;
    v4Note: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    ToolBar3: TToolBar;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Panel1: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2CengJi: TcxGridDBColumn;
    v2IFSel: TcxGridDBColumn;
    v2Column1: 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;
    v2LXRen: TcxGridDBColumn;
    v2LXTel: TcxGridDBColumn;
    v2MLCF: TcxGridDBColumn;
    v2MLNote: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar2: TToolBar;
    TMXSave: TToolButton;
    TAdd: TToolButton;
    TDel: TToolButton;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    v3IFZY: TcxGridDBColumn;
    v3JGCode: TcxGridDBColumn;
    v3JGCengJi: TcxGridDBColumn;
    v3JGFactoryName: TcxGridDBColumn;
    v3GYName: TcxGridDBColumn;
    v3GYDM: TcxGridDBColumn;
    v3MLSeHao: TcxGridDBColumn;
    v3HXNo: TcxGridDBColumn;
    v3MLKZ: TcxGridDBColumn;
    v3MLMF: TcxGridDBColumn;
    v3SZSpec: TcxGridDBColumn;
    v3JWMiDu: TcxGridDBColumn;
    v3MLColor: TcxGridDBColumn;
    v3Price: TcxGridDBColumn;
    v3MLSunHao: TcxGridDBColumn;
    v3JGPrice: TcxGridDBColumn;
    v3XJPrice: TcxGridDBColumn;
    v3JGNote: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar4: TToolBar;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    cxSplitter3: TcxSplitter;
    cxSplitter1: TcxSplitter;
    Panel3: TPanel;
    ToolBar5: TToolBar;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    cxGrid6: TcxGrid;
    TV6: TcxGridDBTableView;
    cxGridDBGYSName: TcxGridDBColumn;
    GYSColorNo: TcxGridDBColumn;
    cxGridDBGYSNote: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    Order_GYS: TClientDataSet;
    DataSource7: TDataSource;
    cxGridPopupMenu6: TcxGridPopupMenu;
    v4ColorEName: TcxGridDBColumn;
    cxSplitter2: TcxSplitter;
    TSave: TToolButton;
    ADOQuery4: TADOQuery;
    ADOQuery6: TADOQuery;
    v1JCCYNo: TcxGridDBColumn;
    ToolButton9: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    v4Ssel: TcxGridDBColumn;
    v4CLRID: TcxGridDBColumn;
    V6GSSeHao: TcxGridDBColumn;
    V6Note1: TcxGridDBColumn;
    v3HXName: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel4: TPanel;
    Label1: TLabel;
    ComboBox1: TComboBox;
    v1GYMXNote: TcxGridDBColumn;
    v1PrtDM: TcxGridDBColumn;
    Panel5: TPanel;
    Label2: TLabel;
    PrtMaxNo: TEdit;
    v1XTNo: TcxGridDBColumn;
    v2MLCYNo: TcxGridDBColumn;
    ToolButton10: TToolButton;
    ToolButton11: TToolButton;
    Label7: TLabel;
    ColorName: TEdit;
    TTPSC: TToolButton;
    ToolButton12: TToolButton;
    SaveDialog1: TSaveDialog;
    ODPat: TOpenDialog;
    RMBarCodeObject1: TRMBarCodeObject;
    IdFTP1: TIdFTP;
    v4TuPian: TcxGridDBColumn;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    adoqueryPicture: TADOQuery;
    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 v1CPNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    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 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 v2MLPricePropertiesEditValueChanged(Sender: TObject);
    procedure v3MLSunHaoPropertiesEditValueChanged(Sender: TObject);
    procedure v3GYDMPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2MLCFPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v4ColorNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TDelClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TMXSaveClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure v2MLNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2MLMFPropertiesEditValueChanged(Sender: TObject);
    procedure v3GYNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxGridDBColumn16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TSaveClick(Sender: TObject);
    procedure v3HXNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TAddClick(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure Tv4CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv4CanFocusRecord(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; var AAllow: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton11Click(Sender: TObject);
    procedure ColorNameChange(Sender: TObject);
    procedure TTPSCClick(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure Tv4FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    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();
    procedure MXGYSSave();
    procedure ReadINIFile();
    { Private declarations }
  public
    FCYID, FCYCode, FCPID, FCPNO, FCPName: string;
    CopyInt: Integer;

    { Public declarations }
  end;

var
  frmCPAdd_ZLR: TfrmCPAdd_ZLR;
  Mach: array of TfrmSlt;

implementation

uses
  U_DataLink, U_Fun, 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_ZLR.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;

function TfrmCPAdd_ZLR.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 := ADOQuery2.fieldbyname('IFZY').AsString;
          FieldByName('JGCode').Value := ADOQuery2.fieldbyname('JGCode').AsString;
          FieldByName('JGCengJi').Value := ADOQuery2.fieldbyname('JGCengJi').AsFloat;
          FieldByName('JGGX').Value := ADOQuery2.fieldbyname('JGGX').AsString;
          FieldByName('GXDM').Value := ADOQuery2.fieldbyname('GXDM').AsString;
          FieldByName('JGFactoryName').Value := ADOQuery2.fieldbyname('JGFactoryName').AsString;
          FieldByName('GYName').Value := ADOQuery2.fieldbyname('GYName').AsString;
          FieldByName('GYDM').Value := ADOQuery2.fieldbyname('GYDM').AsString;
          FieldByName('HXNo').Value := ADOQuery2.fieldbyname('HXNo').AsString;
          FieldByName('MLKZ').Value := ADOQuery2.fieldbyname('MLKZ').AsFloat;
          FieldByName('MLMF').Value := ADOQuery2.fieldbyname('MLMF').AsFloat;
          FieldByName('SZSpec').Value := ADOQuery2.fieldbyname('SZSpec').AsString;
          FieldByName('JWMiDu').Value := ADOQuery2.fieldbyname('JWMiDu').AsString;
          FieldByName('MLColor').Value := ADOQuery2.fieldbyname('MLColor').AsString;
          FieldByName('MLSeHao').Value := ADOQuery2.fieldbyname('MLSeHao').AsString;
          FieldByName('JGNote').Value := ADOQuery2.fieldbyname('JGNote').AsString;
          FieldByName('Price').Value := ADOQuery2.fieldbyname('Price').AsFloat;
          FieldByName('MLSunHao').Value := ADOQuery2.fieldbyname('MLSunHao').AsFloat;
          FieldByName('JGPrice').Value := ADOQuery2.fieldbyname('JGPrice').AsFloat;
          FieldByName('XJPrice').Value := ADOQuery2.fieldbyname('XJPrice').AsFloat;
          Post;
        end;
        next;
      end;
    end;
  except
    ;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

function TfrmCPAdd_ZLR.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);
          //SSetSaveDataCDSNew(ADOQuery2,Tv2,Order_ML,'CP_YDang_ML',0);
          FieldByName('CYID').Value := Trim(FMCYID);
          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 := ADOQueryCmd.fieldbyname('CengJi').AsFloat;
          FieldByName('IFSel').Value := ADOQueryCmd.fieldbyname('IFSel').AsString;
          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;
        {with Order_ML do
        begin
          edit;
          FieldByName('CYID').Value:=Trim(FCYID);
          FieldByName('MLID').Value:=Trim(maxid);
        end;}
        next;
      end;
    end;
  except
    ;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmCPAdd_ZLR.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, 'CL', '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);
          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_ZLR.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('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 := 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_ZLR.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('MLSunHao').Value := Order_JG.fieldbyname('MLSunHao').AsFloat;
          FieldByName('JGPrice').Value := Order_JG.fieldbyname('JGPrice').AsFloat;
          FieldByName('CengJi').Value := 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_ZLR.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_ZLR.InitSubGrid();
begin
  try
    ADOQueryMain.DisableControls;
    ADOQuery1.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select Max(substring(A.JCCYNo,2,5)) PrtMaxNo from CP_YDang A ');
      SQL.Add('where A.CYType=''CP1903260008'' or A.CYType=''CP1905280002'' or A.CYType=''CP1905280001'''); //not exists (select B.CYID from CP_YDang B where B.CYID=A.CYType)
      Open;
    end;
    PrtMaxNo.Text := Trim(ADOQueryMain.fieldbyname('PrtMaxNo').AsString);
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select *,cast('''' as varchar(50)) rownumber from CP_YDang A inner 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 ADOQuery1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('select * from CP_YDang_Color where CYID=''' + trim(FCYID) + '''');
        Open;
      end;
    end
    else
    begin
      with ADOQuery1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('select A.*,B.* ');
        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 ');
        SQL.Add('inner join CP_YDang B on A.CYID=B.CYID where A.CYID=''' + trim(FCYID) + '''');
        Open;
      end;
    end;
    SCreateCDS20(ADOQuery1, Order_Sub);
    SInitCDSData20(ADOQuery1, Order_Sub);
    with ADOQuery4 do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CP_YDang_Color_GYS ');
      sql.Add('where CLRID=''' + trim(Order_Sub.fieldbyname('CLRID').AsString) + '''');
      open;
    end;
    SCreateCDS20(ADOQuery4, Order_GYS);
    SInitCDSData20(ADOQuery4, Order_GYS);
  finally
    ADOQueryMain.EnableControls;
    ADOQuery1.EnableControls;
  end;
end;

procedure TfrmCPAdd_ZLR.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;

procedure TfrmCPAdd_ZLR.FormShow(Sender: TObject);
var
  fsj, FFSj, FFQZSj, FFFSj, FFType: string;
  FFInt, i, j: Integer;
  ii: LongInt;
begin
  ReadCxGrid('样品编辑N', Tv1, '样品管理');
  ReadCxGrid('样品编辑N2', Tv2, '样品管理');
  ReadCxGrid('样品编辑N3', Tv3, '样品管理');
  ReadCxGrid('样品编辑N4', Tv4, '样品管理');
  ReadCxGrid('样品编辑N6', Tv6, '样品管理');
  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=''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=''1''');
    open;
  end;
  SCreateCDS20(ADOQuery2, Order_JG);
  SInitCDSData20(ADOQuery2, Order_JG);
  if CopyInt = 1 then
  begin
    FCYID := '';
    CDS_Sub.edit;
    CDS_Sub.FieldByName('CYID').Value := '';
    CDS_Sub.FieldByName('CYNo').Value := '';
    CDS_Sub.FieldByName('XTNo').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_ZLR.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('样品编辑N', Tv1, '样品管理');
  WriteCxGrid('样品编辑N2', Tv2, '样品管理');
  WriteCxGrid('样品编辑N3', Tv3, '样品管理');
  WriteCxGrid('样品编辑N4', Tv4, '样品管理');
  WriteCxGrid('样品编辑N6', Tv6, '样品管理');
  Close;
end;

function TfrmCPAdd_ZLR.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('CYID').Value := Trim(maxId);
      FieldByName('XTNo').Value := Trim(XTMaxID);
      FieldByName('PrtMaxNo').Value := PrtMaxNo.Text;
      if Trim(CDS_Sub.fieldbyname('CYType').AsString) <> '' then
        FieldByName('CYType').Value := Trim(CDS_Sub.fieldbyname('CYType').AsString)
      else
        FieldByName('CYType').Value := Trim(FCPID);
      if Trim(FCYID) = '' then
        FieldByName('DefFlt4').Value := Fint;
      SSetSaveDataCDSNew(ADOQueryCmd, Tv1, CDS_Sub, 'CP_YDang', 3);
      //SSetSaveDataCDSNew(ADOQueryCmd,Tv6,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;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from CP_YDang where CYNo=''' + Trim(CDS_Sub.fieldbyname('CYNo').AsString) + '''');
      Open;
      if FieldByName('AA').AsInteger > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result := False;
        Application.MessageBox('编号重复！', '提示', 0);
        Exit;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from CP_YDang where XTNo=''' + Trim(CDS_Sub.fieldbyname('XTNo').AsString) + '''');
      Open;
      if FieldByName('AA').AsInteger > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result := False;
        Application.MessageBox('公司编号重复！', '提示', 0);
        Exit;
      end;
    end;
    {if CopyInt=1 then
    begin
      //保存面料信息
      ColMLSavedata(Maxid);
      //保存加工信息
      ColJGSaveData(Maxid);
    end; }
    with CDS_Sub do
    begin
      edit;
      FieldByName('CYID').Value := Trim(maxId);
    end;
    FCYID := Trim(maxId);
    COLSaveData();
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ;
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmCPAdd_ZLR.ToolButton1Click(Sender: TObject);
var
  FCYNo, FGYMXNote: string;
begin
  ToolBar1.SetFocus;
  if CDS_Sub.IsEmpty then
    exit;
  if CDS_Sub.Locate('JCCYNo', null, []) = True then
  begin
    Application.MessageBox('基础编号不能为空！', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('PrtDM', null, []) = True then
  begin
    Application.MessageBox('产品代码不能为空！', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CYName', null, []) = True then
  begin
    Application.MessageBox('公司产品称呼不能为空！', '提示', 0);
    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;
  MXGYSSave();
  with Order_JG do
  begin
    First;
    FCYNo := Trim(CDS_Sub.fieldbyname('JCCYNo').AsString);
    while not Eof do
    begin
      FCYNo := FCYNo + Trim(Order_JG.fieldbyname('GYDM').AsString) + Trim(Order_JG.fieldbyname('MLSeHao').AsString) + Trim(Order_JG.fieldbyname('HXNo').AsString);
      if FGYMXNote = '' then
        FGYMXNote := Trim(Order_JG.fieldbyname('GYName').AsString)
      else
        FGYMXNote := FGYMXNote + '+' + Trim(Order_JG.fieldbyname('GYName').AsString);
      next;
    end;
  end;
  with CDS_Sub do
  begin
    edit;
    FieldByName('CYNo').Value := Trim(FCYNo);
    FieldByName('GYMXNote').Value := Trim(FGYMXNote);
  end;
  with Order_Sub do
  begin
    first;
    while not eof do
    begin
      FCYNo := Trim(CDS_Sub.fieldbyname('JCCYNo').AsString);
      with Order_JG do
      begin
        First;
        while not eof do
        begin
          FCYNo := FCYNo + Trim(Order_JG.fieldbyname('GYDM').AsString) + Trim(Order_JG.fieldbyname('MLSeHao').AsString) + Trim(Order_JG.fieldbyname('HXNo').AsString);
          next;
        end;
      end;
      FCYNo := FCYNo + '#' + Trim(Order_Sub.fieldbyname('ColorNo').AsString);
      edit;
      FieldByName('CYNo').Value := Trim(FCYNo);
      next;
    end;
  end;
  {if CopyInt=1 then
  begin
    if SaveData() then
    begin
      Application.MessageBox('保存成功！','提示',0);
      ModalResult:=1;
    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('JGGX', '', []) = False then
      begin
        JGSavedata(Order_JG, Order_ML);
      end;
    end;
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
  //end;
end;

procedure TfrmCPAdd_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.ToolButton2Click(Sender: TObject);
var
  SH: string;
begin
  if Order_Sub.IsEmpty then
    exit;
  if Order_Sub.FieldByName('CLRID').AsString = '' then
  begin
    SH := Trim(Order_Sub.fieldbyname('ColorNo').AsString);
    if CopyInt = 1 then
    begin
      if SaveData() then
      begin
        //Application.MessageBox('保存成功！','提示',0);
        //ModalResult:=1;
      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('JGGX', '', []) = False then
          begin
            JGSavedata(Order_JG, Order_ML);
          end;
        end;
        //Application.MessageBox('保存成功！','提示',0);
        //ModalResult:=1;
      end;
    end;
    Order_Sub.Locate('ColorNo', SH, []);
  end;
  with Order_GYS do
  begin
    append;
    FieldByName('GSSeHao').Value := Trim(Order_Sub.fieldbyname('ColorNo').AsString);
    post;
  end;
end;

procedure TfrmCPAdd_ZLR.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_ZLR.ToolButton4Click(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_ZLR.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_ZLR.ToolButton9Click(Sender: TObject);
var
  fPrintFile, FFCYID, FFCLRID: string;
  DPS, i, j: Integer;
  Txt, fImagePath, 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['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_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.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_ZLR.v2MLPricePropertiesEditValueChanged(Sender: TObject);
var
  mavlue, FFieldName: string;
  FXJ: double;
begin

end;

procedure TfrmCPAdd_ZLR.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_ZLR.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_ZLR.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_ZLR.v4ColorNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp := TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag := 'ColorName';
    flagname := '颜色';
    fnote := true;
    V1Note.Caption := '颜色英文';
    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);
        FieldByName('ColorEName').Value := Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd_ZLR.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_ZLR.ToolButton3Click(Sender: TObject);
begin
  if Order_GYS.IsEmpty then
    Exit;
  if Trim(Order_GYS.fieldbyname('GYSID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CP_YDang_Color_GYS where GYSID=''' + Trim(Order_GYS.fieldbyname('GYSID').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_GYS.Delete;
end;

procedure TfrmCPAdd_ZLR.TMXSaveClick(Sender: TObject);
begin
  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_ZLR.ToolButton6Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Order_JG.IsEmpty then
    exit;
  if Order_JG.Locate('JGGX', '', []) = false then
    JGSavedata(Order_JG, Order_ML);
end;

procedure TfrmCPAdd_ZLR.v2MLNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmCPManage_YPCXSel := TfrmCPManage_YPCXSel.create(self);
  with frmCPManage_YPCXSel do
  begin
    if showmodal = 1 then
    begin
      with 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('MLCode').Value := Trim(CDS_Main.fieldbyname('XTNo').AsString);
        FieldByName('JWMiDu').Value := Trim(CDS_Main.fieldbyname('DefStr6').AsString);
        FieldByName('MLCYNo').Value := Trim(CDS_Main.fieldbyname('CYNo').AsString);
        if CDS_Main.fieldbyname('CYMF').AsString = '' then
          FieldByName('MLMF').AsFloat := 0
        else
          FieldByName('MLMF').Value := Trim(CDS_Main.fieldbyname('CYMF').AsString);
        if CDS_Main.fieldbyname('CYKZ').AsString = '' then
          FieldByName('MLKZ').AsFloat := 0
        else
          FieldByName('MLKZ').Value := Trim(CDS_Main.fieldbyname('CYKZ').AsString);
        FieldByName('MLCF').Value := Trim(CDS_Main.fieldbyname('CYCF').AsString);
        //post;
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd_ZLR.v2MLMFPropertiesEditValueChanged(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_ZLR.v3GYNamePropertiesButtonClick(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_ZLR.cxGridDBColumn16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmZdyAttachGYS_CX := TfrmZdyAttachGYS_CX.Create(self);
  with frmZdyAttachGYS_CX do
  begin
    if ShowModal = 1 then
    begin
      with Order_GYS do
      begin
        edit;
        FieldByName('GYSName').Value := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmCPAdd_ZLR.MXGYSSave();
var
  maxid: string;
begin
  with Order_GYS do
  begin
    first;
    while not Eof do
    begin
      if FieldByName('GYSID').AsString = '' then
      begin
        if GetLSNo(ADOQuery6, maxid, 'GS', 'CP_YDang_Color_GYS', 4, 1) = False then
        begin
          Application.MessageBox('取最大号失败！', '提示', 0);
          Exit;
        end;
      end
      else
        maxid := Trim(FieldByName('GYSID').AsString);
      with ADOQuery6 do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from CP_YDang_Color_GYS where GYSID=''' + Trim(maxid) + '''');
        Open;
      end;
      with ADOQuery6 do
      begin
        if IsEmpty then
          Append
        else
          Edit;
        FieldByName('GYSID').Value := Trim(maxid);
        FieldByName('CLRID').Value := Trim(Order_Sub.Fieldbyname('CLRID').AsString);
        FieldByName('CYID').Value := Trim(CDS_Sub.Fieldbyname('CYID').AsString);
        SSetSaveDataCDSNew(ADOQuery6, Tv6, Order_GYS, 'CP_YDang_Color_GYS', 0);
        Post;
      end;
      with Order_GYS do
      begin
        edit;
        FieldByName('GYSID').Value := Trim(maxid);
        FieldByName('CYID').Value := Trim(CDS_Sub.Fieldbyname('CYID').AsString);
        FieldByName('CLRID').value := Trim(Order_Sub.Fieldbyname('CLRID').AsString);
      end;
      next;
    end;
  end;
end;

procedure TfrmCPAdd_ZLR.TSaveClick(Sender: TObject);
var
  maxid: string;
begin
  if Order_GYS.IsEmpty then
    exit;
  MXGYSSave();
  Application.MessageBox('保存成功', '提示');
end;

procedure TfrmCPAdd_ZLR.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_ZLR.TAddClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    exit;
  with Order_ML do
  begin
    Append;
    //FieldByName('JGCengJi').Value:=Order_JG.RecordCount+1;
    Post;
  end;
end;

procedure TfrmCPAdd_ZLR.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_ZLR.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_ZLR.Tv4CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if Order_Sub.IsEmpty then
    exit;
  with ADOQuery4 do
  begin
    close;
    sql.Clear;
    sql.Add('select * from CP_YDang_Color_GYS ');
    sql.Add('where CLRID=''' + trim(Order_Sub.fieldbyname('CLRID').AsString) + '''');
    open;
  end;
  SCreateCDS20(ADOQuery4, Order_GYS);
  SInitCDSData20(ADOQuery4, Order_GYS);
end;

procedure TfrmCPAdd_ZLR.Tv4CanFocusRecord(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; var AAllow: Boolean);
begin
  if Order_GYS.IsEmpty = false then
  begin
    if Order_GYS.Locate('GYSID', '', []) = true then
    begin
      MXGYSSave();
    end;
    if Order_GYS.Locate('GYSID', NULL, []) = true then
    begin
      MXGYSSave();
    end;
  end;
end;

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

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

procedure TfrmCPAdd_ZLR.ToolButton10Click(Sender: TObject);
var
  fPrintFile, FFCYID, FFCLRID: string;
  DPS, i, j: Integer;
  Txt, fImagePath, 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['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_ZLR.ToolButton11Click(Sender: TObject);
var
  fPrintFile, FFCYID, FFCLRID: string;
  DPS, i, j: Integer;
  Txt, fImagePath, 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['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_ZLR.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_ZLR.TTPSCClick(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_ZLR.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 TfrmCPAdd_ZLR.ToolButton12Click(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_ZLR.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_ZLR.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_ZLR := nil;
end;

end.

