unit uRecibosClienteReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleRecibosClienteReport,
  uBizRecibosCliente;

type
  IRecibosClienteReportController = interface
  ['{3837DFA3-0A38-4BB2-B0DB-D0615249EA49}']
    procedure Preview(const ID : String);
    procedure Print(const ID : String);
  end;

  TRecibosClienteReportController = class(TInterfacedObject, IRecibosClienteReportController)
  private
    FDataModule : IDataModuleRecibosClienteReport;
    function CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;
  public
    constructor Create;
    destructor Destroy; override;

    procedure Preview(const ID : String);
    procedure Print(const ID : String);
  end;


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorRecibosClientePreview,
  uEditorPreview, uDataModuleRecibosCliente, uEditorBase, cxControls;

{ TRecibosClienteReportController }

constructor TRecibosClienteReportController.Create;
begin
  FDataModule := TDataModuleRecibosCliente.Create(Nil);
end;

function TRecibosClienteReportController.CreateEditor(const AName: String;
  const IID: TGUID; out Intf): Boolean;
begin
  Result := Supports(EditorRegistry.CreateEditor(AName), IID, Intf);
end;


destructor TRecibosClienteReportController.Destroy;
begin
  FDataModule := NIL;
  inherited;
end;

procedure TRecibosClienteReportController.Preview(const ID : String);
var
  AStream: Binary;
  AEditor : IEditorRecibosClientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(ID);
    try
      CreateEditor('EditorRecibosClientePreview', IEditorRecibosClientePreview, AEditor);
      AEditor.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    AEditor.Preview;
end;

procedure TRecibosClienteReportController.Print(const ID : String);
var
  AStream: Binary;
  AEditor : IEditorRecibosClientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
  AStream := FDataModule.GetReport(ID);
    try
      CreateEditor('EditorRecibosClientePreview', IEditorRecibosClientePreview, AEditor);
      AEditor.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    AEditor.Print
end;

end.
