unit rmGclBillsBGDm;
// Report Memory Gcl Bills BianGeng DataModule
// For 支表05 清单变更一览表

interface

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

type
  TGclNode = class
  private
    FB_Code: string;
    FIndexCode: string;
    FName: string;
    FUnits: string;
    FPrice: Double;
    FNewPrice: Double;
    FDealQuantity: Double;
    FDealTotalPrice: Double;
    FQuantity: Double;
    FTotalPrice: Double;
    FCurDealQuantity: Double;
    FCurDealTotalPrice: Double;
    FCurQcQuantity: Double;
    FCurQcTotalPrice: Double;
    FCurQcBGLCode: string;
    FCurPcQuantity: Double;
    FCurPcTotalPrice: Double;
    FCurPcBGLCode: string;
    FCurGatherQuantity: Double;
    FCurGatherTotalPrice: Double;
    FEndDealQuantity: Double;
    FEndDealTotalPrice: Double;
    FEndQcQuantity: Double;
    FEndQcTotalPrice: Double;
    FEndQcBGLCode: string;
    FEndPcQuantity: Double;
    FEndPcTotalPrice: Double;
    FEndPcBGLCode: string;
    FEndGatherQuantity: Double;
    FEndGatherTotalPrice: Double;
    FAddDealQuantity: Double;
    FAddDealTotalPrice: Double;
    FAddQcQuantity: Double;
    FAddQcTotalPrice: Double;
    FAddQcBGLCode: string;
    FAddPcQuantity: Double;
    FAddPcTotalPrice: Double;
    FAddPcBGLCode: string;
    FAddGatherQuantity: Double;
    FAddGatherTotalPrice: Double;
  end;

  TrmType = (rmtPhaseDeal, rmtPhaseQc, rmtPhasePc, rmtPhaseGather,
      rmtEndDeal, rmtEndQc, rmtEndPc, rmtEndGather);
  TrmGatherType = (rmgtGather, rmgtFlow);

  TrmGclBillsBGData = class(TDataModule)
    cdsBills: TClientDataSet;
    cdsBillsB_Code: TStringField;
    cdsBillsIndexCode: TStringField;
    cdsBillsName: TWideStringField;
    cdsBillsUnits: TWideStringField;
    cdsBillsPrice: TFloatField;
    cdsBillsNewPrice: TFloatField;
    cdsBillsDealQuantity: TFloatField;
    cdsBillsDealTotalPrice: TFloatField;
    cdsBillsQuantity: TFloatField;
    cdsBillsTotalPrice: TFloatField;
    cdsBillsCurDealQuantity: TFloatField;
    cdsBillsCurDealTotalPrice: TFloatField;
    cdsBillsCurQcQuantity: TFloatField;
    cdsBillsCurQcTotalPrice: TFloatField;
    cdsBillsCurQcBGLCode: TWideStringField;
    cdsBillsCurPcQuantity: TFloatField;
    cdsBillsCurPcTotalPrice: TFloatField;
    cdsBillsCurPcBGLCode: TWideStringField;
    cdsBillsCurGatherQuantity: TFloatField;
    cdsBillsCurGatherTotalPrice: TFloatField;
    cdsBillsEndDealQuantity: TFloatField;
    cdsBillsEndDealTotalPrice: TFloatField;
    cdsBillsEndQcQuantity: TFloatField;
    cdsBillsEndQcTotalPrice: TFloatField;
    cdsBillsEndPcQuantity: TFloatField;
    cdsBillsEndPcTotalPrice: TFloatField;
    cdsBillsEndGatherQuantity: TFloatField;
    cdsBillsEndGatherTotalPrice: TFloatField;
    cdsBillsAddDealQuantity: TFloatField;
    cdsBillsAddDealTotalPrice: TFloatField;
    cdsBillsAddQcQuantity: TFloatField;
    cdsBillsAddQcTotalPrice: TFloatField;
    cdsBillsAddPcQuantity: TFloatField;
    cdsBillsAddPcTotalPrice: TFloatField;
    cdsBillsAddGatherQuantity: TFloatField;
    cdsBillsAddGatherTotalPrice: TFloatField;
    cdsBillsEndQcBGLCode: TWideStringField;
    cdsBillsEndPcBGLCode: TWideStringField;
    cdsBillsAddQcBGLCode: TWideStringField;
    cdsBillsAddPcBGLCode: TWideStringField;
    cdsBillsSerialNo: TIntegerField;
  private
    FProjectData: TProjectData;
    FrmType: TrmType;
    FrmGatherType: TrmGatherType;
    FGclList: TList;

    procedure FilterGclBills(ANode: TsdIDTreeNode);
    procedure FilterBills(ANode: TsdIDTreeNode);
    procedure WriteBills;

    procedure WriteGclBills(APhaseData: TPhaseData; ANode: TsdIDTreeNode;
      APhaseIndex: Integer);
    procedure WritePhaseBills(APhaseData: TPhaseData; ANode: TsdIDTreeNode;
      APhaseIndex: Integer);
    procedure WritePhaseFlowBills(APhaseIndex: Integer);
    procedure WriteFlowBills;
  public
    constructor Create;
    destructor Destroy; override;

    function AssignData(AProjectData: TProjectData;
      ArmType: TrmType; ArmGatherType: TrmGatherType): TDataSet;
  end;

