unit rmFxBillsAddDm;

interface

uses
  SysUtils, Classes, ProjectData, PhaseData, StageDm, DB, DBClient,
  MCacheTree, sdIDTree, sdDB;

type
  TrmFxBillsAddData = class(TDataModule)
    cdsBills: TClientDataSet;
    cdsBillsB_Code: TStringField;
    cdsBillsCode: TStringField;
    cdsBillsName: TWideStringField;
    cdsBillsUnits: TWideStringField;
    cdsBillsPrice: TFloatField;
    cdsBillsQuantity: TFloatField;
    cdsBillsTotalPrice: TFloatField;
    cdsBillsQuantity1: TFloatField;
    cdsBillsTotalPrice1: TFloatField;
    cdsBillsQuantity2: TFloatField;
    cdsBillsTotalPrice2: TFloatField;
    cdsBillsQuantity3: TFloatField;
    cdsBillsTotalPrice3: TFloatField;
    cdsBillsQuantity4: TFloatField;
    cdsBillsTotalPrice4: TFloatField;
    cdsBillsQuantity5: TFloatField;
    cdsBillsTotalPrice5: TFloatField;
    cdsBillsQuantity6: TFloatField;
    cdsBillsTotalPrice6: TFloatField;
    cdsBillsQuantity7: TFloatField;
    cdsBillsTotalPrice7: TFloatField;
    cdsBillsQuantity8: TFloatField;
    cdsBillsTotalPrice8: TFloatField;
    cdsBillsQuantity9: TFloatField;
    cdsBillsTotalPrice9: TFloatField;
    cdsBillsQuantity10: TFloatField;
    cdsBillsTotalPrice10: TFloatField;
    cdsBillsQuantity11: TFloatField;
    cdsBillsTotalPrice11: TFloatField;
    cdsBillsQuantity12: TFloatField;
    cdsBillsTotalPrice12: TFloatField;
    cdsBillsQuantity13: TFloatField;
    cdsBillsTotalPrice13: TFloatField;
    cdsBillsQuantity14: TFloatField;
    cdsBillsTotalPrice14: TFloatField;
    cdsBillsQuantity15: TFloatField;
    cdsBillsTotalPrice15: TFloatField;
    cdsBillsQuantity16: TFloatField;
    cdsBillsTotalPrice16: TFloatField;
    cdsBillsQuantity17: TFloatField;
    cdsBillsTotalPrice17: TFloatField;
    cdsBillsQuantity18: TFloatField;
    cdsBillsTotalPrice18: TFloatField;
    cdsBillsQuantity19: TFloatField;
    cdsBillsTotalPrice19: TFloatField;
    cdsBillsQuantity20: TFloatField;
    cdsBillsTotalPrice20: TFloatField;
    cdsBillsQuantity21: TFloatField;
    cdsBillsTotalPrice21: TFloatField;
    cdsBillsQuantity22: TFloatField;
    cdsBillsTotalPrice22: TFloatField;
    cdsBillsQuantity23: TFloatField;
    cdsBillsTotalPrice23: TFloatField;
    cdsBillsQuantity24: TFloatField;
    cdsBillsTotalPrice24: TFloatField;
    cdsBillsQuantity25: TFloatField;
    cdsBillsTotalPrice25: TFloatField;
    cdsBillsQuantity26: TFloatField;
    cdsBillsTotalPrice26: TFloatField;
    cdsBillsQuantity27: TFloatField;
    cdsBillsTotalPrice27: TFloatField;
    cdsBillsQuantity28: TFloatField;
    cdsBillsTotalPrice28: TFloatField;
    cdsBillsQuantity29: TFloatField;
    cdsBillsTotalPrice29: TFloatField;
    cdsBillsQuantity30: TFloatField;
    cdsBillsTotalPrice30: TFloatField;
    cdsBillsQuantity31: TFloatField;
    cdsBillsTotalPrice31: TFloatField;
    cdsBillsQuantity32: TFloatField;
    cdsBillsTotalPrice32: TFloatField;
    cdsBillsQuantity33: TFloatField;
    cdsBillsTotalPrice33: TFloatField;
    cdsBillsQuantity34: TFloatField;
    cdsBillsTotalPrice34: TFloatField;
    cdsBillsQuantity35: TFloatField;
    cdsBillsTotalPrice35: TFloatField;
    cdsBillsQuantity36: TFloatField;
    cdsBillsTotalPrice36: TFloatField;
    cdsBillsQuantity37: TFloatField;
    cdsBillsTotalPrice37: TFloatField;
    cdsBillsQuantity38: TFloatField;
    cdsBillsTotalPrice38: TFloatField;
    cdsBillsQuantity39: TFloatField;
    cdsBillsTotalPrice39: TFloatField;
    cdsBillsQuantity40: TFloatField;
    cdsBillsTotalPrice40: TFloatField;
    cdsBillsQuantity41: TFloatField;
    cdsBillsTotalPrice41: TFloatField;
    cdsBillsQuantity42: TFloatField;
    cdsBillsTotalPrice42: TFloatField;
    cdsBillsQuantity43: TFloatField;
    cdsBillsTotalPrice43: TFloatField;
    cdsBillsQuantity44: TFloatField;
    cdsBillsTotalPrice44: TFloatField;
    cdsBillsQuantity45: TFloatField;
    cdsBillsTotalPrice45: TFloatField;
    cdsBillsQuantity46: TFloatField;
    cdsBillsTotalPrice46: TFloatField;
    cdsBillsQuantity47: TFloatField;
    cdsBillsTotalPrice47: TFloatField;
    cdsBillsQuantity48: TFloatField;
    cdsBillsTotalPrice48: TFloatField;
    cdsBillsQuantity49: TFloatField;
    cdsBillsTotalPrice49: TFloatField;
    cdsBillsQuantity50: TFloatField;
    cdsBillsTotalPrice50: TFloatField;
    cdsBillsMemoStr: TWideStringField;
  private
    FProjectData: TProjectData;
    FAllPhaseCacheTree: TAllPhaseCacheTree;

    function AddBills(ANode: TsdIDTreeNode; AParent: TAllPhaseCacheNode): TAllPhaseCacheNode;

    procedure LoadStageData(AStageData: TStageData; APhaseIndex: Integer);
    procedure LoadPhaseData(APhaseIndex: Integer; AProjectData: TProjectData = nil);

    procedure WriteBills(ANode: TAllPhaseCacheNode);
  public
    function AssignData(AProjectData: TProjectData): TDataSet;
  end;

