unit stgGclGather;

interface

uses
  Classes, stgGatherGclCacheData, ProjectData, BillsTree, sdIDTree;

type
  TProgramHintEvent = procedure (const ATenderName: string) of Object;

  TstgSubTenderFileGatherGcl = class
  private
    FCacheData: TstgGatherGclCacheData;
    FProjectData: TProjectData;
    FCurSubTenderID: Integer;
    FLoadHint: TProgramHintEvent;
    procedure GatherDetailData(AGatherGcl: TstgGatherGcl; ASourceNode: TMeasureBillsIDTreeNode);
    procedure GatherSubTenderTreeNode(ANode: TsdIDTreeNode);
    procedure GatherSubTender(ASubTenderID: Integer);
  public
    procedure GatherTo(AGatherGclCacheData: TstgGatherGclCacheData; ASubTenders: TList);

    property LoadHint: TProgramHintEvent read FLoadHint write FLoadHint;
  end;

implementation

uses Math, Globals, mDataRecord, UtilMethods, SysUtils, XMLDoc, XMLIntf;

{ TstgSubTenderFileGatherGcl }

procedure TstgSubTenderFileGatherGcl.GatherDetailData(
  AGatherGcl: TstgGatherGcl; ASourceNode: TMeasureBillsIDTreeNode);
var
  vSubTender: TstgGclSubTenderStageData;
begin
  vSubTender := AGatherGcl.SafeSubTender(FCurSubTenderID);
  vSubTender.AddDetail(ASourceNode);
end;

procedure TstgSubTenderFileGatherGcl.GatherSubTender(ASubTenderID: Integer);
var
  vNode: TsdIDTreeNode;
begin
  FCurSubTenderID := ASubTenderID;
  vNode := ProjectManager.ProjectsTree.FindNode(ASubTenderID);
  if vNode.Rec.ValueByName('Type').AsInteger = 1 then
  begin;
    FLoadHint(vNode.Rec.ValueByName('Name').AsString);
    FProjectData := OpenProjectManager.FindProjectData(ASubTenderID);
    try
      if not Assigned(FProjectData) then
      begin
        FProjectData := TProjectData.Create;
        FProjectData.OpenForSumUpGather(GetMyProjectsFilePath + vNode.Rec.ValueByName('FileName').AsString);
      end;
      FCacheData.AddSubTender(vNode.Rec);
      GatherSubTenderTreeNode(FProjectData.BillsMeasureData.BillsMeasureTree.FirstNode);
    finally
      if not Assigned(OpenProjectManager.FindProjectData(ASubTenderID)) then
        FProjectData.Free;
    end;
  end;
end;

procedure TstgSubTenderFileGatherGcl.GatherSubTenderTreeNode(
  ANode: TsdIDTreeNode);
var
  vNode: TMeasureBillsIDTreeNode;
  vCur: TstgGatherGcl;
begin
  if not Assigned(ANode) then Exit;

  vNode := TMeasureBillsIDTreeNode(ANode);
  if (not vNode.HasChildren) then
  begin
    if (vNode.Rec.B_Code.AsString <> '') then
    begin
      vCur := FCacheData.AddGatherGcl(vNode.Rec);
      GatherDetailData(vCur, vNode);
    end;
  end
  else
    GatherSubTenderTreeNode(ANode.FirstChild);
  GatherSubTenderTreeNode(ANode.NextSibling);
end;

procedure TstgSubTenderFileGatherGcl.GatherTo(AGatherGclCacheData: TstgGatherGclCacheData;
  ASubTenders: TList);
var
  i, iSubTenderID: Integer;
begin
  FCacheData := AGatherGclCacheData;
  for i := 0 to ASubTenders.Count - 1 do
    GatherSubTender(Integer(ASubTenders.Items[i]));
  FCacheData.CalculateAll;
end;

end.