implementation

uses
  ZhAPI, UtilMethods;

{$R *.dfm}

{ TrmGclBillsBGData }

function TrmGclBillsBGData.AssignData(AProjectData: TProjectData;
  ArmType: TrmType; ArmGatherType: TrmGatherType): TDataSet;
begin
  FProjectData := AProjectData;
  FrmType := ArmType;
  FrmGatherType := ArmGatherType;
  cdsBills.Active := True;
  cdsBills.DisableControls;
  try
    cdsBills.EmptyDataSet;
    if FrmGatherType = rmgtGather then
    begin
      FilterBills(FProjectData.BillsMeasureData.BillsMeasureTree.FirstNode);
      WriteBills;
    end
    else if FrmGatherType = rmgtFlow then
      WriteFlowBills;
  finally
    ClearObjects(FGclList);
    cdsBills.EnableControls;
    Result := cdsBills;
  end;
end;

constructor TrmGclBillsBGData.Create;
begin
  inherited Create(nil);
  FGclList := TList.Create;
  cdsBills.IndexFieldNames := 'IndexCode';
end;

destructor TrmGclBillsBGData.Destroy;
begin
  ClearObjects(FGclList);
  FGclList.Free;
  inherited;
end;

procedure TrmGclBillsBGData.FilterBills(ANode: TsdIDTreeNode);
begin
  if not Assigned(ANode) then Exit;
  if ANode.HasChildren then
    FilterBills(ANode.FirstChild)
  else
    FilterGclBills(ANode);
  FilterBills(ANode.NextSibling);
end;

