unit rpgBillsCalcDm;

interface

uses
  SysUtils, Classes, sdDB, sdProvider, ADODB, ProjGatherTree, CalcData;

type
  TrpgBillsCalcData = class(TDataModule)
    sdpBillsCalc: TsdADOProvider;
    sddBillsCalc: TsdDataSet;
  private
    FProjIndex: Integer;
    FProjType: Integer;

    procedure SaveBillsNodeCalc(ANode: TProjGatherTreeNode; AProjCalc: TProjCalc);
    procedure SaveBillsCalc(ATree: TProjGatherTree);
    procedure SaveBillsGather(ATree: TProjGatherTree);
  public
    constructor Create(AConnection: TADOConnection);

    procedure SaveSpecialProjDataTo(ATree: TProjGatherTree; AProjType: Integer; const ATableName: string);
    procedure SaveProjDataTo(ATree: TProjGatherTree; AProjIndex: Integer; const ATableName: string);
    procedure SaveGatherDataTo(ATree: TProjGatherTree; const ATableName: string);
  end;

implementation

uses CacheTree;

{$R *.dfm}

{ TrpgBillsCalcData }

constructor TrpgBillsCalcData.Create(AConnection: TADOConnection);
begin
  inherited Create(nil);
  sdpBillsCalc.Connection := AConnection;
end;

procedure TrpgBillsCalcData.SaveBillsCalc(ATree: TProjGatherTree);
var
  iNode: Integer;
  vNode: TProjGatherTreeNode;
begin
  for iNode := 0 to ATree.CacheNodes.Count - 1 do
  begin
    vNode := TProjGatherTreeNode(ATree.CacheNodes.Items[iNode]);
    if FProjType = 0 then
      SaveBillsNodeCalc(vNode, vNode.Proj[FProjIndex])
    else if FProjType > 0 then
      SaveBillsNodeCalc(vNode, vNode.SpecialProj[FProjType-1]);
  end;
end;

procedure TrpgBillsCalcData.SaveBillsGather(ATree: TProjGatherTree);
var
  iNode: Integer;
  vNode: TProjGatherTreeNode;
begin
  for iNode := 0 to ATree.CacheNodes.Count - 1 do
  begin
    vNode := TProjGatherTreeNode(ATree.CacheNodes.Items[iNode]);
    SaveBillsNodeCalc(vNode, vNode.GatherCalc);
  end;
end;

procedure TrpgBillsCalcData.SaveBillsNodeCalc(ANode: TProjGatherTreeNode;
  AProjCalc: TProjCalc);
var
  Rec: TsdDataRecord;
