unit StandardLib;

interface

uses
  Classes, Connections, ZhAPI, StandardBillsDm;

type
  TStandardLib = class
  private
    FLibConnection: TCommonConnection;
    FStandardBillsData: TStandardBillsData;

    function GetFileName: string;
    function GetLibName: string;
  public
    constructor Create;
    destructor Destroy; override;

    procedure Open(const AFileName: string);
    procedure SaveSaasData;

    property FileName: string read GetFileName;
    property LibName: string read GetLibName;

    property StandardBillsData: TStandardBillsData read FStandardBillsData;
  end;

implementation

{ TStandardLib }

uses
  UpdateDataBase;

constructor TStandardLib.Create;
begin
  FLibConnection := TCommonConnection.Create;
  FStandardBillsData := TStandardBillsData.Create(FLibConnection.Connection);
end;

destructor TStandardLib.Destroy;
begin
  FStandardBillsData.Free;
  FLibConnection.Free;
  inherited;
end;

function TStandardLib.GetFileName: string;
begin
  Result := FLibConnection.FileName;
end;

function TStandardLib.GetLibName: string;
begin
  Result := ExtractSimpleFileName(FileName);
end;

procedure TStandardLib.Open(const AFileName: string);

  procedure Update;
  var
    Updater: TStandBillsUpdateDB;
  begin
    Updater := TStandBillsUpdateDB.Create;
    try
      Updater.Update(FLibConnection);
    finally
      Updater.Free;
    end;
  end;

begin
  FLibConnection.Open(AFileName);
  Update;
  FStandardBillsData.Open(FLibConnection.Connection);
  SaveSaasData;
end;

procedure TStandardLib.SaveSaasData;
begin
  FStandardBillsData.CalcCacheData(StandardBillsData.BillsTree.FirstNode, 1, '');
  FStandardBillsData.sddStdBills.Save;
end;

end.
