unit BillsPosTree;
// 清单--部位明细树 (同导入计量Saas相关，不做他用)

interface

uses
  Classes, CacheTree, ZhAPI;

type
  TPosNode = class
  private
    FQuantity: Double;
    FMemoStr: string;
    FName: string;
    FDrawingCode: string;
  public
    property Name: string read FName write FName;
    property Quantity: Double read FQuantity write FQuantity;
    property MemoStr: string read FMemoStr write FMemoStr;
    property DrawingCode: string read FDrawingCode write FDrawingCode;
  end;

  TBillsPosTreeNode = class(TCacheNode)
  private
    FPosList: TList;

    FQuantity: Double;
    FPrice: Double;
    FDgnQty1: Double;
    FDgnQty2: Double;
    FUnits: string;
    FDrawingCode: string;
    FName: string;
    FCode: string;
    FMemoStr: string;
    FB_Code: string;
    FTotalPrice: Double;
    function GetPos(AIndex: Integer): TPosNode;
    function GetPosCount: Integer;
  public
    constructor Create(ACacheTree: TCacheTree; AID: Integer); override;
    destructor Destroy; override;

    function AddPos: TPosNode;
    procedure Calculate;

    property Code: string read FCode write FCode;
    property B_Code: string read FB_Code write FB_Code;
    property Name: string read FName write FName;
    property Units: string read FUnits write FUnits;
    property Price: Double read FPrice write FPrice;
    property Quantity: Double read FQuantity write FQuantity;
    property TotalPrice: Double read FTotalPrice write FTotalPrice;
    property DgnQty1: Double read FDgnQty1 write FDgnQty1;
    property DgnQty2: Double read FDgnQty2 write FDgnQty2;
    property DrawingCode: string read FDrawingCode write FDrawingCode;
    property MemoStr: string read FMemoStr write FMemoStr;

    property PosCount: Integer read GetPosCount;
    property Pos[AIndex: Integer]: TPosNode read GetPos;
  end;

  TBillsPosTree = class(TCacheTree)
  private
  protected
    function GetNewNode: TCacheNode; override;
  public
    procedure CalculateAll;
  end;

implementation

{ TBillsPosTreeNode }

function TBillsPosTreeNode.AddPos: TPosNode;
begin
  Result := TPosNode.Create;
  FPosList.Add(Result);
end;

procedure TBillsPosTreeNode.Calculate;
var
  fQuantity: Double;
  iPos: Integer;
begin
  fQuantity := 0;
  for iPos := 0 to FPosList.Count - 1 do
    fQuantity := fQuantity + TPosNode(FPosList.Items[iPos]).Quantity;
  Quantity := fQuantity;
  TotalPrice := Quantity * Price;
end;

constructor TBillsPosTreeNode.Create(ACacheTree: TCacheTree; AID: Integer);
begin
  inherited;
  FPosList := TList.Create;
end;

destructor TBillsPosTreeNode.Destroy;
begin
  ClearObjects(FPosList);
  FPosList.Free;
  inherited;
end;

function TBillsPosTreeNode.GetPos(AIndex: Integer): TPosNode;
begin
  Result := TPosNode(FPosList.Items[AIndex]);
end;

function TBillsPosTreeNode.GetPosCount: Integer;
begin
  Result := FPosList.Count;
end;

{ TBillsPosTree }

procedure TBillsPosTree.CalculateAll;
var
  i: Integer;
  vNode: TBillsPosTreeNode;
begin
  for i := 0 to CacheNodes.Count - 1 do
  begin
    vNode := TBillsPosTreeNode(CacheNodes[i]);
    if (vNode.PosCount > 0) then
      vNode.Calculate;
  end;
end;

function TBillsPosTree.GetNewNode: TCacheNode;
begin
  Result := TBillsPosTreeNode.Create(Self, GetNewNodeID);
  CacheNodes.Add(Result);
end;

end.