procedure TrmGclBillsBGData.FilterGclBills(ANode: TsdIDTreeNode);

  function CreateGclNode(ARec: TsdDataRecord): TGclNode;
  begin
    Result := TGclNode.Create;
    FGclList.Add(Result);
    Result.FB_Code := ARec.ValueByName('B_Code').AsString;
    Result.FIndexCode := B_CodeToIndexCode(ARec.ValueByName('B_Code').AsString);
    Result.FName := ARec.ValueByName('Name').AsString;
    Result.FUnits := ARec.ValueByName('Units').AsString;
    Result.FPrice := ARec.ValueByName('Price').AsFloat;
    Result.FNewPrice := ARec.ValueByName('NewPrice').AsFloat;
  end;

  function GetGclNode(ARec: TsdDataRecord): TGclNode;
  var
    I: Integer;
    GclNode: TGclNode;
  begin
    Result := nil;
    for I := 0 to FGclList.Count - 1 do
    begin
      GclNode := TGclNode(FGclList.Items[I]);
      if SameText(GclNode.FB_Code, ARec.ValueByName('B_Code').AsString) and
          SameText(GclNode.FName, ARec.ValueByName('Name').AsString) and
          SameText(GclNode.FUnits, ARec.ValueByName('Units').AsString) and
          (GclNode.FPrice = ARec.ValueByName('Price').AsFloat) then
      begin
        Result := GclNode;
        Break;
      end;
    end;
    if not Assigned(Result) then
      Result := CreateGclNode(ARec);
  end;

  procedure LoadPhaseData(AGclNode: TGclNode; AID: Integer);
  var
    Rec: TsdDataRecord;
  begin
    Rec := TProjectData(FProjectData).PhaseData.StageData.StageRecord(AID);
    if not Assigned(Rec) then Exit;
    AGclNode.FCurDealQuantity := AGclNode.FCurDealQuantity + Rec.ValueByName('DealQuantity').AsFloat;
    AGclNode.FCurDealTotalPrice := AGclNode.FCurDealTotalPrice + Rec.ValueByName('DealTotalPrice').AsFloat;
    AGclNode.FCurQcQuantity := AGclNode.FCurQcQuantity + Rec.ValueByName('QcQuantity').AsFloat;
    AGclNode.FCurQcTotalPrice := AGclNode.FCurQcTotalPrice + Rec.ValueByName('QcTotalPrice').AsFloat;
    AGclNode.FCurQcBGLCode := MergeRelaBGL(AGclNode.FCurQcBGLCode, Rec.ValueByName('QcBGLCode').AsString);
    AGclNode.FCurPcQuantity := AGclNode.FCurPcQuantity + Rec.ValueByName('PcQuantity').AsFloat;
    AGclNode.FCurPcTotalPrice := AGclNode.FCurPcTotalPrice + Rec.ValueByName('PcTotalPrice').AsFloat;
    AGclNode.FCurPcBGLCode := MergeRelaBGL(AGclNode.FCurPcBGLCode, Rec.ValueByName('PcBGLCode').AsString);
    AGclNode.FCurGatherQuantity := AGclNode.FCurGatherQuantity + Rec.ValueByName('GatherQuantity').AsFloat;
    AGclNode.FCurGatherTotalPrice := AGclNode.FCurGatherTotalPrice + Rec.ValueByName('GatherTotalPrice').AsFloat;

    AGclNode.FEndDealQuantity := AGclNode.FEndDealQuantity + Rec.ValueByName('EndDealQuantity').AsFloat;
    AGclNode.FEndDealTotalPrice := AGclNode.FEndDealTotalPrice + Rec.ValueByName('EndDealTotalPrice').AsFloat;
    AGclNode.FEndQcQuantity := AGclNode.FEndQcQuantity + Rec.ValueByName('EndQcQuantity').AsFloat;
    AGclNode.FEndQcTotalPrice := AGclNode.FEndQcTotalPrice + Rec.ValueByName('EndQcTotalPrice').AsFloat;
    AGclNode.FEndQcBGLCode := MergeRelaBGL(AGclNode.FEndQcBGLCode, Rec.ValueByName('EndQcBGLCode').AsString);
    AGclNode.FEndPcQuantity := AGclNode.FEndPcQuantity + Rec.ValueByName('EndPcQuantity').AsFloat;
    AGclNode.FEndPcTotalPrice := AGclNode.FEndPcTotalPrice + Rec.ValueByName('EndPcTotalPrice').AsFloat;
    AGclNode.FEndPcBGLCode := MergeRelaBGL(AGclNode.FEndPcBGLCode, Rec.ValueByName('EndPcBGLCode').AsString);
    AGclNode.FEndGatherQuantity := AGclNode.FEndGatherQuantity + Rec.ValueByName('EndGatherQuantity').AsFloat;
    AGclNode.FEndGatherTotalPrice := AGclNode.FEndGatherTotalPrice + Rec.ValueByName('EndGatherTotalPrice').AsFloat;
  end;

  procedure LoadMeasureData(AGclNode: TGclNode; ARec: TsdDataRecord);
  begin
    AGclNode.FQuantity := AGclNode.FQuantity + ARec.ValueByName('Quantity').AsFloat;
    AGclNode.FTotalPrice := AGclNode.FTotalPrice + ARec.ValueByName('TotalPrice').AsFloat;
    if TProjectData(FProjectData).ProjProperties.PhaseCount = 0 then Exit;
    LoadPhaseData(AGclNode, ARec.ValueByName('ID').AsInteger);
    AGclNode.FAddDealQuantity := AGclNode.FAddDealQuantity + ARec.ValueByName('AddDealQuantity').AsFloat;
    AGclNode.FAddDealTotalPrice := AGclNode.FAddDealTotalPrice + ARec.ValueByName('AddDealTotalPrice').AsFloat;
    AGclNode.FAddQcQuantity := AGclNode.FAddQcQuantity + ARec.ValueByName('AddQcQuantity').AsFloat;
    AGclNode.FAddQcTotalPrice := AGclNode.FAddQcTotalPrice + ARec.ValueByName('AddQcTotalPrice').AsFloat;
    AGclNode.FAddQcBGLCode := MergeRelaBGL(AGclNode.FAddQcBGLCode, ARec.ValueByName('AddQcBGLCode').AsString);
    AGclNode.FAddPcQuantity := AGclNode.FAddPcQuantity + ARec.ValueByName('AddPcQuantity').AsFloat;
    AGclNode.FAddPcTotalPrice := AGclNode.FAddPcTotalPrice + ARec.ValueByName('AddPcTotalPrice').AsFloat;
    AGclNode.FAddPcBGLCode := MergeRelaBGL(AGclNode.FAddPcBGLCode, ARec.ValueByName('AddPcBGLCode').AsString);
    AGclNode.FAddGatherQuantity := AGclNode.FAddGatherQuantity + ARec.ValueByName('AddGatherQuantity').AsFloat;
    AGclNode.FAddGatherTotalPrice := AGclNode.FAddGatherTotalPrice + ARec.ValueByName('AddGatherTotalPrice').AsFloat;
  end;

  procedure LoadDealData(AGclNode: TGclNode; AB_Code: string);
  var
    DealRec: TsdDataRecord;
  begin
    DealRec := TProjectData(FProjectData).DealBillsData.DealRecord(AB_Code);
    if Assigned(DealRec) then
    begin
      AGclNode.FDealQuantity := DealRec.ValueByName('Quantity').AsFloat;
      AGclNode.FDealTotalPrice := DealRec.ValueByName('TotalPrice').AsFloat;
    end;
  end;