begin
  Rec := sddBillsCalc.Add;
  Rec.ValueByName('ID').AsInteger := ANode.ID;
  Rec.ValueByName('ProjID').AsInteger := FProjIndex;
  Rec.ValueByName('ProjType').AsInteger := FProjType;

  Rec.ValueByName('OrgQuantity').AsFloat := AProjCalc.Compile.Org.Quantity;
  Rec.ValueByName('OrgTotalPrice').AsFloat := AProjCalc.Compile.Org.TotalPrice;
  Rec.ValueByName('OrgTotalPrice_Rc').AsFloat := AProjCalc.Compile.Org.TotalPrice_Rc;
  Rec.ValueByName('MisQuantity').AsFloat := AProjCalc.Compile.Mis.Quantity;
  Rec.ValueByName('MisTotalPrice').AsFloat := AProjCalc.Compile.Mis.TotalPrice;
  Rec.ValueByName('MisTotalPrice_Rc').AsFloat := AProjCalc.Compile.Mis.TotalPrice_Rc;
  Rec.ValueByName('OthQuantity').AsFloat := AProjCalc.Compile.Oth.Quantity;
  Rec.ValueByName('OthTotalPrice').AsFloat := AProjCalc.Compile.Oth.TotalPrice;
  Rec.ValueByName('OthTotalPrice_Rc').AsFloat := AProjCalc.Compile.Oth.TotalPrice_Rc;
  Rec.ValueByName('Quantity').AsFloat := AProjCalc.Compile.SubTotal.Quantity;
  Rec.ValueByName('TotalPrice').AsFloat := AProjCalc.Compile.SubTotal.TotalPrice;
  Rec.ValueByName('TotalPrice_Rc').AsFloat := AProjCalc.Compile.SubTotal.TotalPrice_Rc;

  Rec.ValueByName('AddDealQuantity').AsFloat := AProjCalc.AddMeasure.Deal.Quantity;
  Rec.ValueByName('AddDealTotalPrice').AsFloat := AProjCalc.AddMeasure.Deal.TotalPrice;
  Rec.ValueByName('AddDealTotalPrice_Rc').AsFloat := AProjCalc.AddMeasure.Deal.TotalPrice_Rc;
  Rec.ValueByName('AddQcQuantity').AsFloat := AProjCalc.AddMeasure.Qc.Quantity;
  Rec.ValueByName('AddQcTotalPrice').AsFloat := AProjCalc.AddMeasure.Qc.TotalPrice;
  Rec.ValueByName('AddQcTotalPrice_Rc').AsFloat := AProjCalc.AddMeasure.Qc.TotalPrice_Rc;
  Rec.ValueByName('AddGatherQuantity').AsFloat := AProjCalc.AddMeasure.Gather.Quantity;
  Rec.ValueByName('AddGatherTotalPrice').AsFloat := AProjCalc.AddMeasure.Gather.TotalPrice;
  Rec.ValueByName('AddGatherTotalPrice_Rc').AsFloat := AProjCalc.AddMeasure.Gather.TotalPrice_Rc;

  Rec.ValueByName('CurDealQuantity').AsFloat := AProjCalc.CurMeasure.Deal.Quantity;
  Rec.ValueByName('CurDealTotalPrice').AsFloat := AProjCalc.CurMeasure.Deal.TotalPrice;
  Rec.ValueByName('CurDealTotalPrice_Rc').AsFloat := AProjCalc.CurMeasure.Deal.TotalPrice_Rc;
  Rec.ValueByName('CurQcQuantity').AsFloat := AProjCalc.CurMeasure.Qc.Quantity;
  Rec.ValueByName('CurQcTotalPrice').AsFloat := AProjCalc.CurMeasure.Qc.TotalPrice;
  Rec.ValueByName('CurQcTotalPrice_Rc').AsFloat := AProjCalc.CurMeasure.Qc.TotalPrice_Rc;
  Rec.ValueByName('CurGatherQuantity').AsFloat := AProjCalc.CurMeasure.Gather.Quantity;
  Rec.ValueByName('CurGatherTotalPrice').AsFloat := AProjCalc.CurMeasure.Gather.TotalPrice;
  Rec.ValueByName('CurGatherTotalPrice_Rc').AsFloat := AProjCalc.CurMeasure.Gather.TotalPrice_Rc;

  Rec.ValueByName('PreDealQuantity').AsFloat := AProjCalc.PreMeasure.Deal.Quantity;
  Rec.ValueByName('PreDealTotalPrice').AsFloat := AProjCalc.PreMeasure.Deal.TotalPrice;
  Rec.ValueByName('PreDealTotalPrice_Rc').AsFloat := AProjCalc.PreMeasure.Deal.TotalPrice_Rc;
  Rec.ValueByName('PreQcQuantity').AsFloat := AProjCalc.PreMeasure.Qc.Quantity;
  Rec.ValueByName('PreQcTotalPrice').AsFloat := AProjCalc.PreMeasure.Qc.TotalPrice;
  Rec.ValueByName('PreQcTotalPrice_Rc').AsFloat := AProjCalc.PreMeasure.Qc.TotalPrice_Rc;
  Rec.ValueByName('PreGatherQuantity').AsFloat := AProjCalc.PreMeasure.Gather.Quantity;
  Rec.ValueByName('PreGatherTotalPrice').AsFloat := AProjCalc.PreMeasure.Gather.TotalPrice;
  Rec.ValueByName('PreGatherTotalPrice_Rc').AsFloat := AProjCalc.PreMeasure.Gather.TotalPrice_Rc;

  Rec.ValueByName('EndDealQuantity').AsFloat := AProjCalc.EndMeasure.Deal.Quantity;
  Rec.ValueByName('EndDealTotalPrice').AsFloat := AProjCalc.EndMeasure.Deal.TotalPrice;
  Rec.ValueByName('EndDealTotalPrice_Rc').AsFloat := AProjCalc.EndMeasure.Deal.TotalPrice_Rc;
  Rec.ValueByName('EndQcQuantity').AsFloat := AProjCalc.EndMeasure.Qc.Quantity;
  Rec.ValueByName('EndQcTotalPrice').AsFloat := AProjCalc.EndMeasure.Qc.TotalPrice;
  Rec.ValueByName('EndQcTotalPrice_Rc').AsFloat := AProjCalc.EndMeasure.Qc.TotalPrice_Rc;
  Rec.ValueByName('EndGatherQuantity').AsFloat := AProjCalc.EndMeasure.Gather.Quantity;
  Rec.ValueByName('EndGatherTotalPrice').AsFloat := AProjCalc.EndMeasure.Gather.TotalPrice;
  Rec.ValueByName('EndGatherTotalPrice_Rc').AsFloat := AProjCalc.EndMeasure.Gather.TotalPrice_Rc;

  Rec.ValueByName('ZoneDealQuantity').AsFloat := AProjCalc.ZoneMeasure.Deal.Quantity;
  Rec.ValueByName('ZoneDealTotalPrice').AsFloat := AProjCalc.ZoneMeasure.Deal.TotalPrice;
  Rec.ValueByName('ZoneDealTotalPrice_Rc').AsFloat := AProjCalc.ZoneMeasure.Deal.TotalPrice_Rc;
  Rec.ValueByName('ZoneQcQuantity').AsFloat := AProjCalc.ZoneMeasure.Qc.Quantity;
  Rec.ValueByName('ZoneQcTotalPrice').AsFloat := AProjCalc.ZoneMeasure.Qc.TotalPrice;
  Rec.ValueByName('ZoneQcTotalPrice_Rc').AsFloat := AProjCalc.ZoneMeasure.Qc.TotalPrice_Rc;
  Rec.ValueByName('ZoneGatherQuantity').AsFloat := AProjCalc.ZoneMeasure.Gather.Quantity;
  Rec.ValueByName('ZoneGatherTotalPrice').AsFloat := AProjCalc.ZoneMeasure.Gather.TotalPrice;
  Rec.ValueByName('ZoneGatherTotalPrice_Rc').AsFloat := AProjCalc.ZoneMeasure.Gather.TotalPrice_Rc;

  Rec.ValueByName('DgnQuantity1').AsFloat := AProjCalc.DgnQuantity1;
  Rec.ValueByName('DgnQuantity2').AsFloat := AProjCalc.DgnQuantity2;
  Rec.ValueByName('DealDgnQuantity1').AsFloat := AProjCalc.DealDgnQuantity1;
  Rec.ValueByName('DealDgnQuantity2').AsFloat := AProjCalc.DealDgnQuantity2;
  Rec.ValueByName('CDgnQuantity1').AsFloat := AProjCalc.CDgnQuantity1;
  Rec.ValueByName('CDgnQuantity2').AsFloat := AProjCalc.CDgnQuantity2;
