unit rmGclBillsAddDm;

interface

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

type
  TP_DoubleArray = array [1..50] of Double;

  TGclAddNode = class
    FB_Code: string;
    FIndexCode: string;
    FName: string;
    FUnits: string;
    FPrice: Double;
    FDealQuantity: Double;
    FDealTotalPrice: Double;
    FQuantity: Double;
    FTotalPrice: Double;
    FP_Quantity: TP_DoubleArray;
    FP_TotalPrice: TP_DoubleArray;
  end;

  TrmGclBillsAddData = class(TDataModule)
    cdsBills: TClientDataSet;
    cdsBillsB_Code: TStringField;
    cdsBillsIndexCode: TStringField;
    cdsBillsName: TWideStringField;
    cdsBillsUnits: TWideStringField;
    cdsBillsPrice: TFloatField;
    cdsBillsDealQuantity: TFloatField;
    cdsBillsDealTotalPrice: 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;
  private
    FProjectData: TProjectData;
    FGclAddList: TList;

    function FindGclNode(ARec: TsdDataRecord): TGclAddNode;
    procedure FilterGclBills(ANode: TsdIDTreeNode);
    procedure FilterBills(ANode: TsdIDTreeNode);

    procedure LoadStageData(StageData: TStageData; APhaseIndex: Integer);
    procedure LoadPhaseData(APhaseIndex: Integer);

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

implementation

uses
  ZhAPI, UtilMethods;

{$R *.dfm}

{ TrmGclBillsAddData }

function TrmGclBillsAddData.AssignData(AProjectData: TProjectData): TDataSet;
var
  iPhase: Integer;
begin
  FProjectData := AProjectData;
  FGclAddList := TList.Create;
  cdsBills.DisableControls;
  cdsBills.Active := True;
  cdsBills.EmptyDataSet;
  try
    FilterBills(FProjectData.BillsCompileData.BillsCompileTree.FirstNode);
    for iPhase := 1 to FProjectData.ProjProperties.PhaseCount do
      LoadPhaseData(iPhase);
    WriteBills;
  finally
    Result := cdsBills;
    cdsBills.EnableControls;
    ClearObjects(FGclAddList);
    FGclAddList.Free;
  end;
end;

