unit rmOtherReport1Dm;

interface

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

const
  FixedID: array [1..10] of Integer = (1, 2, 3, 21, 57, 36, 25, 26, 15, 34);
  XiangCode: array [1..10] of string = ('1-1', '1-2', '1-3', '1-4', '1-5', '1-6', '1-7', '1-8', '1-9', '1-10');

type
  TDataArray = array [1..10] of Double;

  TrmOtherReport1Data = class(TDataModule)
    cdsOtherReport1: TClientDataSet;
    cdsOtherReport1Value1: TFloatField;
    cdsOtherReport1Value1_1: TFloatField;
    cdsOtherReport1Value1_2: TFloatField;
    cdsOtherReport1Value1_3: TFloatField;
    cdsOtherReport1Value1_4: TFloatField;
    cdsOtherReport1Value1_5: TFloatField;
    cdsOtherReport1Value1_6: TFloatField;
    cdsOtherReport1Value1_7: TFloatField;
    cdsOtherReport1Value1_8: TFloatField;
    cdsOtherReport1Value1_9: TFloatField;
    cdsOtherReport1Value1_10: TFloatField;
    cdsOtherReport1Value2: TFloatField;
    cdsOtherReport1Value3: TFloatField;
    cdsOtherReport1Value4: TFloatField;
    cdsOtherReport1Value5: TFloatField;
    cdsOtherReport1Value6: TFloatField;
    cdsOtherReport1Value7: TFloatField;
    cdsOtherReport1Value8: TFloatField;
    cdsOtherReport1Value9: TFloatField;
    cdsOtherReport1Value10: TFloatField;
    cdsOtherReport1Value: TFloatField;
  private
    FProjectData: TProjectData;
    FFixedAB: TDataArray;
    FFixedFinal: TDataArray;
    FXiangAB: TDataArray;
    FXiangFinal: TDataArray;
    FGatherAB: Double;
    FGatherFinal: Double;

    procedure ResetDataZero;

    function OpenProject(AProject: TSelectProject): TProjectData;
    procedure FreeProject(AProjectData: TProjectData);

    procedure GatherFixedData(var AArray: TDataArray; AIndex: Integer);
    procedure GatherXiangData(var AArray: TDataArray; AIndex: Integer);

    procedure GatherProject(AProject: TSelectProject);

    procedure WriteData;
  public
    function AssignData(AProjects: TList): TDataSet;
  end;

implementation

uses
  Globals, UtilMethods;

{$R *.dfm}

{ TrmOtherReport1Data }

function TrmOtherReport1Data.AssignData(AProjects: TList): TDataSet;
var
  iProject: Integer;
  SelectProject: TSelectProject;
begin
  cdsOtherReport1.Active := True;
  cdsOtherReport1.DisableControls;
  cdsOtherReport1.EmptyDataSet;
  ResetDataZero;
  try
    for iProject := 0 to AProjects.Count - 1 do
      GatherProject(TSelectProject(AProjects.Items[iProject]));
    WriteData;
  finally
    cdsOtherReport1.EnableControls;
    Result := cdsOtherReport1;
  end;
end;

procedure TrmOtherReport1Data.FreeProject(AProjectData: TProjectData);
begin
  if not Assigned(OpenProjectManager.FindProjectData(FProjectData.ProjectID)) then
    FProjectData.Free;
end;

procedure TrmOtherReport1Data.GatherFixedData(
  var AArray: TDataArray; AIndex: Integer);
var
  i: Integer;
begin
  for i := Low(FixedID) to High(FixedID) do
    AArray[i] := AArray[i] + FProjectData.BillsData.GetGatherTotalPrice(FixedID[i], AIndex);
end;

