unit ReportPrepare;

interface

uses
  rdpBillsGatherDm, ProjectData;

type
  TReportPrepare = class
  private
    FProjectData: TProjectData;
    FPrepareGclGather: Boolean;

    procedure GatherGclAndSave;
    procedure UpdateGclGatherTables;
    procedure PrepareGclGatherData;
  public
    constructor Create(AProjectData: TProjectData);
    destructor Destroy; override;

    procedure PrepareData(AFlag: Integer);
  end;

implementation

uses
  ScAutoUpdateUnit, rdpTables, ComObj, UtilMethods, ZhAPI, Classes,
  ConditionalDefines;

{ TReportPrepare }

constructor TReportPrepare.Create(AProjectData: TProjectData);
begin
  FProjectData := AProjectData;
  FPrepareGclGather := False;
  FProjectData.ClearReportPrepareData;
end;

destructor TReportPrepare.Destroy;
begin
  FProjectData.ClearReportPrepareData;
end;

procedure TReportPrepare.GatherGclAndSave;
var
  DataSaver: TrdpBillsGatherData;
begin
  DataSaver := TrdpBillsGatherData.Create(FProjectData);
  try
    DataSaver.LoadGclBillsGatherData;
  finally
    DataSaver.Free;
  end;
end;

procedure TReportPrepare.PrepareData(AFlag: Integer);
begin
  case AFlag of
    1: PrepareGclGatherData;
  end;
  // For Test
  if _IsDebugView then
    FProjectData.SaveDebugFile('ReportDataPrepare.dat');
end;

procedure TReportPrepare.PrepareGclGatherData;
begin
  if FPrepareGclGather then Exit;

  try
    UpdateGclGatherTables;
    GatherGclAndSave;
    FPrepareGclGather := True;
  except
    FPrepareGclGather := False;
  end;
end;

procedure TReportPrepare.UpdateGclGatherTables;
var
  Updater: TScUpdater;
begin
  Updater := TScUpdater.Create;
  try
    Updater.ForceUpdate := True;
    Updater.Open('', FProjectData.ADOConnection, '', '');
    Updater.AddTableDef(S_rdpLeafXmjs, @td_rdpLeafXmjs, Length(td_rdpLeafXmjs), False, False);
    Updater.AddTableDef(S_rdpGclBills, @td_rdpGclBills, Length(td_rdpGclBills), False, False);
    Updater.AddTableDef(S_rdpDetailGclBills, @td_rdpDetailGclBills, Length(td_rdpDetailGclBills), False, False);
    Updater.AddTableDef(S_rdpDetailDealBills, @td_rdpDetailDealBills, Length(td_rdpDetailDealBills), False, False);
    Updater.AddTableDef(S_rdpDetailBGLBills, @td_rdpDetailBGLBills, Length(td_rdpDetailBGLBills), False, False);
    Updater.ExcuteUpdate;
  finally
    Updater.Free;
  end;
end;

end.
