unit U_TradePlanProfitDetails;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  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,math;

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;
    PM_CKKC: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    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;
    cxGridLevel1: TcxGridLevel;
    Saleser: TcxTextEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn91: TcxGridDBColumn;
    cxGridDBColumn92: TcxGridDBColumn;
    cxGridDBColumn93: TcxGridDBColumn;
    cxGridDBColumn94: TcxGridDBColumn;
    cxGridDBColumn95: TcxGridDBColumn;
    cxGridDBColumn96: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    cxGridDBColumn97: TcxGridDBColumn;
    cxGridDBColumn98: TcxGridDBColumn;
    cxGridDBColumn99: TcxGridDBColumn;
    cxGridDBColumn100: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column28: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column29: TcxGridDBColumn;
    Tv1Column30: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column31: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column37: TcxGridDBColumn;
    Tv1Column38: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    cxGridDBColumn101: TcxGridDBColumn;
    cxGridDBColumn102: TcxGridDBColumn;
    Tv1Column26: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column32: TcxGridDBColumn;
    Tv1Column34: TcxGridDBColumn;
    Tv1Column33: TcxGridDBColumn;
    Tv1Column36: TcxGridDBColumn;
    Tv1Column35: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    ConDate: TcxDateEdit;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    TV2Column8: TcxGridDBColumn;
    TV2Column9: TcxGridDBColumn;
    TV2Column10: TcxGridDBColumn;
    TV2Column11: TcxGridDBColumn;
    TV2Column12: TcxGridDBColumn;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    cxGrid5: TcxGrid;
    TV5: TcxGridDBTableView;
    v2CRTime: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2FactoryName: TcxGridDBColumn;
    v2OrderNo: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    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_Plan ');
    sql.Add(' @CXType=''单个合同''  ');
    sql.Add(',@ConNo=' + quotedstr(Trim(FConNo)));
    Open;
  end;
  SCSHData(ADOQueryTemp, ScrollBox1, 2);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec  P_Trade_Profit_Plan ');
    sql.Add(' @CXType=''应收款''  ');
    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_Plan ');
    sql.Add(' @CXType=''用料成本''  ');
    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_Plan ');
    sql.Add(' @CXType=''加工成本''  ');
    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_Plan ');
    sql.Add(' @CXType=''用料成本''  ');
    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_Plan ');
    sql.Add(' @CXType=''费用''  ');
    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_Plan ');
//    sql.Add(' @CXType=''应收款''  ');
//    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
  GroupBox5.Width := ceil(Self.Width / 2);
  GroupBox4.Width := ceil(Self.Width / 2);
//  cxGrid1.Width := ceil(Self.Width / 2);
//  cxGrid6.Width := ceil(Self.Width / 2);
//  GroupBox2.Height := ceil((Self.Height - ToolBar1.Height - pnl1.Height) / 4);
//  GroupBox3.Height := ceil((Self.Height - ToolBar1.Height - pnl1.Height) / 4);
//  GroupBox4.Height := ceil((Self.Height - ToolBar1.Height - pnl1.Height) / 4);
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.