implementation

uses CacheTree;

{$R *.dfm}

{ TrmFxBillsAddData }

function TrmFxBillsAddData.AddBills(ANode: TsdIDTreeNode;
  AParent: TAllPhaseCacheNode): TAllPhaseCacheNode;
begin
  if not Assigned(ANode) then Exit;
  Result := FAllPhaseCacheTree.AddNode(ANode.ID, AParent, nil);
  Result.Code := ANode.Rec.ValueByName('Code').AsString;
  Result.B_Code := ANode.Rec.ValueByName('B_Code').AsString;
  Result.Name := ANode.Rec.ValueByName('Name').AsString;
  Result.Units := ANode.Rec.ValueByName('Units').AsString;
  Result.Price := ANode.Rec.ValueByName('Price').AsFloat;
  Result.Quantity := ANode.Rec.ValueByName('Quantity').AsFloat;
  Result.TotalPrice := ANode.Rec.ValueByName('TotalPrice').AsFloat;  
  AddBills(ANode.FirstChild, Result);
  AddBills(ANode.NextSibling, AParent);
end;

function TrmFxBillsAddData.AssignData(
  AProjectData: TProjectData): TDataSet;
var
  iPhase: Integer;
begin
  FProjectData := AProjectData;
  cdsBills.DisableControls;
  cdsBills.Active := True;
  cdsBills.EmptyDataSet;
  FAllPhaseCacheTree := TAllPhaseCacheTree.Create;
  try
    AddBills(FProjectData.BillsCompileData.BillsCompileTree.FirstNode, nil);
    for iPhase := 1 to FProjectData.ProjProperties.PhaseCount do
      LoadPhaseData(iPhase, FProjectData);
    WriteBills(TAllPhaseCacheNode(FAllPhaseCacheTree.FirstNode));
  finally
    Result := cdsBills;
    FAllPhaseCacheTree.Free;
    cdsBills.EnableControls;
  end;
