unit U_BaoGuanInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, ComCtrls, ToolWin,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxButtonEdit, cxTextEdit, StdCtrls, BtnEdit,
  ExtCtrls, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle,
  dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary,
  dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin,
  dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue;

type
  TfrmBaoGuanInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column5: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column3: TcxGridDBColumn;
    Panel1: TPanel;
    ToolBar2: TToolBar;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    Label13: TLabel;
    A4FPNO: TEdit;
    Label4: TLabel;
    A6PONO: TEdit;
    Label5: TLabel;
    BGTAITOU: TBtnEditA;
    Label8: TLabel;
    days: TEdit;
    Label9: TLabel;
    DCNO: TBtnEditA;
    B6ChuYunGang: TBtnEditA;
    B7DaoHuoGang: TBtnEditA;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    SHFS: TBtnEditA;
    Label19: TLabel;
    Label2: TLabel;
    OrdConPrcNote: TBtnEditA;
    Label18: TLabel;
    F2YunFee: TEdit;
    Label22: TLabel;
    F3BaoFee: TEdit;
    v1Column1: TcxGridDBColumn;
    VCC6BGPrice: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    Label27: TLabel;
    KHName: TBtnEditA;
    Label28: TLabel;
    ZMXingZhi: TBtnEditA;
    Label30: TLabel;
    HTDate: TDateTimePicker;
    v1YSKeZhong: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    JSBZ: TComboBox;
    Label31: TLabel;
    Label1: TLabel;
    ZhuMaiTou: TMemo;
    Label3: TLabel;
    SellNOTE: TMemo;
    NOTE: TMemo;
    Label6: TLabel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    YWY: TComboBox;
    khcode: TEdit;
    Tv1Column7: TcxGridDBColumn;
    Label7: TLabel;
    chuandate: TDateTimePicker;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    ToolButton4: TToolButton;
    TOCOUNTRY: TBtnEditA;
    Label10: TLabel;
    VCKHPrice: TcxGridDBColumn;
    Label11: TLabel;
    YWZB: TComboBox;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column17PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1ZZJGouPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column12PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TCXLJClick(Sender: TObject);
    procedure B6ChuYunGangBtnClick(Sender: TObject);
    procedure YWYDropDown(Sender: TObject);
    procedure KHNameBtnClick(Sender: TObject);
    procedure DCNOBtnClick(Sender: TObject);
    procedure Tv1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column12PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column9PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column7PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column15PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure YWZBDropDown(Sender: TObject);
    procedure SHFSBtnClick(Sender: TObject);
    procedure YWYChange(Sender: TObject);
    procedure YWYChange1();
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId, canshu3: string;
    CopyStr: string;
  end;

var
  frmBaoGuanInPut: TfrmBaoGuanInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_ZdyAttachment, U_ProductOrderList_Sel,
  U_DCDList_Sel, U_ZdyAttachGYS, U_ZDYHelp4;

{$R *.dfm}

procedure TfrmBaoGuanInPut.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmBaoGuanInPut.YWYChange1();
begin
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.add('select udept from SY_User where username=' + Quotedstr(trim(YWY.Text)));
    open;
  end;
  YWZB.Text := ADOQueryTemp.fieldbyname('udept').AsString;
end;