procedure TrmOtherReport1Data.GatherProject(AProject: TSelectProject);
begin
  FProjectData := OpenProject(AProject);
  try
    if AProject.IsAB then
    begin
      GatherFixedData(FFixedAB, 0);
      GatherXiangData(FXiangAB, 0);
      FGatherAB := FProjectData.BillsData.Settlement[0];
    end
    else
    begin
      GatherFixedData(FFixedFinal, 4);
      GatherXiangData(FXiangFinal, 4);
      FGatherFinal := FGatherFinal + FProjectData.BillsData.Settlement[4];
    end;
  finally
    FreeProject(FProjectData);
  end;
end;

procedure TrmOtherReport1Data.GatherXiangData(
  var AArray: TDataArray; AIndex: Integer);
var
  i: Integer;
  Rec: TsdDataRecord;
begin
  for i := Low(XiangCode) to High(XiangCode) do
  begin
    Rec := FProjectData.BillsData.sddBills.Locate('Code', XiangCode[i]);
    if Assigned(Rec) then
    begin
      case AIndex of
        0: AArray[i] := AArray[i] + Rec.ValueByName('TotalPrice').AsFloat;
        4: AArray[i] := AArray[i] + Rec.ValueByName('AddGatherTotalPrice').AsFloat;
      end;
    end;
  end;
end;

function TrmOtherReport1Data.OpenProject(AProject: TSelectProject): TProjectData;
var
  Rec: TsdDataRecord;
begin
  Result := OpenProjectManager.FindProjectData(AProject.ProjectID);
  if not Assigned(Result) then
  begin
    Rec := ProjectManager.sddProjectsInfo.FindKey('idxID', AProject.ProjectID);
    Result := TProjectData.Create;
    Result.OpenForReport(GetMyProjectsFilePath + Rec.ValueByName('FileName').AsString);
  end;
end;

procedure TrmOtherReport1Data.ResetDataZero;
begin
  FillChar(FFixedAB, SizeOf(FFixedAB), 0);
  FillChar(FFixedFinal, SizeOf(FFixedFinal), 0);
  FillChar(FXiangAB, SizeOf(FXiangAB), 0);
  FillChar(FXiangFinal, SizeOf(FXiangFinal), 0);
  FGatherAB := 0;
  FGatherFinal := 0;
end;

