unit U_BaoguanInPut;

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, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, dxDateTimeWheelPicker, dxGDIPlusClasses,
  Vcl.Menus, MovePanel, cxCheckBox, cxImage, cxDBEdit, Vcl.DBCtrls, U_BaseInput,
  System.ImageList, Vcl.ImgList, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdExplicitTLSClientServerBase, IdFTP, ShellAPI, cxSplitter,
  dxSkinWXI, dxScrollbarAnnotations;

type
  TfrmBaoguanInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_2: TDataSource;
    BG_Sub: TClientDataSet;
    Label10: TLabel;
    ConNo: TEdit;
    Label14: TLabel;
    CKS: TBtnEditC;
    cxgrdpmn1: TcxGridPopupMenu;
    pm_sub: TPopupMenu;
    N1: TMenuItem;
    DataSource1: TDataSource;
    ADOQueryImage: TADOQuery;
    DS_ImgHX: TDataSource;
    Qry_ImgHX: TADOQuery;
    IdFTP1: TIdFTP;
    N2: TMenuItem;
    ToolButton3: TToolButton;
    Panel1: TPanel;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    ToolBar3: TToolBar;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    DataSource2: TDataSource;
    Sub_CB: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOGY1: TADOQuery;
    ADOGY2: TADOQuery;
    ADOGY3: TADOQuery;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    VC_SCSCode: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    TV2Column8: TcxGridDBColumn;
    TV2Column9: TcxGridDBColumn;
    TV2Column10: TcxGridDBColumn;
    TV2Column11: TcxGridDBColumn;
    TV2Column12: TcxGridDBColumn;
    TV2Column13: TcxGridDBColumn;
    TV2Column14: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    FPNO: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    FPDate: TDateTimePicker;
    Label4: TLabel;
    ZMXZ: TBtnEditC;
    Label3: TLabel;
    Label5: TLabel;
    PONO: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    HTDate: TDateTimePicker;
    Label8: TLabel;
    HB: TBtnEditC;
    Label11: TLabel;
    Label15: TLabel;
    LJKA: TBtnEditC;
    Label17: TLabel;
    ChuyunG: TBtnEditC;
    Label20: TLabel;
    BZType: TBtnEditC;
    Label21: TLabel;
    Label22: TLabel;
    MYType: TBtnEditC;
    Label24: TLabel;
    SBDate: TDateTimePicker;
    Label25: TLabel;
    BGTK: TBtnEditC;
    Label26: TLabel;
    Label27: TLabel;
    HYDL: TBtnEditC;
    Label28: TLabel;
    Label30: TLabel;
    CGS: TBtnEditC;
    Label32: TLabel;
    CustName: TBtnEditC;
    Label33: TLabel;
    Label35: TLabel;
    ZZMDG: TBtnEditC;
    DPNo: TEdit;
    HGCode: TBtnEditC;
    YunFee: TEdit;
    BANo: TEdit;
    BaoFee: TEdit;
    Note: TEdit;
    TDNo: TEdit;
    Label12: TLabel;
    KHDate: TDateTimePicker;
    Label13: TLabel;
    batchNo: TEdit;
    Label9: TLabel;
    HG: TBtnEditC;
    Label16: TLabel;
    YSType: TBtnEditC;
    Label19: TLabel;
    DYG: TBtnEditC;
    Label18: TLabel;
    MYG: TBtnEditC;
    Label36: TLabel;
    DaohuoG: TBtnEditC;
    Label23: TLabel;
    JHType: TBtnEditC;
    TV2Column15: TcxGridDBColumn;
    TV2Column16: TcxGridDBColumn;
    GPM_1: TcxGridPopupMenu;
    Label29: TLabel;
    Label31: TLabel;
    Label34: TLabel;
    notifier: TBtnEditC;
    MDGDL: TBtnEditC;
    chukouG: TBtnEditC;
    TV2Column17: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure CKSBtnDnClick(Sender: TObject);
    procedure v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SalesmanBtnUpClick(Sender: TObject);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
    procedure v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
    procedure QiYunBtnUpClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure Pic_Image1DblClick(Sender: TObject);
    procedure cxDBImage1DblClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ExchangeRateKeyPress(Sender: TObject; var Key: Char);
    procedure ShippMentKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure YunFeeKeyPress(Sender: TObject; var Key: Char);
    procedure BaoFeeKeyPress(Sender: TObject; var Key: Char);
    procedure cxGridDBColumn2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure CKSBtnUpClick(Sender: TObject);
    procedure CustNameBtnUpClick(Sender: TObject);
//    procedure PBPriceKeyPress(Sender: TObject; var Key: Char);
//    procedure Button1Click(Sender: TObject);
  private
    FLeft, FTop: Integer;
    fuserName, Fstatus: string;

//    procedure InitGX();
    procedure JSMoney();
    procedure JSCB();
    procedure CBMX();
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function TestData(): Boolean;
    function SaveData(): Boolean;
    function SaveCB(): Boolean;
    function GetLSNo1(ADOQueryTmp: TADOQuery; // 取主键
      var mMaxNo: string; mFlag: string; mTable: string; mlen: Integer; mtype: Integer = 0): Boolean;
    procedure SetXH();
    procedure LookImage(FileName: string);
    { Private declarations }
  public
    CopyInt: Integer;
    PState: string;
    fFlileFlag: string;
    FBGMId: string;
    FAuthority, FGSTT: string;
    { Public declarations }
  end;

var
  frmBaoguanInPut: TfrmBaoguanInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_ClothInfoSel, U_PictureUpload,
  U_ZDYHelp_Img,U_ZDYHelp_KH;

{$R *.dfm}
function TfrmBaoguanInPut.GetLSNo1(ADOQueryTmp: TADOQuery; var mMaxNo: string; mFlag: string; mTable: string; mlen: Integer; mtype: Integer = 0): Boolean;
begin
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('exec Get_SY_MaxBH1 ');
      sql.Add(' ' + quotedStr(mFlag));
      sql.Add(',' + quotedStr(mTable));
      sql.Add(',' + intTostr(mlen));
      sql.Add(',' + intTostr(mtype));
      // ShowMessage(SQL.Text);
      Open;

      if Recordcount > 0 then
      begin
        mMaxNo := Trim(fieldByName('MaxBH').AsString);
        if mMaxNo <> '' then
          Result := true
        else
          Result := false;
      end
      else
      begin
        Result := false;
      end;

    end;
    if not Result then
      application.MessageBox(PWideChar('无法生成流水号(' + mFlag + ')'), '提示信息', MB_ICONINFORMATION);

  except
    Result := false;
    application.MessageBox(PWideChar('无法生成流水号(' + mFlag + ')'), '提示信息', MB_ICONINFORMATION);

  end;
