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, cxSplitter,
  cxButtonEdit, cxCheckBox;

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;
    DataSource1: 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;
    cxSplitter1: TcxSplitter;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    v1P_ChnName: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    v1Quantity: TcxGridDBColumn;
    v1UnitName: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel4: TPanel;
    Label12: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Tv3Column1: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Panel2: TPanel;
    Button2: TButton;
    Button3: TButton;
    ClientDataSet3: TClientDataSet;
    DataSource3: TDataSource;
    cxGridPopupMenu3: TcxGridPopupMenu;
    ClientDataSet2: TClientDataSet;
    DataSource2: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    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);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    Fint:Integer;
    LCYID:String;
    procedure InitSubGrid();
    function  SaveData():Boolean;
    function  SaveDataOne():Boolean;
    { Private declarations }
  public
    FCYID,FCYCode,FCPID,FCPNO:String;
    CopyInt:Integer;

    { Public declarations }
  end;

var
  frmKHGetCPAdd: TfrmKHGetCPAdd;

implementation
uses
  U_DataLink,U_RTFun;

{$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();
  KHName.Text:=Trim(CDS_Sub.fieldbyname('KHName').AsString);
  CKDate.Date:=CDS_Sub.fieldbyname('CKdate').AsDateTime;
  CKNO.Text:=Trim(CDS_Sub.fieldbyname('CKNO').AsString);
  if Trim(FCYID)='' then
  begin
    CKDate.Date:=SGetServerDate(ADOQueryTemp);
  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('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;
function TfrmKHGetCPAdd.SaveDataOne():Boolean;
var
  maxId,FCKNO,SubId: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;
    CKNO.Text:=Trim(FCKNO);
    with CDS_Sub 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('CKdate').Value:=CKDate.Date;
        RTSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'CP_YDang_KHGet',3);
        FieldByName('CostPrice').Value:=CDS_Sub.fieldbyname('CostPrice').Value;
        Post;
      end;
      Edit;
      FieldByName('CKID').Value:=Trim(maxId);
    end;
    ClientDataSet3.DisableControls;
    with ClientDataSet3 do
    begin
      First;
      while not Eof do
      begin
        if ClientDataSet3.FieldByName('SSel').AsBoolean=True then
        begin
          if GetLSNo(ADOQueryCmd,SubId,'BC','CK_YP_CR',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet3.EnableControls;
            Application.MessageBox('取最大号失败！','提示',0);
            Exit;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            SQL.Add('select * from CK_YP_CR where BCId='''+Trim(SubId)+'''');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('BCId').Value:=Trim(SubId);
            RTSetSaveDataCDS(ADOQueryCmd,Tv3,ClientDataSet3,'CK_YP_CR',99);
            FieldByName('QYCKID').Value:=Trim(CDS_Sub.fieldbyname('CKID').AsString);
            FieldByName('CRTime').Value:=CKDate.Date;
            FieldByName('CYID').Value:=Trim(LCYID);
            FieldByName('CYColor').Value:=ClientDataSet3.fieldbyname('CYColor').Value;
            FieldByName('CYType').Value:=ClientDataSet3.fieldbyname('CYType').Value;
            FieldByName('QtyUnit').Value:=ClientDataSet3.fieldbyname('KCQtyUnit').Value;
            
            FieldByName('Qty').Value:=ClientDataSet3.fieldbyname('CKQty').Value;
            FieldByName('QtyFlag').Value:=-1;
            FieldByName('CRID').Value:=ClientDataSet3.fieldbyname('CRID').Value;
            FieldByName('CRFlag').Value:='出库';
            FieldByName('CRType').Value:='客户取样出库';
            FieldByName('KHName').Value:=Trim(KHName.Text);
            FieldByName('Filler').Value:=Trim(DName);
            Post;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_YP_KC Set KCQty=(select sum(Qty*QtyFlag) from CK_YP_CR A where A.CRID=CK_YP_KC.CRID)');
            sql.Add(' where CRID='+Trim(ClientDataSet3.fieldbyname('CRID').AsString));
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ClientDataSet3.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmKHGetCPAdd.ToolButton1Click(Sender: TObject);
begin
  
  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;
  FFCYID:string;
begin
  if Key=#13 then
  begin
    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;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      Application.MessageBox('此样品不存在!','提示',0);
      Exit;
    end;
    LCYID:=Trim(ADOQueryTemp.fieldbyname('CYID').AsString);
    CYID.Text:='';
    Panel4.Visible:=True;
    Panel4.Refresh;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select  A.CRID,A.BCID,A.KCQty,A.KCQtyUnit,A.CYColor,A.KCNote,A.CYType ');
      sql.Add(' ,A.CRPrice,A.BZTYpe,A.PBFactory,A.RCFactory,A.HJGFactory,A.CYID,A.WeiZhi,A.MiLenNote');
      SQL.Add(',B.CYNO,B.CYName,B.CYMF,B.CYKZ,B.CYSpec,B.CYCF,B.CYHX,B.CYEName,B.CYECF,B.CYColorEng');
      sql.Add(',MLen=dbo.F_Get_MLen(A.CYID)');
      sql.Add(',CKQty=Cast(NUll as decimal(18,2))');
      sql.Add(' from CK_YP_KC A ');
      sql.Add(' left join CP_YDang B on A.CYID=B.CYID ');
      sql.add('where A.CYID='''+Trim(LCYID)+''' and A.KCQty>0');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet3);
    SInitCDSData20(ADOQueryTemp,ClientDataSet3);

    {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('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;
      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;
      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;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update CK_YP_KC Set KCQty=(select sum(Qty*QtyFlag) from CK_YP_CR A where A.CRID=CK_YP_KC.CRID');
      sql.Add(' and isnull(A.QYCKID,'''')<>'''+Trim(CDS_Sub.fieldbyname('CKID').AsString)+''')');
      sql.Add(' where CRID in');
      sql.Add('(select CRID from CK_YP_CR where isnull(QYCKID,'''')='''+Trim(CDS_Sub.fieldbyname('CKID').AsString)+''')');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CK_YP_CR where isnull(QYCKID,'''')='''+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;

procedure TfrmKHGetCPAdd.Panel10MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND,$F012,0);
end;

procedure TfrmKHGetCPAdd.Image2Click(Sender: TObject);
begin
  Panel4.Visible:=False;
end;

procedure TfrmKHGetCPAdd.Button3Click(Sender: TObject);
begin
  Panel4.Visible:=False;
end;

procedure TfrmKHGetCPAdd.Button2Click(Sender: TObject);
var
  FReal:Double;
begin
  if ClientDataSet3.IsEmpty then
  begin
    if Application.MessageBox('此样品没有库存，确定只报价吗？','提示',32+4)<>IDYES then Exit;
  end else
  begin
    if ClientDataSet3.Locate('SSel',True,[])=False then
    begin
      if Application.MessageBox('确定只报价,不出库吗？','提示',32+4)<>IDYES then Exit;
    end else
    begin
      ClientDataSet3.DisableControls;
      with ClientDataSet3 do
      begin
        First;
        while not eof do
        begin
          if ClientDataSet3.FieldByName('SSel').AsBoolean=True then
          begin
            if Trim(ClientDataSet3.fieldbyname('CKQty').AsString)='' then
            begin
              ClientDataSet3.EnableControls;
              Application.MessageBox('出库数量不能为空!','提示',0);
              Exit;
            end else
            if ClientDataSet3.fieldbyname('CKQty').Value=0 then
            begin
              ClientDataSet3.EnableControls;
              Application.MessageBox('出库数量不能为零!','提示',0);
              Exit;
            end else
            if ClientDataSet3.fieldbyname('CKQty').Value<0 then
            begin
              ClientDataSet3.EnableControls;
              Application.MessageBox('出库数量不能小于零!','提示',0);
              Exit;
            end  else
            if ClientDataSet3.fieldbyname('CKQty').Value>ClientDataSet3.fieldbyname('KCQty').Value then
            begin
              ClientDataSet3.EnableControls;
              Application.MessageBox('出库数量不能大于库存数量!','提示',0);
              Exit;
            end;
          end;
          next;
        end;
      end;
      ClientDataSet3.EnableControls;
    end;
  end;

  if Trim(KHName.Text)='' then
  begin
    Application.MessageBox('客户不能为空!','提示',0);
    Exit;
  end;
  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(LCYID)+'''');
    sql.Add(' or CYNo='''+Trim(LCYID)+'''');
    Open;
   
  end;
  if ADOQueryTemp.IsEmpty then Exit;
  with CDS_Sub do
  begin
    Append;
    FieldByName('CYID').Value:=Trim(LCYID);
    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('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;
    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;
    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;
  if SaveDataOne() then
  begin
    Panel4.Visible:=False;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select  A.CRTime,A.CRID,A.BCID,A.Qty,A.QtyUnit,A.CYColor,A.Note,A.CYType,A.BCID ');
      sql.Add(' ,A.CRPrice,A.BZTYpe,A.PBFactory,A.RCFactory,A.HJGFactory,A.CYID,A.WeiZhi,A.MiLenNote');
      SQL.Add(',B.CYNO,B.CYName,B.CYMF,B.CYKZ,B.CYSpec,B.CYCF,B.CYHX,B.CYEName,B.CYECF,B.CYColorEng');
      sql.Add(',MLen=dbo.F_Get_MLen(A.CYID),C.KCQty,A.CYID ');
      SQL.Add(' from CK_YP_CR A inner join CP_YDang B on A.CYID=B.CYID ');
      sql.Add(' inner join CK_YP_KC C on A.CRID=C.CRID');
      sql.Add(' where A.QYCKID='''+Trim(CDS_Sub.fieldbyname('CKID').AsString)+'''');
      sql.Add(' and CRFlag=''出库'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet2);
    SInitCDSData20(ADOQueryTemp,ClientDataSet2);
  end;
end;

procedure TfrmKHGetCPAdd.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select  A.CRTime,A.CRID,A.BCID,A.Qty,A.QtyUnit,A.CYColor,A.Note,A.CYType,A.BCID ');
    sql.Add(' ,A.CRPrice,A.BZTYpe,A.PBFactory,A.RCFactory,A.HJGFactory,A.CYID,A.WeiZhi,A.MiLenNote');
    SQL.Add(',B.CYNO,B.CYName,B.CYMF,B.CYKZ,B.CYSpec,B.CYCF,B.CYHX,B.CYEName,B.CYECF,B.CYColorEng');
    sql.Add(',MLen=dbo.F_Get_MLen(A.CYID),C.KCQty,A.CYID ');
    SQL.Add(' from CK_YP_CR A inner join CP_YDang B on A.CYID=B.CYID ');
    sql.Add(' inner join CK_YP_KC C on A.CRID=C.CRID');
    sql.Add(' where A.QYCKID='''+Trim(CDS_Sub.fieldbyname('CKID').AsString)+'''');
    sql.Add(' and CRFlag=''出库'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;

end.
