unit U_BGJCKMXGYSInput;

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

type
  TfrmBGJCKMXGYSInput = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Panel2: TPanel;
    ADOQueryTemp: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    ClientDataSet7: TClientDataSet;
    cxGridPopupMenu7: TcxGridPopupMenu;
    DataSource7: TDataSource;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    Panel3: TPanel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel4: TPanel;
    Panel5: TPanel;
    Label14: TLabel;
    Panel6: TPanel;
    ComTaiTou: TEdit;
    Label2: TLabel;
    BGDate: TDateTimePicker;
    v2Column1: TcxGridDBColumn;
    Label4: TLabel;
    EditCount: TEdit;
    CDS_BGName: TClientDataSet;
    cxGridPopupMenu5: TcxGridPopupMenu;
    DS_BGName: TDataSource;
    Label1: TLabel;
    OrderNo: TEdit;
    ClientDataSet3: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource3: TDataSource;
    cxGridPopupMenu3: TcxGridPopupMenu;
    ToolBar3: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    cxGrid6: TcxGrid;
    Tv6: TcxGridDBTableView;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridLevel6: TcxGridLevel;
    Tv6Column1: TcxGridDBColumn;
    Label8: TLabel;
    BCOrderNo: TEdit;
    v2Column2: TcxGridDBColumn;
    ClientDataSet6: TClientDataSet;
    cxGridPopupMenu6: TcxGridPopupMenu;
    DataSource6: TDataSource;
    Label12: TLabel;
    MLenStr: TEdit;
    cxGrid5: TcxGrid;
    TVBGName: TcxGridDBTableView;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    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;
    Panel7: TPanel;
    cxGrid7: TcxGrid;
    Tv7: TcxGridDBTableView;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    cxGrid4: TcxGrid;
    TvFee: TcxGridDBTableView;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    TvFeeColumn2: TcxGridDBColumn;
    TvFeeColumn1: TcxGridDBColumn;
    TvFeeColumn3: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    DS_Fee: TDataSource;
    cxGridPopupMenu4: TcxGridPopupMenu;
    CDS_Fee: TClientDataSet;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    ToolButton5: TToolButton;
    ClientDataSetJS: TClientDataSet;
    v6Column1: TcxGridDBColumn;
    ClientDataSetJS1: TClientDataSet;
    ToolButton6: TToolButton;
    Label3: TLabel;
    ChengJiaoType: TBtnEditA;
    Label5: TLabel;
    HaiYunFee: TEdit;
    Label6: TLabel;
    BaoFee: TEdit;
    Label7: TLabel;
    Label9: TLabel;
    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 ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    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 ToolButton4Click(Sender: TObject);
    procedure v2Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
  private
    { Private declarations }
    function SaveData():Boolean;
    procedure InitGridMD();
  public
    { Public declarations }
    FOrderNo,FBGID,FBCID:String;
  end;

var
  frmBGJCKMXGYSInput: TfrmBGJCKMXGYSInput;

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

{$R *.dfm}

procedure TfrmBGJCKMXGYSInput.FormDestroy(Sender: TObject);
begin
  frmBGJCKMXGYSInput:=nil;
end;

procedure TfrmBGJCKMXGYSInput.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('tv2',Tv2,'报关明细');
  WriteCxGrid('tv6',Tv6,'报关明细');
  Close;
end;

procedure TfrmBGJCKMXGYSInput.FormShow(Sender: TObject);
begin
  ReadCxGrid('tv2',Tv2,'报关明细');
  ReadCxGrid('tv6',Tv6,'报关明细');
  BGDate.DateTime:=SGetServerDate(ADOQueryTemp);
  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_Main_MD_HZ where BGID='''+Trim(FBGID)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Fee);
  SInitCDSData20(ADOQueryTemp,CDS_Fee);
  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;
end;

procedure TfrmBGJCKMXGYSInput.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 TfrmBGJCKMXGYSInput.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;

procedure TfrmBGJCKMXGYSInput.ToolButton1Click(Sender: TObject);
begin
  with ClientDataSet2 do
  begin
    Append;
    if Trim(MLenStr.Text)<>'' then
    begin
      FieldByName('MLen').Value:=StrToFloat(MLenStr.Text);
    end;
    Post;
  end;
end;

procedure TfrmBGJCKMXGYSInput.ToolButton2Click(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then Exit;
  if Trim(ClientDataSet2.fieldbyname('BFID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_BG_KPFee where BFID='''+Trim(ClientDataSet2.fieldbyname('BFID').AsString)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet2.Delete;
