unit rmWuJiuZqzfGatherDm;

interface

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

type
  TrmWuJiuZqzfGatherData = class(TDataModule)
    cdsZqzf: TClientDataSet;
    cdsZqzfPhaseIndex: TIntegerField;
    cdsZqzfQyht: TFloatField;
    cdsZqzfFhqd: TFloatField;
    cdsZqzfBg: TFloatField;
    cdsZqzfSp: TFloatField;
    cdsZqzfWy: TFloatField;
    cdsZqzfCljg: TFloatField;
    cdsZqzfBgh: TFloatField;
    cdsZqzfEndGather: TFloatField;
    cdsZqzfPreGather: TFloatField;
    cdsZqzfCurGather: TFloatField;
    cdsZqzfAddKhBlj: TFloatField;
    cdsZqzfCurKhBlj: TFloatField;
    cdsZqzfAddKhNmggz: TFloatField;
    cdsZqzfCurKhNmggz: TFloatField;
    cdsZqzfDyyfk: TFloatField;
    cdsZqzfAddKhDyyfk: TFloatField;
    cdsZqzfCurKhDyyfk: TFloatField;
    cdsZqzfAddKAddedTax: TFloatField;
    cdsZqzfCurKAddedTax: TFloatField;
    cdsZqzfAddSjzf: TFloatField;
    cdsZqzfCurSjzf: TFloatField;
    cdsZqzfMemo: TWideStringField;
    cdsZqzftzpf: TFloatField;
    cdsZqzfjj: TFloatField;
    cdsZqzffk: TFloatField;
    cdsZqzfspje: TFloatField;
    cdsZqzfCurYlnmggzbzj: TFloatField;
    cdsZqzfAddYlnmggzbzj: TFloatField;
    cdsZqzfCurZlbzj: TFloatField;
    cdsZqzfAddZlbzj: TFloatField;
    cdsZqzfCurYlgcdjjk: TFloatField;
    cdsZqzfAddYlgcdjjk: TFloatField;
  private
    FQyht: Double;
    FFhqd: Double;
    FBg: Double;
    FSp: Double;
    FWy: Double;
    FCljg: Double;
    FTzpf: Double;
    procedure GatherMainData(AProjectData: TProjectData);
    procedure LoadPhaseData(APhaseIndex: Integer; AProjectData: TProjectData = nil);
  public
    function AssignData(AProjectData: TProjectData): TDataSet;
  end;

implementation

uses BillsDm, BGLDm, DealPaymentDm, PhaseData, PhasePayDm, ConstUnit;

{$R *.dfm}

{ TrmWuJiuZqzfGatherData }

function TrmWuJiuZqzfGatherData.AssignData(
  AProjectData: TProjectData): TDataSet;
var
  iPhase: Integer;
begin
  cdsZqzf.Active := True;
  cdsZqzf.EmptyDataSet;
  GatherMainData(AProjectData);
  for iPhase := 1 to AProjectData.ProjProperties.PhaseCount do
    LoadPhaseData(iPhase, AProjectData);
  Result := cdsZqzf;
end;

procedure TrmWuJiuZqzfGatherData.GatherMainData(AProjectData: TProjectData);

  function FindChild(const AName: String; AParent: TsdIDTreeNode = nil): TsdIDTreeNode;
  var
    vNode: TsdIDTreeNode;
  begin
    Result := nil;
    if Assigned(AParent) then
      vNode := AParent.FirstChild
    else
      vNode := AProjectData.BillsCompileData.BillsCompileTree.FirstNode;
    while Assigned(vNode) do
    begin
      if Pos(AName, vNode.Rec.ValueByName('Name').AsString) > 0 then
      begin
        Result := vNode;
        Break;
      end
      else
        vNode := vNode.NextSibling;
    end;
  end;

  function FindNode(const AName: String; AParent: TsdIDTreeNode = nil): TsdIDTreeNode;
  var
    vNode: TsdIDTreeNode;
    i, iCount: Integer;
  begin
    Result := nil;
    i := 0;
    if Assigned(AParent) then
    begin
      vNode := AParent.FirstChild;
      iCount := AParent.PosterityCount;
    end
    else
    begin
      vNode := AProjectData.BillsCompileData.BillsCompileTree.FirstNode;
      iCount := AProjectData.BillsCompileData.BillsCompileTree.Count;
    end;
    while Assigned(vNode) and (i < iCount) do
    begin
      if Pos(AName, vNode.Rec.ValueByName('Name').AsString) > 0 then
      begin
        Result := vNode;
        Break;
      end
      else
        vNode := vNode.NextNode;
      Inc(i);
    end;
  end;

