unit U_CPAdd;

interface

uses                             
  Windows, Messages, 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,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore,
  dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee,
  dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans,
  dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky,
  dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, dxSkinscxPCPainter;

type
  TfrmCPAdd = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    CDS_Sub: TClientDataSet;
    ADOQueryMain: TADOQuery;
    DataSource2: TDataSource;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column41: TcxGridDBColumn;
    Panel1: TPanel;
    CheckBox1: TCheckBox;
    v1Column12: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column30: TcxGridDBColumn;
    v1Column31: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    v1Column33: TcxGridDBColumn;
    v1Column34: TcxGridDBColumn;
    v1Column35: TcxGridDBColumn;
    v1Column36: TcxGridDBColumn;
    v1Column37: TcxGridDBColumn;
    v1Column38: TcxGridDBColumn;
    v1Column39: TcxGridDBColumn;
    v1Column40: TcxGridDBColumn;
    v1Column42: TcxGridDBColumn;
    v1Column43: TcxGridDBColumn;
    v1Column44: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure v1Column7PropertiesEditValueChanged(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 v1Column20PropertiesEditValueChanged(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure v1Column31PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column30PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    canshu1: string;
    Fint: Integer;
    procedure InitSubGrid();
    function SaveData(): Boolean;
    { Private declarations }
  public
    FCYID, FCYCode, FCPID, FCPNO: string;
    CopyInt: Integer;

    { Public declarations }
  end;

var
  frmCPAdd: TfrmCPAdd;

implementation

uses
  U_DataLink, U_Fun, U_iniParam, U_ZDYHelp;

{$R *.dfm}

procedure TfrmCPAdd.InitSubGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from CP_YDang where CYID=''' + Trim(FCYID) + '''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Sub);
    SInitCDSData20(ADOQueryMain, CDS_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
  //canshu1:=Trim(DParameters1);
  ReadCxGrid('样品编辑YT1', Tv1, '样品管理');
  InitSubGrid();
  if CopyInt = 1 then
  begin
    FCYID := '';
  end;
{  if Trim(YPCodeType)<>'' then
  begin
    if Trim(FCYID)='' then
    begin
      //取前缀
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select CYNO AA from CP_YDang ');
          SQL.Add('where Cast(CYID as int)=(select isnull(Max(Cast(CYID as int)),0) from CP_YDang where CYType='''+Trim(FCPID)+''')');
          Open;
        end;
        if Trim(ADOQueryTemp.fieldbyname('AA').asstring)='' then
        begin
          FFQZSj:=Trim(FCPNO);
        end else
        begin
          fsj:=Trim(ADOQueryTemp.fieldbyname('AA').asstring);
          FFInt:=0;
          i:=0;
          FFFSj:=Trim(Copy(fsj,Length(FCPNO)+1,Length(fsj)));
          while FFInt<=0 do
          begin
            FFSj:=Trim(Copy(fsj,Length(FCPNO)+1+i,Length(fsj)));
            if TryStrToInt(FFSj,ii) then
            begin
              if StrToInt(FFSj)>=0 then
              begin
                i:=i+1;
              end else
              begin
                FFInt:=1;
                FFQZSj:=Trim(Copy(fsj,1,Length(FCPNO)+i+1));//前缀字符串
              end;
              if i=Length(FFFSj) then
              begin
                FFInt:=1;
                FFQZSj:=Trim(FCPNO);
              end;
            end else
            begin
              FFInt:=1;
              FFQZSj:=Trim(Copy(fsj,1,Length(FCPNO)+i+1));//前缀字符串
            end;
          end;
        end;
      //取前缀
      //取后缀
      if Trim(YPCodeType)='大类' then
      begin
        i:=99;
        while i>=1 do
        begin
          if i=99 then
          begin
            FFType:=Trim(FCPID);
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CP_Type where CPID='''+Trim(FFType)+'''');
            Open;
            i:=fieldbyname('CPLevel').AsInteger;
            if i>1 then
              FFType:=Trim(fieldbyname('CPParent').AsString)
            else
            if i=1 then
            begin
              FFType:=Trim(fieldbyname('CPID').AsString);
              i:=0;
            end;
          end;
        end;

        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('exec P_Select_CP_YDang_MaxNo :CPType');
          Parameters.ParamByName('CPType').Value:=Trim(FFType);
          Open;
        end;
      end else
      if Trim(YPCodeType)='小类' then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select CYNO AA from CP_YDang ');
          SQL.Add('where Cast(CYID as int)=(select isnull(Max(Cast(CYID as int)),0) from CP_YDang where CYType='''+Trim(FCPID)+''')');
          Open;
        end;
      end;
        if Trim(ADOQueryTemp.fieldbyname('AA').asstring)='' then
        begin
          FFFSj:='0001';
        end else
        begin
          fsj:=Trim(ADOQueryTemp.fieldbyname('AA').asstring);
          FFInt:=0;
          i:=0;
          FFFSj:='';
          fsj:=Trim( Copy( fsj,Length(FFQZSj)+1,Length(fsj) ) );
          while FFInt<=0 do
          begin
            FFSj:=Trim(Copy(fsj,(Length(fsj)-i),(Length(fsj))));
            if TryStrToInt(FFSj,ii) then
            begin
              if StrToInt(FFSj)>=0 then
              begin
                FFFSj:=Trim(FFSj);//后缀数字字符串
              end else
              begin
                FFInt:=1;
              end;
              i:=i+1;
              if i=Length(fsj) then
              begin
                FFInt:=1;
              end;
            end else
            begin
              FFInt:=1;
            end;
          end;
          i:=Length(FFFSj); //数字编号长度   3
          FFInt:=StrToInt(FFFSj)+1;//最大号
          j:=Length(Trim(IntToStr(FFInt))); //最大号位数 1
          FFFSj:=Trim(IntToStr(FFInt));//后缀数字字符串
          while j<i do
          begin
            FFFSj:='0'+Trim(FFFsj);
            j:=j+1
          end;
        end;
      //取后缀
      fsj:=Trim(FFQZSj)+Trim(FFFSj);
      if CopyInt=0 then
        CDS_Sub.Append
      else
      if CopyInt=1 then
        CDS_Sub.Edit;
      CDS_Sub.FieldByName('CYNo').Value:=Trim(fsj);
      CDS_Sub.Post;
    end;
  end else }
  begin
    if CDS_Sub.IsEmpty then
    begin
      CDS_Sub.Append;
      CDS_Sub.Post;
    end;
  end;

end;

procedure TfrmCPAdd.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('样品编辑YT1', Tv1, '样品管理');
  Close;
end;

function TfrmCPAdd.SaveData(): Boolean;
var
  maxId: string;
begin
//  try
//    ADOQueryCmd.Connection.BeginTrans;
//    CDS_Sub.DisableControls;
//    with CDS_Sub do
//    begin
//      First;
//      while not eof do
//      begin
//        if Trim(FCYID) = '' then
//        begin
//          if GetLSNo(ADOQueryCmd, maxId, '', 'CP_YDang', 3, 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;
//            FieldbyName('Filler').Value := trim(DName);
//          end
//          else
//          begin
//            Edit;
//            FieldbyName('editer').Value := trim(DName);
//            FieldbyName('editTime').Value := now();
//          end;
//          FieldByName('CYID').Value := Trim(maxId);
//          FieldByName('CYType').Value := Trim(FCPID);
//          SSetSaveDataCDSNew(ADOQueryCmd, Tv1, CDS_Sub, 'CP_YDang', 3);
//          if Trim(CDS_Sub.fieldbyname('CYPriceKg').AsString) = '' then
//            FieldByName('CYPriceKg').Value := 0
//          else
//            FieldByName('CYPriceKg').Value := CDS_Sub.fieldbyname('CYPriceKg').AsString;
//          if Trim(CDS_Sub.fieldbyname('CYPriceM').AsString) = '' then
//            FieldByName('CYPriceM').Value := 0
//          else
//            FieldByName('CYPriceM').Value := CDS_Sub.fieldbyname('CYPriceM').AsString;
//          FieldByName('Price1').Value := CDS_Sub.fieldbyname('Price1').AsFloat;
//          FieldByName('Price2').Value := CDS_Sub.fieldbyname('Price2').AsFloat;
//          FieldByName('Price3').Value := CDS_Sub.fieldbyname('Price3').AsFloat;
//          FieldByName('Price4').Value := CDS_Sub.fieldbyname('Price4').AsFloat;
//          FieldByName('Price5').Value := CDS_Sub.fieldbyname('Price5').AsFloat;
//          FieldByName('Price6').Value := CDS_Sub.fieldbyname('Price6').AsFloat;
//          FieldByName('Price7').Value := CDS_Sub.fieldbyname('Price7').AsFloat;
//          FieldByName('Price8').Value := CDS_Sub.fieldbyname('Price8').AsFloat;
//          Post;
//        end;
//        with ADOQueryTemp 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;
////        ADOQueryCmd.Connection.CommitTrans;
//        with CDS_Sub do
//        begin
//          Edit;
//          FieldByName('CYID').Value := Trim(maxId);
//          FieldByName('CYType').Value := Trim(FCPID);
//          Post;
//        end;
//        next;
//      end;
//    end;
//    CDS_Sub.EnableControls;
//    ADOQueryCmd.Connection.CommitTrans;
//    Result := True;
//  except
//    Result := False;
//    ADOQueryCmd.Connection.RollbackTrans;
//    Application.MessageBox('保存失败!', '提示', 0);
//  end;


  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CP_YDang where CYID=''' + Trim(FCYID) + '''');
          open;
        end;
        FCYID := Trim(ADOQueryTemp.fieldbyname('CYID').AsString);
        if Trim(FCYID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, maxId, '', 'CP_YDang', 3, 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;
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldbyName('editer').Value := trim(DName);
            FieldbyName('editTime').Value := now();
          end;
          FieldByName('CYID').Value := Trim(maxId);
          FieldByName('CYType').Value := Trim(FCPID);
          SSetSaveDataCDSNew(ADOQueryCmd, Tv1, CDS_Sub, 'CP_YDang', 3);
          if Trim(CDS_Sub.fieldbyname('CYPriceKg').AsString) = '' then
            FieldByName('CYPriceKg').Value := 0
          else
            FieldByName('CYPriceKg').Value := CDS_Sub.fieldbyname('CYPriceKg').AsString;
          if Trim(CDS_Sub.fieldbyname('CYPriceM').AsString) = '' then
            FieldByName('CYPriceM').Value := 0
          else
            FieldByName('CYPriceM').Value := CDS_Sub.fieldbyname('CYPriceM').AsString;
          FieldByName('Price1').Value := CDS_Sub.fieldbyname('Price1').AsFloat;
          FieldByName('Price2').Value := CDS_Sub.fieldbyname('Price2').AsFloat;
          FieldByName('Price3').Value := CDS_Sub.fieldbyname('Price3').AsFloat;
          FieldByName('Price4').Value := CDS_Sub.fieldbyname('Price4').AsFloat;
          FieldByName('Price5').Value := CDS_Sub.fieldbyname('Price5').AsFloat;
          FieldByName('Price6').Value := CDS_Sub.fieldbyname('Price6').AsFloat;
          FieldByName('Price7').Value := CDS_Sub.fieldbyname('Price7').AsFloat;
          FieldByName('Price8').Value := CDS_Sub.fieldbyname('Price8').AsFloat;
          Post;
        end;
        with ADOQueryTemp 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
             CDS_Sub.EnableControls; 
            ADOQueryCmd.Connection.RollbackTrans;
            Result := False;
            Application.MessageBox('编号重复！', '提示', 0);
            Exit;
          end;
        end;
        with CDS_Sub do
        begin
          Edit;
          FieldByName('CYID').Value := Trim(maxId);
          FieldByName('CYType').Value := Trim(FCPID);
          Post;
        end;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmCPAdd.ToolButton1Click(Sender: TObject);
begin
  if CDS_Sub.Locate('CYNo', null, []) = True then
  begin
    Application.MessageBox('产品编号不能为空！', '提示', 0);
    Exit;
  end;
  ToolBar1.SetFocus;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmCPAdd.v1Column7PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FieldName, PriceKg, MF, KZ: string;
  FReal: Double;
begin
  mvalue := Trim(TcxTextEdit(Sender).EditingText);
  with CDS_Sub do
  begin
    Edit;
    if TryStrToFloat(mvalue, FReal) = False then
      FieldByName('CYMF').Value := mvalue
    else
    begin
      if Trim(canshu1) = '英寸' then
        FieldByName('CYMF').Value := mvalue + '"'
      else
        FieldByName('CYMF').Value := mvalue + 'CM';
    end;
    Post;
  end;
  FieldName := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  PriceKg := Trim(CDS_Sub.fieldbyname('CYPriceKg').AsString);
  if Trim(PriceKg) = '' then
  begin
    PriceKg := '0';
  end;
  MF := Trim(CDS_Sub.fieldbyname('CYMF').AsString);
  KZ := Trim(CDS_Sub.fieldbyname('CYKZ').AsString);
  if Trim(canshu1) <> '英寸' then
    MF := Copy(MF, 1, Pos('C', MF) - 1)
  else
    MF := Copy(MF, 1, Pos('"', MF) - 1);
  KZ := Copy(KZ, 1, Pos('g', KZ) - 1);
  if TryStrToFloat(MF, FReal) = False then
  begin
    MF := '0';
  end;
  if TryStrToFloat(KZ, FReal) = False then
  begin
    KZ := '0';
  end;
  with CDS_Sub do
  begin
    Edit;
    if Trim(canshu1) <> '英寸' then
      FieldByName('CYPriceM').Value := StrToFloat(PriceKg) * StrToFloat(MF) * StrToFloat(KZ) * 1.00 / 100000
    else
      FieldByName('CYPriceM').Value := StrToFloat(PriceKg) * StrToFloat(MF) * 2.54 * StrToFloat(KZ) * 1.00 / 100000;
    Post;
  end;
end;

procedure TfrmCPAdd.v1Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FieldName, PriceKg, MF, KZ: string;
  FReal: Double;
begin
  mvalue := Trim(TcxTextEdit(Sender).EditingText);
  with CDS_Sub do
  begin
    Edit;
    if TryStrToFloat(mvalue, FReal) = False then
      FieldByName('CYKZ').Value := mvalue
    else
      FieldByName('CYKZ').Value := mvalue + 'g/㎡';
    Post;
  end;
  FieldName := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  PriceKg := Trim(CDS_Sub.fieldbyname('CYPriceKg').AsString);
  if Trim(PriceKg) = '' then
  begin
    PriceKg := '0';
  end;
  MF := Trim(CDS_Sub.fieldbyname('CYMF').AsString);
  KZ := Trim(CDS_Sub.fieldbyname('CYKZ').AsString);
  if Trim(canshu1) <> '英寸' then
    MF := Copy(MF, 1, Pos('C', MF) - 1)
  else
    MF := Copy(MF, 1, Pos('"', MF) - 1);
  KZ := Copy(KZ, 1, Pos('g', KZ) - 1);
  if TryStrToFloat(MF, FReal) = False then
  begin
    MF := '0';
  end;
  if TryStrToFloat(KZ, FReal) = False then
  begin
    KZ := '0';
  end;
  with CDS_Sub do
  begin
    Edit;
    if Trim(canshu1) <> '英寸' then
      FieldByName('CYPriceM').Value := StrToFloat(PriceKg) * StrToFloat(MF) * StrToFloat(KZ) * 1.00 / 100000
    else
      FieldByName('CYPriceM').Value := StrToFloat(PriceKg) * StrToFloat(MF) * 2.54 * StrToFloat(KZ) * 1.00 / 100000;
    Post;
  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.v1Column20PropertiesEditValueChanged(Sender: TObject);
var
  FieldName, mvalue, MF, KZ, PriceKg: string;
  FReal: Double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FieldName := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FieldName).Value := mvalue;
    Post;
  end;
  PriceKg := Trim(CDS_Sub.fieldbyname('CYPriceKg').AsString);
  if Trim(PriceKg) = '' then
  begin
    PriceKg := '0';
  end;
  MF := Trim(CDS_Sub.fieldbyname('CYMF').AsString);
  KZ := Trim(CDS_Sub.fieldbyname('CYKZ').AsString);
  if Trim(canshu1) <> '英寸' then
    MF := Copy(MF, 1, Pos('C', MF) - 1)
  else
    MF := Copy(MF, 1, Pos('"', MF) - 1);
  KZ := Copy(KZ, 1, Pos('g', KZ) - 1);
  if TryStrToFloat(MF, FReal) = False then
  begin
    MF := '0';
  end;
  if TryStrToFloat(KZ, FReal) = False then
  begin
    KZ := '0';
  end;
  with CDS_Sub do
  begin
    Edit;
    if Trim(canshu1) <> '英寸' then
      FieldByName('CYPriceM').Value := StrToFloat(PriceKg) * StrToFloat(MF) * StrToFloat(KZ) * 1.00 / 100000
    else
      FieldByName('CYPriceM').Value := StrToFloat(PriceKg) * StrToFloat(MF) * 2.54 * StrToFloat(KZ) * 1.00 / 100000;
    Post;
  end;

end;

procedure TfrmCPAdd.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked = True then
  begin
    canshu1 := '英寸';
  end
  else
  begin
    canshu1 := '';
  end;
end;

procedure TfrmCPAdd.v1Column31PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FieldName, PriceKg, MF, KZ: string;
  FReal: Double;
begin
  mvalue := Trim(TcxTextEdit(Sender).EditingText);
  with CDS_Sub do
  begin
    Edit;
    if TryStrToFloat(mvalue, FReal) = False then
      FieldByName('PBKZ').Value := mvalue
    else
      FieldByName('PBKZ').Value := mvalue + 'g/㎡';
    Post;
  end;

end;

procedure TfrmCPAdd.v1Column30PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FieldName, PriceKg, MF, KZ: string;
  FReal: Double;
begin
  mvalue := Trim(TcxTextEdit(Sender).EditingText);
  with CDS_Sub do
  begin
    Edit;
    if TryStrToFloat(mvalue, FReal) = False then
      FieldByName('PBMF').Value := mvalue
    else
    begin
      if Trim(canshu1) = '英寸' then
        FieldByName('PBMF').Value := mvalue + '"'
      else
        FieldByName('PBMF').Value := mvalue + 'CM';
    end;
    Post;
  end;

end;

procedure TfrmCPAdd.ToolButton2Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty = False then
  begin
    CopyAddRowCDS(CDS_Sub);
    with CDS_Sub do
    begin
      Edit;
      FieldByName('CYNo').Value := null;
      FieldByName('DefStr2').Value := '';
      FieldByName('CYHX').Value := '';
      FieldByName('defstr3').Value := '';
      FieldByName('defstr4').Value := '';
      FieldByName('defstr5').Value := '';
      FieldByName('defnote5').Value := '';
      FieldByName('defstr6').Value := '';
      FieldByName('CYName').Value := '';
      FieldByName('CYPriceKg').Value := 0;
      FieldByName('defnote3').Value := '';
      FieldByName('defnote4').Value := '';
      FieldByName('Price1').Value := 0;
      FieldByName('Price2').Value := 0;
      FieldByName('CYColor').Value := '';
      FieldByName('CYKZ').Value := '';
      FieldByName('CYMF').Value := '';
      FieldByName('CYECF').Value := '';
      FieldByName('CYCF').Value := '';
      FieldByName('defstr17').Value := '';
      FieldByName('defstr16').Value := '';
      FieldByName('defstr6').Value := '';
      FieldByName('defstr15').Value := '';
      FieldByName('CYColorEng').Value := '';
      FieldByName('defstr14').Value := '';
      FieldByName('CYSpec').Value := '';
      FieldByName('defnote2').Value := '';
      FieldByName('defstr13').Value := '';
      FieldByName('defstr12').Value := '';
      FieldByName('defstr11').Value := '';
      FieldByName('defstr10').Value := '';
      FieldByName('defstr9').Value := '';
      FieldByName('defstr8').Value := '';
      FieldByName('PBKZ').Value := '';
      FieldByName('PBMF').Value := '';
      FieldByName('CYMZ').Value := '';
      FieldByName('CYPriceM').Value := 0;
      FieldByName('defstr1').Value := '';
      FieldByName('CYEName').Value := '';
      FieldByName('Price6').Value := 0;
      FieldByName('Price3').Value := 0;
      FieldByName('Price4').Value := 0;
      FieldByName('Price5').Value := 0;
      FieldByName('CYNote').Value := 0;
      post;
    end;
  end;

end;

procedure TfrmCPAdd.ToolButton3Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('CYNo').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_YDang where CYNo=''' + Trim(CDS_Sub.fieldbyname('CYNo').AsString) + '''');
//      sql.Add(' and CRQtyFlag=-1');
      Open;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete CP_YDang where CYNo=''' + Trim(CDS_Sub.fieldbyname('CYNo').AsString) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end
  else
  begin
    CDS_Sub.Delete;
  end;
end;

end.

