unit U_TradePlanProfitDetails;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  System.Math, Dialogs, StdCtrls, ExtCtrls, cxStyles, cxCustomData, cxGraphics,
  cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxCheckBox, cxCalendar, ADODB, DBClient,
  cxGridCustomPopupMenu, cxGridPopupMenu, Menus, cxTextEdit, cxButtonEdit,
  ComCtrls, cxContainer, cxMaskEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator,   dxDateRanges,
  dxBarBuiltInMenu, cxDropDownEdit, BtnEdit, Vcl.ToolWin, 
      
    
     
     
     
    
    
    
    
    
     
     
     
    
    
     dxScrollbarAnnotations,
  dxCore, cxDateUtils;

type
  TfrmTradePlanProfitDetails = class(TForm)
    GroupBox1: TGroupBox;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    CDS_4: TClientDataSet;
    DS_4: TDataSource;
    GPM_2: TcxGridPopupMenu;
    GPM_1: TcxGridPopupMenu;
    GPM_4: TcxGridPopupMenu;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ScrollBox1: TScrollBox;
    Label15: TLabel;
    Label7: TLabel;
    Label25: TLabel;
    Label30: TLabel;
    Label32: TLabel;
    Label57: TLabel;
    DS_3: TDataSource;
    CDS_3: TClientDataSet;
    GPM_3: TcxGridPopupMenu;
    GPM_CKKC: TcxGridPopupMenu;
    CDS_7: TClientDataSet;
    DS_7: TDataSource;
    Label3: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    ConNo: TcxTextEdit;
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    ToolButton3: TToolButton;
    TBClose: TToolButton;
    DS_5: TDataSource;
    CDS_5: TClientDataSet;
    GPM_5: TcxGridPopupMenu;
    GPM_6: TcxGridPopupMenu;
    CDS_6: TClientDataSet;
    DS_6: TDataSource;
    Label1: TLabel;
    Label2: TLabel;
    YJLRAmount: TcxTextEdit;
    YJLRRatio: TcxTextEdit;
    Panel1: TPanel;
    Panel2: TPanel;
    GroupBox5: TGroupBox;
    GroupBox2: TGroupBox;
    GroupBox4: TGroupBox;
    GroupBox6: TGroupBox;
    Label5: TLabel;
    Label6: TLabel;
    SJLRAmount: TcxTextEdit;
    SJLRRatio: TcxTextEdit;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    Tv1Column22: TcxGridDBColumn;
    v2CRTime: TcxGridDBColumn;
    Tv1Column23: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2FactoryName: TcxGridDBColumn;
    v2OrderNo: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column24: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    Tv1Column25: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridDBColumn32: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    cxGridDBColumn34: TcxGridDBColumn;
    cxGridDBColumn35: TcxGridDBColumn;
    cxGridDBColumn36: TcxGridDBColumn;
    cxGridDBColumn37: TcxGridDBColumn;
    cxGridDBColumn38: TcxGridDBColumn;
    cxGridDBColumn39: TcxGridDBColumn;
    cxGridDBColumn40: TcxGridDBColumn;
    cxGridDBColumn41: TcxGridDBColumn;
    cxGridDBColumn42: TcxGridDBColumn;
    cxGridDBColumn43: TcxGridDBColumn;
    cxGridDBColumn44: TcxGridDBColumn;
    cxGridDBColumn45: TcxGridDBColumn;
    cxGridDBColumn46: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    cxGridDBColumn47: TcxGridDBColumn;
    cxGridDBColumn48: TcxGridDBColumn;
    cxGridDBColumn49: TcxGridDBColumn;
    cxGridDBColumn50: TcxGridDBColumn;
    cxGridDBColumn51: TcxGridDBColumn;
    cxGridDBColumn52: TcxGridDBColumn;
    cxGridDBColumn53: TcxGridDBColumn;
    cxGridDBColumn54: TcxGridDBColumn;
    cxGridDBColumn55: TcxGridDBColumn;
    cxGridDBColumn56: TcxGridDBColumn;
    cxGridDBColumn57: TcxGridDBColumn;
    cxGridDBColumn58: TcxGridDBColumn;
    cxGridDBColumn59: TcxGridDBColumn;
    cxGridDBColumn60: TcxGridDBColumn;
    cxGridDBColumn61: TcxGridDBColumn;
    cxGridDBColumn62: TcxGridDBColumn;
    cxGridDBColumn63: TcxGridDBColumn;
    cxGridDBColumn64: TcxGridDBColumn;
    cxGridDBColumn65: TcxGridDBColumn;
    cxGridDBColumn66: TcxGridDBColumn;
    cxGridDBColumn67: TcxGridDBColumn;
    cxGridDBColumn68: TcxGridDBColumn;
    cxGridDBColumn69: TcxGridDBColumn;
    cxGridDBColumn70: TcxGridDBColumn;
    cxGridDBColumn71: TcxGridDBColumn;
    cxGridDBColumn72: TcxGridDBColumn;
    cxGridDBColumn73: TcxGridDBColumn;
    cxGridDBColumn74: TcxGridDBColumn;
    cxGridDBColumn75: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    cxGrid5: TcxGrid;
    TV5: TcxGridDBTableView;
    cxGridDBColumn76: TcxGridDBColumn;
    cxGridDBColumn77: TcxGridDBColumn;
    cxGridDBColumn78: TcxGridDBColumn;
    cxGridDBColumn79: TcxGridDBColumn;
    cxGridDBColumn80: TcxGridDBColumn;
    cxGridDBColumn81: TcxGridDBColumn;
    cxGridDBColumn82: TcxGridDBColumn;
    cxGridDBColumn83: TcxGridDBColumn;
    cxGridDBColumn84: TcxGridDBColumn;
    cxGridDBColumn85: TcxGridDBColumn;
    cxGridDBColumn86: TcxGridDBColumn;
    cxGridDBColumn87: TcxGridDBColumn;
    cxGridDBColumn88: TcxGridDBColumn;
    cxGridDBColumn89: TcxGridDBColumn;
    cxGridDBColumn90: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    cxGrid6: TcxGrid;
    TV6: TcxGridDBTableView;
    cxGridDBColumn91: TcxGridDBColumn;
    cxGridDBColumn92: TcxGridDBColumn;
    cxGridDBColumn93: TcxGridDBColumn;
    cxGridDBColumn94: TcxGridDBColumn;
    cxGridDBColumn95: TcxGridDBColumn;
    cxGridDBColumn96: TcxGridDBColumn;
    cxGridDBColumn97: TcxGridDBColumn;
    cxGridDBColumn98: TcxGridDBColumn;
    cxGridDBColumn99: TcxGridDBColumn;
    cxGridDBColumn100: TcxGridDBColumn;
    cxGridDBColumn101: TcxGridDBColumn;
    cxGridDBColumn102: TcxGridDBColumn;
    cxGridDBColumn103: TcxGridDBColumn;
    cxGridDBColumn104: TcxGridDBColumn;
    cxGridDBColumn105: TcxGridDBColumn;
    cxGridDBColumn106: TcxGridDBColumn;
    cxGridDBColumn107: TcxGridDBColumn;
    cxGridDBColumn108: TcxGridDBColumn;
    cxGridDBColumn109: TcxGridDBColumn;
    cxGridDBColumn110: TcxGridDBColumn;
    cxGridDBColumn111: TcxGridDBColumn;
    cxGridDBColumn112: TcxGridDBColumn;
    cxGridDBColumn113: TcxGridDBColumn;
    cxGridDBColumn114: TcxGridDBColumn;
    cxGridDBColumn115: TcxGridDBColumn;
    cxGridDBColumn116: TcxGridDBColumn;
    cxGridDBColumn117: TcxGridDBColumn;
    cxGridDBColumn118: TcxGridDBColumn;
    cxGridDBColumn119: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    Saleser: TcxTextEdit;
    Panel3: TPanel;
    GroupBox3: TGroupBox;
    GroupBox7: TGroupBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    cxGridDBColumn120: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridDBColumn121: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    Tv1Column26: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    cxGridDBColumn122: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    cxGridDBColumn123: TcxGridDBColumn;
    cxGridDBColumn124: TcxGridDBColumn;
    cxGridDBColumn125: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid7: TcxGrid;
    TV7: TcxGridDBTableView;
    cxGridDBColumn128: TcxGridDBColumn;
    cxGridDBColumn129: TcxGridDBColumn;
    cxGridDBColumn130: TcxGridDBColumn;
    cxGridDBColumn131: TcxGridDBColumn;
    cxGridDBColumn132: TcxGridDBColumn;
    cxGridDBColumn133: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    cxGridDBColumn134: TcxGridDBColumn;
    cxGridDBColumn135: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    cxGridDBColumn136: TcxGridDBColumn;
    cxGridDBColumn137: TcxGridDBColumn;
    cxGridDBColumn138: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxGridDBColumn139: TcxGridDBColumn;
    cxGridDBColumn140: TcxGridDBColumn;
    cxGridDBColumn141: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    cxGridDBColumn142: TcxGridDBColumn;
    cxGridDBColumn143: TcxGridDBColumn;
    cxGridDBColumn144: TcxGridDBColumn;
    cxGridDBColumn145: TcxGridDBColumn;
    cxGridDBColumn146: TcxGridDBColumn;
    cxGridDBColumn147: TcxGridDBColumn;
    cxGridDBColumn148: TcxGridDBColumn;
    cxGridDBColumn149: TcxGridDBColumn;
    cxGridDBColumn150: TcxGridDBColumn;
    cxGridDBColumn151: TcxGridDBColumn;
    cxGridDBColumn152: TcxGridDBColumn;
    cxGridDBColumn153: TcxGridDBColumn;
    cxGridDBColumn154: TcxGridDBColumn;
    cxGridDBColumn155: TcxGridDBColumn;
    cxGridDBColumn156: TcxGridDBColumn;
    cxGridDBColumn157: TcxGridDBColumn;
    cxGridDBColumn158: TcxGridDBColumn;
    cxGridDBColumn159: TcxGridDBColumn;
    cxGridDBColumn160: TcxGridDBColumn;
    cxGridDBColumn161: TcxGridDBColumn;
    cxGridDBColumn162: TcxGridDBColumn;
    cxGridDBColumn163: TcxGridDBColumn;
    cxGridDBColumn164: TcxGridDBColumn;
    cxGridLevel6: TcxGridLevel;
    ConDate: TcxDateEdit;
    Label4: TLabel;
    BuyName: TcxTextEdit;
    Tv1Column28: TcxGridDBColumn;
    TV7Column1: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure cxGridDBColumn46PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn14PropertiesEditValueChanged(Sender: TObject);
    procedure TV2Column14PropertiesEditValueChanged(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    procedure InitOrder();
    { Private declarations }
  public
    FConNo: string;
    { Public declarations }
  end;

var
  frmTradePlanProfitDetails: TfrmTradePlanProfitDetails;

implementation

uses
  U_DataLink, U_RTFun;
{$R *.dfm}

procedure TfrmTradePlanProfitDetails.InitOrder();
begin

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''总利润''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(FConNo)));
    Open;
  end;
  SCSHData(ADOQueryTemp, ScrollBox1, 0);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''调拨入库''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(FConNo)));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''调拨出库''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(FConNo)));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_7);
  SInitCDSData(ADOQueryTemp, CDS_7);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''采购''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(FConNo)));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''加工''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(FConNo)));