var
  vQtFyNode, vChild: TsdIDTreeNode;
begin
  // 签约合同金额
  FQyht := AProjectData.ProjProperties.ContractPrice;
  // 复核清单金额
  vQtFyNode := FindChild('建安工程其他费用', AProjectData.BillsCompileData.BillsCompileTree.FindNode(1));
  with AProjectData.BillsData do
    FFhqd := FirstPart[0] + SecondPart[0] + ThirdPart[0] + ReservedFee[0];
  if Assigned(vQtFyNode) then
    FFhqd := FFhqd - vQtFyNode.Rec.ValueByName('TotalPrice').AsFloat;
  // 变更金额
  FBg := AProjectData.BGLData.TotalPrice;
  // 索赔金额
  vChild := FindNode('索赔', vQtFyNode);
  if Assigned(vChild) then
    FSp := vChild.Rec.ValueByName('TotalPrice').AsFloat
  else
    FSp := 0;
  // 违约罚金
  vChild := FindNode('违约罚金', vQtFyNode);
  if Assigned(vChild) then
    FWy := vChild.Rec.ValueByName('TotalPrice').AsFloat
  else
    FWy := 0;
  // 材料价格调整金额
  vChild := FindNode('价差调整', vQtFyNode);
  if Assigned(vChild) then
    FCljg := vChild.Rec.ValueByName('TotalPrice').AsFloat
  else
    FCljg := 0;

  FTzpf := AProjectData.BillsData.FirstPart[0] +
      AProjectData.BillsData.SecondPart[0] +
      AProjectData.BillsData.ThirdPart[0] +
      AProjectData.BillsData.ReservedFee[0];
end;

procedure TrmWuJiuZqzfGatherData.LoadPhaseData(APhaseIndex: Integer;
  AProjectData: TProjectData);

  function GetDealPayID(const AName: string; AMinus: Boolean = false): Integer;
  var
    iRec: Integer;
    Rec: TsdDataRecord;
  begin
    Result := -1;
    with AProjectData.DealPaymentData do
      for iRec := 0 to sddDealPayment.RecordCount - 1 do
      begin
        Rec := sddDealPayment.Records[iRec];
        if (Pos(AName, Rec.ValueByName('Name').AsString) > 0) and (Rec.ValueByName('IsMinus').AsBoolean = AMinus) then
        begin
          Result := Rec.ValueByName('ID').AsInteger;
          Break;
        end;
      end;
  end;

  procedure GetDealPayRecords(AList: TList; AName: string; AMinus: Boolean = false; ACalcType: Integer = 0);
  var
    iRec: Integer;
    Rec: TsdDataRecord;
  begin
    AList.Clear;
    with AProjectData.DealPaymentData do
      for iRec := 0 to sddDealPayment.RecordCount - 1 do
      begin
        Rec :=  sddDealPayment.Records[iRec];
        if (Pos(AName, Rec.ValueByName('Name').AsString) > 0) and (Rec.ValueByName('IsMinus').AsBoolean = AMinus)
           and (Rec.ValueByName('CalcType').AsInteger = ACalcType) then
        begin
          AList.Add(Rec);
        end;
      end;
  end;

  procedure GetDealPayRecords2(AList: TList; AName: string);
  var
    iRec: Integer;
    Rec: TsdDataRecord;
  begin
    AList.Clear;
    with AProjectData.DealPaymentData do
      for iRec := 0 to sddDealPayment.RecordCount - 1 do
      begin
        Rec :=  sddDealPayment.Records[iRec];
        if (Pos(AName, Rec.ValueByName('Name').AsString) > 0) then
        begin
          AList.Add(Rec);
        end;
      end;
  end;

var
  PhaseData: TPhaseData;
  sTPField: string;
  iDealPayId: Integer;
  Rec, MainRec: TsdDataRecord;
  vRecList: TList;
  iRec: Integer;
