unit stgSubGatherFile;
// 导入导出汇总结果

interface

uses
  stgSubGatherFileDm, ADODB, sdDB, stgGatherDm, SysUtils, StageDm, mDataRecord;

type
  TstgSubGatherFileHelper = class
  private
    FTempFile: string;
    FConnection: TADOConnection;
    FGatherData: TstgSubGatherData;
  public
    constructor Create;
    destructor Destroy; override;

    procedure Open(const AFileName: string);
    procedure Close;
    procedure SaveTo(const AFileName: string);

    property Connection: TADOConnection read FConnection;
    property GatherData: TstgSubGatherData read FGatherData;
  end;

  TstgSubGatherFileExportor = class(TstgSubGatherFileHelper)
  private
    procedure LoadMemoryRecord(ARec: TsdDataRecord);
    procedure LoadMemoryGatherData(AGatherData: TstgGatherData);
  public
    procedure ExportGatherDataTo(AGatherData:TstgGatherData; const AFileName: string);
  end;

  TstgSubGatherFileImportor = class(TstgSubGatherFileHelper)
  private
    procedure ClearOldData(AStageData: TStageData);
    procedure ImportGatherData(AStageData: TStageData);
  public
    procedure ImportGatherDataTo(AStageData: TStageData; const AFileName: string);
  end;

implementation


uses
  UtilMethods, ZhAPI, Connections, stgTables, ScAutoUpdateUnit, Math;

{ TstgSubGatherFileExportor }

procedure TstgSubGatherFileExportor.ExportGatherDataTo(
  AGatherData: TstgGatherData; const AFileName: string);
begin
  Open(GetEmptyDataBaseFileName);
  try
    LoadMemoryGatherData(AGatherData);
  finally                             
    SaveTo(AFileName);
  end;
end;

procedure TstgSubGatherFileExportor.LoadMemoryRecord(ARec: TsdDataRecord);
var
  vRec: TsdDataRecord;
begin
  vRec := GatherData.sddBills.Add;
  vRec.ValueByName('ID').AsInteger := ARec.ValueByName('ID').AsInteger;
  vRec.ValueByName('ParentID').AsInteger := ARec.ValueByName('ParentID').AsInteger;
  vRec.ValueByName('NextSiblingID').AsInteger := ARec.ValueByName('NextSiblingID').AsInteger;
  vRec.ValueByName('Code').AsString := ARec.ValueByName('Code').AsString;
  vRec.ValueByName('B_Code').AsString := ARec.ValueByName('B_Code').AsString;
  vRec.ValueByName('Name').AsString := ARec.ValueByName('Name').AsString;
  vRec.ValueByName('Units').AsString := ARec.ValueByName('Units').AsString;
  vRec.ValueByName('IsSumBase').AsBoolean := ARec.ValueByName('IsSumBase').AsBoolean;
  vRec.ValueByName('IsLeaf').AsBoolean := ARec.ValueByName('IsLeaf').AsBoolean;
  vRec.ValueByName('DealQuantity').AsFloat := ARec.ValueByName('DealQuantity').AsFloat;
  vRec.ValueByName('DealTotalPrice').AsFloat := ARec.ValueByName('DealTotalPrice').AsFloat;
  vRec.ValueByName('QcQuantity').AsFloat := ARec.ValueByName('QcQuantity').AsFloat;
  vRec.ValueByName('QcTotalPrice').AsFloat := ARec.ValueByName('QcTotalPrice').AsFloat;
  vRec.ValueByName('QcBGLCode').AsString := ARec.ValueByName('QcBGLCode').AsString;
  vRec.ValueByName('QcBGLNum').AsString := ARec.ValueByName('QcBGLNum').AsString;
end;

procedure TstgSubGatherFileExportor.LoadMemoryGatherData(
  AGatherData: TstgGatherData);
var
  i: Integer;
  vRec: TsdDataRecord;
begin
  GatherData.sddBills.BeginUpdate;
  try
    for i := 0 to AGatherData.sddGatherTree.RecordCount - 1 do
    begin
      vRec := AGatherData.sddGatherTree.Records[i];
      LoadMemoryRecord(vRec);
    end;
  finally
    GatherData.sddBills.EndUpdate;
  end;
end;

{ TstgSubGatherFileHelper }