end;
function TfrmBGJCKMXGYSInput.SaveData():Boolean;
var
  maxno,maxsubno,maxNQNo:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    ///保存主表
    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);
      Post;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_BG_CK where BCorderno='''+Trim(BCOrderNo.Text)+'''');
      sql.Add(' and Valid=''Y'' ');
      Open;
    end;
    if ADOQueryTemp.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('订单识别号重复!','提示',0);
      Exit;
    end;

    ///保存开票费用子表
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet2.fieldbyname('BFId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxsubno,'BF','JYOrder_BG_KPFee',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxsubno:=Trim(ClientDataSet2.fieldbyname('BFId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_BG_KPFee where BFId='''+Trim(maxsubno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet2.fieldbyname('BFId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('BGId').Value:=Trim(FBGID);
          FieldByName('BCId').Value:=Trim(maxno);
          FieldByName('BFId').Value:=Trim(maxsubno);
          RTSetSaveDataCDS(ADOQueryCmd,Tv2,ClientDataSet2,'JYOrder_BG_KPFee',0);
          FieldByName('FactoryNo').Value:=ClientDataSet2.fieldbyname('FactoryNo').Value;
          FieldByName('PSName').Value:=ClientDataSet2.fieldbyname('PSName').Value;
          Post;
        end;
        ClientDataSet2.Edit;
        ClientDataSet2.FieldByName('BFId').Value:=Trim(maxsubno);
        //Order_Sub.Post;
        Next;
      end;
    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);
          FieldByName('BCId').Value:=Trim(maxno);
          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;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmBGJCKMXGYSInput.TBSaveClick(Sender: TObject);
var
  FReal:Double;//a39978874
begin
  ToolBar1.SetFocus;
  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 SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;


end;

procedure TfrmBGJCKMXGYSInput.Tv7FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  InitGridMD();
end;
procedure TfrmBGJCKMXGYSInput.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 TfrmBGJCKMXGYSInput.cxPageControl1Change(Sender: TObject);
begin
  InitGridMD();
end;

procedure TfrmBGJCKMXGYSInput.Tv7DblClick(Sender: TObject);
begin
  with ClientDataSet2 do
  begin
    Append;
    FieldByName('FactoryName').Value:=ClientDataSet7.fieldbyname('FactoryName').Value;
    FieldByName('FactoryNo').Value:=ClientDataSet7.fieldbyname('FactoryNo').Value;
    FieldByName('PSName').Value:=ClientDataSet7.fieldbyname('PSName').Value;
    if Trim(ClientDataSet7.fieldbyname('BQUnit').AsString)<>'M' then
    begin
      FieldByName('BFQty').Value:=ClientDataSet7.fieldbyname('BQQty').Value;
      if Trim(MLenStr.Text)<>'' then
      begin
        FieldByName('BFMQty').Value:=ClientDataSet7.fieldbyname('BQQty').Value*strtofloat(MLenStr.Text);
      end;
    end else
    begin
      FieldByName('BFMQty').Value:=ClientDataSet7.fieldbyname('BQQty').Value;
    end;
    FieldByName('BFUnit').Value:='M';
    if Trim(MLenStr.Text)<>'' then
    begin
      FieldByName('MLen').Value:=StrToFloat(MLenStr.Text);
    end;
    Post;
  end;
end;

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

procedure TfrmBGJCKMXGYSInput.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 TfrmBGJCKMXGYSInput.cxGridDBColumn4PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FName,FPrice,FQty,FMQty,FMoney,FMLen: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;
  FQty:=Trim(ClientDataSet2.fieldbyname('BFQty').AsString);
  if Trim(FQty)='' then
  begin
    FQty:='0';
  end;
  FMLen:=Trim(ClientDataSet2.fieldbyname('MLen').AsString);
  if Trim(FMLen)='' then
  begin
    FMLen:='0';
  end;
  if (Trim(FName)='MLen') or (Trim(FName)='BFQty') then
  begin
    with ClientDataSet2 do
    begin
      Edit;
      FieldByName('BFMQty').Value:=StrToFloat(FQty)*StrToFloat(FMLen);
      Post;
    end;
  end;
  FPrice:=Trim(ClientDataSet2.fieldbyname('BFPrice').AsString);
  FQty:=Trim(ClientDataSet2.fieldbyname('BFQty').AsString);
  FMQty:=Trim(ClientDataSet2.fieldbyname('BFMQty').AsString);
  FMoney:=Trim(ClientDataSet2.fieldbyname('BFMoney').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 ClientDataSet2 do
  begin
    Edit;
    if Trim(FName)='BFPrice' then
    begin
      if Trim(ClientDataSet2.fieldbyname('BFUnit').AsString)='M' then
      begin
        FieldByName('BFMoney').Value:=StrToFloat(FPrice)*StrToFloat(FMQty);
      end else
      begin
        FieldByName('BFMoney').Value:=StrToFloat(FPrice)*StrToFloat(FQty);
      end;
    end else
    begin
      if Trim(ClientDataSet2.fieldbyname('BFUnit').AsString)='M' then
      begin
        if Trim(FMQty)<>'0' then
          FieldByName('BFPrice').Value:=StrToFloat(FMoney)*1.00/StrToFloat(FMQty)
        else
          FieldByName('BFPrice').Value:=0;
      end else
      begin
        if Trim(FQty)<>'0' then
          FieldByName('BFPrice').Value:=StrToFloat(FMoney)*1.00/StrToFloat(FQty)
        else
          FieldByName('BFPrice').Value:=0;
      end;
    end;
    Post;
  end;
end;

procedure TfrmBGJCKMXGYSInput.ToolButton4Click(Sender: TObject);
begin
  if ClientDataSet6.IsEmpty then Exit;
  if Trim(ClientDataSet6.fieldbyname('NQID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_BG_CKName_Qty where NQID='''+Trim(ClientDataSet6.fieldbyname('NQID').AsString)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet6.Delete;
end;

procedure TfrmBGJCKMXGYSInput.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 TfrmBGJCKMXGYSInput.ToolButton5Click(Sender: TObject);
var
  Fqty1,Fqty2:string;
  Qty1,Qty2:Double;
  i:Integer;
begin
  if ClientDataSet2.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_BG_KPFee where 1<>1');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSetJS);
  SInitCDSData20(ADOQueryTemp,ClientDataSetJS);
  SCreateCDS20(ADOQueryTemp,ClientDataSetJS1);
  SInitCDSData20(ADOQueryTemp,ClientDataSetJS1);
  ClientDataSet2.DisableControls;
  with ClientDataSet2 do
  begin
    First;
    while not Eof do
    begin
      with ClientDataSetJS do
      begin
        Append;
        Post;
      end;
      with ClientDataSetJS1 do
      begin
        Append;
        Post;
      end;
      for i:=0 to Tv2.ColumnCount-1 do
      begin
        with ClientDataSetJS do
        begin
          Edit;
          FieldByName(Tv2.Columns[i].DataBinding.FieldName).Value:=ClientDataSet2.fieldbyname(Tv2.Columns[i].DataBinding.FieldName).Value;
          Post;
        end;
        with ClientDataSetJS1 do
        begin
          Edit;
          FieldByName(Tv2.Columns[i].DataBinding.FieldName).Value:=ClientDataSet2.fieldbyname(Tv2.Columns[i].DataBinding.FieldName).Value;
          Post;
        end;
      end;
      Next;
    end;
  end;
  ClientDataSet2.EnableControls;
  ClientDataSet2.DisableControls;

  with ClientDataSet2 do
  begin
    First;
    while not Eof do
    begin
      if Trim(ClientDataSet2.FieldByName('PSXH').AsString)<>'' then
      begin
        if Trim(ClientDataSet2.FieldByName('PSXH').AsString)<>'1' then
        begin
          Qty1:=0;
          Qty2:=0;
          with ClientDataSetJS do
          begin
            First;
            while not Eof do
            begin
              if ClientDataSetJS.FieldByName('PSXH').AsInteger=ClientDataSet2.FieldByName('PSXH').AsInteger-1 then
              begin
                if Trim(ClientDataSetJS.fieldbyname('BFUnit').AsString)='M' then
                  Qty1:=Qty1+ ClientDataSetJS.FieldByName('BFMQty').AsFloat
                else
                  Qty1:=Qty1+ ClientDataSetJS.FieldByName('BFQty').AsFloat;
              end;
              Next;
            end;
          end;
          with ClientDataSetJS1 do
          begin
            First;
            while not Eof do
            begin
              if ClientDataSetJS1.FieldByName('PSXH').AsInteger=ClientDataSet2.FieldByName('PSXH').AsInteger then
              begin
                if Trim(ClientDataSetJS1.fieldbyname('BFUnit').AsString)='M' then
                  Qty2:=Qty2+ ClientDataSetJS1.FieldByName('BFMQty').AsFloat
                else
                  Qty2:=Qty2+ ClientDataSetJS1.FieldByName('BFQty').AsFloat;
              end;
              Next;
            end;
          end;
          Edit;
          FieldByName('PSSHQty').Value:=Qty1-Qty2;
          Post;
        end;
      end;
      Next;
    end;
  end;
  ClientDataSet2.EnableControls;
end;

procedure TfrmBGJCKMXGYSInput.ToolButton6Click(Sender: TObject);
begin
  if Trim(EditCount.Text)='1' then Exit;
  if Application.MessageBox('确定要执行复制操作吗？执行此操作后,之前的数据将作废!','提示',32+4)<>IDYES then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_BG_KPFee where BCID=');
    sql.Add('(select BCID from JYOrder_BG_CK CK where CK.BCOrderNo='''+Trim(BCOrderNo.Text)+''' and Valid=''Y'' )');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_BG_KPFee where BCID=');
      sql.Add('(select Top 1 BCID from JYOrder_BG_CK CK where CK.BCOrderNo='''+Trim(BCOrderNo.Text)+''' order by BCID desc )');
      Open;
    end;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
  with ClientDataSet2 do
  begin
    First;
    while not eof do
    begin
      Edit;
      FieldByName('BFID').Value:='';
      FieldByName('BCID').Value:='';
      FieldByName('BGID').Value:='';
      Post;
      next;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_BG_CKName_Qty where BCID=');
    sql.Add('(select BCID from JYOrder_BG_CK CK where CK.BCOrderNo='''+Trim(BCOrderNo.Text)+''' and Valid=''Y'' )');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_BG_CKName_Qty where BCID=');
      sql.Add('(select Top 1 BCID from JYOrder_BG_CK CK where CK.BCOrderNo='''+Trim(BCOrderNo.Text)+''' order by BCID desc )');
      Open;
    end;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet6);
  SInitCDSData20(ADOQueryTemp,ClientDataSet6);
  with ClientDataSet6 do
  begin
    First;
    while not eof do
    begin
      Edit;
      FieldByName('NQID').Value:='';
      FieldByName('BCID').Value:='';
      FieldByName('BGID').Value:='';
      Post;
      next;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_BG_CK Set Valid=''N'' where BCID=');
    sql.Add('(select BCID from JYOrder_BG_CK CK where CK.BCOrderNo='''+Trim(BCOrderNo.Text)+''' and Valid=''Y'' )');
    ExecSQL;
  end;
end;

end.
