unit U_KHGetCPAdd;

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, cxContainer, cxTextEdit,
  cxMaskEdit, cxDropDownEdit, cxCalendar, StdCtrls, ExtCtrls;

type
  TfrmKHGetCPAdd = 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;
    v1Column12: TcxGridDBColumn;
    Panel1: TPanel;
    CYID: TEdit;
    Label1: TLabel;
    KHName: TEdit;
    Label3: TLabel;
    CKDate: TcxDateEdit;
    v1Column13: TcxGridDBColumn;
    CIF: TEdit;
    HuiLv: TEdit;
    LiRunLv: TEdit;
    v1Column14: TcxGridDBColumn;
    PriceType: TComboBox;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    CKNO: TEdit;
    Button1: TButton;
    CKYwy: TEdit;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CYIDKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure v1Column14PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column16PropertiesEditValueChanged(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    Fint:Integer;
    procedure InitSubGrid();
    function  SaveData():Boolean;
    { Private declarations }
  public
    FCYID,FCYCode,FCPID,FCPNO:String;
    CopyInt:Integer;

    { Public declarations }
  end;

var
  frmKHGetCPAdd: TfrmKHGetCPAdd;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmKHGetCPAdd.InitSubGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('exec P_Select_Get_CP_YDang :CKID,:Begdate,:EndDate,:PState');
      Parameters.ParamByName('CKID').Value:=Trim(FCYID);
      Parameters.ParamByName('Begdate').Value:='';
      Parameters.ParamByName('EndDate').Value:='';
      Parameters.ParamByName('PState').Value:=3;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Sub);
    SInitCDSData20(ADOQueryMain,CDS_Sub);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmKHGetCPAdd.FormShow(Sender: TObject);
var
  fsj:String;
begin

  ReadCxGrid('客户拿样IN',Tv1,'样品管理');
  InitSubGrid();
  IF not CDS_Sub.IsEmpty then
  begin
    KHName.Text:=Trim(CDS_Sub.fieldbyname('KHName').AsString);
    CKDate.Date:=CDS_Sub.fieldbyname('CKdate').AsDateTime;
    CKNO.Text:=Trim(CDS_Sub.fieldbyname('CKNO').AsString);
    CKYwy.Text:=Trim(CDS_Sub.fieldbyname('CKYwy').AsString);
  end;
  if Trim(FCYID)='' then
  begin
    CKDate.Date:=SGetServerDate(ADOQueryTemp);
    CKYwy.Text:=Dname;
  end;
  if CopyInt=99 then
  begin
    CKNO.Text:='';
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('CKID').Value:='';
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
  end;
end;

procedure TfrmKHGetCPAdd.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('客户拿样IN',Tv1,'样品管理');
  Close;