procedure TstgSubGatherFileHelper.Close;
begin
  FConnection.Close;
  if FileExists(FTempFile) then
    DeleteFile(FTempFile);
end;

constructor TstgSubGatherFileHelper.Create;
begin
  FConnection := TADOConnection.Create(nil);
  FConnection.LoginPrompt := False;
  FGatherData := TstgSubGatherData.Create(nil);
end;

destructor TstgSubGatherFileHelper.Destroy;
begin
  Close;
  FGatherData.Free;
  FConnection.Free;
  inherited;
end;

procedure TstgSubGatherFileHelper.Open(const AFileName: string);

  procedure UpdateDataTables;
  var
    Updater: TScUpdater;
  begin
    Updater := TScUpdater.Create;
    try
      Updater.ForceUpdate := True;
      Updater.Open('', FConnection, '', '');
      Updater.AddTableDef(sStgBills, @tdStgBills, Length(tdStgBills), False, False);
      Updater.ExcuteUpdate;
    finally
      Updater.Free;
    end;
  end;

begin
  FTempFile := GetTempFileName;
  CopyFileOrFolder(AFileName, FTempFile);
  FConnection.ConnectionString := Format(SAdoConnectStr, [FTempFile]);
  FConnection.Open;
  UpdateDataTables;
  GatherData.Open(FConnection);
end;

procedure TstgSubGatherFileHelper.SaveTo(const AFileName: string);
begin
  FGatherData.Save;
  CopyFileOrFolder(FTempFile, AFileName);
end;

{ TstgSubGatherFileImportor }

procedure TstgSubGatherFileImportor.ClearOldData(AStageData: TStageData);
var
  i: Integer;
  vRec: TStageRecord;
begin
  for i := 0 to AStageData.sddStage.RecordCount - 1 do
  begin
    vRec := TStageRecord(AStageData.sddStage.Records[i]);
    vRec.DealQuantity.AsFloat := 0;
    vRec.DealTotalPrice.AsFloat := 0;
    vRec.DealFlag.AsInteger := 0;
    vRec.DealFormula.AsString := '';
    // to do clear QuantityChange Data, reCalculate ExecutionRate & UsedQuantity
    //vRec.QcQuantity.AsFloat := 0;
    //vRec.QcTotalPrice.AsFloat := 0;
    //vRec.QcFlag.AsInteger := 0;
    //vRec.QcFormula.AsString := '';
    //vRec.QcBGLCode.AsString := '';
    //vRec.QcBGLNum.AsString := '';
    vRec.EndDealQuantity.AsFloat := vRec.PreDealQuantity.AsFloat;
    vRec.EndDealTotalPrice.AsFloat := vRec.PreDealTotalPrice.AsFloat;
    //vRec.EndQcQuantity.AsFloat := vRec.PreQcQuantity.AsFloat;
    //vRec.EndQcTotalPrice.AsFloat := vRec.PreQcTotalPrice.AsFloat;
    //vRec.EndQcBGLCode.AsString := vRec.PreQcBGLCode.AsString;
    //vRec.EndQcBGLNum.AsString := vRec.PreQcBGLNum.AsString;
  end;
end;

procedure TstgSubGatherFileImportor.ImportGatherData(
  AStageData: TStageData);
var
  i: Integer;
  vOrgRec: TsdDataRecord;
  vStageRec: TStageRecord;
begin
  for i := 0 to GatherData.sddBills.RecordCount - 1 do
  begin
    vOrgRec := GatherData.sddBills.Records[i];
    if vOrgRec.ValueByName('IsSumBase').AsBoolean and vOrgRec.ValueByName('IsLeaf').AsBoolean then
    begin
      vStageRec := AStageData.StageRecordWithAdd(vOrgRec.ValueByName('ID').AsInteger);
      vStageRec.DealQuantity.AsFloat := vOrgRec.ValueByName('DealQuantity').AsFloat;
    end;
  end;
end;

procedure TstgSubGatherFileImportor.ImportGatherDataTo(
  AStageData: TStageData; const AFileName: string);
begin
  Open(AFileName);
  AStageData.sddStage.BeginUpdate;
  AStageData.BeforeBatchOperation;
  try
    ClearOldData(AStageData);
    ImportGatherData(AStageData);
  finally
    AStageData.AfterBatchOperation;
    AStageData.sddStage.EndUpdate;
  end;
end;

end.