//    ShowMessage(sql.Text);
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_3);
  SInitCDSData(ADOQueryTemp, CDS_3);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''其他''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(FConNo)));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_4);
  SInitCDSData(ADOQueryTemp, CDS_4);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''费用''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(FConNo)));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_5);
  SInitCDSData(ADOQueryTemp, CDS_5);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Con ');
    sql.Add('@PFType=''应收''  ');
    sql.Add(',@ConNo=  ' + quotedstr(Trim(FConNo)));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_6);
  SInitCDSData(ADOQueryTemp, CDS_6);
end;

procedure TfrmTradePlanProfitDetails.FormDestroy(Sender: TObject);
begin
  frmTradePlanProfitDetails := nil;
end;

procedure TfrmTradePlanProfitDetails.FormResize(Sender: TObject);
begin
  Panel1.Height := Ceil((Self.Height - ToolBar1.Height - ScrollBox1.Height) / 4);
  Panel3.Height := Ceil((Self.Height - ToolBar1.Height - ScrollBox1.Height) / 4);
  Panel2.Height := Ceil((Self.Height - ToolBar1.Height - ScrollBox1.Height) / 4);
  GroupBox1.Height := Ceil((Self.Height - ToolBar1.Height - ScrollBox1.Height) / 4);

  GroupBox5.Width := Ceil(Panel1.Width / 2);
  GroupBox3.Width := Ceil(Panel3.Width / 2);
  GroupBox4.Width := Ceil(Panel2.Width / 2);