var
  Rec, StageRec: TsdDataRecord;
  GclNode: TGclNode;
begin
  if not Assigned(ANode) then Exit;
  Rec := ANode.Rec;
  if Rec.ValueByName('B_Code').AsString = '' then Exit;
  StageRec := TProjectData(FProjectData).PhaseData.StageData.StageRecord(ANode.ID);
  if not Assigned(StageRec) then Exit;

  case FrmType of
    rmtPhaseDeal: if StageRec.ValueByName('DealTotalPrice').AsFloat = 0 then Exit;
    rmtPhaseQc: if StageRec.ValueByName('QcTotalPrice').AsFloat = 0 then Exit;
    rmtPhasePc: if StageRec.ValueByName('PcTotalPrice').AsFloat = 0 then Exit;
    rmtPhaseGather: if StageRec.ValueByName('GatherTotalPrice').AsFloat = 0 then Exit;
    rmtEndDeal: if StageRec.ValueByName('EndDealTotalPrice').AsFloat = 0 then Exit;
    rmtEndQc: if StageRec.ValueByName('EndQcTotalPrice').AsFloat = 0 then Exit;
    rmtEndPc: if StageRec.ValueByName('EndPcTotalPrice').AsFloat = 0 then Exit;
    rmtEndGather: if StageRec.ValueByName('EndGatherTotalPrice').AsFloat = 0 then Exit;
  end;

  GclNode := GetGclNode(Rec);
  LoadDealData(GclNode, Rec.ValueByName('B_Code').AsString);
  LoadMeasureData(GclNode, Rec);
