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;

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;
    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 v1Column13PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  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_RTFun,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('样品编辑YT',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('样品编辑YT',Tv1,'样品管理');
  Close;
end;
function TfrmCPAdd.SaveData():Boolean;
var
  maxId:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCYID)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxId,'','CP_YDang',4,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('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;
      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;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmCPAdd.ToolButton1Click(Sender: TObject);
var
  FFFFCPNO:string;
begin
  if CDS_Sub.Locate('CYNo',null,[])=True then
  begin
    Application.MessageBox('产品编号不能为空！','提示',0);
    Exit;
  end;
  {if CDS_Sub.Locate('CYName',null,[])=True then
  begin
    Application.MessageBox('中文名称不能为空！','提示',0);
    Exit;
  end;   }
  FFFFCPNO:=Trim(CDS_Sub.fieldbyname('CYNo').AsString);
  if Pos(':',FFFFCPNO)>0 then
  begin
    Application.MessageBox('产品编号不允许存在冒号(:)！','',0);
    Exit;
  end;
  if Pos('：',FFFFCPNO)>0 then
  begin
    Application.MessageBox('产品编号不允许存在冒号(：)！','',0);
    Exit;
  end;
  if Pos('\',FFFFCPNO)>0 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.v1Column13PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YPWeiZhi';
      flagname:='陈列位置';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('DefStr2').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