end;

procedure TrpgBillsCalcData.SaveGatherDataTo(ATree: TProjGatherTree;
  const ATableName: string);
begin
  FProjIndex := -2;
  FProjType := 0;

  sdpBillsCalc.TableName := ATableName;
  sddBillsCalc.Open;
  sddBillsCalc.BeginUpdate;
  try
    SaveBillsGather(ATree);
    SaveBillsNodeCalc(ATree.GatherNode, ATree.GatherNode.GatherCalc);
  finally
    sddBillsCalc.EndUpdate;
    sddBillsCalc.Save;
  end;
end;

procedure TrpgBillsCalcData.SaveProjDataTo(ATree: TProjGatherTree; AProjIndex: Integer;
  const ATableName: string);
begin
  FProjIndex := AProjIndex;
  FProjType := 0;

  sdpBillsCalc.TableName := ATableName;
  sddBillsCalc.Open;
  sddBillsCalc.BeginUpdate;
  try
    SaveBillsCalc(ATree);
    SaveBillsNodeCalc(ATree.GatherNode, ATree.GatherNode.Proj[AProjIndex]);
  finally
    sddBillsCalc.EndUpdate;
    sddBillsCalc.Save;
  end;
end;

procedure TrpgBillsCalcData.SaveSpecialProjDataTo(ATree: TProjGatherTree;
  AProjType: Integer; const ATableName: string);
begin
  FProjIndex := -3;
  FProjType := AProjType;

  sdpBillsCalc.TableName := ATableName;
  sddBillsCalc.Open;
  sddBillsCalc.BeginUpdate;
  try
    SaveBillsCalc(ATree);
    SaveBillsNodeCalc(ATree.GatherNode, ATree.GatherNode.SpecialProj[AProjType-1]);
  finally
    sddBillsCalc.EndUpdate;
    sddBillsCalc.Save;
  end;
end;

end.
