unit U_YpBaoJiaMx;

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

type
  TfrmYpBaoJiaMx = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label4: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label13: TLabel;
    Label6: TLabel;
    Label20: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Label24: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    Label28: TLabel;
    BjNo: TEdit;
    Jhtime: TDateTimePicker;
    Bjtime: TDateTimePicker;
    CustomerName: TcxButtonEdit;
    GyNote: TEdit;
    BjCode: TcxButtonEdit;
    BjName: TEdit;
    BjMF: TEdit;
    BjKZ: TEdit;
    Qty: TEdit;
    BjZKS: TEdit;
    BJer: TcxButtonEdit;
    BJPhone: TEdit;
    ScrollBox2: TScrollBox;
    Label34: TLabel;
    Label35: TLabel;
    Label36: TLabel;
    Label37: TLabel;
    Label38: TLabel;
    Label39: TLabel;
    Label40: TLabel;
    Label50: TLabel;
    Label51: TLabel;
    Label52: TLabel;
    Label53: TLabel;
    Label54: TLabel;
    HclCost: TEdit;
    YfCost: TEdit;
    KfCost: TEdit;
    YwCost: TEdit;
    LxCost: TEdit;
    DjBzCost: TEdit;
    Price: TEdit;
    CbPrice: TEdit;
    SdCost: TEdit;
    Lr: TEdit;
    Lrd: TEdit;
    ZzCost: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column10: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column5: TcxGridDBColumn;
    Label5: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    BjNote: TMemo;
    HclPrice: TEdit;
    CustomerLevel: TEdit;
    BlKfCost: TEdit;
    BlLxCost: TEdit;
    BlYwCost: TEdit;
    BlSdCost: TEdit;
    Label12: TLabel;
    Chker: TEdit;
    Label14: TLabel;
    Chktime: TDateTimePicker;
    Label15: TLabel;
    Label16: TLabel;
    ZshCost: TEdit;
    BlZshCost: TEdit;
    Label17: TLabel;
    PpShCost: TEdit;
    BlPpShCost: TEdit;
    Label18: TLabel;
    CpBfCost: TEdit;
    BLCpBfCost: TEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ZzCostKeyPress(Sender: TObject; var Key: Char);
    procedure HclCostExit(Sender: TObject);
    procedure v1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure CustomerNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure BjZKSExit(Sender: TObject);
    procedure ScrollBox2Click(Sender: TObject);
    procedure ScrollBox1Click(Sender: TObject);
  private
    FXS: Integer;
    procedure InitData();
    procedure InitWlPb();
    procedure upWlPb();
    function RoundFloat(f: double; i: integer): double;
    procedure CBHJ();
    function Setstring(cString: string): string; 
    { Private declarations }
  public
    PState, PCopyInt: Integer;
    FMainId, FConNo: string;
    FFChaShu: string;
    { Public declarations }
  end;

var
  frmYpBaoJiaMx: TfrmYpBaoJiaMx;

implementation

uses
  U_DataLink, U_ZDYHelp, U_Fun, U_ZdyAttachment;

{$R *.dfm}
function TfrmYpBaoJiaMx.Setstring(cString: string): string;   {提取数字}
var
  i: integer;
  str: string;
begin
  str := '';
  for i := 1 to length(cString) do
  begin
    if Pos(cString[i], '0123456789.') <= 0 then
      break
    else
      case cString[i] of
        '.':
          str := str + '.';

        '0':
          str := str + '0';
        '1':
          str := str + '1';
        '2':
          str := str + '2';
        '3':
          str := str + '3';
        '4':
          str := str + '4';
        '5':
          str := str + '5';
        '6':
          str := str + '6';
        '7':
          str := str + '7';
        '8':
          str := str + '8';
        '9':
          str := str + '9';
      end;
  end;
  Setstring := str;
end;

procedure TfrmYpBaoJiaMx.upWlPb();
var
  MBiLi, MMmkz, MYlShb, MWlPrice, MWlMoney, MBjZKS: Double;