procedure TrmOtherReport1Data.WriteData;
begin
  //Åú×¼¸ÅËã
  cdsOtherReport1.Append;
  cdsOtherReport1Value1.AsFloat := FFixedAB[1];
  cdsOtherReport1Value2.AsFloat := FFixedAB[2];
  cdsOtherReport1Value3.AsFloat := FFixedAB[3];
  cdsOtherReport1Value4.AsFloat := FFixedAB[4];
  cdsOtherReport1Value5.AsFloat := FFixedAB[5];
  cdsOtherReport1Value6.AsFloat := FFixedAB[6];
  cdsOtherReport1Value7.AsFloat := FFixedAB[7];
  cdsOtherReport1Value8.AsFloat := FFixedAB[8];
  cdsOtherReport1Value9.AsFloat := FFixedAB[9];
  cdsOtherReport1Value10.AsFloat := FFixedAB[10];

  cdsOtherReport1Value1_1.AsFloat := FXiangAB[1];
  cdsOtherReport1Value1_2.AsFloat := FXiangAB[2];
  cdsOtherReport1Value1_3.AsFloat := FXiangAB[3];
  cdsOtherReport1Value1_4.AsFloat := FXiangAB[4];
  cdsOtherReport1Value1_5.AsFloat := FXiangAB[5];
  cdsOtherReport1Value1_6.AsFloat := FXiangAB[6];
  cdsOtherReport1Value1_7.AsFloat := FXiangAB[7];
  cdsOtherReport1Value1_8.AsFloat := FXiangAB[8];
  cdsOtherReport1Value1_9.AsFloat := FXiangAB[9];
  cdsOtherReport1Value1_10.AsFloat := FXiangAB[10];

  cdsOtherReport1Value.AsFloat := FGatherAB;
  cdsOtherReport1.Post;
  // ¾öËã
  cdsOtherReport1.Append;
  cdsOtherReport1Value1.AsFloat := FFixedFinal[1];
  cdsOtherReport1Value2.AsFloat := FFixedFinal[2];
  cdsOtherReport1Value3.AsFloat := FFixedFinal[3];
  cdsOtherReport1Value4.AsFloat := FFixedFinal[4];
  cdsOtherReport1Value5.AsFloat := FFixedFinal[5];
  cdsOtherReport1Value6.AsFloat := FFixedFinal[6];
  cdsOtherReport1Value7.AsFloat := FFixedFinal[7];
  cdsOtherReport1Value8.AsFloat := FFixedFinal[8];
  cdsOtherReport1Value9.AsFloat := FFixedFinal[9];
  cdsOtherReport1Value10.AsFloat := FFixedFinal[10];

  cdsOtherReport1Value1_1.AsFloat := FXiangFinal[1];
  cdsOtherReport1Value1_2.AsFloat := FXiangFinal[2];
  cdsOtherReport1Value1_3.AsFloat := FXiangFinal[3];
  cdsOtherReport1Value1_4.AsFloat := FXiangFinal[4];
  cdsOtherReport1Value1_5.AsFloat := FXiangFinal[5];
  cdsOtherReport1Value1_6.AsFloat := FXiangFinal[6];
  cdsOtherReport1Value1_7.AsFloat := FXiangFinal[7];
  cdsOtherReport1Value1_8.AsFloat := FXiangFinal[8];
  cdsOtherReport1Value1_9.AsFloat := FXiangFinal[9];
  cdsOtherReport1Value1_10.AsFloat := FXiangFinal[10];

  cdsOtherReport1Value.AsFloat := FGatherFinal;
  cdsOtherReport1.Post;
  // Ôö¼õ
  cdsOtherReport1.Append;
  cdsOtherReport1Value1.AsFloat := FFixedFinal[1] - FFixedAB[1];
  cdsOtherReport1Value2.AsFloat := FFixedFinal[2] - FFixedAB[2];
  cdsOtherReport1Value3.AsFloat := FFixedFinal[3] - FFixedAB[3];
  cdsOtherReport1Value4.AsFloat := FFixedFinal[4] - FFixedAB[4];
  cdsOtherReport1Value5.AsFloat := FFixedFinal[5] - FFixedAB[5];
  cdsOtherReport1Value6.AsFloat := FFixedFinal[6] - FFixedAB[6];
  cdsOtherReport1Value7.AsFloat := FFixedFinal[7] - FFixedAB[7];
  cdsOtherReport1Value8.AsFloat := FFixedFinal[8] - FFixedAB[8];
  cdsOtherReport1Value9.AsFloat := FFixedFinal[9] - FFixedAB[9];
  cdsOtherReport1Value10.AsFloat := FFixedFinal[10] - FFixedAB[10];

  cdsOtherReport1Value1_1.AsFloat := FXiangFinal[1] - FXiangAB[1];
  cdsOtherReport1Value1_2.AsFloat := FXiangFinal[2] - FXiangAB[2];
  cdsOtherReport1Value1_3.AsFloat := FXiangFinal[3] - FXiangAB[3];
  cdsOtherReport1Value1_4.AsFloat := FXiangFinal[4] - FXiangAB[4];
  cdsOtherReport1Value1_5.AsFloat := FXiangFinal[5] - FXiangAB[5];
  cdsOtherReport1Value1_6.AsFloat := FXiangFinal[6] - FXiangAB[6];
  cdsOtherReport1Value1_7.AsFloat := FXiangFinal[7] - FXiangAB[7];
  cdsOtherReport1Value1_8.AsFloat := FXiangFinal[8] - FXiangAB[8];
  cdsOtherReport1Value1_9.AsFloat := FXiangFinal[9] - FXiangAB[9];
  cdsOtherReport1Value1_10.AsFloat := FXiangFinal[10] - FXiangAB[10];

  cdsOtherReport1Value.AsFloat := FGatherFinal - FGatherAB;
  cdsOtherReport1.Post;
end;

end.
