unit uRecibosProveedorReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleRecibosProveedorReport,
  uBizRecibosProveedor;

type
  IRecibosProveedorReportController = interface(IControllerBase)
  ['{33009D46-9B5C-4255-AC5E-8AA93A1DDB7B}']
    procedure Preview(const ID : integer);
    procedure Print(const ID : integer);
  end;

  TRecibosProveedorReportController = class(TControllerBase, IRecibosProveedorReportController)
  private
    FDataModule : IDataModuleRecibosProveedorReport;
    function CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;
  public
    constructor Create; override;
    destructor Destroy; override;
  
    procedure Preview(const ID : integer);
    procedure Print(const ID : integer);
  end;


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorRecibosProveedorPreview,
  uEditorPreview, uDataModuleRecibosProveedor, uEditorBase, cxControls;

{ TRecibosProveedorReportController }

constructor TRecibosProveedorReportController.Create;
begin
  inherited;
  FDataModule := TDataModuleRecibosProveedor.Create(Nil);
end;

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


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

procedure TRecibosProveedorReportController.Preview(const ID : integer);
var
  AStream: Binary;
  AEditor : IEditorRecibosProveedorPreview;
begin
  AEditor := NIL;

  AStream := FDataModule.GetReport(ID);
  try
    CreateEditor('EditorRecibosProveedorPreview', IEditorRecibosProveedorPreview, AEditor);
    if Assigned(AEditor) then
      try
        AEditor.LoadFromStream(AStream);
        AEditor.Preview;
      finally
        AEditor.Release;
        AEditor := Nil;
      end;
  finally
    FreeAndNil(AStream);
  end;
end;

procedure TRecibosProveedorReportController.Print(const ID : integer);
var
  AStream: Binary;
  AEditor : IEditorRecibosProveedorPreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(ID);
    try
      CreateEditor('EditorRecibosProveedorPreview', IEditorRecibosProveedorPreview, AEditor);
      if Assigned(AEditor) then
        try
          AEditor.LoadFromStream(AStream);
          AEditor.Print;
        finally
          AEditor.Release;
          AEditor := NIL;
        end;
    finally
      FreeAndNil(AStream);
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