end;

procedure TfrmTradePlanProfitDetails.cxGridDBColumn14PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  MExchangeRate, MPrice, MQty: Double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(TV3.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if FFieldName = 'ExchangeRate' then
  begin
    if (mvalue = '') or (mvalue = '0') then
      mvalue := '1';
  end
  else
  begin
    if mvalue = '' then
      mvalue := '0';
  end;

  with CDS_3 do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
    if CDS_3.FieldByName('ExchangeRate').AsFloat = 0 then
      MExchangeRate := 1
    else
      MExchangeRate := CDS_3.FieldByName('ExchangeRate').AsFloat;

    MPrice := CDS_3.FieldByName('Price').AsFloat;
    MQty := CDS_3.FieldByName('Qty').AsFloat;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update Trade_Profit    ');
    sql.Add('set ExchangeRate=' + FloatToStr(MExchangeRate));
    sql.Add(', Qty=' + FloatToStr(MQty));
    sql.Add(', Price=' + FloatToStr(MPrice));
    sql.Add(', Amount=' + FloatToStr(RoundFloat((MExchangeRate * MQty * MPrice), 2)));
    sql.Add('where PFID=' + CDS_3.fieldbyname('PFIDStr').AsString);
    ExecSQL;
  end;
  with CDS_3 do
  begin
    Edit;
    FieldByName('Amount').Value := RoundFloat((MExchangeRate * MQty * MPrice), 2);
    Post;
  end;
  TV3.Controller.EditingController.ShowEdit();
end;

procedure TfrmTradePlanProfitDetails.cxGridDBColumn46PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  MExchangeRate, MPrice, MQty: Double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv5.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if FFieldName = 'ExchangeRate' then
  begin
    if (mvalue = '') or (mvalue = '0') then
      mvalue := '1';
  end
  else
  begin
    if mvalue = '' then
      mvalue := '0';
  end;

  with CDS_5 do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
    if CDS_5.FieldByName('ExchangeRate').AsFloat = 0 then
      MExchangeRate := 1
    else
      MExchangeRate := CDS_5.FieldByName('ExchangeRate').AsFloat;

    MPrice := CDS_5.FieldByName('Price').AsFloat;
    MQty := CDS_5.FieldByName('Qty').AsFloat;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update Trade_Profit    ');
    sql.Add('set ExchangeRate=' + FloatToStr(MExchangeRate));
    sql.Add(', Qty=' + FloatToStr(MQty));
    sql.Add(', Price=' + FloatToStr(MPrice));
    sql.Add(', Amount=' + FloatToStr(RoundFloat((MExchangeRate * MQty * MPrice), 2)));
    sql.Add('where PFID=' + CDS_5.fieldbyname('PFIDStr').AsString);
    ExecSQL;
  end;
  with CDS_5 do
  begin
    Edit;
    FieldByName('Amount').Value := RoundFloat((MExchangeRate * MQty * MPrice), 2);
    Post;
  end;
  Tv5.Controller.EditingController.ShowEdit();
end;

procedure TfrmTradePlanProfitDetails.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cafree;
end;

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

procedure TfrmTradePlanProfitDetails.TBRafreshClick(Sender: TObject);
begin

  InitOrder();
end;

procedure TfrmTradePlanProfitDetails.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid('订单明细进度Tv1', Tv1, '订单明细进度管理');
  WriteCxGrid('订单明细进度Tv2', Tv2, '订单明细进度管理');
  WriteCxGrid('订单明细进度Tv3', Tv3, '订单明细进度管理');
  WriteCxGrid('订单明细进度Tv4', Tv4, '订单明细进度管理');
  WriteCxGrid('订单明细进度Tv5', Tv5, '订单明细进度管理');
  WriteCxGrid('订单明细进度Tv6', Tv6, '订单明细进度管理');
end;

procedure TfrmTradePlanProfitDetails.TV2Column14PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  MExchangeRate, MPrice, MQty: Double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(TV2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if FFieldName = 'ExchangeRate' then
  begin
    if (mvalue = '') or (mvalue = '0') then
      mvalue := '1';
  end
  else
  begin
    if mvalue = '' then
      mvalue := '0';
  end;

  with CDS_2 do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
    if CDS_2.FieldByName('ExchangeRate').AsFloat = 0 then
      MExchangeRate := 1
    else
      MExchangeRate := CDS_2.FieldByName('ExchangeRate').AsFloat;

    MPrice := CDS_2.FieldByName('Price').AsFloat;
    MQty := CDS_2.FieldByName('Qty').AsFloat;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update Trade_Profit    ');
    sql.Add('set ExchangeRate=' + FloatToStr(MExchangeRate));
    sql.Add(', Qty=' + FloatToStr(MQty));
    sql.Add(', Price=' + FloatToStr(MPrice));
    sql.Add(', Amount=' + FloatToStr(RoundFloat((MExchangeRate * MQty * MPrice), 2)));
    sql.Add('where PFID=' + CDS_2.fieldbyname('PFIDStr').AsString);
    ExecSQL;
  end;
  with CDS_2 do
  begin
    Edit;
    FieldByName('Amount').Value := RoundFloat((MExchangeRate * MQty * MPrice), 2);
    Post;
  end;
  TV2.Controller.EditingController.ShowEdit();
end;

procedure TfrmTradePlanProfitDetails.FormShow(Sender: TObject);
begin
  ReadCxGrid('订单明细进度Tv1', Tv1, '订单明细进度管理');
  ReadCxGrid('订单明细进度Tv2', Tv2, '订单明细进度管理');
  ReadCxGrid('订单明细进度Tv3', Tv3, '订单明细进度管理');
  ReadCxGrid('订单明细进度Tv4', Tv4, '订单明细进度管理');
  ReadCxGrid('订单明细进度Tv5', Tv5, '订单明细进度管理');
  ReadCxGrid('订单明细进度Tv6', Tv6, '订单明细进度管理');
  InitOrder();
end;

end.

