unit U_BGFeeInPut;

interface

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

type
  TfrmBGFeeInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox11: TScrollBox;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    v1PRTPrice: TcxGridDBColumn;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    ScrollBox1: TScrollBox;
    TiDanDate: TDateTimePicker;
    Label7: TLabel;
    TiDanNo: TEdit;
    Label3: TLabel;
    Label10: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label15: TLabel;
    BMNo: TEdit;
    YuLuDanDate: TDateTimePicker;
    FromPlace: TBtnEditC;
    ToPlace: TBtnEditC;
    ZhuangXiangDate: TDateTimePicker;
    ChuanDate: TDateTimePicker;
    ChuanGS: TBtnEditC;
    ChuanNo: TEdit;
    XiangType: TBtnEditC;
    Label16: TLabel;
    YunDiG: TBtnEditC;
    Label17: TLabel;
    ChengJiaoType: TBtnEditC;
    Label11: TLabel;
    YuTuiDanDate: TDateTimePicker;
    Label19: TLabel;
    JiaoTuiDanDate: TDateTimePicker;
    TiDanType: TComboBox;
    Note: TRichEdit;
    Panel1: TPanel;
    ToolBar3: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Column1: TcxGridDBColumn;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu2: TcxGridPopupMenu;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure FromPlaceBtnUpClick(Sender: TObject);
    procedure v1Column4PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxGridDBColumn1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxGridDBColumn2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    procedure GetBtnCText(Sender: TObject);
    { Private declarations }
  public
    PState,CopyInt:Integer;
    FMainId,FFMainId:String;
    FXS:Integer;
    { Public declarations }
  end;

var
  frmBGFeeInPut: TfrmBGFeeInPut;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_BGLDManage;

{$R *.dfm}

procedure TfrmBGFeeInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('报关费用录入',Tv1,'报关费用');
end;