end;

procedure TrmGclBillsBGData.WriteBills;
var
  I: Integer;
  GclNode: TGclNode;
begin
  for I := 0 to FGclList.Count - 1 do
  begin
    GclNode := TGclNode(FGclList.Items[I]);
    cdsBills.Append;
    cdsBillsB_Code.AsString := GclNode.FB_Code;
    cdsBillsIndexCode.AsString := GclNode.FIndexCode;
    cdsBillsName.AsString := GclNode.FName;
    cdsBillsUnits.AsString := GclNode.FUnits;
    cdsBillsPrice.AsFloat := GclNode.FPrice;
    cdsBillsNewPrice.AsFloat := GclNode.FNewPrice;

    cdsBillsDealQuantity.AsFloat := GclNode.FDealQuantity;
    cdsBillsDealTotalPrice.AsFloat := GclNode.FDealTotalPrice;

    cdsBillsQuantity.AsFloat := GclNode.FQuantity;
    cdsBillsTotalPrice.AsFloat := GclNode.FTotalPrice;

    cdsBillsCurDealQuantity.AsFloat := GclNode.FCurDealQuantity;
    cdsBillsCurDealTotalPrice.AsFloat := GclNode.FCurDealTotalPrice;
    cdsBillsCurQcQuantity.AsFloat := GclNode.FCurQcQuantity;
    cdsBillsCurQcTotalPrice.AsFloat := GclNode.FCurQcTotalPrice;
    cdsBillsCurQcBGLCode.AsString := GclNode.FCurQcBGLCode;
    cdsBillsCurPcQuantity.AsFloat := GclNode.FCurPcQuantity;
    cdsBillsCurPcTotalPrice.AsFloat := GclNode.FCurPcTotalPrice;
    cdsBillsCurPcBGLCode.AsString := GclNode.FCurPcBGLCode;
    cdsBillsCurGatherQuantity.AsFloat := GclNode.FCurGatherQuantity;
    cdsBillsCurGatherTotalPrice.AsFloat := GclNode.FCurGatherTotalPrice;

    cdsBillsEndDealQuantity.AsFloat := GclNode.FEndDealQuantity;
    cdsBillsEndDealTotalPrice.AsFloat := GclNode.FEndDealTotalPrice;
    cdsBillsEndQcQuantity.AsFloat := GclNode.FEndQcQuantity;
    cdsBillsEndQcTotalPrice.AsFloat := GclNode.FEndQcTotalPrice;
    cdsBillsEndQcBGLCode.AsString := GclNode.FEndQcBGLCode;
    cdsBillsEndPcQuantity.AsFloat := GclNode.FEndPcQuantity;
    cdsBillsEndPcTotalPrice.AsFloat := GclNode.FEndPcTotalPrice;
    cdsBillsEndPcBGLCode.AsString := GclNode.FEndPcBGLCode;
    cdsBillsEndGatherQuantity.AsFloat := GclNode.FEndGatherQuantity;
    cdsBillsEndGatherTotalPrice.AsFloat := GclNode.FEndGatherTotalPrice;

    cdsBillsAddDealQuantity.AsFloat := GclNode.FAddDealQuantity;
    cdsBillsAddDealTotalPrice.AsFloat := GclNode.FAddDealTotalPrice;
    cdsBillsAddQcQuantity.AsFloat := GclNode.FAddQcQuantity;
    cdsBillsAddQcTotalPrice.AsFloat := GclNode.FAddQcTotalPrice;
    cdsBillsAddQcBGLCode.AsString := GclNode.FAddQcBGLCode;
    cdsBillsAddPcQuantity.AsFloat := GclNode.FAddPcQuantity;
    cdsBillsAddPcTotalPrice.AsFloat := GclNode.FAddPcTotalPrice;
    cdsBillsAddPcBGLCode.AsString := GclNode.FAddPcBGLCode;    
    cdsBillsAddGatherQuantity.AsFloat := GclNode.FAddGatherQuantity;
    cdsBillsAddGatherTotalPrice.AsFloat := GclNode.FAddGatherTotalPrice;
    cdsBills.Post;
  end;
