unit U_BGJCKMXInput;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, BtnEdit, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles,
  cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB,
  cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, DBClient, ADODB, cxButtonEdit,
  cxCheckBox, cxCalendar, cxDropDownEdit, cxPC, cxTextEdit;

type
  TfrmBGJCKMXInput = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Panel77: TPanel;
    ADOQueryTemp: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    ClientDataSet7: TClientDataSet;
    cxGridPopupMenu7: TcxGridPopupMenu;
    DataSource7: TDataSource;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel6: TPanel;
    cxGrid3: TcxGrid;
    Tv7: TcxGridDBTableView;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    CDS_BGName: TClientDataSet;
    cxGridPopupMenu5: TcxGridPopupMenu;
    DS_BGName: TDataSource;
    ClientDataSet3: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource3: TDataSource;
    cxGridPopupMenu3: TcxGridPopupMenu;
    cxGrid6: TcxGrid;
    Tv6: TcxGridDBTableView;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridLevel6: TcxGridLevel;
    Tv6Column1: TcxGridDBColumn;
    Tv6Column2: TcxGridDBColumn;
    ClientDataSet6: TClientDataSet;
    cxGridPopupMenu6: TcxGridPopupMenu;
    DataSource6: TDataSource;
    v6Column1: TcxGridDBColumn;
    v6Column2: TcxGridDBColumn;
    v6Column3: TcxGridDBColumn;
    v6Column4: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2BCOrderNo: TcxGridDBColumn;
    v6BCOrderNo: TcxGridDBColumn;
    Panel5: TPanel;
    Label14: TLabel;
    Label4: TLabel;
    Label1: TLabel;
    Label12: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label13: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    cxGrid5: TcxGrid;
    TVBGName: TcxGridDBTableView;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    EditCount: TEdit;
    OrderNo: TEdit;
    MLenStr: TEdit;
    ChengJiaoType: TBtnEditA;
    HaiYunFee: TEdit;
    BaoFee: TEdit;
    HuoYuanPlace: TBtnEditA;
    ChuKouKA: TBtnEditA;
    YunDiG: TBtnEditA;
    Panel2: TPanel;
    Label2: TLabel;
    Label8: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    BGDate: TDateTimePicker;
    BCOrderNo: TEdit;
    Edit1: TEdit;
    Edit2: TEdit;
    CheckBox1: TCheckBox;
    cxGrid1: TcxGrid;
    Tv3: TcxGridDBTableView;
    v3Column1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    v3Column16: TcxGridDBColumn;
    v3Column17: TcxGridDBColumn;
    v3Column18: TcxGridDBColumn;
    v3Column20: TcxGridDBColumn;
    v3Column19: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    v3Column10: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolButton1: TToolButton;
    ComTaiTou: TBtnEditA;
    procedure FormDestroy(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv7CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure cxGridDBColumn2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TBSaveClick(Sender: TObject);
    procedure Tv7FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure cxPageControl1Change(Sender: TObject);
    procedure Tv7DblClick(Sender: TObject);
    procedure TVBGNameDblClick(Sender: TObject);
    procedure cxGridDBColumn1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxGridDBColumn4PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v6Column2PropertiesEditValueChanged(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure HuoYuanPlaceBtnClick(Sender: TObject);
    procedure ChuKouKABtnClick(Sender: TObject);
    procedure YunDiGBtnClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ComTaiTouBtnClick(Sender: TObject);
    procedure ChengJiaoTypeBtnClick(Sender: TObject);
  private
    { Private declarations }
    FInt:String;
    function SaveData():Boolean;
    procedure InitGridMD();
  public
    { Public declarations }
    FOrderNo,FBGID,FBCID:String;
  end;

var
  frmBGJCKMXInput: TfrmBGJCKMXInput;

implementation
uses
  U_DataLink,U_RTFun, U_ZDYHelp, U_ZDYHelpSel,U_ModuleNote, U_GYSList;

{$R *.dfm}

procedure TfrmBGJCKMXInput.FormDestroy(Sender: TObject);
begin
  frmBGJCKMXInput:=nil;
end;

procedure TfrmBGJCKMXInput.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('tv2',tv2,'报关明细审核');
  WriteCxGrid('tv6',tv6,'报关明细审核');
  Close;
end;

procedure TfrmBGJCKMXInput.FormShow(Sender: TObject);
var
  FMoney,FMQty,FKgQty:String;
begin

  BGDate.DateTime:=SGetServerDate(ADOQueryTemp);
  ReadCxGrid('tv2',tv2,'报关明细审核');
  ReadCxGrid('tv6',tv6,'报关明细审核');
  v2BCOrderNo.Visible:=False;
  v2BCOrderNo.Hidden:=True;
  v6BCOrderNo.Visible:=False;
  v6BCOrderNo.Hidden:=True;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYorder_BG where BGID='''+Trim(FBGID)+'''');
    Open;
  end;
  SCSHDataNew(ADOQueryTemp,Panel5,2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYorder_BG_CK where BCID='''+Trim(FBCID)+'''');
    Open;
  end;
  SCSHDataNew(ADOQueryTemp,Panel2,2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_BG_Qty where BGID='''+Trim(FBGID)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet7);
  SInitCDSData20(ADOQueryTemp,ClientDataSet7);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_BG_Name where BGID='''+Trim(FBGID)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_BGName);
  SInitCDSData20(ADOQueryTemp,CDS_BGName);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_BG_CKName_Qty where BCID='''+Trim(FBCID)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet6);
  SInitCDSData20(ADOQueryTemp,ClientDataSet6);
  BCOrderNo.SetFocus;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(BFMoney),0) BFMoney from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+'''');
    open;
  end;
  FMoney:=Trim(ADOQueryTemp.fieldbyname('BFMoney').AsString);
  if Trim(FMoney)='' then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(BFQty),0) KgQty from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+'''');
    sql.Add(' and PSXH=(select Max(PSXH) from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+''')');
    open;
  end;
  FKgQty:=Trim(ADOQueryTemp.fieldbyname('KgQty').AsString);
  if Trim(FKgQty)<>'' then
  begin
    if StrToFloat(FKgQty)>0 then
    begin
      Edit1.Text:=FloatToStr(SSWR(StrToFloat(FMoney)*1.00/StrToFloat(FKgQty)));
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(BFMQty),0) MQty from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+'''');
    sql.Add(' and PSXH=(select Max(PSXH) from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+''')');
    open;
  end;
  FMQty:=Trim(ADOQueryTemp.fieldbyname('MQty').AsString);
  if Trim(FMQty)<>'' then
  begin
    if StrToFloat(FMQty)>0 then
    begin
      Edit2.Text:=FloatToStr(SSWR(StrToFloat(FMoney)*1.00/StrToFloat(FMQty)));
    end;
  end;