function TfrmBaoGuanInPut.SaveCKData(): Boolean;
var
  FJMID, Maxno, MaxSubNo, FSCID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_BaoGuan_Main where BGID=''' + Trim(FBCId) + '''');
      Open;
    end;
    FBCId := Trim(ADOQueryTemp.fieldbyname('BGID').AsString);
    if Trim(FBCId) = '' then
    begin
      if GetLSNo(ADOQueryCmd, Maxno, 'BG', 'JYOrder_BaoGuan_Main', 3, 1) = False then
      begin
        Result := False;
        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_BaoGuan_Main where BGID=''' + Trim(Maxno) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FBCId) = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillerCode').Value := Trim(DCode);
//        FieldByName('status').Value := Trim(DCode);
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditerCode').Value := Trim(DCode);
        FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
      end;
      FieldByName('BGID').Value := Trim(Maxno);
      RTSetsavedata(ADOQueryCmd, 'JYOrder_BaoGuan_Main', Panel1, 1);
      RTSetsavedata(ADOQueryCmd, 'JYOrder_BaoGuan_Main', Panel1, 2);
      Post;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_BaoGuan_Main where A4FPNO=''' + Trim(A4FPNO.Text) + ''' and Valid=''Y'' ');
      Open;
    end;
    if ADOQueryTemp.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('此发票号已经被使用，不能保存!', '提示', 0);
      Exit;
    end;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_BaoGuan_Sub where BSID=''' + Trim(CDS_Sub.fieldbyname('BSID').AsString) + '''');
          Open;
        end;
        FSCID := Trim(ADOQueryTemp.fieldbyname('BSID').AsString);
        if Trim(FSCID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, MaxSubNo, 'BS', 'JYOrder_BaoGuan_Sub', 3, 1) = False then
          begin
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取报关资料子编号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          MaxSubNo := Trim(FSCID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_BaoGuan_Sub where BSID=''' + Trim(MaxSubNo) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FSCID) = '' then
          begin
            Append;
            FieldByName('SFiller').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('SEditer').Value := Trim(DName);
            FieldByName('SEditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          FieldByName('BGID').Value := Trim(Maxno);
          FieldByName('BSID').Value := Trim(MaxSubNo);
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'JYOrder_BaoGuan_Sub', 2);
          Post;
        end;
        Edit;
        FieldByName('BSID').Value := Trim(MaxSubNo);
        FieldByName('BGID').Value := Trim(Maxno);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JYOrder_BaoGuan_Main Set ');
      sql.Add(' C7BGMoneyHZ=(select Sum(C7BGMoney) from JYOrder_BaoGuan_Sub A where A.BGID=JYOrder_BaoGuan_Main.BGID and A.SValid=''Y'' )');
      sql.Add(',E1BZQtyHZ=(select Sum(E1BZQty) from JYOrder_BaoGuan_Sub A where A.BGID=JYOrder_BaoGuan_Main.BGID and A.SValid=''Y'' )');
      sql.Add(',E2ChiMaQtyHZ=(select Sum(E2ChiMaQty) from JYOrder_BaoGuan_Sub A where A.BGID=JYOrder_BaoGuan_Main.BGID and A.SValid=''Y'' )');
      sql.Add(',E3MaoZHZ=(select Sum(E3MaoZ) from JYOrder_BaoGuan_Sub A where A.BGID=JYOrder_BaoGuan_Main.BGID and A.SValid=''Y'' )');
      sql.Add(',E4JingZHZ=(select Sum(E4JingZ) from JYOrder_BaoGuan_Sub A where A.BGID=JYOrder_BaoGuan_Main.BGID and A.SValid=''Y'' )');
      sql.Add(' where BGID=''' + Trim(Maxno) + '''');
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JYOrder_BaoGuan_Sub Set ');
      SQL.Add('C7BGMoney=C6BGPrice*C4BGQty');
      sql.Add(' where BGID=''' + Trim(Maxno) + '''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    FBCId := Trim(Maxno);
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;

end;

procedure TfrmBaoGuanInPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('报关资料录入YD', Tv1, '报关管理12');
  Close;
end;

procedure TfrmBaoGuanInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('报关资料录入YD', Tv1, '报关管理12');
  if canshu3 = '业务员' then
  begin
    VCC6BGPrice.Visible := False;
    VCC6BGPrice.Hidden := True;
  end
  else
  begin
    VCC6BGPrice.Visible := True;
    VCC6BGPrice.Hidden := False;
  end;
  if canshu3 = '单证' then
  begin
    VCKHPrice.Visible := False;
    VCKHPrice.Hidden := True;
  end
  else
  begin
    VCKHPrice.Visible := True;
    VCKHPrice.Hidden := False;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from JYOrder_BaoGuan_Main A');
    sql.Add(' where BGID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCSHDataNew(ADOQueryTemp, Panel1, 1);
  SCSHDataNew(ADOQueryTemp, Panel1, 2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from JYOrder_BaoGuan_Sub A');
    sql.Add(' where BGID=''' + Trim(FBCId) + '''');
    sql.Add(' and SValid=''Y'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Sub);
  SInitCDSData20(ADOQueryTemp, CDS_Sub);
  if CopyStr = '99' then
  begin
    FBCId := '';
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('BGID').Value := Null;
        FieldByName('BSID').Value := Null;
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;

    SHFS.Text := '';
  end;
  if Trim(FBCId) = '' then
  begin
//    YWY.Text := Trim(DName);
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add(' select A.* ');
//      sql.Add(' from SY_USER A');
//      sql.Add(' where USERNAME=' + QuotedStr(Trim(DName)));
//      Open;
//    end;
//    YWZB.Text := Trim(ADOQueryTemp.fieldbyname('UDEPT').AsString);
    HTDate.Date := SGetServerDate(ADOQueryTemp);
    chuandate.Date := SGetServerDate(ADOQueryTemp);
  end;

end;

procedure TfrmBaoGuanInPut.TBSaveClick(Sender: TObject);
var
  FReal: Double;
  i: Integer;
begin
//  with Panel1 do
//  begin
//    for i:=0 to ControlCount-1 do
//    begin
//      if Controls[i].Tag=1 then
//      begin
//        if Controls[i] is TLabel then continue;
//        if Controls[i].Tag<>1 then  continue;
//        if Controls[i] is TEdit then
//        begin
//          if Trim(TEdit(Controls[i]).Text)='' then
//          begin
//            Application.MessageBox('蓝色标注的信息为必填，请填写！','提示',0);
//            Exit;
//          end;
//        end;
//        if Controls[i] is TBtnEditA then
//        begin
//          if Trim(TBtnEditA(Controls[i]).Text)='' then
//          begin
//            Application.MessageBox('蓝色标注的信息为必填，请填写！','提示',0);
//            Exit;
//          end;
//        end;
//      end;
//    end;
//  end;

  if FBCId <> '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_BaoGuan_Main where BGID=''' + Trim(FBCId) + '''');
      Open;
    end;
    if ADOQueryTemp.fieldbyname('BGSTATUS').AsString = '√' then
    begin
      Application.MessageBox('已报关不允许修改!', '提示', 0);
      Exit;
    end;
  end;

  if ywzb.Text = '' then
  begin
    Application.MessageBox('业务组别不能为空!', '提示', 0);
    Exit;
  end;
  if SHFS.Text = '' then
  begin
    Application.MessageBox('收汇方式不能为空!', '提示', 0);
    Exit;
  end;

  if TryStrToFloat(F2YunFee.Text, FReal) = False then
  begin
    Application.MessageBox('运费非法数字!', '提示', 0);
    Exit;
  end;
  if TryStrToFloat(F3BaoFee.Text, FReal) = False then
  begin
    Application.MessageBox('保费非法数字!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('C3BGName', Null, []) then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(DCNO.Text) = '' then
  begin
    Application.MessageBox('订舱单不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('C4BGQty', Null, []) then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('C5BGUnit', Null, []) then
  begin
    Application.MessageBox('单位不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('C6BGPrice', Null, []) then
  begin
    Application.MessageBox('单价不能为空!', '提示', 0);
    Exit;
  end;
  with CDS_SUB do
  begin
    First;
    while not Eof do
    begin
      if (CDS_SUB.FieldByName('C6BGPrice').Value > 3.5) then
      begin
        if Application.MessageBox('报关单价大于3.5,是否属实？', '提示', 32 + 4) <> IDYES then
          Exit;
      end;

      if (CDS_SUB.FieldByName('C6BGPrice').Value <> 0) and (StrToFloatDef(CDS_SUB.FieldByName('C7BGMoney').AsString, 0) = 0) then
      begin
        Edit;
        FieldByName('C7BGMoney').Value := CDS_SUB.FieldByName('C6BGPrice').Value * CDS_SUB.FieldByName('C4BGQty').Value;
        Post;
      end;

      Next;
    end;
  end;
//  if CDS_Sub.Locate('C7BGMoney', Null, []) then
//  begin
//    Application.MessageBox('金额不能为空!', '提示', 0);
//    Exit;
//  end;
  {if CDS_Sub.Locate('E2ChiMaQty',Null,[]) then
  begin
    Application.MessageBox('尺码不能为空!','提示',0);
    Exit;
  end;}
//  if CDS_Sub.Locate('E4JingZ', Null, []) then
//  begin
//    Application.MessageBox('净重不能为空!', '提示', 0);
//    Exit;
//  end;

  ToolBar1.SetFocus;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmBaoGuanInPut.ToolButton2Click(Sender: TObject);
var
  i: Integer;
begin
  //CopyAddRowCDS(CDS_Sub);
  {with CDS_Sub do
  begin
    Edit;
    FieldByName('BGID').Value:=Null;
    FieldByName('BSID').Value:=Null;
    Post;
  end; }


  frmProductOrderList_Sel := TfrmProductOrderList_Sel.Create(self);
  with frmProductOrderList_Sel do
  begin
    if showmodal = 1 then
    begin
      while frmProductOrderList_Sel.Order_Main.Locate('SSel', True, []) do
      begin
        Self.CDS_Sub.Append;
        i := CDS_Sub.RecordCount;
        Self.CDS_Sub.FieldByName('XHInt').Value := i + 1;
        Self.CDS_Sub.FieldbyName('C6BGPrice').Value := 0;
//        Self.CDS_Sub.FieldbyName('Money').Value := 0;
        Self.CDS_Sub.fieldbyname('orderno').Value := Order_Main.fieldbyname('Orderno').asstring;
        Self.CDS_Sub.fieldbyname('FromMainId').Value := Order_Main.fieldbyname('MainId').asstring;
        Self.CDS_Sub.fieldbyname('C3BGNameEng').Value := Order_Main.fieldbyname('YWNAME').asstring;
        Self.CDS_Sub.fieldbyname('C3BGName').Value := Order_Main.fieldbyname('MPRTCODENAME').asstring;
        Self.CDS_Sub.fieldbyname('YSFuKuan').Value := Order_Main.fieldbyname('MPRTMF').asstring;
        Self.CDS_Sub.fieldbyname('YSKeZhong').Value := Order_Main.fieldbyname('MPRTKZ').asstring;
        Self.CDS_Sub.fieldbyname('YSChenFen').Value := Order_Main.fieldbyname('MPRTCF').asstring;
        Self.CDS_Sub.fieldbyname('C4BGQty').Value := Order_Main.fieldbyname('PRTORDERQTY').ASFLOAT;

        Self.CDS_Sub.fieldbyname('C5BGUnit').Value := Order_Main.fieldbyname('orderunit').asstring;

        Self.CDS_Sub.fieldbyname('ZZFF').Value := Order_Main.fieldbyname('BPBigType').asstring;
        Self.CDS_Sub.fieldbyname('ZDDATE').Value := Order_Main.fieldbyname('filltime').AsDateTime;
        Self.CDS_Sub.fieldbyname('GYLX').Value := Order_Main.fieldbyname('JGType').asstring;
        Self.CDS_Sub.fieldbyname('JSUNIT').Value := '包';
        Self.CDS_Sub.fieldbyname('WL').Value := '平纹';
        Self.CDS_Sub.fieldbyname('sj').Value := '是';
        Self.CDS_Sub.fieldbyname('GCNAME').Value := '绍兴市稳胜纺织品有限公司';
        Self.CDS_Sub.fieldbyname('GCDM').Value := '4';
        YWY.ItemIndex := YWY.Items.IndexOf(trim(Order_Main.fieldbyname('YWY').AsString));
        YWYChange1();
        Self.CDS_Sub.Post;
        frmProductOrderList_Sel.Order_Main.Delete;
      end;
    end;
    free;
  end;
end;

procedure TfrmBaoGuanInPut.ToolButton3Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('BSID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_BaoGuan_Sub Set SValid=''N'',SEditer=''' + Trim(DName) + ''',SEditTime=getdate() ');
      sql.Add(' where BSID=''' + Trim(CDS_Sub.fieldbyname('BSID').AsString) + '''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JYOrder_BaoGuan_Main Set ');
      sql.Add(' C7BGMoneyHZ=(select Sum(C7BGMoney) from JYOrder_BaoGuan_Sub A where A.BGID=JYOrder_BaoGuan_Main.BGID and A.SValid=''Y'' )');
      sql.Add(',E1BZQtyHZ=(select Sum(E1BZQty) from JYOrder_BaoGuan_Sub A where A.BGID=JYOrder_BaoGuan_Main.BGID and A.SValid=''Y'' )');
      sql.Add(',E2ChiMaQtyHZ=(select Sum(E2ChiMaQty) from JYOrder_BaoGuan_Sub A where A.BGID=JYOrder_BaoGuan_Main.BGID and A.SValid=''Y'' )');
      sql.Add(',E3MaoZHZ=(select Sum(E3MaoZ) from JYOrder_BaoGuan_Sub A where A.BGID=JYOrder_BaoGuan_Main.BGID and A.SValid=''Y'' )');
      sql.Add(',E4JingZHZ=(select Sum(E4JingZ) from JYOrder_BaoGuan_Sub A where A.BGID=JYOrder_BaoGuan_Main.BGID and A.SValid=''Y'' )');
      sql.Add(' where BGID=''' + Trim(CDS_Sub.fieldbyname('BGID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_Sub.Delete;
end;

procedure TfrmBaoGuanInPut.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'C3BGName';
      flagname := '报关品名';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('C3BGName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select Top 1 * from JYOrder_BaoGuan_Sub where C3BGName=''' + Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString) + '''');
          sql.add(' and isnull(C2HSNO,'''')<>'''' ');
          sql.Add(' order by SFillTime desc');
          Open;
        end;
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('C2HSNO').Value := Trim(ADOQueryTemp.fieldbyname('C2HSNO').asstring);
          FieldByName('C3BGNameEng').Value := Trim(ADOQueryTemp.fieldbyname('C3BGNameEng').asstring);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBaoGuanInPut.v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YSChenFen';
      flagname := '成分含量';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('YSChenFen').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBaoGuanInPut.v1Column16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YSPinPai';
      flagname := '品牌';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('YSPinPai').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBaoGuanInPut.v1Column17PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YSShengChanShang';
      flagname := '生产商';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('YSShengChanShang').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBaoGuanInPut.v1Column3PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FName, FPrice, FQty, FBaoGangFee: string;
begin
  FName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FName).Value := mvalue;
    Post;
  end;
  FPrice := Trim(CDS_Sub.fieldbyname('C6BGPrice').AsString);
  FQty := Trim(CDS_Sub.fieldbyname('C4BGQty').AsString);
  if Trim(FPrice) = '' then
  begin
    FPrice := '0';
  end;
  if Trim(FQty) = '' then
  begin
    FQty := '0';
  end;
  with CDS_Sub do
  begin
    Edit;
    FieldByName('C7BGMoney').Value := RoundFloat(StrToFloat(FPrice) * StrToFloat(FQty), 2);
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmBaoGuanInPut.ToolButton1Click(Sender: TObject);
var
  FValue, FFValue, FColumn, FFColumn: string;
begin
  if CDS_Sub.IsEmpty then
    Exit;

  //FColumn:=tv1.Columns[Tv1.Controller.FocusedColumnIndex].DataBinding.FieldName;
  //FFColumn:=Tv1.Columns[Tv1.Controller.FocusedColumnIndex].Summary.GroupFormat;
  FColumn := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  FFColumn := Tv1.Controller.FocusedColumn.Summary.GroupFormat;
  FValue := Trim(CDS_Sub.fieldbyname(FColumn).AsString);

  if Trim(FFColumn) <> '' then
  begin
    FFValue := Trim(CDS_Sub.fieldbyname(FFColumn).AsString);
  end;
  if Trim(FColumn) = 'C7BGMoney' then
  begin
    Exit;
  end;

  with CDS_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      Edit;
      if FValue = '' then
      begin
        CDS_Sub.FieldByName(FColumn).Value := null;
      end
      else
      begin
        CDS_Sub.FieldByName(FColumn).Value := FValue;
      end;
      if Trim(FFColumn) <> '' then
      begin
        if FFValue = '' then
        begin
          CDS_Sub.FieldByName(FFColumn).Value := null;
        end
        else
        begin
          CDS_Sub.FieldByName(FFColumn).Value := FFValue;
        end;
      end;
      if (FColumn = 'C6BGPrice') or (FColumn = 'C4BGQty') then
      begin

        FieldbyName('C7BGMoney').Value := strtofloat(format('%.2f', [FieldByName('C6BGPrice').AsFloat * FieldByName('C4BGQty').ASFLOAT]));

       


//        CDS_Main.FieldByName('money').Value := CDS_Main.FieldByName('price').AsFloat * CDS_Main.FieldByName('qty').AsFloat;
      end;

      Post;
      Next;
    end;
    EnableControls;
  end;

end;

procedure TfrmBaoGuanInPut.ToolButton4Click(Sender: TObject);
var
  fsj: string;
begin
  if FBCId = '' then
    Exit;
  if Application.MessageBox('确定要执行审核操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JYOrder_BaoGuan_Main Set Status=''已审核'',ChkTime=getdate(),Chker=''' + Trim(DName) + '''');
      sql.Add(' where BGID=''' + Trim(FBCId) + '''');
      ExecSQL;
    end;
    Application.MessageBox('操作成功!', '提示', 0);
  except

    Application.MessageBox('操作异常!', '提示', 0);
  end;

end;

procedure TfrmBaoGuanInPut.v1ZZJGouPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'ZZJGou';
      flagname := '组织结构';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ZZJGou').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBaoGuanInPut.v1Column12PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'E5MaiTou';
      flagname := '唛头';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('E5MaiTou').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBaoGuanInPut.TCXLJClick(Sender: TObject);
begin
  if not Assigned(DataLink_DDMD) then
    DataLink_DDMD := TDataLink_DDMD.Create(Application);
  try
    with DataLink_DDMD.ADOLink do
    begin
      //if not Connected then
      begin
        Connected := false;
        ConnectionString := DConString;
        LoginPrompt := false;
        Connected := true;
      end;
    end;
  except
    application.MessageBox('数据库连接失败！', '错误', mb_Ok + MB_ICONERROR);
  end;
end;

procedure TfrmBaoGuanInPut.B6ChuYunGangBtnClick(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);
//         ShowMessage(flag);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if flag = 'TOCOUNTRY' then
      begin
        fnote := True;
        V1Note.Caption := '中文名称';
        V1Name.Caption := '英文名称';
      end
      else if (flag = 'LCTOPlace') or (flag = 'LCFromPlace') or (flag = 'YSFS') then
      begin
        fnote := True;
        V1Note.Caption := '备注';
        V1Name.Caption := '名称';
//        V1Name.Caption := '英文名称';
      end
      else
      begin
        fnote := false;
      end;
//      if (flag = 'B6ChuYunGang') or (flag = 'B7DaoHuoGang') or (flag = 'ZMXingZhi')  then
//      begin
//        fnote := True;
//        V1Note.Caption := '备注';
////        V1Name.Caption := '英文名称';
//      end
//      else
//      begin
//        fnote := false;
//      end;

      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmBaoGuanInPut.YWYDropDown(Sender: TObject);
begin
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('SELECT * FROM SY_User WHERE Udept LIKE ''%客服%'' ');
//
//    Open;
//  end;
//  YWY.Items.Clear;
//  while not ADOQueryTemp.eof do
//  begin
//    YWY.Items.Add(Trim(ADOQueryTemp.fieldbyname('username').AsString));
//    ADOQueryTemp.next;
//  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where type=''jhdywy'' ');

    Open;
  end;
  YWY.Items.Clear;
  while not ADOQueryTemp.eof do
  begin
    YWY.Items.Add(Trim(ADOQueryTemp.fieldbyname('zdyname').AsString));
    ADOQueryTemp.next;
  end;
end;

procedure TfrmBaoGuanInPut.KHNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin

      if ShowModal = 1 then
      begin
        self.khcode.Text := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
        self.KHName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmBaoGuanInPut.DCNOBtnClick(Sender: TObject);
begin
  try
    frmDCDList_Sel := TfrmDCDList_Sel.Create(Application);
    with frmDCDList_Sel do
    begin

      if ShowModal = 1 then
      begin
        self.TOCOUNTRY.Text := Trim(Order_Main.fieldbyname('TOCOUNTRY').AsString);
        self.DCNO.Text := Trim(Order_Main.fieldbyname('DCNO').AsString);
      end;
    end;
  finally
    frmDCDList_Sel.Free;
  end;
end;

procedure TfrmBaoGuanInPut.Tv1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := tfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin

      if ShowModal = 1 then
      begin
        with CDS_Sub do
        begin
          Edit;
          fieldbyname('gcdm').Value := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
          fieldbyname('gcname').Value := Trim(CDS_HZ.fieldbyname('CoName').AsString);
          fieldbyname('gcaddress').Value := Trim(CDS_HZ.fieldbyname('Coaddress').AsString);
        end;

      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmBaoGuanInPut.Tv1Column12PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'zzff';
      flagname := '织造方法';
      if ShowModal = 1 then
      begin
        with CDS_Sub do
        begin
          edit;
          fieldbyname('zzff').AsString := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;

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

procedure TfrmBaoGuanInPut.Tv1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'wenlu';
      flagname := '纹路';
      if ShowModal = 1 then
      begin
        with CDS_Sub do
        begin
          edit;
          fieldbyname('wl').AsString := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmBaoGuanInPut.Tv1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'GYLX';
      flagname := '工艺类型';
      if ShowModal = 1 then
      begin
        with CDS_Sub do
        begin
          edit;
          fieldbyname('gylx').AsString := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;

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

procedure TfrmBaoGuanInPut.Tv1Column9PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JSUNIT';
      flagname := '件数单位';
      if ShowModal = 1 then
      begin
        with CDS_Sub do
        begin
          edit;
          fieldbyname('JSUNIT').AsString := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;

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

procedure TfrmBaoGuanInPut.Tv1Column7PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FName, FPrice, FQty, FBaoGangFee: string;
begin
  FName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FName).Value := mvalue;
    Post;
  end;
end;

procedure TfrmBaoGuanInPut.Tv1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := tfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin

      if ShowModal = 1 then
      begin
        with CDS_Sub do
        begin
          Edit;
          fieldbyname('gcdm').Value := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
          fieldbyname('gcname').Value := Trim(CDS_HZ.fieldbyname('CoName').AsString);
          fieldbyname('gcaddress').Value := Trim(CDS_HZ.fieldbyname('Coaddress').AsString);
        end;

      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmBaoGuanInPut.v1Column15PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'BGMF';
      flagname := '门幅';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('YSFuKuan').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBaoGuanInPut.YWZBDropDown(Sender: TObject);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('SELECT * FROM (');
    sql.Add('SELECT DISTINCT UDEPT FROM SY_User WHERE Udept LIKE ''%客服%'')AA  ');
    SQL.Add(' ORDER BY CHARINDEX( UDEPT,N''客服一组,客服二组,客服三组,客服四组,客服五组,客服六组,客服七组,客服八组,客服九组,客服十组,客服十一组,客服十二组'')');

    Open;
  end;
  YWZB.Items.Clear;
  while not ADOQueryTemp.eof do
  begin
    YWZB.Items.Add(Trim(ADOQueryTemp.fieldbyname('UDEPT').AsString));
    ADOQueryTemp.next;
  end;
end;

procedure TfrmBaoGuanInPut.SHFSBtnClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp4 := TfrmZDYHelp4.Create(Application);
    with frmZDYHelp4 do
    begin

      flag := Copy(fsj, 1, FWZ - 1);
//         ShowMessage(flag);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      TBAdd.Visible := False;
      TBDel.Visible := False;
      TBEdit.Visible := False;
      TBSave.Visible := False;
      if flag = 'TOCOUNTRY' then
      begin
        fnote := True;
        V1Note.Caption := '中文名称';
        V1Name.Caption := '英文名称';
      end
      else if (flag = 'LCTOPlace') or (flag = 'LCFromPlace') or (flag = 'YSFS') then
      begin
        fnote := True;
        V1Note.Caption := '备注';
        V1Name.Caption := '名称';
//        V1Name.Caption := '英文名称';
      end
      else if (flag = 'SHFS') then
      begin
        fnote := True;
        V1Note.Caption := '天数';
        V1Name.Caption := '收汇方式';
//        V1Name.Caption := '英文名称';
      end
      else
      begin
        fnote := false;
      end;
//      if (flag = 'B6ChuYunGang') or (flag = 'B7DaoHuoGang') or (flag = 'ZMXingZhi')  then
//      begin
//        fnote := True;
//        V1Note.Caption := '备注';
////        V1Name.Caption := '英文名称';
//      end
//      else
//      begin
//        fnote := false;
//      end;

      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        if (flag = 'SHFS') then
        begin
          days.Text := Trim(ClientDataSet1.fieldbyname('note').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp4.Free;
  end;
end;

procedure TfrmBaoGuanInPut.YWYChange(Sender: TObject);
begin
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.add('select udept from SY_User where username=' + Quotedstr(trim(YWY.Text)));
    open;
  end;
  YWZB.Text := ADOQueryTemp.fieldbyname('udept').AsString;
//  YWZB.ItemIndex := YWZB.Items.IndexOf(trim(ADOQueryTemp.fieldbyname('udept').AsString));
end;

end.