end;

procedure TrmGclBillsBGData.WriteFlowBills;
var
  iPhase: Integer;
begin
  for iPhase := 1 to FProjectData.PhaseIndex do
    WritePhaseFlowBills(iPhase);
end;

procedure TrmGclBillsBGData.WriteGclBills(APhaseData: TPhaseData;
  ANode: TsdIDTreeNode; APhaseIndex: Integer);

  procedure WriteGclBillsData(ARec, AStageRec: TsdDataRecord);
  begin
    cdsBills.Append;
    cdsBillsB_Code.AsString := ARec.ValueByName('B_Code').AsString;
    cdsBillsIndexCode.AsString := B_CodeToIndexCode(ARec.ValueByName('B_Code').AsString);
    cdsBillsName.AsString := ARec.ValueByName('Name').AsString;
    cdsBillsUnits.AsString := ARec.ValueByName('Units').AsString;
    cdsBillsPrice.AsFloat := ARec.ValueByName('Price').AsFloat;
    cdsBillsNewPrice.AsFloat := ARec.ValueByName('NewPrice').AsFloat;

    cdsBillsQuantity.AsFloat := ARec.ValueByName('Quantity').AsFloat;
    cdsBillsTotalPrice.AsFloat := ARec.ValueByName('TotalPrice').AsFloat;

    cdsBillsCurDealQuantity.AsFloat := AStageRec.ValueByName('DealQuantity').AsFloat;
    cdsBillsCurDealTotalPrice.AsFloat := AStageRec.ValueByName('DealTotalPrice').AsFloat;
    cdsBillsCurQcQuantity.AsFloat := AStageRec.ValueByName('QcQuantity').AsFloat;
    cdsBillsCurQcTotalPrice.AsFloat := AStageRec.ValueByName('QcTotalPrice').AsFloat;
    cdsBillsCurQcBGLCode.AsString := AStageRec.ValueByName('QcBGLCode').AsString;
    cdsBillsCurPcQuantity.AsFloat := AStageRec.ValueByName('PcQuantity').AsFloat;
    cdsBillsCurPcTotalPrice.AsFloat := AStageRec.ValueByName('PcTotalPrice').AsFloat;
    cdsBillsCurPcBGLCode.AsString := AStageRec.ValueByName('PcBGLCode').AsString;
    cdsBillsCurGatherQuantity.AsFloat := AStageRec.ValueByName('GatherQuantity').AsFloat;
    cdsBillsCurGatherTotalPrice.AsFloat := AStageRec.ValueByName('GatherTotalPrice').AsFloat;

    cdsBillsEndDealQuantity.AsFloat := AStageRec.ValueByName('EndDealQuantity').AsFloat;
    cdsBillsEndDealTotalPrice.AsFloat := AStageRec.ValueByName('EndDealTotalPrice').AsFloat;
    cdsBillsEndQcQuantity.AsFloat := AStageRec.ValueByName('EndQcQuantity').AsFloat;
    cdsBillsEndQcTotalPrice.AsFloat := AStageRec.ValueByName('EndQcTotalPrice').AsFloat;
    cdsBillsEndQcBGLCode.AsString := AStageRec.ValueByName('EndQcBGLCode').AsString;
    cdsBillsEndPcQuantity.AsFloat := AStageRec.ValueByName('EndPcQuantity').AsFloat;
    cdsBillsEndPcTotalPrice.AsFloat := AStageRec.ValueByName('EndPcTotalPrice').AsFloat;
    cdsBillsEndPcBGLCode.AsString := AStageRec.ValueByName('EndPcBGLCode').AsString;
    cdsBillsEndGatherQuantity.AsFloat := AStageRec.ValueByName('EndGatherQuantity').AsFloat;
    cdsBillsEndGatherTotalPrice.AsFloat := AStageRec.ValueByName('EndGatherTotalPrice').AsFloat;

    cdsBillsAddDealQuantity.AsFloat := ARec.ValueByName('AddDealQuantity').AsFloat;
    cdsBillsAddDealTotalPrice.AsFloat := ARec.ValueByName('AddDealTotalPrice').AsFloat;
    cdsBillsAddQcQuantity.AsFloat := ARec.ValueByName('AddQcQuantity').AsFloat;
    cdsBillsAddQcTotalPrice.AsFloat := ARec.ValueByName('AddQcTotalPrice').AsFloat;
    cdsBillsAddQcBGLCode.AsString := ARec.ValueByName('AddQcBGLCode').AsString;
    cdsBillsAddPcQuantity.AsFloat := ARec.ValueByName('AddPcQuantity').AsFloat;
    cdsBillsAddPcTotalPrice.AsFloat := ARec.ValueByName('AddPcTotalPrice').AsFloat;
    cdsBillsAddPcBGLCode.AsString := ARec.ValueByName('AddPcBGLCode').AsString;
    cdsBillsAddGatherQuantity.AsFloat := ARec.ValueByName('AddGatherQuantity').AsFloat;
    cdsBillsAddGatherTotalPrice.AsFloat := ARec.ValueByName('AddGatherTotalPrice').AsFloat;

    cdsBillsSerialNo.AsInteger := APhaseIndex;
    cdsBills.Post;
  end;