end;

procedure TfrmBaoguanInPut.SetXH();
var
  i: Integer;
begin
  with BG_Sub do
  begin
    First;
    i := 1;
    while not Eof do
    begin
      with BG_Sub do
      begin
        Edit;
        FieldByName('XHNoTemp').Value := i;
        Post;
      end;
      i := i + 1;
      Next;
    end;
  end;

  with BG_Sub do
  begin
    BG_Sub.IndexFieldNames := '';
    First;
    i := 1;
    while not Eof do
    begin
      with BG_Sub do
      begin
        Edit;
        FieldByName('SerialNo').Value := FieldByName('XHNoTemp').AsInteger;
        Post;
      end;
      i := i + 1;
      Next;
    end;
    BG_Sub.IndexFieldNames := 'SerialNo';
  end;
end;

procedure TfrmBaoguanInPut.ShippMentKeyPress(Sender: TObject; var Key: Char);
begin
  if not (((Key >= '0') and (Key <= '9')) or (Key = '.') or (Key = #8)) then
  begin
    Key := #0;
  end
  else if (pos('.', TEdit(Sender).text) <> 0) then
  begin
    if Key = '.' then
    begin
      Key := #0;
    end;
  end;

end;



//procedure TfrmSalesConInPut.InitGX();
//var
//  i: Integer;
//  xh: string;
//begin
//  if BG_Sub.IsEmpty then
//    exit;
//  with ADOQuery1 do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('select * from BS_Cloth_Glide  A  ');
//    sql.Add('left join Bs_Cloth_Info B on A.BCIID=B.BCIID');
//    sql.Add('where B.C_Code =' + quotedstr((BG_Sub.fieldbyname('C_Code').AsString)));
//    open;
//  end;
//  i := 1;
//  xh := '1';
////  if ADOQuery1.ISEmpty = False then
////  begin
//
//  while i <= 3 do
//  begin
//    with ADOQuery2 do
//    begin
//      close;
//      sql.Clear;
//      sql.Add('select  A.* from (select X.ProcessName+''-''+X.ProcessTypeName AS GX,X.BCIID,X.GlideIdx from BS_Cloth_Glide X) A  ');
//      sql.Add('left join Bs_Cloth_Info B on A.BCIID=B.BCIID');
//      sql.Add('where B.C_Code =' + quotedstr((BG_Sub.fieldbyname('C_Code').AsString)));
//      sql.Add('and A.GlideIdx =''' + Trim(xh) + '''');
////        showmessage(SQL.text);
//      open;
//    end;
//
//    case i of
//      1:
//        begin
//          if Trim(ADOQuery2.fieldbyname('GX').AsString) <> '' then
//          begin
//            Label36.Caption := Trim(ADOQuery2.fieldbyname('GX').AsString) + '加工费:';
//          end
//          else
//          begin
//            Label36.Caption := '工艺1加工费：';
//          end;
//
//        end;
//      2:
//        begin
//          if Trim(ADOQuery2.fieldbyname('GX').AsString) <> '' then
//          begin
//            Label37.Caption := Trim(ADOQuery2.fieldbyname('GX').AsString) + '加工费:';
//          end
//          else
//          begin
//            Label37.Caption := '工艺2加工费：';
//          end;
//        end;
//      3:
//        begin
//          if Trim(ADOQuery2.fieldbyname('GX').AsString) <> '' then
//          begin
//            Label38.Caption := Trim(ADOQuery2.fieldbyname('GX').AsString) + '加工费:';
//          end
//          else
//          begin
//            Label38.Caption := '工艺3加工费：';
//          end;
//        end;
//    end;
//
//    i := i + 1;
//    xh := IntToStr(i);
////    end;
//  end;
//
//end;

procedure TfrmBaoguanInPut.JSMoney();
var
  FConQty, FConPrice, FC_Money: Double;
begin
  with BG_Sub do
  begin
    FConQty := Fieldbyname('ConQty').AsFloat;
    FConPrice := Fieldbyname('ConPrice').AsFloat;
  end;
  //计算金额，保留2位小数
  FC_Money := RoundFloat(FConQty * FConPrice, 2);
  with BG_Sub do
  begin
    Edit;
    FieldByName('ConTotalAmount').Value := FC_Money;
    Post;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmBaoguanInPut.BaoFeeKeyPress(Sender: TObject; var Key: Char);
begin
  if not (((Key >= '0') and (Key <= '9')) or (Key = '.') or (Key = #8)) then
  begin
    Key := #0;
  end
  else if (pos('.', TEdit(Sender).text) <> 0) then
  begin
    if Key = '.' then
    begin
      Key := #0;
    end;
  end;

end;

procedure TfrmBaoguanInPut.CBMX();
begin
  with ADOQuery2 do
  begin
    close;
    sql.Clear;
    sql.Add('select COUNT(A.BCIID) S from BS_Cloth_Glide A  ');
    sql.Add('left join Bs_Cloth_Info B on B.BCIID =A.BCIID');
    sql.Add('where B.C_Code =' + quotedstr((BG_Sub.fieldbyname('C_Code').AsString)));
    open;
  end;
  //查询工艺1
  with ADOGY1 do
  begin
    close;
    sql.Clear;
    sql.Add('select ISNULL(A.ProcessName, '''')+ISNULL(''_''+A.ProcessTypeName, '''') GY from BS_Cloth_Glide A   ');
    sql.Add('left join Bs_Cloth_Info B on B.BCIID =A.BCIID');
    sql.Add('where B.C_Code =' + quotedstr((BG_Sub.fieldbyname('C_Code').AsString)));
    sql.Add('and A.GlideIdx=1 ');
    open;
  end;
  //查询工艺2
  with ADOGY2 do
  begin
    close;
    sql.Clear;
    sql.Add('select ISNULL(A.ProcessName, '''')+ISNULL(''_''+A.ProcessTypeName, '''') GY from BS_Cloth_Glide A   ');
    sql.Add('left join Bs_Cloth_Info B on B.BCIID =A.BCIID');
    sql.Add('where B.C_Code =' + quotedstr((BG_Sub.fieldbyname('C_Code').AsString)));
    sql.Add('and A.GlideIdx=2 ');
    open;
  end;
  //查询工艺3
  with ADOGY3 do
  begin
    close;
    sql.Clear;
    sql.Add('select ISNULL(A.ProcessName, '''')+ISNULL(''_''+A.ProcessTypeName, '''') GY from BS_Cloth_Glide A   ');
    sql.Add('left join Bs_Cloth_Info B on B.BCIID =A.BCIID');
    sql.Add('where B.C_Code =' + quotedstr((BG_Sub.fieldbyname('C_Code').AsString)));
    sql.Add('and A.GlideIdx=3 ');
    open;
  end;

  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from BS_Contract_Sub_CB ');
    sql.Add('where ConSid=' + quotedstr(trim(BG_Sub.fieldbyname('Consid').AsString)));
//    sql.Add(' order by SerialNo ');
//    ShowMessage(sql.text);
    Open;
  end;
  SCreateCDS(ADOQuery1, Sub_CB);
  SInitCDSData(ADOQuery1, Sub_CB);

  if ADOQuery1.IsEmpty then
  begin
    with Sub_CB do
    begin
      Sub_CB.EmptyDataSet;
      Append;
      FieldByName('SerialNo').Value := 1;
      FieldByName('ConSid').Value := Trim(BG_Sub.fieldbyname('ConSid').AsString);
      FieldByName('CB_Name').Value := '坯布价';
      FieldByName('CB_Price').Value := '0';
      post;
      Append;
      FieldByName('SerialNo').Value := 2;
      FieldByName('ConSid').Value := Trim(BG_Sub.fieldbyname('ConSid').AsString);
      FieldByName('CB_Name').Value := '缩率(%)';
      FieldByName('CB_Price').Value := '0';
      post;
      Append;
      FieldByName('SerialNo').Value := 3;
      FieldByName('ConSid').Value := Trim(BG_Sub.fieldbyname('ConSid').AsString);
      FieldByName('CB_Name').Value := '染费';
      FieldByName('CB_Price').Value := '0';
      post;
      Append;
      FieldByName('SerialNo').Value := 4;
      FieldByName('ConSid').Value := Trim(BG_Sub.fieldbyname('ConSid').AsString);
      FieldByName('CB_Name').Value := '米长';
      FieldByName('CB_Price').Value := '1';
      post;
      Append;
      FieldByName('SerialNo').Value := 5;
      FieldByName('ConSid').Value := Trim(BG_Sub.fieldbyname('ConSid').AsString);
      FieldByName('CB_Name').Value := '工艺缩率(%)';
      FieldByName('CB_Price').Value := '0';
      post;
      Append;
      FieldByName('SerialNo').Value := 6;
      FieldByName('ConSid').Value := Trim(BG_Sub.fieldbyname('ConSid').AsString);
      FieldByName('CB_Name').Value := '面布价';
      FieldByName('CB_Price').Value := '0';
      post;

      Append;
      FieldByName('SerialNo').Value := 7;
      FieldByName('ConSid').Value := Trim(BG_Sub.fieldbyname('ConSid').AsString);
      if ADOQuery2.fieldbyname('S').AsInteger > 0 then
      begin
        FieldByName('CB_Name').Value := Trim(ADOGY1.fieldbyname('GY').AsString);
      end
      else
      begin
        FieldByName('CB_Name').Value := '工艺1加工费';
      end;
      FieldByName('CB_Price').Value := '0';
      post;

      Append;
      FieldByName('SerialNo').Value := 8;
      FieldByName('ConSid').Value := Trim(BG_Sub.fieldbyname('ConSid').AsString);
      if ADOQuery2.fieldbyname('S').AsInteger > 1 then
      begin
        FieldByName('CB_Name').Value := Trim(ADOGY2.fieldbyname('GY').AsString);
      end
      else
      begin
        FieldByName('CB_Name').Value := '工艺2加工费';
      end;
      FieldByName('CB_Price').Value := '0';
      post;

      Append;
      FieldByName('SerialNo').Value := 9;
      FieldByName('ConSid').Value := Trim(BG_Sub.fieldbyname('ConSid').AsString);
      if ADOQuery2.fieldbyname('S').AsInteger > 2 then
      begin
        FieldByName('CB_Name').Value := Trim(ADOGY3.fieldbyname('GY').AsString);
      end
      else
      begin
        FieldByName('CB_Name').Value := '工艺3加工费';
      end;
      FieldByName('CB_Price').Value := '0';
      post;

      append;
      FieldByName('SerialNo').Value := 10;
      FieldByName('ConSid').Value := Trim(BG_Sub.fieldbyname('ConSid').AsString);
      FieldByName('CB_Name').Value := '包装费';
      FieldByName('CB_Price').Value := '0';
      post;

      Append;
      FieldByName('SerialNo').Value := 11;
      FieldByName('ConSid').Value := Trim(BG_Sub.fieldbyname('ConSid').AsString);
      FieldByName('CB_Name').Value := '汇率';
      FieldByName('CB_Price').Value := '1';
      post;

      Append;
      FieldByName('SerialNo').Value := 12;
      FieldByName('ConSid').Value := Trim(BG_Sub.fieldbyname('ConSid').AsString);
      FieldByName('CB_Name').Value := '运费';
      FieldByName('CB_Price').Value := '0';
      post;

      Append;
      FieldByName('SerialNo').Value := 13;
      FieldByName('ConSid').Value := Trim(BG_Sub.fieldbyname('ConSid').AsString);
      FieldByName('CB_Name').Value := '佣金(%)';
      FieldByName('CB_Price').Value := '0';
      post;

    end;
  end;
end;

procedure TfrmBaoguanInPut.JSCB();
var
  FLRL, FSYGCB: string;
  i: Integer;
  FPBPrice, Fsl, Frf, FMQty, FMBPrice, FGY1, FGY2, FGY3: Double;
  Fbzf, Fhl, Fyf, Fyj, FXSPrice, FYGCB, FLR, FGYSL: Double;
begin
  i := 1;
  with Sub_CB do
  begin
    First;
    while not Eof do
    begin
      case i of
        1:
          begin
            FPBPrice := Sub_CB.fieldbyname('CB_Price').Asfloat;
          end;
        2:
          begin
            Fsl := (100 - Sub_CB.fieldbyname('CB_Price').Asfloat) / 100;
          end;
        3:
          begin
            Frf := Sub_CB.fieldbyname('CB_Price').Asfloat;
          end;
        4:
          begin
            FMQty := Sub_CB.fieldbyname('CB_Price').Asfloat;
          end;
        5:
          begin
            FGYSL := (100 - Sub_CB.fieldbyname('CB_Price').Asfloat) / 100;
          end;
        6:
          begin
            FMBPrice := Sub_CB.fieldbyname('CB_Price').Asfloat;
          end;
        7:
          begin
            FGY1 := Sub_CB.fieldbyname('CB_Price').Asfloat;
          end;
        8:
          begin
            FGY2 := Sub_CB.fieldbyname('CB_Price').Asfloat;
          end;
        9:
          begin
            FGY3 := Sub_CB.fieldbyname('CB_Price').Asfloat;
          end;
        10:
          begin
            Fbzf := Sub_CB.fieldbyname('CB_Price').Asfloat;
          end;
        11:
          begin
            Fhl := Sub_CB.fieldbyname('CB_Price').Asfloat;
          end;
        12:
          begin
            Fyf := Sub_CB.fieldbyname('CB_Price').Asfloat;
          end;
        13:
          begin
            Fyj := (100 - Sub_CB.fieldbyname('CB_Price').Asfloat) / 100;
          end;

      end;
      i := i + 1;
      Next;
    end;
  end;

  FXSPrice := BG_Sub.fieldbyname('ConPrice').Asfloat;
  if FPBPrice = 0 then
  begin
    Application.MessageBox('坯布价不能为0!', '提示', 0);
    Exit;
  end;
  if Fsl = 0 then
  begin
    Application.MessageBox('缩率不能为0!', '提示', 0);
    Exit;
  end;
  if FGYSL = 0 then
  begin
    Application.MessageBox('工艺缩率不能为0!', '提示', 0);
    Exit;
  end;
  if FMQty = 0 then
  begin
    Application.MessageBox('米长不能为0!', '提示', 0);
    Exit;
  end;
  if Fhl = 0 then
  begin
    Application.MessageBox('汇率不能为0!', '提示', 0);
    Exit;
  end;
  if Fyj = 0 then
  begin
    Application.MessageBox('佣金不能为0!', '提示', 0);
    Exit;
  end;

  FYGCB := RoundFloat((((((FPBPrice / Fsl + Frf) / FMQty + FMBPrice + FGY1 + FGY2 + FGY3) / FGYSL + Fbzf) / Fhl + Fyf) / Fyj), 2);
  FLR := RoundFloat((FXSPrice - FYGCB) / FYGCB, 3) * 100;

  FSYGCB := floatToStr(FYGCB);
  FLRL := floatToStr(FLR) + '%';

//  showmessage('预估成本：'+TRIM(FSYGCB)+'利润率：'+tRIM(FLRL)+'是否要保存！');
  if Application.MessageBox(PChar('预估成本：' + TRIM(FSYGCB) + '利润率：' + tRIM(FLRL) + '是否要保存！'), '提示', 32 + 4) <> IDYES then
    Exit;
  if SaveCb() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
  end;
//  Sub_CB.post;

  with BG_Sub do
  begin
    Edit;
    FieldByName('Estimated').Value := FloatToStr(FYGCB);
    FieldByName('ProfitRate').Value := FLRL;
    Post;
  end;

  if SaveData() then
  begin
    Application.MessageBox('预估成本保存成功！', '提示', 0);
  end;
end;
//begin
//
//

////  tv1.Controller.EditingController.ShowEdit();
//end;

function TfrmBaoguanInPut.TestData(): Boolean;
begin
  try
//    if Trim(ConNo.Text) = '' then
//      raise Exception.Create('合同号不能为空！');
    if Trim(CKS.Text) = '' then
      raise Exception.Create('出口商不能为空！');
    if Trim(FPNo.Text) = '' then
      raise Exception.Create('发票号不能为空！');
    if Trim(ChuyunG.Text) = '' then
      raise Exception.Create('出运港不能为空！');
    if Trim(DaohuoG.Text) = '' then
      raise Exception.Create('到货港不能为空！');
    if Trim(MYG.Text) = '' then
      raise Exception.Create('贸易国不能为空！');
    if Trim(DYG.Text) = '' then
      raise Exception.Create('抵运国不能为空！');
    if Trim(YSType.Text) = '' then
      raise Exception.Create('运输方式不能为空！');
    if Trim(MYType.Text) = '' then
      raise Exception.Create('贸易方式不能为空！');
    if Trim(YunFee.Text) = '' then
      raise Exception.Create('运费不能为空！');
    if Trim(BaoFee.Text) = '' then
      raise Exception.Create('保费不能为空！');
    if Trim(CustName.Text) = '' then
      raise Exception.Create('客户名称不能为空！');
//    if Trim(ZZMDG.Text) = '' then
//      raise Exception.Create('最终目的国不能为空！');

    if BG_Sub.IsEmpty then
      raise Exception.Create('明细不能为空！');
//    if BG_Sub.Locate('C_Code', null, []) then
//      raise Exception.Create('产品编号不能为空！');
    if BG_Sub.Locate('C_Name', null, []) then
      raise Exception.Create('品名不能为空！');
    if BG_Sub.Locate('C_EName', '', []) then
      raise Exception.Create('英文品名不能为空！');

    if BG_Sub.Locate('C_Composition', null, []) then
      raise Exception.Create('成分不能为空！');
    if BG_Sub.Locate('ZZFF', null, []) then
      raise Exception.Create('织造方法不能为空！');
//    if BG_Sub.Locate('RZType', '', []) then
//      raise Exception.Create('染整方式不能为空！');

    if BG_Sub.Locate('C_Width', null, []) then
      raise Exception.Create('门幅不能为空！');
    if BG_Sub.Locate('brand', null, []) then
      raise Exception.Create('品牌不能为空！');
    if BG_Sub.Locate('manufacturer', '', []) then
      raise Exception.Create('生产厂商不能为空！');

    if BG_Sub.Locate('HSNO', null, []) then
      raise Exception.Create('HSNO不能为空！');
    if BG_Sub.Locate('BGQty', null, []) then
      raise Exception.Create('数量不能为空！');
    if BG_Sub.Locate('BGUnit', '', []) then
      raise Exception.Create('单位不能为空！');

    if BG_Sub.Locate('BGPrice', null, []) then
      raise Exception.Create('单价不能为空！');
    if BG_Sub.Locate('BGMoney', null, []) then
      raise Exception.Create('金额不能为空！');
    if BG_Sub.Locate('BZQty', Null, []) then
      raise Exception.Create('包装数量不能为空！');

    if BG_Sub.Locate('BZUnit', null, []) then
      raise Exception.Create('包装单位不能为空！');
    if BG_Sub.Locate('Size', null, []) then
      raise Exception.Create('尺码不能为空！');
    if BG_Sub.Locate('GrossWeight', NULL, []) then
      raise Exception.Create('毛重不能为空！');
    if BG_Sub.Locate('NetWeight', NULL, []) then
      raise Exception.Create('净重不能为空！');
    if BG_Sub.Locate('BGUnit;ZSQty', VarArrayOf(['Kg', Null]), [loPartialKey]) then
    begin
      Application.MessageBox('数量单位为KG时，折算米数不能为空!', '提示', 0);
      Exit;
    end;

    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmBaoguanInPut.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmBaoguanInPut.InitData();
var
  maxno: string;
begin
  //刷新产品信息
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from BaoGuan_Sub ');
    sql.Add('where BGMId=''' + Trim(FBGMId) + '''');
//    sql.Add(' order by SerialNo ');
    Open;
  end;
  SCreateCDS(ADOTemp, BG_Sub);
  SInitCDSData(ADOTemp, BG_Sub);

   //刷新条款信息
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BaoGuan_Main where BGMId=''' + Trim(FBGMId) + '''');
    Open;
  end;
  SCSHData(ADOTemp, ScrollBox1, 2);

  Fstatus := Trim(ADOTemp.FieldByName('status').AsString);

  if PState = '新增' then
  begin
    FPDate.DateTime := SGetServerDateTime(ADOTemp);
    HTDate.DateTime := FPDate.DateTime;
    KHDate.DateTime := FPDate.DateTime;
    SBDate.DateTime := FPDate.DateTime;

  end;

  if PState = '复制' then
  begin

    PState := '新增';
    FBGMId := '';
    FPDate.DateTime := SGetServerDateTime(ADOTemp);
    with BG_Sub do
    begin
      First;
      while not Eof do
      begin
        if GetLSNo(ADOCmd, maxno, 'BGS', 'BaoGuan_Sub', 4, 1) = False then
        begin
          Application.MessageBox('取子流水号失败！', '提示', 0);
          Exit;
        end;
        Edit;
        FieldByName('BGMId').Value := '';
        FieldByName('BGSId').Value := maxno;
        Post;
        Next;
      end;
    end;
  end;

end;

procedure TfrmBaoguanInPut.N1Click(Sender: TObject);
var
  FValue, FFValue, FColumn, FFColumn, MHXSID: string;
  MXHNO: Integer;
begin
//  if Fstatus = '1' then
//  begin
//    application.MessageBox('合同已审核，无法操作！', '提示信息', 0);
//    exit;
//  end;
//  MXHNO := BG_Sub.FieldByName('XHNo').ASInteger;
//  FColumn := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
//  FValue := Trim(BG_Sub.fieldbyname(FColumn).AsString);
//  MHXSID := Trim(BG_Sub.fieldbyname('HXSID').AsString);
//  with BG_Sub do
//  begin
//    DisableControls;
//    First;
//    while not Eof do
//    begin
//      if FieldByName('SSel').AsBoolean then
//      begin
//        Edit;
//
//        if FValue = '' then
//        begin
//          BG_Sub.FieldByName(FColumn).Value := null;
//        end
//        else
//        begin
//          BG_Sub.FieldByName(FColumn).Value := FValue;
//        end;
//        if FColumn = 'C_HX' then
//        begin
//          BG_Sub.FieldByName('HXSID').Value := MHXSID;
//        end;
//        Post;
//        JSMoney();
//      end;
//      Next;
//    end;
//    EnableControls;
//  end;
//  BG_Sub.Locate('XHNO', MXHNO, []);
end;

procedure TfrmBaoguanInPut.N2Click(Sender: TObject);
var
  FValue, MC_Code, MC_Craft, MC_Name, MC_Spec, MC_CF, MC_Color, MC_MF, MC_KZ, MC_Note, MC_Unit, MGcHuaNo, MSuoLv, MC_EName: string;
  MXHNO: Integer;
begin
//  if Fstatus = '1' then
//  begin
//    application.MessageBox('合同已审核，无法操作！', '提示信息', 0);
//    exit;
//  end;
//
//  MXHNO := BG_Sub.FieldByName('XHNo').ASInteger;
//  MC_Code := BG_Sub.fieldbyname('C_Code').AsString;
//  MC_Craft := BG_Sub.fieldbyname('C_Craft').AsString;
//  MC_Name := BG_Sub.fieldbyname('C_Name').AsString;
//  MC_Spec := BG_Sub.fieldbyname('C_Spec').AsString;
//  MC_CF := BG_Sub.fieldbyname('C_CF').AsString;
//  MC_Color := BG_Sub.fieldbyname('C_Color').AsString;
//  MC_MF := BG_Sub.fieldbyname('C_MF').AsString;
//  MC_KZ := BG_Sub.fieldbyname('C_KZ').AsString;
//  MC_Note := BG_Sub.fieldbyname('C_Note').AsString;
//  MC_Unit := BG_Sub.fieldbyname('C_Unit').AsString;
//  MSuoLv := BG_Sub.fieldbyname('SuoLv').AsString;
//  MGcHuaNo := BG_Sub.fieldbyname('GcHuaNo').AsString;
//  MC_EName := BG_Sub.fieldbyname('C_EName').AsString;
//
//  with BG_Sub do
//  begin
//    DisableControls;
//    First;
//    while not Eof do
//    begin
//      if FieldByName('SSel').AsBoolean then
//      begin
//        Edit;
//        BG_Sub.fieldbyname('C_Code').Value := MC_Code;
//        BG_Sub.fieldbyname('C_Craft').Value := MC_Craft;
//        BG_Sub.fieldbyname('C_Name').Value := MC_Name;
//        BG_Sub.fieldbyname('C_Spec').Value := MC_Spec;
//        BG_Sub.fieldbyname('C_CF').Value := MC_CF;
//        BG_Sub.fieldbyname('C_Color').Value := MC_Color;
//        BG_Sub.fieldbyname('C_MF').Value := MC_MF;
//        BG_Sub.fieldbyname('C_KZ').Value := MC_KZ;
//        BG_Sub.fieldbyname('C_Note').Value := MC_Note;
//        BG_Sub.fieldbyname('C_Unit').Value := MC_Unit;
//        BG_Sub.fieldbyname('SuoLv').Value := MSuoLv;
//        BG_Sub.fieldbyname('GcHuaNo').Value := MGcHuaNo;
//        BG_Sub.fieldbyname('C_EName').Value := MC_EName;
//        Post;
//
//      end;
//      Next;
//    end;
//    EnableControls;
//  end;
//  BG_Sub.Locate('XHNO', MXHNO, []);
end;

//procedure TfrmSalesConInPut.PBPriceKeyPress(Sender: TObject; var Key: Char);
//begin
//  if not (((Key >= '0') and (Key <= '9')) or (Key = '.') or (Key = #8)) then
//  begin
//    Key := #0;
//  end
//  else if (pos('.', TEdit(Sender).text) <> 0) then
//  begin
//    if Key = '.' then
//    begin
//      Key := #0;
//    end;
//  end;
//
//end;

procedure TfrmBaoguanInPut.Pic_Image1DblClick(Sender: TObject);
begin
  if TcxDBImage(Sender).Picture.Height = 0 then
    exit;

  LookImage(ADOQueryImage.fieldbyname(TcxDBImage(Sender).Hint).asstring);
end;

procedure TfrmBaoguanInPut.LookImage(FileName: string);
var
  sFieldName: string;
begin
  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';
  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);
  sFieldName := sFieldName + '\' + trim(FileName);
  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin
    try
      IdFTP1.Get(fFlileFlag + '\' + Trim(FileName), sFieldName, true, false);
    except
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
end;

procedure TfrmBaoguanInPut.QiYunBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(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);
      MainType := '报关管理';
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

procedure TfrmBaoguanInPut.FormShow(Sender: TObject);
begin
//  inherited;
  readCxGrid(trim(self.Caption), Tv2, '报关管理');
  fFlileFlag := UserDataFlag + 'HX';
  InitData();

end;

function TfrmBaoguanInPut.SaveCB(): Boolean;
var
  MConSid, MCBName, MSCBID, MaxNo: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
////////////////            成本              ///////////////////
//
//   if not Sub_CB.IsEmpty then
//    begin
//      with Sub_cb do
//      begin
//        Sub_cb.First;
//        while not Eof do
//        begin
//          MConSid := Trim(Sub_cb.fieldbyname('ConSid').AsString);
//          MCBName := Trim(Sub_cb.fieldbyname('CB_Name').AsString);
//
//          with ADOQueryCmd do
//          begin
//            Close;
//            sql.Clear;
//            sql.Add('select * from BS_Contract_sub_CB where ConSid=' + quotedstr(Trim(MConSid)));
//            Sql.Add('and CB_Name=' + quotedstr(Trim(MCBName)));
//            Open;
//          end;
//          with ADOQueryCmd do
//          begin
//            if ADOQueryCmd.IsEmpty then
//            begin
//              Append;
//            end
//            else
//            begin
//              Edit;
//            end;
//
//            RTSetSaveDataCDS(ADOQueryCmd, Tv2, Sub_CB, 'BS_Contract_sub_CB', 0);
//            Post;
//          end;
//          with Sub_CB do
//          begin
//            Edit;
//            FieldByName('ConSid').Value := Trim(MConSid);
//            FieldByName('CB_Name').Value := Trim(MCBName);
//            Post;
//          end;
//          Sub_CB.Next;
//        end;
//      end;
//    end;
//
//
//    //////成本/////////////////////
//    if not Sub_CB.IsEmpty then
//    begin
    with Sub_CB do
    begin
      Sub_CB.First;
      while not Eof do
      begin
        MSCBID := Trim(Sub_CB.fieldbyname('SCBID').AsString);
        if Trim(MSCBID) = '' then
        begin
          if GetLSNo(ADOQuery1, MaxNo, 'CB', 'BS_Contract_Sub_CB', 4, 1) = False then
          begin
            ADOQuery1.Connection.RollbackTrans;
            Application.MessageBox('取号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          MaxNo := trim(MSCBID);
        end;

        with ADOQuery1 do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Bs_Contract_Sub_CB where SCBID=' + quotedstr(Trim(MSCBID)));
          Open;
        end;
        with ADOQuery1 do
        begin
          if Trim(MSCBID) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('SCBID').Value := Trim(MaxNo);
//            FieldByName('COID').Value := Trim(MSCBID);
          RTSetSaveDataCDS(ADOQuery1, Tv2, Sub_Cb, 'BS_Contract_Sub_CB', 0);
          Post;
        end;
        with sub_Cb do
        begin
          Edit;
          FieldByName('SCBID').Value := Trim(MaxNo);
          Post;
        end;
        Sub_CB.Next;
      end;
    end;
//    end;

    ADOQueryCmd.Connection.CommitTrans;
//    FCCID := Trim(MaxSubId);
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
    Result := false;
  end;
end;

function TfrmBaoguanInPut.SaveData(): Boolean;
var
  maxno, MQZ, MLS, MYR: string;
  fconNO, fmxType, fDate: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
////////////////       保存主表          //////////////////////
    if Trim(FBGMId) = '' then
    begin
      if GetLSNo(ADOTemp, maxno, 'BGM', 'BaoGuan_Main', 4, 1) = False then
        raise Exception.Create('取最大号失败！');

//      fDate := FormatDateTime('yy', SGetServerDate(ADOTemp));

    end
    else
    begin
      maxno := Trim(FBGMId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from BaoGuan_Main where BGMId=''' + Trim(FBGMId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FBGMId) = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('status').Value := '0';
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      FieldByName('BGMId').Value := Trim(maxno);
      RTSetsavedata(ADOCmd, 'BaoGuan_Main', ScrollBox1, 2);

      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BaoGuan_Main where FPNo=''' + Trim(FPNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
      raise Exception.Create('发票号重复！');

    FBGMId := Trim(maxno);
////////////////       保存主表          //////////////////////
///
////////////////       保存子表          //////////////////////
    with BG_Sub do
    begin
      First;
      while not Eof do
      begin
        maxno := Trim(BG_Sub.fieldbyname('BGSId').AsString);
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from BaoGuan_Sub where   BGSId=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if ADOCmd.IsEmpty then
            Append
          else
            Edit;
          RTSetSaveDataCDS(ADOCmd, Tv2, BG_Sub, 'BaoGuan_Sub', 0);
          FieldByName('BGMId').Value := Trim(FBGMId);
          FieldByName('BGSId').Value := Trim(maxno);

          Post;
        end;
        with BG_Sub do
        begin
          Edit;
          FieldByName('BGSId').Value := Trim(maxno);
          Post;
        end;
        Next;
      end;
    end;
////////////////       保存子表          //////////////////////
///
////////////////       更新合同          //////////////////////
//    with ADOCmd do
//    begin
//      close;
//      sql.Clear;
//      sql.Add('EXEC  P_Con_Up_PiQty   ');
//      sql.Add('@DataID=' + quotedstr(Trim(FConMId)));
//      execsql;
//    end;
////////////////       更新合同          //////////////////////

    ADOCmd.Connection.CommitTrans;
    Result := True;
  except

    ADOCmd.Connection.RollbackTrans;

    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

    Result := False;
  end;
end;

procedure TfrmBaoguanInPut.TBSaveClick(Sender: TObject);
begin
//  ConDate.SetFocus;

  if TestData() = false then
    exit;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
  end;
end;

procedure TfrmBaoguanInPut.v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName, FLRL: string;
  FConQty, FConPrice, FC_Money, FEstimated, FLR: Double;        //个数，单价，金额, 预估成本
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with BG_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    FConQty := Fieldbyname('BGQty').AsFloat;
    FConPrice := Fieldbyname('BGPrice').AsFloat;
  end;
  FC_Money := RoundFloat(FConQty * FConPrice, 2);

  with BG_Sub do
  begin
    Edit;
    FieldByName('BGQty').Value := FConQty;
    FieldByName('BGPrice').Value := FConPrice;
    FieldByName('BGMoney').Value := FC_Money;
    Post;
  end;

  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmBaoguanInPut.YunFeeKeyPress(Sender: TObject; var Key: Char);
begin
  if not (((Key >= '0') and (Key <= '9')) or (Key = '.') or (Key = #8)) then
  begin
    Key := #0;
  end
  else if (pos('.', TEdit(Sender).text) <> 0) then
  begin
    if Key = '.' then
    begin
      Key := #0;
    end;
  end;

end;

procedure TfrmBaoguanInPut.ToolButton4Click(Sender: TObject);
var
  i: Integer;
  maxno: string;
begin
  if GetLSNo(ADOCmd, maxno, 'BGS', 'BaoGuan_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;

  i := BG_Sub.RecordCount;      //当前的dataset有多少行记录
  i := i + 1;
  CopyAddRow(Tv2, BG_Sub);
  with BG_Sub do
  begin
    Edit;
    FieldByName('SSEL').Value := FALSE;
    FieldByName('SerialNo').Value := i;
    FieldByName('BGSId').Value := maxno;
//    FieldByName('C_Color').Value := '';
//    FieldByName('ConQty').Value := null;
//    FieldByName('Estimated').Value := '';
//    FieldByName('ProfitRate').Value := '';
    Post;
  end;
  SetXH();
end;

procedure TfrmBaoguanInPut.ToolButton5Click(Sender: TObject);
begin
  if BG_Sub.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
//  while BG_Sub.Locate('SSel', True, []) do
//  begin
//    with ADOTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('select * from  Trade_Plan_Sub where ConSId=''' + Trim(BG_Sub.fieldbyname('ConSId').AsString) + '''');
//      open;
//    end;
//    if ADOTemp.IsEmpty = false then
//    begin
//      exit;
//      application.MessageBox('该明细已经在生产！', '提示信息');
//    end;
  if Trim(BG_Sub.fieldbyname('BGSId').AsString) <> '' then
  begin
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete BaoGuan_Sub where BGSId=''' + Trim(BG_Sub.fieldbyname('BGSId').AsString) + '''');
      ExecSQL;
    end;
//    end;
    BG_Sub.Delete;
  end;

  SetXH();
end;

procedure TfrmBaoguanInPut.ToolButton1Click(Sender: TObject);
begin
  if SaveCB() then
  begin
    Application.MessageBox('运算值保存成功!', '提示', 0);
    ModalResult := 1;
  end;

end;

procedure TfrmBaoguanInPut.ToolButton2Click(Sender: TObject);
begin
  JSCB();

end;

procedure TfrmBaoguanInPut.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv2, '报关管理');
end;

procedure TfrmBaoguanInPut.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin

  if PState = '成本预估' then
  begin
    CBMX();
  end;


//  TV2.DataController.Filter.Clear;
end;

procedure TfrmBaoguanInPut.cxDBImage1DblClick(Sender: TObject);
begin
  if TcxDBImage(Sender).Picture.Height = 0 then
    exit;

  LookImage(Qry_ImgHX.fieldbyname(TcxDBImage(Sender).Hint).asstring);
end;

procedure TfrmBaoguanInPut.cxGridDBColumn2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'BGName';
      flagname := '报关品名';

      if ShowModal = 1 then
      begin
        with Self.BG_Sub do
        begin
          Edit;
          FieldByName('C_Name').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select Top 1 * from BaoGuan_Sub where C_Name=''' + Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString) + '''');
          sql.add(' and isnull(HSNO,'''')<>'''' ');
          sql.Add(' order by SFillTime desc');
          Open;
        end;
        with Self.BG_Sub do
        begin
          Edit;
          FieldByName('HSNO').Value := Trim(ADOQueryTemp.fieldbyname('HSNO').asstring);
          FieldByName('C_EName').Value := Trim(ADOQueryTemp.fieldbyname('C_EName').asstring);
          FieldByName('C_Composition').Value := Trim(ADOQueryTemp.fieldbyname('C_Composition').asstring);
          FieldByName('ZZFF').Value := Trim(ADOQueryTemp.fieldbyname('ZZFF').asstring);
          FieldByName('RZType').Value := Trim(ADOQueryTemp.fieldbyname('RZType').asstring);
          FieldByName('BGUnit').Value := Trim(ADOQueryTemp.fieldbyname('BGUnit').asstring);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmBaoguanInPut.ExchangeRateKeyPress(Sender: TObject; var Key: Char);
begin
  if not (((Key >= '0') and (Key <= '9')) or (Key = '.') or (Key = #8)) then
  begin
    Key := #0;
  end
  else if (pos('.', TEdit(Sender).text) <> 0) then
  begin
    if Key = '.' then
    begin
      Key := #0;
    end;
  end;
end;

//procedure TfrmSalesConInPut.Button1Click(Sender: TObject);
//var
//  FPBPrice, Fsl, Frf, FMQty, FGY1, FGY2, FGY3: Double;
//  Fbzf, Fhl, Fyf, Fyj, FXSPrice, FYGCB, FLR: Double;
//begin
//  FPBPrice := 0;
//  Fsl := 0;
//  Frf := 0;
//  FMQty := 0;
//  FGY1 := 0;
//  FGY2 := 0;
//  FGY3 := 0;
//  Fbzf := 0;
//  Fhl := 0;
//  Fyf := 0;
//  Fyj := 0;
//  FXSPrice := 0;
//  if GY1Price.text <> '' then
//  begin
//    FGY1 := StrToFloat(GY1Price.text);
//  end;
//  if GY2Price.text <> '' then
//  begin
//    FGY2 := StrToFloat(GY2Price.text);
//  end;
//  if GY3Price.text <> '' then
//  begin
//    FGY3 := StrToFloat(GY3Price.text);
//  end;
//  if PBPrice.text <> '' then
//  begin
//    FPBPrice := StrToFloat(PBPrice.text);
//  end
//   else
//  begin
//    Application.MessageBox('坯布价格不能为空!', '提示', 0);
//    Exit;
//  end;
//  if suolv.text <> '' then
//  begin
//    Fsl := StrToFloat(suolv.text);
//  end
//  else
//  begin
//
//    Application.MessageBox('缩率不能为空!', '提示', 0);
//    Exit;
//
//  end;
//  if RPrice.text <> '' then
//  begin
//    Frf := StrToFloat(RPrice.text);
//  end;
//
//  if MQty.text <> '' then
//  begin
//    FMQty := StrToFloat(MQty.text);
//  end;
//   if PackPrice.text <> '' then
//  begin
//    Fbzf := StrToFloat(PackPrice.text);
//  end;
//   if exchange.text <> '' then
//  begin
//    Fhl := StrToFloat(exchange.text);
//  end;
//   if freight.text <> '' then
//  begin
//    Fyf := StrToFloat(freight.text);
//  end;
//
//  if brokerage.text <> '' then
//  begin
//    Fyj := StrToFloat(brokerage.text);
//  end
//  else
//  begin
//    Application.MessageBox('佣金不能为空!', '提示', 0);
//    Exit;
//  end;
//
//  if conPrice.text <> '' then
//  begin
//    FXSPrice := StrToFloat(conPrice.text);
//  end;
//
//
//  FYGCB := RoundFloat(((((FPBPrice / Fsl + Frf) / FMQty + FGY1 + FGY2 + FGY3 + Fbzf) / Fhl + Fyf) / Fyj), 2);
//  FLR := RoundFloat((FXSPrice - FYGCB) / FYGCB, 2)*100;
//
//  Estimated.text := FloatToStr(FYGCB);
//  ProfitRate.text := FloatToStr(FLR)+'%';
//
//   if Application.MessageBox('是否要保存！', '提示', 32 + 4) <> IDYES then
//    Exit;
//
//  with ADOQuery1 do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add(' Update BS_Contract_Sub Set PBPrice=''' + Trim(PBPrice.text) + ''' ');
//    sql.Add(',suolv=''' + Trim(suolv.text) + ''',RPrice=''' + Trim(RPrice.text) + ''' ');
//    sql.Add(',MQty=''' + Trim(MQty.text) + ''',GY1Price=''' + Trim(GY1Price.text) + ''' ');
//    sql.Add(',GY2Price=''' + Trim(GY2Price.text) + ''',GY3Price=''' + Trim(GY3Price.text) + ''' ');
//    sql.Add(',PackPrice=''' + Trim(PackPrice.text) + ''',exchange=''' + Trim(exchange.text) + ''' ');
//    sql.Add(',freight=''' + Trim(freight.text) + ''',brokerage=''' + Trim(brokerage.text) + ''' ');
//    sql.Add(',Estimated=''' + Trim(Estimated.text) + ''',profitrate=''' + Trim(profitrate.text) + ''' ');
//    sql.Add(' where ConSid=''' + Trim(BG_Sub.fieldbyname('ConSid').AsString) + ''' ');
//
//    ExecSQL;
//  end;
//end;

procedure TfrmBaoguanInPut.CKSBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmBaoguanInPut.CKSBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp_Img := TfrmZDYHelp_Img.Create(Application);
    with frmZDYHelp_Img do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      MainType := '报关管理';
      if ShowModal = 1 then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select Top 1 * from BaoGuan_Main where CKS=''' + Trim(ClientDataSet1.fieldbyname('ZDYName').AsString) + '''');
          sql.Add(' order by FillTime desc');
          Open;
        end;
        DPNO.Text := ADOQueryTemp.fieldbyname('DPNO').ASString;
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        HGCode.Text := Trim(ClientDataSet1.fieldbyname('Note3').AsString);
      end;
    end;
  finally
    frmZDYHelp_Img.Free;
  end;
end;

procedure TfrmBaoguanInPut.CustNameBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp_KH := TfrmZDYHelp_KH.Create(Application);
    with frmZDYHelp_KH do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      MainType := '报关管理';
      fnote1:=True;
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        PONO.Text := Trim(ClientDataSet1.fieldbyname('Note1').AsString);
      end;
    end;
  finally
    frmZDYHelp_KH.Free;
  end;
end;
//begin
//  try
//    frmCompanySel := TfrmCompanySel.Create(Application);
//    with frmCompanySel do
//    begin
//      FCoType := '客户';
////      if Self.FAuthority = '录入' then
////        FAuthority := '理单业务';
//      if ShowModal = 1 then
//      begin
//
//        self.CustName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoName').AsString);
//        self.PoNO.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
//      end;
//    end;
//
//  finally
//    frmCompanySel.Free;
//  end;

//end;

procedure TfrmBaoguanInPut.v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then
      begin
        BG_Sub.Edit;
        BG_Sub.fieldbyname('PRTCodeName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        BG_Sub.fieldbyname('PRTCode').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        BG_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  TV2.Controller.EditingController.ShowEdit();
end;

procedure TfrmBaoguanInPut.v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'C_MF';
      flagname := '门幅信息';
      if ShowModal = 1 then
      begin
        BG_Sub.Edit;
        BG_Sub.fieldbyname('C_MF').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   BG_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        BG_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  TV2.Controller.EditingController.ShowEdit();
end;

procedure TfrmBaoguanInPut.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'C_KZ';
      flagname := '克重信息';
      if ShowModal = 1 then
      begin
        BG_Sub.Edit;
        BG_Sub.fieldbyname('C_KZ').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   BG_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        BG_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  TV2.Controller.EditingController.ShowEdit();
end;

procedure TfrmBaoguanInPut.v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  MCYNo: string;
begin
  frmClothInfoSel := TfrmClothInfoSel.Create(self);
  with frmClothInfoSel do
  begin
    if showmodal = 1 then
    begin
      BG_Sub.Edit;
      BG_Sub.fieldbyname('C_Code').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Code').asstring;

      BG_Sub.fieldbyname('C_Name').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Name').asstring;
      BG_Sub.fieldbyname('C_EName').Value := frmClothInfoSel.CDS_1.fieldbyname('C_EName').asstring;
      BG_Sub.fieldbyname('C_Spec').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Spec').asstring;
      BG_Sub.fieldbyname('C_Composition').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Composition').asstring;
      BG_Sub.fieldbyname('C_Color').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Color').asstring;
      BG_Sub.fieldbyname('C_Width').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Width').asstring;
      BG_Sub.fieldbyname('C_GramWeight').Value := frmClothInfoSel.CDS_1.fieldbyname('C_GramWeight').asstring;
      BG_Sub.Post;
    end;
    free;
  end;

  TV2.Controller.EditingController.ShowEdit();
end;

procedure TfrmBaoguanInPut.SalesmanBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(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
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmBaoguanInPut.v1Column15PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with BG_Sub do
  begin
    Edit;
    FieldByName('YongJinPrice').Value := mvalue;
    FieldByName('YongJin').Value := FieldByName('YongJinPrice').AsFloat * FieldByName('PRTOrderQty').AsFloat;
    Post;
  end;
end;

end.