begin
  PhaseData := TPhaseData.Create(AProjectData);
  vRecList := TList.Create;
  try
    PhaseData.SimpleOpen(Format('%s\Phase%d.dat', [AProjectData.TempPath, APhaseIndex]));
    cdsZqzf.Append;
    cdsZqzfPhaseIndex.AsInteger := APhaseIndex;
    cdsZqzfQyht.AsFloat := FQyht;
    cdsZqzfFhqd.AsFloat := FFhqd;
    cdsZqzfBg.AsFloat := AProjectData.BGLData.EndTotalPrice[APhaseIndex];
    cdsZqzfSp.AsFloat := FSp;
    cdsZqzfWy.AsFloat := FWy;
    cdsZqzfCljg.AsFloat := FCljg;//PhaseData.StageData.PriceMargin[2, 4];
    cdsZqzfBgh.AsFloat := cdsZqzfFhqd.AsFloat + cdsZqzfBg.AsFloat + cdsZqzfSp.AsFloat + cdsZqzfWy.AsFloat + cdsZqzfCljg.AsFloat;
    cdsZqzftzpf.AsFloat := FTzpf;
    sTPField := 'TotalPrice' + IntToStr(TPhaseData(PhaseData).PhaseProperty.AuditCount);
    with PhaseData.PhasePayData do
    begin
      iDealPayId := GetDealPayID('本期完成计量');
      Rec := sddPhasePay.Locate('ID', iDealPayId);
      cdsZqzfEndGather.AsFloat := Rec.ValueByName('End' + sTPField).AsFloat;
      cdsZqzfPreGather.AsFloat := Rec.ValueByName('Pre' + sTPField).AsFloat;
      cdsZqzfCurGather.AsFloat := Rec.ValueByName(sTPField).AsFloat;

      GetDealPayRecords(vRecList, '保留金', true);
      for iRec := 0 to vRecList.Count - 1 do
      begin
        iDealPayId := TsdDataRecord(vRecList.Items[iRec]).ValueByName('ID').AsInteger;
        Rec := sddPhasePay.Locate('ID', iDealPayId);
        if Assigned(Rec) then
        begin
          cdsZqzfAddKhBlj.AsFloat := cdsZqzfAddKhBlj.AsFloat + Rec.ValueByName('End' + sTPField).AsFloat;
          cdsZqzfCurKhBlj.AsFloat := cdsZqzfCurKhBlj.AsFloat + Rec.ValueByName(sTPField).AsFloat;
        end;
      end;

      GetDealPayRecords(vRecList, '农民工工资', true);
      for iRec := 0 to vRecList.Count - 1 do
      begin
        iDealPayId := TsdDataRecord(vRecList.Items[iRec]).ValueByName('ID').AsInteger;
        Rec := sddPhasePay.Locate('ID', iDealPayId);
        if Assigned(Rec) then
        begin
          cdsZqzfAddKhNmggz.AsFloat := cdsZqzfAddKhNmggz.AsFloat + Rec.ValueByName('End' + sTPField).AsFloat;
          cdsZqzfCurKhNmggz.AsFloat := cdsZqzfCurKhNmggz.AsFloat + Rec.ValueByName(sTPField).AsFloat;
        end;
      end;

      GetDealPayRecords(vRecList, '动员预付款', false);
      for iRec := 0 to vRecList.Count - 1 do
      begin
        iDealPayId := TsdDataRecord(vRecList.Items[iRec]).ValueByName('ID').AsInteger;
        Rec := sddPhasePay.Locate('ID', iDealPayId);
        if Assigned(Rec) then
        begin
          cdsZqzfDyyfk.AsFloat := cdsZqzfDyyfk.AsFloat + Rec.ValueByName(sTPField).AsFloat;
        end;
      end;

      GetDealPayRecords(vRecList, '动员预付款', true);
      for iRec := 0 to vRecList.Count - 1 do
      begin
        iDealPayId := TsdDataRecord(vRecList.Items[iRec]).ValueByName('ID').AsInteger;
        Rec := sddPhasePay.Locate('ID', iDealPayId);
        if Assigned(Rec) then
        begin
          cdsZqzfAddKhDyyfk.AsFloat := cdsZqzfAddKhDyyfk.AsFloat + Rec.ValueByName('End' + sTPField).AsFloat;
          cdsZqzfCurKhDyyfk.AsFloat := cdsZqzfCurKhDyyfk.AsFloat + Rec.ValueByName(sTPField).AsFloat;
        end;
      end;

      GetDealPayRecords(vRecList, '增值税', true);
      for iRec := 0 to vRecList.Count - 1 do
      begin
        iDealPayId := TsdDataRecord(vRecList.Items[iRec]).ValueByName('ID').AsInteger;
        Rec := sddPhasePay.Locate('ID', iDealPayId);
        if Assigned(Rec) then
        begin
          cdsZqzfAddKAddedTax.AsFloat := cdsZqzfAddKAddedTax.AsFloat + Rec.ValueByName('End' + sTPField).AsFloat;
          cdsZqzfCurKAddedTax.AsFloat := cdsZqzfCurKAddedTax.AsFloat + Rec.ValueByName(sTPField).AsFloat;
        end;
      end;

      iDealPayId := GetDealPayID('本期应付');
      Rec := sddPhasePay.Locate('ID', iDealPayId);
      if Assigned(Rec) then
      begin
        cdsZqzfAddSjzf.AsFloat := Rec.ValueByName('End' + sTPField).AsFloat;
        cdsZqzfCurSjzf.AsFloat := Rec.ValueByName(sTPField).AsFloat;
      end;

      GetDealPayRecords2(vRecList, '奖金');
      for iRec := 0 to vRecList.Count - 1 do
      begin
        iDealPayId := TsdDataRecord(vRecList.Items[iRec]).ValueByName('ID').AsInteger;
        Rec := sddPhasePay.Locate('ID', iDealPayId);
        if Assigned(Rec) then
        begin
          cdsZqzfjj.AsFloat := cdsZqzfjj.AsFloat + Rec.ValueByName(sTPField).AsFloat;
        end;
      end;

      GetDealPayRecords2(vRecList, '罚款');
      for iRec := 0 to vRecList.Count - 1 do
      begin
        iDealPayId := TsdDataRecord(vRecList.Items[iRec]).ValueByName('ID').AsInteger;
        Rec := sddPhasePay.Locate('ID', iDealPayId);
        if Assigned(Rec) then
        begin
          cdsZqzffk.AsFloat := cdsZqzffk.AsFloat + Rec.ValueByName(sTPField).AsFloat;
        end;
      end;

      GetDealPayRecords2(vRecList, '索赔金额');
      for iRec := 0 to vRecList.Count - 1 do
      begin
        iDealPayId := TsdDataRecord(vRecList.Items[iRec]).ValueByName('ID').AsInteger;
        Rec := sddPhasePay.Locate('ID', iDealPayId);
        if Assigned(Rec) then
        begin
          cdsZqzfspje.AsFloat := cdsZqzfspje.AsFloat + Rec.ValueByName(sTPField).AsFloat;
        end;
      end;

      GetDealPayRecords2(vRecList, '预留农民工工资保证金');
      for iRec := 0 to vRecList.Count - 1 do
      begin
        MainRec := TsdDataRecord(vRecList.Items[iRec]);
        iDealPayId := MainRec.ValueByName('ID').AsInteger;
        Rec := sddPhasePay.Locate('ID', iDealPayId);
        if Assigned(Rec) then
        begin
          cdsZqzfAddYlnmggzbzj.AsFloat := cdsZqzfAddYlnmggzbzj.AsFloat + Rec.ValueByName('End' + sTPField).AsFloat;
          //cdsZqzfAddYlnmggzbzj.AsFloat := cdsZqzfAddYlnmggzbzj.AsFloat + MainRec.ValueByName(sTPField).AsFloat;
          cdsZqzfCurYlnmggzbzj.AsFloat := cdsZqzfCurYlnmggzbzj.AsFloat + Rec.ValueByName(sTPField).AsFloat;
        end;
      end;

      GetDealPayRecords2(vRecList, '质量保证金');
      for iRec := 0 to vRecList.Count - 1 do
      begin
        MainRec := TsdDataRecord(vRecList.Items[iRec]);
        iDealPayId := MainRec.ValueByName('ID').AsInteger;
        Rec := sddPhasePay.Locate('ID', iDealPayId);
        if Assigned(Rec) then
        begin
          cdsZqzfAddZlbzj.AsFloat := cdsZqzfAddZlbzj.AsFloat + Rec.ValueByName('End' + sTPField).AsFloat;
          //cdsZqzfAddZlbzj.AsFloat := cdsZqzfAddZlbzj.AsFloat + MainRec.ValueByName(sTPField).AsFloat;
          cdsZqzfCurZlbzj.AsFloat := cdsZqzfCurZlbzj.AsFloat + Rec.ValueByName(sTPField).AsFloat;
        end;
      end;

      GetDealPayRecords2(vRecList, '预留工程待结价款');
      for iRec := 0 to vRecList.Count - 1 do
      begin
        MainRec := TsdDataRecord(vRecList.Items[iRec]);
        iDealPayId := MainRec.ValueByName('ID').AsInteger;
        Rec := sddPhasePay.Locate('ID', iDealPayId);
        if Assigned(Rec) then
        begin
          cdsZqzfAddYlgcdjjk.AsFloat := cdsZqzfAddYlgcdjjk.AsFloat + Rec.ValueByName('End' + sTPField).AsFloat;
          //cdsZqzfAddYlgcdjjk.AsFloat := cdsZqzfAddYlgcdjjk.AsFloat + MainRec.ValueByName(sTPField).AsFloat;
          cdsZqzfCurYlgcdjjk.AsFloat := cdsZqzfCurYlgcdjjk.AsFloat + Rec.ValueByName(sTPField).AsFloat;
        end;
      end;
    end;
    cdsZqzf.Post;
  finally
    vRecList.Free;
    PhaseData.Free;
  end;
end;

end.