procedure TfrmBGFeeInPut.InitData();
begin
  ZhuangXiangDate.Date:=SGetServerDate(ADOTemp);
  ChuanDate.Date:=ZhuangXiangDate.Date;
  YuLuDanDate.Date:=ZhuangXiangDate.Date;
  TiDanDate.Date:=ZhuangXiangDate.Date;
  YuTuiDanDate.Date:=ZhuangXiangDate.Date;
  JiaoTuiDanDate.Date:=ZhuangXiangDate.Date;
  ZhuangXiangDate.Checked:=False;
  ChuanDate.Checked:=False;
  YuLuDanDate.Checked:=False;
  TiDanDate.Checked:=False;
  YuTuiDanDate.Checked:=False;
  JiaoTuiDanDate.Checked:=False;
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from BGFee_Sub ');
    if PState=1 then
    begin
      sql.Add('where BMId='''+Trim(FMainId)+'''');
    end;
    if PState=0 then
    begin
      sql.Add(' where 1<>1');
    end;
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BGFee_Main where BMId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,ScrollBox1,2);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BGFee_HuoDai where BMID='''+Trim(FMainId)+'''');
    open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet2);
  SInitCDSData20(ADOQuery1,ClientDataSet2);
end;

procedure TfrmBGFeeInPut.ZDYHelp(FButn:TcxButtonEdit;LType:string);
var
  FType,ZDYName,FText:String;
begin
end;

procedure TfrmBGFeeInPut.FormShow(Sender: TObject);
begin
  readCxGrid('报关费用录入',Tv1,'报关费用');
  InitData();
end;

function TfrmBGFeeInPut.SaveData():Boolean;
var
  maxno,maxsubno,maxsubnoBH:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'BM','BGFee_Main',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取主表最大号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from BGFee_Main where BMId='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('BMId').Value:=Trim(maxno);
      RTSetsavedata(ADOCmd,'BGFee_Main',ScrollBox1,2);
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
     // FieldByName('Note').Value:=Trim(Note.Text);
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BGFee_Main where BMNo='''+Trim(BMNo.Text)+'''');
      Open;
    end;
    if ADOCmd.RecordCount>1 then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('提单运号重复!','提示',0);
      Exit;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate BGFee_Main Set Filler='''+Trim(DName)+'''');
      sql.Add(' where BMId='''+Trim(maxno)+'''');
      ExecSQL;
    end;

    ///保存子表
    
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('BSId').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxsubno,'BS','BGFee_Sub',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxsubno:=Trim(Order_Sub.fieldbyname('BSId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from BGFee_Sub where BMID='''+Trim(maxno)+'''');
          sql.Add(' and BSId='''+Trim(maxsubno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('BSId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('BMID').Value:=Trim(maxno);
          FieldByName('BSId').Value:=Trim(maxsubno);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'BGFee_Sub',0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('BSId').Value:=Trim(maxsubno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    //保存费用子表
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        if Trim(ClientDataSet2.fieldbyname('BHId').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxsubnoBH,'BH','BGFee_HuoDai',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxsubnoBH:=Trim(ClientDataSet2.fieldbyname('BHId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from BGFee_HuoDai where BHID='''+Trim(maxsubnoBH)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(ClientDataSet2.fieldbyname('BHID').AsString)='' then
            Append
          else
            Edit;
          FieldByName('BMID').Value:=Trim(maxno);
          FieldByName('BHID').Value:=Trim(maxsubnoBH);
          RTSetSaveDataCDS(ADOCmd,Tv2,ClientDataSet2,'BGFee_HuoDai',0);
          Post;
        end;
        ClientDataSet2.Edit;
        ClientDataSet2.FieldByName('BHID').Value:=Trim(maxsubnoBH);
        //Order_Sub.Post;
        Next;
      end;
    end;

    ADOCmd.Connection.CommitTrans;
    FMainId:=Trim(maxno);
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;



procedure TfrmBGFeeInPut.TBSaveClick(Sender: TObject);
var
  FReal:Double;
begin
  ToolBar1.SetFocus;
  if Trim(BMNo.Text)='' then
  begin
    Application.MessageBox('提单运号不能为空！','提示',0);
    Exit;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet2.IsEmpty then
  begin
    Application.MessageBox('费用明细不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
  end;
end;

procedure TfrmBGFeeInPut.ToolButton1Click(Sender: TObject);
begin
  CopyAddRow(Tv1,Order_Sub);
end;

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

procedure TfrmBGFeeInPut.CustomerNoNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text:='';
  TBtnEditC(Sender).TxtCode:='';
end;

procedure TfrmBGFeeInPut.FromPlaceBtnUpClick(Sender: TObject);
begin
  GetBtnCText(Sender);
end;
procedure TfrmBGFeeInPut.GetBtnCText(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TBtnEditC(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if ShowModal=1 then
      begin
        TBtnEditC(Sender).Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBGFeeInPut.v1Column4PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmBGLDManage:=TfrmBGLDManage.Create(Application);
    with frmBGLDManage do
    begin
      FSel:='99';
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('OrderNo').Value:=Trim(frmBGLDManage.Order_Main.fieldbyname('OrderNo').AsString);
          FieldByName('BGName').Value:=Trim(frmBGLDManage.CDS_19.fieldbyname('BGName').AsString);
          FieldByName('HSCode').Value:=Trim(frmBGLDManage.CDS_BGName.fieldbyname('HSCode').AsString);
          FieldByName('PS').Value:=frmBGLDManage.CDS_BGName.fieldbyname('CangKuPS').Value;
          FieldByName('JZQty').Value:=frmBGLDManage.CDS_19.fieldbyname('NQkgQty').Value;
          FieldByName('MiQty').Value:=frmBGLDManage.CDS_19.fieldbyname('NQMQty').Value;
          FieldByName('BGMoney').Value:=frmBGLDManage.CDS_19.fieldbyname('BGMoney').Value;
          //Post;
        end;
      end;
    end;
  finally
    frmBGLDManage.Free;
  end;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where OrderNo='''+Trim(Order_Sub.fieldbyname('OrderNo').AsString)+'''');
    Open;
  end;
  with Order_Sub do
  begin
    Edit;
    FieldByName('BGCF').Value:=Trim(ADOTemp.fieldbyname('MPRTCF').AsString);
    //Post;
  end;
end;

procedure TfrmBGFeeInPut.ToolButton3Click(Sender: TObject);
begin
  with ClientDataSet2 do
  begin
    Append;

    Post;
  end;
end;

procedure TfrmBGFeeInPut.cxGridDBColumn1PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='HDName';
      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
        with Self .ClientDataSet2 do
        begin
          Edit;
          FieldByName('HDName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          FieldByName('HDLXPerson').Value:=Trim(ClientDataSet1.fieldbyname('ZdyStr1').AsString);
          FieldByName('HDLXFS').Value:=Trim(ClientDataSet1.fieldbyname('ZdyStr2').AsString);
          FieldByName('HDBankName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyStr3').AsString);
          FieldByName('HDBankNo').Value:=Trim(ClientDataSet1.fieldbyname('ZdyStr4').AsString);
        end;

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

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

end.