end;

procedure TrmFxBillsAddData.LoadPhaseData(APhaseIndex: Integer; AProjectData: TProjectData = nil);
var
  PhaseData: TPhaseData;
  StageData: TStageData;
begin
  PhaseData := TPhaseData.Create(AProjectData);
  try
    PhaseData.SimpleOpen(Format('%s\Phase%d.dat', [FProjectData.TempPath, APhaseIndex]));
    try
      StageData := TStageData.Create(PhaseData);
      StageData.TableName := PhaseData.StageTableName[PhaseData.AuditCount];
      StageData.Open(PhaseData.ADOConnection);
      LoadStageData(StageData, APhaseIndex);
    finally
      StageData.Free;
    end;
  finally
    PhaseData.Free;
  end;
end;

procedure TrmFxBillsAddData.LoadStageData(AStageData: TStageData;
  APhaseIndex: Integer);
var
  i: Integer;
  StageRec: TsdDataRecord;
  stnNode: TsdIDTreeNode;
  Node: TAllPhaseCacheNode;
begin
  for i := 0 to AStageData.sddStage.RecordCount - 1 do
  begin
    StageRec := AStageData.sddStage.Records[i];
    with FProjectData.BillsCompileData.BillsCompileTree do
      stnNode := FindNode(StageRec.ValueByName('BillsID').AsInteger);
    Node := FAllPhaseCacheTree.FindNode(StageRec.ValueByName('BillsID').AsInteger);
    if Assigned(Node) then
    begin
      Node.FP_Quantity[APhaseIndex] := StageRec.ValueByName('GatherQuantity').AsFloat;
      Node.FP_TotalPrice[APhaseIndex] := StageRec.ValueByName('GatherTotalPrice').AsFloat;
    end;
  end;
end;