begin
  if Order_Sub.IsEmpty then
    Exit;
  MBjZKS := StrToFloatDef(BjZKS.Text, 0);
  with Order_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      MBiLi := fieldbyname('BiLi').AsFloat;
      MYlShb := fieldbyname('YlShb').AsFloat;
      MWlPrice := fieldbyname('WlPrice').AsFloat;
      MMmkz := RoundFloat(MBjZKS * MBiLi / 100, 2);
      MWlMoney := RoundFloat(MWlPrice * MMmkz / 1000 * (MYlShb + 100) / 100, 2);
      Edit;
      fieldbyname('Mmkz').Value := MMmkz;
      fieldbyname('WlMoney').Value := MWlMoney;
      Next;
    end;
    EnableControls;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmYpBaoJiaMx.InitWlPb();
begin
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from BaoJiaSub A ');
    sql.Add(' where A.BjMainID=(select max(BjMainID) from BaoJiaMain where BjCode=' + Quotedstr(Trim(BjCode.Text)) + ')');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOTemp, Order_Sub);
  SInitCDSData20(ADOTemp, Order_Sub);
end;

function TfrmYpBaoJiaMx.RoundFloat(f: double; i: integer): double;
var
  s: string;
  ef: extended;
begin
  s := '#.' + StringOfChar('0', i);
  ef := StrToFloat(FloatToStr(f)); //防止浮点运算的误差
  result := StrToFloat(FormatFloat(s, ef));
end;

procedure TfrmYpBaoJiaMx.CBHJ();
var
  MWlCost, MHclPrice, MZzCost, MHclCost, MDjBzCost, MKfCost, MBlKfCost, MYfCost, MLxCost, MBlLxCost, MYwCost, MBlYwCost, MSdCost, MBlSdCost, MBjZKS, MCbPrice, MPrice, MLr, MLrd: Double;
begin
  if TV1.DataController.Summary.FooterSummaryValues[0] <> NULL then
    MWlCost := TV1.DataController.Summary.FooterSummaryValues[0]
  else
    MWlCost := 0;
  MBjZKS := StrToFloatDef(BjZKS.Text, 0);
  MZzCost := StrToFloatDef(ZzCost.Text, 0);
  MHclPrice := StrToFloatDef(HclPrice.Text, 0);
  MDjBzCost := StrToFloatDef(DjBzCost.Text, 0);
  MPrice := StrToFloatDef(Price.Text, 0);
  MHclCost := RoundFloat(MBjZKS * MHclPrice / 1000, 2);
  HclCost.Text := FloatToStr(MHclCost);
  MBlKfCost := StrToFloatDef(BlKfCost.Text, 0);
   //开发
  MKfCost := RoundFloat((MWlCost + MZzCost + MHclCost + MDjBzCost) * MBlKfCost / 100, 2);
  KfCost.Text := FloatToStr(MKfCost);
   //运费
  MYfCost := StrToFloatDef(YfCost.Text, 0);
   //利息
  MBlLxCost := StrToFloatDef(BlLxCost.Text, 0);
  MLxCost := RoundFloat((MWlCost + MZzCost + MHclCost + MDjBzCost + MKfCost + MYfCost) * MBlLxCost / 100, 2);
  LxCost.Text := FloatToStr(MLxCost);
   //业务费
  MBlYwCost := StrToFloatDef(BlYwCost.Text, 0);
  MYwCost := RoundFloat((MWlCost + MZzCost + MHclCost + MDjBzCost + MKfCost + MYfCost + MLxCost) * MBlYwCost / 100, 2);
  YwCost.Text := FloatToStr(MYwCost);
   //税点
  MBlSdCost := StrToFloatDef(BlSdCost.Text, 0);
  MSdCost := RoundFloat((MWlCost + MZzCost + MHclCost + MDjBzCost + MKfCost + MYfCost + MLxCost + MYwCost) * MBlSdCost / 100, 2);
  SdCost.Text := FloatToStr(MSdCost);

  MCbPrice := MWlCost + MZzCost + MHclCost + MDjBzCost + MKfCost + MYfCost + MLxCost + MYwCost + MSdCost;
  MCbPrice := RoundFloat(MCbPrice, 2);
  CbPrice.Text := FloatToStr(MCbPrice);
  if MPrice > 0 then
  begin
    MLr := MPrice - MCbPrice;
    MLrd := RoundFloat(MLr / MCbPrice * 100, 2);
    Lr.Text := FloatToStr(MLr);
    Lrd.Text := FloatToStr(MLrd);
  end;