procedure TrmGclBillsAddData.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 TrmGclBillsAddData.FilterGclBills(ANode: TsdIDTreeNode);

  function CreateGclNode(ARec: TsdDataRecord): TGclAddNode;
  begin
    Result := TGclAddNode.Create;
    FGclAddList.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;
  end;

  function GetGclNode(ARec: TsdDataRecord): TGclAddNode;
  begin
    Result := FindGclNode(ARec);
    if not Assigned(Result) then
      Result := CreateGclNode(ARec);
  end;

  procedure LoadDealData(AGclNode: TGclAddNode; 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: TGclAddNode;
begin
  if not Assigned(ANode) then Exit;
  Rec := ANode.Rec;
  if Rec.ValueByName('B_Code').AsString = '' then Exit;
  GclNode := GetGclNode(Rec);
  LoadDealData(GclNode, Rec.ValueByName('B_Code').AsString);
  GclNode.FQuantity := GclNode.FQuantity + Rec.ValueByName('Quantity').AsFloat;
  GclNode.FTotalPrice := GclNode.FTotalPrice + Rec.ValueByName('TotalPrice').AsFloat;
end;

function TrmGclBillsAddData.FindGclNode(ARec: TsdDataRecord): TGclAddNode;
var
  I: Integer;
  GclNode: TGclAddNode;
begin
  Result := nil;
  for I := 0 to FGclAddList.Count - 1 do
  begin
    GclNode := TGclAddNode(FGclAddList.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;
end;

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

procedure TrmGclBillsAddData.LoadStageData(
  StageData: TStageData; APhaseIndex: Integer);
var
  i: Integer;
  StageRec: TsdDataRecord;
  stnNode: TsdIDTreeNode;
  GclNode: TGclAddNode;
begin
  for i := 0 to StageData.sddStage.RecordCount - 1 do
  begin
    StageRec := StageData.sddStage.Records[i];
    with FProjectData.BillsCompileData.BillsCompileTree do
      stnNode := FindNode(StageRec.ValueByName('BillsID').AsInteger);
    // 非最底层工程量清单不添加数据
    if stnNode.HasChildren or (stnNode.Rec.ValueByName('B_Code').AsString = '') then Continue;
    GclNode := FindGclNode(stnNode.Rec);
    if Assigned(GclNode) then
    begin
      GclNode.FP_Quantity[APhaseIndex] := StageRec.ValueByName('GatherQuantity').AsFloat;
      GclNode.FP_TotalPrice[APhaseIndex] := StageRec.ValueByName('GatherTotalPrice').AsFloat;
    end;
  end;
end;

procedure TrmGclBillsAddData.WriteBills;
var
  iGcl: Integer;
  GclAddNode: TGclAddNode;
begin
  for iGcl := 0 to FGclAddList.Count - 1 do
  begin
    GclAddNode := TGclAddNode(FGclAddList.Items[iGcl]);
    cdsBills.Append;
    cdsBillsB_Code.AsString := GclAddNode.FB_Code;
    cdsBillsIndexCode.AsString := GclAddNode.FIndexCode;
    cdsBillsName.AsString := GclAddNode.FName;
    cdsBillsUnits.AsString := GclAddNode.FUnits;
    cdsBillsPrice.AsFloat := GclAddNode.FPrice;
    cdsBillsDealQuantity.AsFloat := GclAddNode.FDealQuantity;
    cdsBillsDealTotalPrice.AsFloat := GclAddNode.FDealTotalPrice;
    cdsBillsQuantity.AsFloat := GclAddNode.FQuantity;
    cdsBillsTotalPrice.AsFloat := GclAddNode.FTotalPrice;
    cdsBillsQuantity1.AsFloat := GclAddNode.FP_Quantity[1];
    cdsBillsTotalPrice1.AsFloat := GclAddNode.FP_TotalPrice[1];
    cdsBillsQuantity2.AsFloat := GclAddNode.FP_Quantity[2];
    cdsBillsTotalPrice2.AsFloat := GclAddNode.FP_TotalPrice[2];
    cdsBillsQuantity3.AsFloat := GclAddNode.FP_Quantity[3];
    cdsBillsTotalPrice3.AsFloat := GclAddNode.FP_TotalPrice[3];
    cdsBillsQuantity4.AsFloat := GclAddNode.FP_Quantity[4];
    cdsBillsTotalPrice4.AsFloat := GclAddNode.FP_TotalPrice[4];
    cdsBillsQuantity5.AsFloat := GclAddNode.FP_Quantity[5];
    cdsBillsTotalPrice5.AsFloat := GclAddNode.FP_TotalPrice[5];
    cdsBillsQuantity6.AsFloat := GclAddNode.FP_Quantity[6];
    cdsBillsTotalPrice6.AsFloat := GclAddNode.FP_TotalPrice[6];
    cdsBillsQuantity7.AsFloat := GclAddNode.FP_Quantity[7];
    cdsBillsTotalPrice7.AsFloat := GclAddNode.FP_TotalPrice[7];
    cdsBillsQuantity8.AsFloat := GclAddNode.FP_Quantity[8];
    cdsBillsTotalPrice8.AsFloat := GclAddNode.FP_TotalPrice[8];
    cdsBillsQuantity9.AsFloat := GclAddNode.FP_Quantity[9];
    cdsBillsTotalPrice9.AsFloat := GclAddNode.FP_TotalPrice[9];
    cdsBillsQuantity10.AsFloat := GclAddNode.FP_Quantity[10];
    cdsBillsTotalPrice10.AsFloat := GclAddNode.FP_TotalPrice[10];
    cdsBillsQuantity11.AsFloat := GclAddNode.FP_Quantity[11];
    cdsBillsTotalPrice11.AsFloat := GclAddNode.FP_TotalPrice[11];
    cdsBillsQuantity12.AsFloat := GclAddNode.FP_Quantity[12];
    cdsBillsTotalPrice12.AsFloat := GclAddNode.FP_TotalPrice[12];
    cdsBillsQuantity13.AsFloat := GclAddNode.FP_Quantity[13];
    cdsBillsTotalPrice13.AsFloat := GclAddNode.FP_TotalPrice[13];
    cdsBillsQuantity14.AsFloat := GclAddNode.FP_Quantity[14];
    cdsBillsTotalPrice14.AsFloat := GclAddNode.FP_TotalPrice[14];
    cdsBillsQuantity15.AsFloat := GclAddNode.FP_Quantity[15];
    cdsBillsTotalPrice15.AsFloat := GclAddNode.FP_TotalPrice[15];
    cdsBillsQuantity16.AsFloat := GclAddNode.FP_Quantity[16];
    cdsBillsTotalPrice16.AsFloat := GclAddNode.FP_TotalPrice[16];
    cdsBillsQuantity17.AsFloat := GclAddNode.FP_Quantity[17];
    cdsBillsTotalPrice17.AsFloat := GclAddNode.FP_TotalPrice[17];
    cdsBillsQuantity18.AsFloat := GclAddNode.FP_Quantity[18];
    cdsBillsTotalPrice18.AsFloat := GclAddNode.FP_TotalPrice[18];
    cdsBillsQuantity19.AsFloat := GclAddNode.FP_Quantity[19];
    cdsBillsTotalPrice19.AsFloat := GclAddNode.FP_TotalPrice[19];
    cdsBillsQuantity20.AsFloat := GclAddNode.FP_Quantity[20];
    cdsBillsTotalPrice20.AsFloat := GclAddNode.FP_TotalPrice[20];
    cdsBillsQuantity21.AsFloat := GclAddNode.FP_Quantity[21];
    cdsBillsTotalPrice21.AsFloat := GclAddNode.FP_TotalPrice[21];
    cdsBillsQuantity22.AsFloat := GclAddNode.FP_Quantity[22];
    cdsBillsTotalPrice22.AsFloat := GclAddNode.FP_TotalPrice[22];
    cdsBillsQuantity23.AsFloat := GclAddNode.FP_Quantity[23];
    cdsBillsTotalPrice23.AsFloat := GclAddNode.FP_TotalPrice[23];
    cdsBillsQuantity24.AsFloat := GclAddNode.FP_Quantity[24];
    cdsBillsTotalPrice24.AsFloat := GclAddNode.FP_TotalPrice[24];
    cdsBillsQuantity25.AsFloat := GclAddNode.FP_Quantity[25];
    cdsBillsTotalPrice25.AsFloat := GclAddNode.FP_TotalPrice[25];
    cdsBillsQuantity26.AsFloat := GclAddNode.FP_Quantity[26];
    cdsBillsTotalPrice26.AsFloat := GclAddNode.FP_TotalPrice[26];
    cdsBillsQuantity27.AsFloat := GclAddNode.FP_Quantity[27];
    cdsBillsTotalPrice27.AsFloat := GclAddNode.FP_TotalPrice[27];
    cdsBillsQuantity28.AsFloat := GclAddNode.FP_Quantity[28];
    cdsBillsTotalPrice28.AsFloat := GclAddNode.FP_TotalPrice[28];
    cdsBillsQuantity29.AsFloat := GclAddNode.FP_Quantity[29];
    cdsBillsTotalPrice29.AsFloat := GclAddNode.FP_TotalPrice[29];
    cdsBillsQuantity30.AsFloat := GclAddNode.FP_Quantity[30];
    cdsBillsTotalPrice30.AsFloat := GclAddNode.FP_TotalPrice[30];
    cdsBillsQuantity31.AsFloat := GclAddNode.FP_Quantity[31];
    cdsBillsTotalPrice31.AsFloat := GclAddNode.FP_TotalPrice[31];
    cdsBillsQuantity32.AsFloat := GclAddNode.FP_Quantity[32];
    cdsBillsTotalPrice32.AsFloat := GclAddNode.FP_TotalPrice[32];
    cdsBillsQuantity33.AsFloat := GclAddNode.FP_Quantity[33];
    cdsBillsTotalPrice33.AsFloat := GclAddNode.FP_TotalPrice[33];
    cdsBillsQuantity34.AsFloat := GclAddNode.FP_Quantity[34];
    cdsBillsTotalPrice34.AsFloat := GclAddNode.FP_TotalPrice[34];
    cdsBillsQuantity35.AsFloat := GclAddNode.FP_Quantity[35];
    cdsBillsTotalPrice35.AsFloat := GclAddNode.FP_TotalPrice[35];
    cdsBillsQuantity36.AsFloat := GclAddNode.FP_Quantity[36];
    cdsBillsTotalPrice36.AsFloat := GclAddNode.FP_TotalPrice[36];
    cdsBillsQuantity37.AsFloat := GclAddNode.FP_Quantity[37];
    cdsBillsTotalPrice37.AsFloat := GclAddNode.FP_TotalPrice[37];
    cdsBillsQuantity38.AsFloat := GclAddNode.FP_Quantity[38];
    cdsBillsTotalPrice38.AsFloat := GclAddNode.FP_TotalPrice[38];
    cdsBillsQuantity39.AsFloat := GclAddNode.FP_Quantity[39];
    cdsBillsTotalPrice39.AsFloat := GclAddNode.FP_TotalPrice[39];
    cdsBillsQuantity40.AsFloat := GclAddNode.FP_Quantity[40];
    cdsBillsTotalPrice40.AsFloat := GclAddNode.FP_TotalPrice[40];
    cdsBillsQuantity41.AsFloat := GclAddNode.FP_Quantity[41];
    cdsBillsTotalPrice41.AsFloat := GclAddNode.FP_TotalPrice[41];
    cdsBillsQuantity42.AsFloat := GclAddNode.FP_Quantity[42];
    cdsBillsTotalPrice42.AsFloat := GclAddNode.FP_TotalPrice[42];
    cdsBillsQuantity43.AsFloat := GclAddNode.FP_Quantity[43];
    cdsBillsTotalPrice43.AsFloat := GclAddNode.FP_TotalPrice[43];
    cdsBillsQuantity44.AsFloat := GclAddNode.FP_Quantity[44];
    cdsBillsTotalPrice44.AsFloat := GclAddNode.FP_TotalPrice[44];
    cdsBillsQuantity45.AsFloat := GclAddNode.FP_Quantity[45];
    cdsBillsTotalPrice45.AsFloat := GclAddNode.FP_TotalPrice[45];
    cdsBillsQuantity46.AsFloat := GclAddNode.FP_Quantity[46];
    cdsBillsTotalPrice46.AsFloat := GclAddNode.FP_TotalPrice[46];
    cdsBillsQuantity47.AsFloat := GclAddNode.FP_Quantity[47];
    cdsBillsTotalPrice47.AsFloat := GclAddNode.FP_TotalPrice[47];
    cdsBillsQuantity48.AsFloat := GclAddNode.FP_Quantity[48];
    cdsBillsTotalPrice48.AsFloat := GclAddNode.FP_TotalPrice[48];
    cdsBillsQuantity49.AsFloat := GclAddNode.FP_Quantity[49];
    cdsBillsTotalPrice49.AsFloat := GclAddNode.FP_TotalPrice[49];
    cdsBillsQuantity50.AsFloat := GclAddNode.FP_Quantity[50];
    cdsBillsTotalPrice50.AsFloat := GclAddNode.FP_TotalPrice[50];
    cdsBills.Post;
  end;
end;

end.