end;

procedure TfrmBGJCKMXInput.Tv7CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ClientDataSet2 do
  begin
    Append;
    FieldByName('PSName').Value:=Trim(ClientDataSet7.fieldbyname('PSName10').AsString);
    FieldByName('FactoryNo').Value:=Trim(ClientDataSet7.fieldbyname('FactoryNo').AsString);
    FieldByName('FactoryName').Value:=Trim(ClientDataSet7.fieldbyname('FactoryName').AsString);
    FieldByName('BQUnit').Value:=Trim(ClientDataSet7.fieldbyname('QtyUnit').AsString);
    FieldByName('BQPS').Value:=ClientDataSet7.fieldbyname('MDPS').Value;
    FieldByName('BQQty').Value:=ClientDataSet7.fieldbyname('MDQty').Value;
    Post;
  end;
end;

procedure TfrmBGJCKMXInput.cxGridDBColumn2PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with ClientDataSet2 do
        begin
          edit;
          FieldByName('FactoryNo').Value:=Trim(frmGYSList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('FactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

function TfrmBGJCKMXInput.SaveData():Boolean;
var
  maxno,maxsubno,maxNQNo:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYorder_BG where BGID='''+Trim(FBGID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      RTSetsavedata(ADOQueryCmd,'JYorder_BG',Panel5,2);
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      post;
    end;
    //SCSHDataNew(ADOQueryTemp,Panel5,2);
    ///保存主表
    if Trim(FBCID)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxno,'BC','JYOrder_BG_CK',3,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FBCID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JYOrder_BG_CK where BCId='''+Trim(FBCID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FBCID)='' then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('BGID').Value:=Trim(FBGID);
      FieldByName('BCID').Value:=Trim(maxno);
     // RTSetsavedata(ADOQueryCmd,'JYOrder_BG_CK',Panel2,2);
      //FieldByName('BGNote').Value:=Trim(BGNote.Text);
      if Trim(FInt)<>'99' then
      begin
        FieldByName('Chker').Value:=Trim(DName);
        FieldByName('Chktime').Value:=SGetServerDateTime(ADOQueryTemp);
        FieldByName('ChkStatus').Value:='审核通过';
      end;

      Post;
    end;

    ///保存报关名称信息子表
    with ClientDataSet6 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet6.fieldbyname('NQId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxNQNo,'NQId','JYOrder_BG_CKName_Qty',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxNQNo:=Trim(ClientDataSet6.fieldbyname('NQId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_BG_CKName_Qty where NQId='''+Trim(maxNQNo)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet6.fieldbyname('NQId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('BGId').Value:=Trim(FBGID);
          {if CheckBox1.Checked=False then
          begin
            FieldByName('BCId').Value:=Trim(maxno);
          end;}
          FieldByName('NQId').Value:=Trim(maxNQNo);
          RTSetSaveDataCDS(ADOQueryCmd,Tv6,ClientDataSet6,'JYOrder_BG_CKName_Qty',0);
          Post;
        end;
        ClientDataSet6.Edit;
        ClientDataSet6.FieldByName('NQId').Value:=Trim(maxNQNo);
        //Order_Sub.Post;
        Next;
      end;
    end;
    if CheckBox1.Checked then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate JYOrder_BG_CK Set Chker='''+Trim(DName)+''',ChkStatus=''审核通过'' ');
        sql.Add(',ChkTime=Getdate()');
        sql.Add(' where BGID='''+Trim(FBGID)+'''');
        sql.Add(' and isnull(SSTIme,'''')<>'''' ');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmBGJCKMXInput.TBSaveClick(Sender: TObject);
var
  FReal:Double;//a39978874
begin
  ToolBar1.SetFocus;
  FInt:='';
  if Trim(BCOrderNo.Text)='' then
  begin
    Application.MessageBox('订单号不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.IsEmpty then
  begin
    Application.MessageBox('费用明细不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('BFName',Null,[])=True then
  begin
    Application.MessageBox('费用名称不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('BFQty',Null,[])=True then
  begin
    Application.MessageBox('费用数量不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('BFPrice',Null,[])=True then
  begin
    Application.MessageBox('费用单价不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('BFUnit',Null,[])=True then
  begin
    Application.MessageBox('费用单位不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('BFMoney',Null,[])=True then
  begin
    Application.MessageBox('费用金额不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet6.IsEmpty 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 TfrmBGJCKMXInput.Tv7FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  InitGridMD();
end;
procedure TfrmBGJCKMXInput.InitGridMD();
var
  fsj:string;
begin
  try
    ADOQueryTemp.DisableControls;
    with ADOQueryTemp do
    begin
      Close;
      Filtered:=False;
      sql.Clear;
      sql.Add('exec P_View_MD_BGMX :begdate,:enddate,:orderno,:MDType,:WSql');
      Parameters.ParamByName('begdate').Value:='';
      Parameters.ParamByName('enddate').Value:='';
      Parameters.ParamByName('orderno').Value:=Trim(orderNo.Text);
      Parameters.ParamByName('MDType').Value:='';
      fsj:=' and isnull(JMM.CKName,'''')='''' ';
      fsj:=fsj+' and JMM.PSName='''+Trim(ClientDataSet7.fieldbyname('PSName').AsString)+'''';
      fsj:=fsj+' and JMM.FactoryName='''+Trim(ClientDataSet7.fieldbyname('FactoryName').AsString)+'''';
      Parameters.ParamByName('WSql').Value:=fsj;
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet3);
    SInitCDSData20(ADOQueryTemp,ClientDataSet3);
  finally
    ADOQueryTemp.EnableControls;
  end;
end;

procedure TfrmBGJCKMXInput.cxPageControl1Change(Sender: TObject);
begin
  InitGridMD();
end;

procedure TfrmBGJCKMXInput.Tv7DblClick(Sender: TObject);
begin
  {with ClientDataSet2 do
  begin
    Append;
    FieldByName('FactoryName').Value:=ClientDataSet7.fieldbyname('FactoryName').Value;
    FieldByName('FactoryNo').Value:=ClientDataSet7.fieldbyname('FactoryNo').Value;
    FieldByName('BFQty').Value:=ClientDataSet7.fieldbyname('BQQty').Value;
    FieldByName('BFUnit').Value:=ClientDataSet7.fieldbyname('BQUnit').Value;
    Post;
  end; }
end;

procedure TfrmBGJCKMXInput.TVBGNameDblClick(Sender: TObject);
begin
  with ClientDataSet6 do
  begin
    Append;
    FieldByName('BGName').Value:=CDS_BGName.fieldbyname('BGName').Value;
    Post;
  end;
end;

procedure TfrmBGJCKMXInput.cxGridDBColumn1PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='BFName';
      flagname:='报关费用名称';
      if ShowModal=1 then
      begin
        with Self.ClientDataSet2 do
        begin
          Edit;
          FieldByName('BFName').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBGJCKMXInput.cxGridDBColumn4PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FName,FPrice,FQty:string;
begin
  FName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with ClientDataSet2 do
  begin
    Edit;
    FieldByName(FName).Value:=mvalue;
    Post;
  end;
  FPrice:=Trim(ClientDataSet2.fieldbyname('BFPrice').AsString);
  FQty:=Trim(ClientDataSet2.fieldbyname('BFQty').AsString);
  if Trim(FPrice)='' then
  begin
    FPrice:='0';
  end;
  if Trim(FQty)='' then
  begin
    FQty:='0';
  end;
  with ClientDataSet2 do
  begin
    Edit;
    FieldByName('BFMoney').Value:=StrToFloat(FPrice)*StrToFloat(FQty);
    Post;
  end;
end;

procedure TfrmBGJCKMXInput.v2Column2PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with ClientDataSet2 do
        begin
          edit;
          FieldByName('FactoryNo').Value:=Trim(frmGYSList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('FactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmBGJCKMXInput.v6Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FName,FPrice,FQty,FMQty,FMoney,FMLen:string;
begin
  FName:=Trim(Tv6.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with ClientDataSet6 do
  begin
    Edit;
    FieldByName(FName).Value:=mvalue;
    Post;
  end;
  FQty:=Trim(ClientDataSet6.fieldbyname('NQkgQty').AsString);
  if Trim(FQty)='' then
  begin
    FQty:='0';
  end;
  FMQty:=Trim(ClientDataSet6.fieldbyname('NQMQty').AsString);
  if Trim(FMQty)='' then
  begin
    FMQty:='0';
  end;
  FPrice:=Trim(ClientDataSet6.fieldbyname('BGPrice').AsString);
  FMoney:=Trim(ClientDataSet6.fieldbyname('BGMoney').AsString);
  if Trim(FPrice)='' then
  begin
    FPrice:='0';
  end;
  if Trim(FQty)='' then
  begin
    FQty:='0';
  end;
  if Trim(FMQty)='' then
  begin
    FMQty:='0';
  end;
  if Trim(FMoney)='' then
  begin
    FMoney:='0';
  end;
  with ClientDataSet6 do
  begin
    Edit;
    if Trim(FName)='BGPrice' then
    begin
      if Trim(ClientDataSet6.fieldbyname('NQUnit').AsString)='M' then
      begin
        FieldByName('BGMoney').Value:=StrToFloat(FPrice)*StrToFloat(FMQty);
      end else
      begin
        FieldByName('BGMoney').Value:=StrToFloat(FPrice)*StrToFloat(FQty);
      end;
    end else
    begin
      if Trim(ClientDataSet6.fieldbyname('NQUnit').AsString)='M' then
      begin
        if Trim(FMQty)<>'0' then
          FieldByName('BGPrice').Value:=StrToFloat(FMoney)*1.00/StrToFloat(FMQty)
        else
          FieldByName('BGPrice').Value:=0;
      end else
      begin
        if Trim(FQty)<>'0' then
          FieldByName('BGPrice').Value:=StrToFloat(FMoney)*1.00/StrToFloat(FQty)
        else
          FieldByName('BGPrice').Value:=0;
      end;
    end;
    Post;
  end;
end;

procedure TfrmBGJCKMXInput.CheckBox1Click(Sender: TObject);
var
  FMoney,FMQty,FKgQty:String;
begin
  if CheckBox1.Checked=False then
  begin
    v2BCOrderNo.Visible:=False;
    v2BCOrderNo.Hidden:=True;
    v6BCOrderNo.Visible:=False;
    v6BCOrderNo.Hidden:=True;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet2);
    SInitCDSData20(ADOQueryTemp,ClientDataSet2);
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_BG_CKName_Qty where BCID='''+Trim(FBCID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet6);
    SInitCDSData20(ADOQueryTemp,ClientDataSet6);
    BCOrderNo.SetFocus;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select isnull(sum(BFMoney),0) BFMoney from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+'''');
      open;
    end;
    FMoney:=Trim(ADOQueryTemp.fieldbyname('BFMoney').AsString);
    if Trim(FMoney)='' then Exit;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select isnull(sum(BFQty),0) KgQty from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+'''');
      sql.Add(' and PSXH=(select Max(PSXH) from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+''')');
      open;
    end;
    FKgQty:=Trim(ADOQueryTemp.fieldbyname('KgQty').AsString);
    if Trim(FKgQty)<>'' then
    begin
      if StrToFloat(FKgQty)>0 then
      begin
        Edit1.Text:=FloatToStr(SSWR(StrToFloat(FMoney)*1.00/StrToFloat(FKgQty)));
      end;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select isnull(sum(BFMQty),0) MQty from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+'''');
      sql.Add(' and PSXH=(select Max(PSXH) from JYOrder_BG_KPFee where BCID='''+Trim(FBCID)+''')');
      open;
    end;
    FMQty:=Trim(ADOQueryTemp.fieldbyname('MQty').AsString);
    if Trim(FMQty)<>'' then
    begin
      if StrToFloat(FMQty)>0 then
      begin
        Edit2.Text:=FloatToStr(SSWR(StrToFloat(FMoney)*1.00/StrToFloat(FMQty)));
      end;
    end;
  end else
  begin
    v2BCOrderNo.Visible:=True;
    v2BCOrderNo.Hidden:=False;
    v6BCOrderNo.Visible:=True;
    v6BCOrderNo.Hidden:=False;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.BCOrderNo ');
      sql.Add(' from JYOrder_BG_KPFee A');
      sql.Add(' inner join JYOrder_BG_CK B on A.BCID=B.BCID');
      sql.Add(' where B.BGID='''+Trim(FBGID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet2);
    SInitCDSData20(ADOQueryTemp,ClientDataSet2);
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.BCOrderNo ');
      sql.Add(' from JYOrder_BG_CKName_Qty A');
      sql.Add(' inner join JYOrder_BG_CK B on A.BCID=B.BCID');
      sql.Add(' where B.BGID='''+Trim(FBGID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet6);
    SInitCDSData20(ADOQueryTemp,ClientDataSet6);
    BCOrderNo.SetFocus;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select isnull(sum(BFMoney),0) BFMoney from JYOrder_BG_KPFee where BGID='''+Trim(FBGID)+'''');
      open;
    end;
    FMoney:=Trim(ADOQueryTemp.fieldbyname('BFMoney').AsString);
    if Trim(FMoney)='' then Exit;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select isnull(sum(BFQty),0) KgQty from JYOrder_BG_KPFee where BGID='''+Trim(FBGID)+'''');
      sql.Add(' and PSXH=(select Max(PSXH) from JYOrder_BG_KPFee where BGID='''+Trim(FBGID)+''')');
      open;
    end;
    FKgQty:=Trim(ADOQueryTemp.fieldbyname('KgQty').AsString);
    if Trim(FKgQty)<>'' then
    begin
      if StrToFloat(FKgQty)>0 then
      begin
        Edit1.Text:=FloatToStr(SSWR(StrToFloat(FMoney)*1.00/StrToFloat(FKgQty)));
      end;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select isnull(sum(BFMQty),0) MQty from JYOrder_BG_KPFee where BGID='''+Trim(FBGID)+'''');
      sql.Add(' and PSXH=(select Max(PSXH) from JYOrder_BG_KPFee where BGID='''+Trim(FBGID)+''')');
      open;
    end;
    FMQty:=Trim(ADOQueryTemp.fieldbyname('MQty').AsString);
    if Trim(FMQty)<>'' then
    begin
      if StrToFloat(FMQty)>0 then
      begin
        Edit2.Text:=FloatToStr(SSWR(StrToFloat(FMoney)*1.00/StrToFloat(FMQty)));
      end;
    end;
  end;


end;

procedure TfrmBGJCKMXInput.HuoYuanPlaceBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='HuoYuanPlace';
      flagname:='货源地';
      if ShowModal=1 then
      begin
        //if Application.MessageBox('确定要更改货源地吗？','提示',32+4)<>IDYES then Exit;
        begin
          HuoYuanPlace.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          {with Self.ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update JYOrder_BG Set HuoYuanPlace='''+Trim(HuoYuanPlace.Text)+''',EditTime=Getdate(),Editer='''+Trim(DName)+'''');
            sql.Add(' where BGID='''+Trim(FBGID)+'''');
            ExecSQL;
          end;}
        end;

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBGJCKMXInput.ChuKouKABtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ChuKouKA';
      flagname:='出口口岸';
      if ShowModal=1 then
      begin
        //if Application.MessageBox('确定要更改货源地吗？','提示',32+4)<>IDYES then Exit;
        begin
          ChuKouKA.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
         { with Self.ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update JYOrder_BG Set ChuKouKA='''+Trim(ChuKouKA.Text)+''',EditTime=Getdate(),Editer='''+Trim(DName)+'''');
            sql.Add(' where BGID='''+Trim(FBGID)+'''');
            ExecSQL;
          end;}
        end;

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBGJCKMXInput.YunDiGBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YunDiG';
      flagname:='运抵国';
      if ShowModal=1 then
      begin
        //if Application.MessageBox('确定要更改货源地吗？','提示',32+4)<>IDYES then Exit;
        begin
          YunDiG.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          {with Self.ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update JYOrder_BG Set YunDiG='''+Trim(YunDiG.Text)+''',EditTime=Getdate(),Editer='''+Trim(DName)+'''');
            sql.Add(' where BGID='''+Trim(FBGID)+'''');
            ExecSQL;
          end; }
        end;

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBGJCKMXInput.ToolButton1Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  FInt:='99';
  if Trim(BCOrderNo.Text)='' then
  begin
    Application.MessageBox('订单号不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.IsEmpty then
  begin
    Application.MessageBox('费用明细不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('BFName',Null,[])=True then
  begin
    Application.MessageBox('费用名称不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('BFQty',Null,[])=True then
  begin
    Application.MessageBox('费用数量不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('BFPrice',Null,[])=True then
  begin
    Application.MessageBox('费用单价不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('BFUnit',Null,[])=True then
  begin
    Application.MessageBox('费用单位不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('BFMoney',Null,[])=True then
  begin
    Application.MessageBox('费用金额不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet6.IsEmpty 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 TfrmBGJCKMXInput.ComTaiTouBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GSTT';
      flagname:='报关单位';
      V1ZdyStr1.Visible:=True;
      V1ZdyStr2.Visible:=True;
      V1ZdyStr3.Visible:=True;
      V1ZdyStr4.Visible:=True;
      V1ZdyStr1.Caption:='简称';
      V1ZdyStr2.Caption:='电话';
      V1ZdyStr3.Caption:='传真';
      V1ZdyStr4.Caption:='地址';
      frmZDYHelp.Align:=alClient;
      if ShowModal=1 then
      begin
        ComTaiTou.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBGJCKMXInput.ChengJiaoTypeBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ChengJiaoType';
      flagname:='成交方式';
      if ShowModal=1 then
      begin
        ChengJiaoType.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