procedure TrmFxBillsAddData.WriteBills(ANode: TAllPhaseCacheNode);
begin
  if not Assigned(ANode) then Exit;
  cdsBills.Append;
  cdsBillsCode.AsString := ANode.Code;
  cdsBillsB_Code.AsString := ANode.B_Code;
  cdsBillsName.AsString := ANode.Name;
  cdsBillsUnits.AsString := ANode.Units;
  cdsBillsPrice.AsFloat := ANode.Price;
  cdsBillsQuantity.AsFloat := ANode.Quantity;
  cdsBillsTotalPrice.AsFloat := ANode.TotalPrice;
  cdsBillsQuantity1.AsFloat := ANode.FP_Quantity[1];
  cdsBillsTotalPrice1.AsFloat := ANode.FP_TotalPrice[1];
  cdsBillsQuantity2.AsFloat := ANode.FP_Quantity[2];
  cdsBillsTotalPrice2.AsFloat := ANode.FP_TotalPrice[2];
  cdsBillsQuantity3.AsFloat := ANode.FP_Quantity[3];
  cdsBillsTotalPrice3.AsFloat := ANode.FP_TotalPrice[3];
  cdsBillsQuantity4.AsFloat := ANode.FP_Quantity[4];
  cdsBillsTotalPrice4.AsFloat := ANode.FP_TotalPrice[4];
  cdsBillsQuantity5.AsFloat := ANode.FP_Quantity[5];
  cdsBillsTotalPrice5.AsFloat := ANode.FP_TotalPrice[5];
  cdsBillsQuantity6.AsFloat := ANode.FP_Quantity[6];
  cdsBillsTotalPrice6.AsFloat := ANode.FP_TotalPrice[6];
  cdsBillsQuantity7.AsFloat := ANode.FP_Quantity[7];
  cdsBillsTotalPrice7.AsFloat := ANode.FP_TotalPrice[7];
  cdsBillsQuantity8.AsFloat := ANode.FP_Quantity[8];
  cdsBillsTotalPrice8.AsFloat := ANode.FP_TotalPrice[8];
  cdsBillsQuantity9.AsFloat := ANode.FP_Quantity[9];
  cdsBillsTotalPrice9.AsFloat := ANode.FP_TotalPrice[9];
  cdsBillsQuantity10.AsFloat := ANode.FP_Quantity[10];
  cdsBillsTotalPrice10.AsFloat := ANode.FP_TotalPrice[10];
  cdsBillsQuantity11.AsFloat := ANode.FP_Quantity[11];
  cdsBillsTotalPrice11.AsFloat := ANode.FP_TotalPrice[11];
  cdsBillsQuantity12.AsFloat := ANode.FP_Quantity[12];
  cdsBillsTotalPrice12.AsFloat := ANode.FP_TotalPrice[12];
  cdsBillsQuantity13.AsFloat := ANode.FP_Quantity[13];
  cdsBillsTotalPrice13.AsFloat := ANode.FP_TotalPrice[13];
  cdsBillsQuantity14.AsFloat := ANode.FP_Quantity[14];
  cdsBillsTotalPrice14.AsFloat := ANode.FP_TotalPrice[14];
  cdsBillsQuantity15.AsFloat := ANode.FP_Quantity[15];
  cdsBillsTotalPrice15.AsFloat := ANode.FP_TotalPrice[15];
  cdsBillsQuantity16.AsFloat := ANode.FP_Quantity[16];
  cdsBillsTotalPrice16.AsFloat := ANode.FP_TotalPrice[16];
  cdsBillsQuantity17.AsFloat := ANode.FP_Quantity[17];
  cdsBillsTotalPrice17.AsFloat := ANode.FP_TotalPrice[17];
  cdsBillsQuantity18.AsFloat := ANode.FP_Quantity[18];
  cdsBillsTotalPrice18.AsFloat := ANode.FP_TotalPrice[18];
  cdsBillsQuantity19.AsFloat := ANode.FP_Quantity[19];
  cdsBillsTotalPrice19.AsFloat := ANode.FP_TotalPrice[19];
  cdsBillsQuantity20.AsFloat := ANode.FP_Quantity[20];
  cdsBillsTotalPrice20.AsFloat := ANode.FP_TotalPrice[20];
  cdsBillsQuantity21.AsFloat := ANode.FP_Quantity[21];
  cdsBillsTotalPrice21.AsFloat := ANode.FP_TotalPrice[21];
  cdsBillsQuantity22.AsFloat := ANode.FP_Quantity[22];
  cdsBillsTotalPrice22.AsFloat := ANode.FP_TotalPrice[22];
  cdsBillsQuantity23.AsFloat := ANode.FP_Quantity[23];
  cdsBillsTotalPrice23.AsFloat := ANode.FP_TotalPrice[23];
  cdsBillsQuantity24.AsFloat := ANode.FP_Quantity[24];
  cdsBillsTotalPrice24.AsFloat := ANode.FP_TotalPrice[24];
  cdsBillsQuantity25.AsFloat := ANode.FP_Quantity[25];
  cdsBillsTotalPrice25.AsFloat := ANode.FP_TotalPrice[25];
  cdsBillsQuantity26.AsFloat := ANode.FP_Quantity[26];
  cdsBillsTotalPrice26.AsFloat := ANode.FP_TotalPrice[26];
  cdsBillsQuantity27.AsFloat := ANode.FP_Quantity[27];
  cdsBillsTotalPrice27.AsFloat := ANode.FP_TotalPrice[27];
  cdsBillsQuantity28.AsFloat := ANode.FP_Quantity[28];
  cdsBillsTotalPrice28.AsFloat := ANode.FP_TotalPrice[28];
  cdsBillsQuantity29.AsFloat := ANode.FP_Quantity[29];
  cdsBillsTotalPrice29.AsFloat := ANode.FP_TotalPrice[29];
  cdsBillsQuantity30.AsFloat := ANode.FP_Quantity[30];
  cdsBillsTotalPrice30.AsFloat := ANode.FP_TotalPrice[30];
  cdsBillsQuantity31.AsFloat := ANode.FP_Quantity[31];
  cdsBillsTotalPrice31.AsFloat := ANode.FP_TotalPrice[31];
  cdsBillsQuantity32.AsFloat := ANode.FP_Quantity[32];
  cdsBillsTotalPrice32.AsFloat := ANode.FP_TotalPrice[32];
  cdsBillsQuantity33.AsFloat := ANode.FP_Quantity[33];
  cdsBillsTotalPrice33.AsFloat := ANode.FP_TotalPrice[33];
  cdsBillsQuantity34.AsFloat := ANode.FP_Quantity[34];
  cdsBillsTotalPrice34.AsFloat := ANode.FP_TotalPrice[34];
  cdsBillsQuantity35.AsFloat := ANode.FP_Quantity[35];
  cdsBillsTotalPrice35.AsFloat := ANode.FP_TotalPrice[35];
  cdsBillsQuantity36.AsFloat := ANode.FP_Quantity[36];
  cdsBillsTotalPrice36.AsFloat := ANode.FP_TotalPrice[36];
  cdsBillsQuantity37.AsFloat := ANode.FP_Quantity[37];
  cdsBillsTotalPrice37.AsFloat := ANode.FP_TotalPrice[37];
  cdsBillsQuantity38.AsFloat := ANode.FP_Quantity[38];
  cdsBillsTotalPrice38.AsFloat := ANode.FP_TotalPrice[38];
  cdsBillsQuantity39.AsFloat := ANode.FP_Quantity[39];
  cdsBillsTotalPrice39.AsFloat := ANode.FP_TotalPrice[39];
  cdsBillsQuantity40.AsFloat := ANode.FP_Quantity[40];
  cdsBillsTotalPrice40.AsFloat := ANode.FP_TotalPrice[40];
  cdsBillsQuantity41.AsFloat := ANode.FP_Quantity[41];
  cdsBillsTotalPrice41.AsFloat := ANode.FP_TotalPrice[41];
  cdsBillsQuantity42.AsFloat := ANode.FP_Quantity[42];
  cdsBillsTotalPrice42.AsFloat := ANode.FP_TotalPrice[42];
  cdsBillsQuantity43.AsFloat := ANode.FP_Quantity[43];
  cdsBillsTotalPrice43.AsFloat := ANode.FP_TotalPrice[43];
  cdsBillsQuantity44.AsFloat := ANode.FP_Quantity[44];
  cdsBillsTotalPrice44.AsFloat := ANode.FP_TotalPrice[44];
  cdsBillsQuantity45.AsFloat := ANode.FP_Quantity[45];
  cdsBillsTotalPrice45.AsFloat := ANode.FP_TotalPrice[45];
  cdsBillsQuantity46.AsFloat := ANode.FP_Quantity[46];
  cdsBillsTotalPrice46.AsFloat := ANode.FP_TotalPrice[46];
  cdsBillsQuantity47.AsFloat := ANode.FP_Quantity[47];
  cdsBillsTotalPrice47.AsFloat := ANode.FP_TotalPrice[47];
  cdsBillsQuantity48.AsFloat := ANode.FP_Quantity[48];
  cdsBillsTotalPrice48.AsFloat := ANode.FP_TotalPrice[48];
  cdsBillsQuantity49.AsFloat := ANode.FP_Quantity[49];
  cdsBillsTotalPrice49.AsFloat := ANode.FP_TotalPrice[49];
  cdsBillsQuantity50.AsFloat := ANode.FP_Quantity[50];
  cdsBillsTotalPrice50.AsFloat := ANode.FP_TotalPrice[50];
  cdsBills.Post;
  WriteBills(TAllPhaseCacheNode(ANode.FirstChild));
  WriteBills(TAllPhaseCacheNode(ANode.NextSibling));
end;

end.