var
  Rec, StageRec: TsdDataRecord;
begin
  if not Assigned(ANode) then Exit;
  Rec := ANode.Rec;
  if Rec.ValueByName('B_Code').AsString = '' then Exit;
  StageRec := APhaseData.StageData.StageRecord(ANode.ID);
  if not Assigned(StageRec) then Exit;

  case FrmType of
    rmtPhaseDeal: if StageRec.ValueByName('DealTotalPrice').AsFloat = 0 then Exit;
    rmtPhaseQc: if StageRec.ValueByName('QcTotalPrice').AsFloat = 0 then Exit;
    rmtPhasePc: if StageRec.ValueByName('PcTotalPrice').AsFloat = 0 then Exit;
    rmtPhaseGather: if StageRec.ValueByName('GatherTotalPrice').AsFloat = 0 then Exit;
    rmtEndDeal: if StageRec.ValueByName('EndDealTotalPrice').AsFloat = 0 then Exit;
    rmtEndQc: if StageRec.ValueByName('EndQcTotalPrice').AsFloat = 0 then Exit;
    rmtEndPc: if StageRec.ValueByName('EndPcTotalPrice').AsFloat = 0 then Exit;
    rmtEndGather: if StageRec.ValueByName('EndGatherTotalPrice').AsFloat = 0 then Exit;
  end;

  WriteGclBillsData(Rec, StageRec);
end;

procedure TrmGclBillsBGData.WritePhaseBills(APhaseData: TPhaseData;
  ANode: TsdIDTreeNode; APhaseIndex: Integer);
begin
  if not Assigned(ANode) then Exit;
  if ANode.HasChildren then
    WritePhaseBills(APhaseData, ANode.FirstChild, APhaseIndex)
  else
    WriteGclBills(APhaseData, ANode, APhaseIndex);
  WritePhaseBills(APhaseData, ANode.NextSibling, APhaseIndex);
end;

procedure TrmGclBillsBGData.WritePhaseFlowBills(APhaseIndex: Integer);
var
  PhaseData: TPhaseData;
begin
  PhaseData := TPhaseData.Create(FProjectData);
  try
    PhaseData.SimpleOpen(Format('%s\Phase%d.dat', [FProjectData.TempPath, APhaseIndex]));
    cdsBills.Append;
    cdsBillsName.AsString := Format('第 %d 期', [APhaseIndex]);
    cdsBillsSerialNo.AsInteger := APhaseIndex;
    cdsBills.Post;
    WritePhaseBills(PhaseData, FProjectData.BillsCompileData.BillsCompileTree.FirstNode, APhaseIndex);
  finally
    PhaseData.Free;
  end;
end;

end.
