unit U_ZdyAttInputCP;

interface

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

type
  TfrmZdyAttInputCP = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column6: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Column16: TcxGridDBColumn;
    v2Column17: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2DEFstr5: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure v2Column6PropertiesEditValueChanged(Sender: TObject);
    procedure v2defstr9PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column5PropertiesEditValueChanged(Sender: TObject);
    procedure v2DEFstr5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column11PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column8PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function  SaveData():Boolean;

  public
    { Public declarations }
    RKFlag,FCYID:String;
    fkhType:string;

  end;

var
  frmZdyAttInputCP: TfrmZdyAttInputCP;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_ZDYHelpSel;

{$R *.dfm}



procedure TfrmZdyAttInputCP.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select * from KH_Zdy_Attachment where ATID='''+Trim(FCYID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmZdyAttInputCP.FormDestroy(Sender: TObject);
begin
  frmZdyAttInputCP:=nil;
end;

procedure TfrmZdyAttInputCP.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmZdyAttInputCP.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('产品资料录入',Tv2,'客户自定义');
  Close;
end;
procedure TfrmZdyAttInputCP.FormShow(Sender: TObject);
begin
  ReadCxGrid('产品资料录入',Tv2,'客户自定义');
  InitGrid();
  if Trim(FCYID)='' then
  begin
    CDS_HZ.Append;
    CDS_HZ.Post;
  end;
end;

procedure TfrmZdyAttInputCP.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

function TfrmZdyAttInputCP.SaveData():Boolean;
var
  maxId,CRID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCYID)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxId,'AT','KH_Zdy_Attachment',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 KH_Zdy_Attachment where ATID='''+Trim(FCYID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FCYID)='' then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('Filltime').Value:=SGetServerDateTime(ADOQueryTemp);
      end else
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('Edittime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('ATID').Value:=Trim(maxId);

      RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'KH_Zdy_Attachment',0);
      FieldByName('ZdyName').Value:=Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
      FieldByName('DEFstr10').Value:=Trim(CDS_HZ.fieldbyname('DEFstr10').AsString);
      FieldByName('ZdyCode').Value:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
      FieldByName('Mrate').Value:=CDS_HZ.fieldbyname('Mrate').AsFloat;
      FieldByName('defflt1').Value:=CDS_HZ.fieldbyname('defflt1').AsFloat;
      FieldByName('defflt2').Value:=CDS_HZ.fieldbyname('defflt2').AsFloat;
      FieldByName('defflt3').Value:=CDS_HZ.fieldbyname('defflt2').AsFloat;
      FieldByName('defflt4').Value:=CDS_HZ.fieldbyname('defflt2').AsFloat;
      FieldByName('defflt5').Value:=CDS_HZ.fieldbyname('defflt2').AsFloat;
      FieldByName('defflt6').Value:=CDS_HZ.fieldbyname('defflt2').AsFloat;
      FieldByName('defflt7').Value:=CDS_HZ.fieldbyname('defflt2').AsFloat;
      FieldByName('defflt8').Value:=CDS_HZ.fieldbyname('defflt2').AsFloat;
      FieldByName('Type').Value:='CP';
      FieldByName('khType').Value:=trim(fkhType);
      FieldByName('Filler').Value:=Trim(DName);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from KH_Zdy where ZdyNo='''+Trim(FCYID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FCYID)='' then
      begin
        Append;
      end else
      begin
        Edit;
      end;
      FieldByName('ZdyNo').Value:=Trim(maxId);
      FieldByName('ZdyName').Value:=Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
      FieldByName('Type').Value:='CP';
      FieldByName('MainType').Value:=Trim(CDS_HZ.fieldbyname('DEFstr5').AsString);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_Zdy_Attachment where ZdyCode='''+Trim(CDS_HZ.fieldbyname('ZdyCode').AsString)+'''');
      sql.Add(' and Type=''CP'' ');
      Open;
    end;
    if ADOQueryCmd.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('产品编号不能重复!','提示',0);
      Exit;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('ATID').Value:=Trim(maxId);
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;

procedure TfrmZdyAttInputCP.ToolButton3Click(Sender: TObject);
begin
  cxGrid2.SetFocus;
  if Trim(CDS_HZ.FieldByName('ZdyCode').AsString)='' then
  begin
    Application.MessageBox('产品编号不能为空!','提示',0);
    Exit;
  end;
  if Trim(CDS_HZ.FieldByName('ZdyName').AsString)='' then
  begin
    Application.MessageBox('产品名称不能为空!','提示',0);
    Exit;
  end;
  if Trim(CDS_HZ.FieldByName('DEFstr5').AsString)='' then
  begin
    Application.MessageBox('类型不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;

  if SaveData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    ModalResult:=1;
  end;
end;


procedure TfrmZdyAttInputCP.v2Column6PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
 { mvalue:=TcxTextEdit(Sender).EditingText;

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'#'THEN
  BEGIn
    mvalue:=trim(mvalue)+'#';
  END;


  with CDS_HZ do
  begin
    Edit;
    FieldByName('ZdyName').Value:=Trim(mvalue);
    Post;
  end;
  self.tv2.Controller.EditingController.ShowEdit();
  }


end;

procedure TfrmZdyAttInputCP.v2defstr9PropertiesEditValueChanged(
  Sender: TObject);

var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'#'THEN
  BEGIn
    mvalue:=trim(mvalue)+'#';
  END;


  with CDS_HZ do
  begin
    Edit;
    FieldByName('ZdyName').Value:=Trim(mvalue);
    Post;
  end;
  self.tv2.Controller.EditingController.ShowEdit();




end;

procedure TfrmZdyAttInputCP.v2Column5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
 { mvalue:=TcxTextEdit(Sender).EditingText;

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'#'THEN
  BEGIn
    mvalue:=trim(mvalue)+'#';
  END;


  with CDS_HZ do
  begin
    Edit;
    FieldByName('DEFstr3').Value:=Trim(mvalue);
    Post;
  end;
  self.tv2.Controller.EditingController.ShowEdit();


 }
end;

procedure TfrmZdyAttInputCP.v2DEFstr5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CPType';
      flagname:='产品类型';
      if ShowModal=1 then
      begin
        CDS_HZ.Edit;
        CDS_HZ.FieldByName('DEFstr5').Value:=Trim(ClientDataSet1.fieldbyname('ZDYname').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmZdyAttInputCP.v2Column11PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
var
  mvalue:String;
  FPrice:Double;
begin
  mvalue:='';
  FPrice:=0;
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='CPHouZhengGY';
      flagname:='产品后整工艺';

      if ShowModal=1 then
      begin
        ClientDataSet1.DisableControls;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean=True then
            begin
              if Trim(mvalue)='' then
                mvalue:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)
              else
                mvalue:=mvalue+'+'+Trim(fieldbyname('ZdyName').AsString);
              FPrice:=FPrice+StrToFloatDef(Trim(ClientDataSet1.fieldbyname('ZdyFlt1').AsString),0);
            end;
            Next;
          end;
        end;
        ClientDataSet1.EnableControls;
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('DEFstr3').Value:=Trim(mvalue);
          FieldByName('DefFlt6').Value:=FPrice;
          FieldByName('DefFlt5').Value:=RoundFloat((StrToFloatDef(Trim(FieldByName('DefFlt1').AsString),0)*(1+StrToFloatDef(Trim(FieldByName('DefFlt7').AsString),0)/100)
                                                   +StrToFloatDef(Trim(FieldByName('DefFlt3').AsString),0))+StrToFloatDef(Trim(FieldByName('DefFlt6').AsString),0)
                                                   *(1+StrToFloatDef(Trim(FieldByName('DefFlt8').AsString),0)/100),2);
        end;
      end;
    end;

  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmZdyAttInputCP.v2Column8PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=Trim(TcxTextEdit(Sender).EditingText);
  if Trim(mvalue)='' then mvalue:='0';
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('DefFlt4').Value:=RoundFloat(StrToFloatDef(Trim(FieldByName('DefFlt1').AsString),0)*(1+StrToFloatDef(Trim(FieldByName('DefFlt7').AsString),0)/100)
                                              +StrToFloatDef(Trim(FieldByName('DefFlt3').AsString),0),2);
    FieldByName('DefFlt5').Value:=RoundFloat((StrToFloatDef(Trim(FieldByName('DefFlt1').AsString),0)*(1+StrToFloatDef(Trim(FieldByName('DefFlt7').AsString),0)/100)
                                              +StrToFloatDef(Trim(FieldByName('DefFlt3').AsString),0))+StrToFloatDef(Trim(FieldByName('DefFlt6').AsString),0)
                                              *(1+StrToFloatDef(Trim(FieldByName('DefFlt8').AsString),0)/100),2);
    Post;
  end;
  Tv2.Controller.EditingController.ShowEdit();
end;

end.