end;
function TfrmKHGetCPAdd.SaveData():Boolean;
var
  maxId,FCKNO:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(CKNO.Text)='' then
    begin
      if GetLSNo(ADOQueryCmd,FCKNO,'','CP_YDang_KHGet',3,1) =False then
      begin
        Result:=False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取拿样编号最大号失败！','提示',0);
        Exit;
      end;
    end else
    begin
      FCKNO:=Trim(CKNO.Text);
    end;
    with CDS_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_Sub.fieldbyname('CKID').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxId,'CK','CP_YDang_KHGet',4,1)=False then
          begin
            Result:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxId:=Trim(CDS_Sub.fieldbyname('CKID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from CP_YDang_KHGet where CKID='''+Trim(CDS_Sub.fieldbyname('CKID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_Sub.fieldbyname('CKID').AsString)='' then
          begin
            Append;
          end else
          begin
            Edit;
          end;
          FieldByName('CKID').Value:=Trim(maxId);
          FieldByName('CKNO').Value:=Trim(FCKNO);
          FieldByName('CYID').Value:=Trim(CDS_Sub.fieldbyname('CYID').AsString);
          FieldByName('KHName').Value:=Trim(KHName.Text);
          FieldByName('CKYwy').Value:=Trim(CKYwy.Text);
          FieldByName('CKdate').Value:=CKDate.Date;
          RTSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'CP_YDang_KHGet',3);
          FieldByName('CostPrice').Value:=CDS_Sub.fieldbyname('CostPrice').Value;
          Post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmKHGetCPAdd.ToolButton1Click(Sender: TObject);
begin
  if Trim(KHName.Text)='' then
  begin
    Application.MessageBox('客户不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.IsEmpty then Exit;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;

procedure TfrmKHGetCPAdd.CYIDKeyPress(Sender: TObject; var Key: Char);
var
  FReal:Double;
begin
  if Key=#13 then
  begin
    if Trim(CIF.Text)<>'' then
    begin
      if TryStrToFloat(Trim(CIF.Text),FReal)=False then
      begin
        CYID.Text:='';
        Application.MessageBox('CIF数字非法!','',0);
        Exit;
      end;
    end;
    if Trim(HuiLv.Text)<>'' then
    begin
      if TryStrToFloat(Trim(HuiLv.Text),FReal)=False then
      begin
        CYID.Text:='';
        Application.MessageBox('汇率数字非法!','',0);
        Exit;
      end;
    end else
    begin
      CYID.Text:='';
      Application.MessageBox('汇率不能为空!','提示',0);
      Exit;
    end;
    if Trim(LiRunLv.Text)<>'' then
    begin
      if TryStrToFloat(Trim(LiRunLv.Text),FReal)=False then
      begin
        CYID.Text:='';
        Application.MessageBox('利润率数字非法!','',0);
        Exit;
      end;
    end else
    begin
      CYID.Text:='';
      Application.MessageBox('利润率不能为空!','提示',0);
      Exit;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select * from CP_YDang where CYID='''+Trim(CYID.Text)+'''');
      sql.Add(' or CYNo='''+Trim(CYID.Text)+'''');
      Open;
      CYID.Text:='';
    end;
    if ADOQueryTemp.IsEmpty then Exit;
    with CDS_Sub do
    begin
      Append;
      FieldByName('CYID').Value:=Trim(ADOQueryTemp.fieldbyname('CYID').AsString);
      FieldByName('CYNo').Value:=Trim(ADOQueryTemp.fieldbyname('CYNo').AsString);
      FieldByName('CYName').Value:=Trim(ADOQueryTemp.fieldbyname('CYName').AsString);
      FieldByName('CYEName').Value:=Trim(ADOQueryTemp.fieldbyname('CYEName').AsString);
      FieldByName('CYSpec').Value:=Trim(ADOQueryTemp.fieldbyname('CYSpec').AsString);
      FieldByName('CYCF').Value:=Trim(ADOQueryTemp.fieldbyname('CYCF').AsString);
      FieldByName('CYECF').Value:=Trim(ADOQueryTemp.fieldbyname('CYECF').AsString);
      FieldByName('CYMF').Value:=Trim(ADOQueryTemp.fieldbyname('CYMF').AsString);
      FieldByName('CYKZ').Value:=Trim(ADOQueryTemp.fieldbyname('CYKZ').AsString);
      FieldByName('CYColor').Value:=Trim(ADOQueryTemp.fieldbyname('CYColor').AsString);
      FieldByName('PriceType').Value:=Trim(PriceType.Text);
      FieldByName('CKYwy').Value:=Trim(DName);
      FieldByName('CYColorEng').Value:=Trim(ADOQueryTemp.fieldbyname('CYColorEng').AsString);
   {   if Trim(PriceType.Text)='公斤价' then
      begin
        FieldByName('CostPrice').Value:=ADOQueryTemp.fieldbyname('CYPriceKg').Value;
      end else
      if Trim(PriceType.Text)='米价' then
      begin
        FieldByName('CostPrice').Value:=ADOQueryTemp.fieldbyname('CYPriceM').Value;
      end else
      if Trim(PriceType.Text)='码价' then
      begin
        FieldByName('CostPrice').Value:=ADOQueryTemp.fieldbyname('CYPriceM').Value*1.00/1.0936;
      end; }
      FieldByName('CostPrice').Value:=ADOQueryTemp.fieldbyname('price6').AsFloat;
      if Trim(CIF.Text)='' then
      begin
        FieldByName('CIF').Value:=0;
      end else
      begin
        FieldByName('CIF').Value:=StrToFloat(CIF.Text);
      end;
      FieldByName('HuiLv').Value:=StrToFloat(HuiLv.Text);
      FieldByName('LiRunLv').Value:=StrToFloat(LiRunLv.Text);
      Post;
    end;
    with CDS_Sub do
    begin
      Edit;
      IF FieldByName('LiRunLv').AsFloat=0.0 then
      begin
        FieldByName('RMBPrice').Value:=0;
        FieldByName('USDPrice').Value:=0;
      end
      else
      begin
        FieldByName('RMBPrice').Value:=FieldByName('CostPrice').AsFloat*1.00/FieldByName('LiRunLv').AsFloat;
        FieldByName('USDPrice').Value:=FieldByName('CostPrice').AsFloat*1.00/FieldByName('LiRunLv').AsFloat/FieldByName('HuiLv').AsFloat+FieldByName('CIF').AsFloat;
      end;
          {
      FieldByName('RMBPrice').Value:=FieldByName('CostPrice').Value
                                     *FieldByName('LiRunLv').Value;
      FieldByName('USDPrice').Value:=FieldByName('CostPrice').Value
                                     *FieldByName('LiRunLv').Value*1.00/FieldByName('HuiLv').Value
                                     +FieldByName('CIF').Value;          }
      Post;
    end;
  end;
end;

procedure TfrmKHGetCPAdd.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.fieldbyname('CKID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除已保存的数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete CP_YDang_KHGet where CKID='''+Trim(CDS_Sub.fieldbyname('CKID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_Sub.Delete;

end;

procedure TfrmKHGetCPAdd.v1Column14PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=Trim(TcxComboBox(Sender).EditingText);
  with CDS_Sub do
  begin
    Edit;
    FieldByName('PriceType').Value:=Trim(mvalue);
    Post;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from CP_YDang where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CDS_Sub.Edit;
    if Trim(CDS_Sub.fieldbyname('PriceType').AsString)='公斤价' then
    begin
      CDS_Sub.FieldByName('CostPrice').Value:=ADOQueryTemp.fieldbyname('CYPriceKg').Value;
    end else
    if Trim(CDS_Sub.fieldbyname('PriceType').AsString)='米价' then
    begin
      CDS_Sub.FieldByName('CostPrice').Value:=ADOQueryTemp.fieldbyname('CYPriceM').Value;
    end else
    if Trim(CDS_Sub.fieldbyname('PriceType').AsString)='码价' then
    begin
      CDS_Sub.FieldByName('CostPrice').Value:=ADOQueryTemp.fieldbyname('CYPriceM').Value*1.00/1.0936;
    end;
    CDS_Sub.Post;
  end else
  begin
    Exit;
  end;
  with CDS_Sub do
  begin
    Edit;
    FieldByName('RMBPrice').Value:=FieldByName('CostPrice').Value
                                   *FieldByName('LiRunLv').Value;
    FieldByName('USDPrice').Value:=FieldByName('CostPrice').Value
                                   *FieldByName('LiRunLv').Value*1.00/FieldByName('HuiLv').Value
                                   +FieldByName('CIF').Value;
    Post;
  end;

end;

procedure TfrmKHGetCPAdd.v1Column16PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,MFieldName:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  MFieldName:=Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Sub do
  begin
    Edit;
    FieldByName(MFieldName).Value:=mvalue;
    Post;
  end;
  with CDS_Sub do
  begin
    Edit;
    FieldByName('RMBPrice').Value:=FieldByName('CostPrice').Value
                                   *FieldByName('LiRunLv').Value;
    FieldByName('USDPrice').Value:=FieldByName('CostPrice').Value
                                   *FieldByName('LiRunLv').Value*1.00/FieldByName('HuiLv').Value
                                   +FieldByName('CIF').Value;
    Post;
  end;
end;

procedure TfrmKHGetCPAdd.Button1Click(Sender: TObject);
begin
  with CDS_Sub do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('PriceType').Value:=Trim(PriceType.Text);
      Post;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add(' select * from CP_YDang where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty=False then
      begin
        CDS_Sub.Edit;
        if Trim(PriceType.Text)='公斤价' then
        begin
          FieldByName('CostPrice').Value:=ADOQueryTemp.fieldbyname('CYPriceKg').Value;
        end else
        if Trim(PriceType.Text)='米价' then
        begin
          FieldByName('CostPrice').Value:=ADOQueryTemp.fieldbyname('CYPriceM').Value;
        end else
        if Trim(PriceType.Text)='码价' then
        begin
          FieldByName('CostPrice').Value:=ADOQueryTemp.fieldbyname('CYPriceM').Value*1.00/1.0936;
        end;
        CDS_Sub.Post;
      end else
      begin
        Exit;
      end;
      with CDS_Sub do
      begin
        Edit;
        FieldByName('RMBPrice').Value:=FieldByName('CostPrice').Value
                                       *FieldByName('LiRunLv').Value;
        FieldByName('USDPrice').Value:=FieldByName('CostPrice').Value
                                       *FieldByName('LiRunLv').Value*1.00/FieldByName('HuiLv').Value
                                       +FieldByName('CIF').Value;
        Post;
      end;
      Next;
    end;
  end;

end;

end.
