unit uFacturasClienteReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleFacturasClienteReport,
  uClientesController, uDetallesFacturaClienteController,
  uBizFacturasCliente;

type
  IFacturasClienteReportController = interface
  ['{A0F41767-4FF6-4BAE-9FC9-894DD721D756}']
    procedure Preview(const AFacturaID : integer);
    procedure Print(const AFacturaID : integer);
  end;

  TFacturasClienteReportController = class(TInterfacedObject, IFacturasClienteReportController)
  private
    FDataModule : IDataModuleFacturasClienteReport;
    function CreateEditor(const AName: String; const IID: TGUID;
      out Intf): Boolean;
  public
    constructor Create;
    destructor Destroy; override;
  
    procedure Preview(const AFacturaID : integer);
    procedure Print(const AFacturaID : integer);
  end;


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorFacturasClientePreview,
  uEditorPreview, uDataModuleFacturasCliente, uEditorBase, cxControls;

{ TFacturasClienteReportController }

constructor TFacturasClienteReportController.Create;
begin
  FDataModule := TDataModuleFacturasCliente.Create(Nil);
end;

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


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

procedure TFacturasClienteReportController.Preview(
  const AFacturaID : integer);
var
  AStream: Binary;
  AEditor : IEditorFacturasClientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  AStream := FDataModule.GetReport(AFacturaID);
  try
    CreateEditor('EditorFacturasClientePreview', IEditorFacturasClientePreview, AEditor);
    AEditor.LoadFromStream(AStream);
  finally
    AStream.Free;
    HideHourglassCursor;
  end;

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

procedure TFacturasClienteReportController.Print(
  const AFacturaID : integer);
var
  AStream: Binary;
  AEditor : IEditorFacturasClientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  AStream := FDataModule.GetReport(AFacturaID);
  try
    CreateEditor('EditorFacturasClientePreview', IEditorFacturasClientePreview, AEditor);
    AEditor.LoadFromStream(AStream);
  finally
    AStream.Free;
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    AEditor.Print
end;

end.