end;

procedure TfrmYpBaoJiaMx.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmYpBaoJiaMx.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*');
    sql.Add('from BaoJiaSub A inner join BaoJiaMain B on A.BjMainID=B.BjMainID');
    sql.Add('where B.BjNo=''' + FMainId + '''');
    Open;
  end;
  SCreateCDS20(ADOQuery1, Order_Sub);
  SInitCDSData20(ADOQuery1, Order_Sub);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*');
    sql.Add('from BaoJiaMain A');
    sql.Add('where A.BjNo=''' + FMainId + '''');
    Open;
  end;
  SCSHData(ADOQuery1, ScrollBox1, 0);
  SCSHData(ADOQuery1, ScrollBox2, 0);
end;

procedure TfrmYpBaoJiaMx.FormShow(Sender: TObject);
begin
  InitData();

end;

procedure TfrmYpBaoJiaMx.FormCreate(Sender: TObject);
begin
  SetWindowLong(ZzCost.Handle, GWL_STYLE, GetWindowLong(ZzCost.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(HclCost.Handle, GWL_STYLE, GetWindowLong(HclCost.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(DjBzCost.Handle, GWL_STYLE, GetWindowLong(DjBzCost.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(KfCost.Handle, GWL_STYLE, GetWindowLong(KfCost.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(YfCost.Handle, GWL_STYLE, GetWindowLong(YfCost.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(LxCost.Handle, GWL_STYLE, GetWindowLong(LxCost.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(YwCost.Handle, GWL_STYLE, GetWindowLong(YwCost.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(SdCost.Handle, GWL_STYLE, GetWindowLong(SdCost.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(ZshCost.Handle, GWL_STYLE, GetWindowLong(ZshCost.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
end;

procedure TfrmYpBaoJiaMx.ZzCostKeyPress(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 TfrmYpBaoJiaMx.HclCostExit(Sender: TObject);
begin
  CBHJ();
end;

procedure TfrmYpBaoJiaMx.v1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mavlue, FFieldName: string;
  MBiLi, MYlShb, MMmkz, MBjZKS, MWlPrice: Double;
begin
  mavlue := TcxTextEdit(Sender).EditingText;
  FFieldName := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if mavlue = '' then
    mavlue := '0';
  with Order_Sub do
  begin
    edit;
    FieldByName(FFieldName).Value := mavlue;
    post;
    MBiLi := FieldByName('BiLi').AsFloat;
    MYlShb := FieldByName('YlShb').AsFloat;
    MWlPrice := FieldByName('WlPrice').AsFloat;
  end;
  MBjZKS := StrToFloatDef(BjZKS.Text, 0);
  MMmkz := RoundFloat(MBjZKS * MBiLi / 100, 2);
  with Order_Sub do
  begin
    edit;
    FieldByName('Mmkz').Value := MMmkz;
    FieldByName('WlMoney').Value := RoundFloat(MMmkz * MWlPrice / 1000 * (MYlShb + 100) / 100, 2);
    post;
  end;
  tv1.Controller.EditingController.ShowEdit();
  CBHJ();
end;

procedure TfrmYpBaoJiaMx.CustomerNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        CustomerName.Text := Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        CustomerLevel.Text := Trim(CDS_HZ.fieldbyname('DEFStr6').AsString);
        if Trim(CustomerLevel.Text) = 'A' then
        begin
          ZzCost.Text := '1.5';
          CBHJ();
        end;
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmYpBaoJiaMx.BjZKSExit(Sender: TObject);
begin
  upWlPb();
  CBHJ();
end;

procedure TfrmYpBaoJiaMx.ScrollBox2Click(Sender: TObject);
begin
  lrd.SetFocus;
end;

procedure TfrmYpBaoJiaMx.ScrollBox1Click(Sender: TObject);
begin
  lrd.SetFocus;
end;

end.

