unit uFacturasProveedorReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleFacturasProveedorReport,
  uClientesController, uDetallesFacturaProveedorController, uBizFacturasProveedor;

type
  IFacturasProveedorReportController = interface(IControllerBase)
  ['{551F82DC-B8DC-482E-A20D-15003E845078}']
    procedure Preview(const AFacturaID : String);
    procedure Print(const AFacturaID : String);
  end;

  TFacturasProveedorReportController = class(TControllerBase, IFacturasProveedorReportController)
  private
    FDataModule : IDataModuleFacturasProveedorReport;
    function CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;
  public
    constructor Create; override;
    destructor Destroy; override;

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


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorFacturasProveedorPreview,
  uEditorPreview, uDataModuleFacturasProveedor, uEditorBase, cxControls;

{ TFacturasProveedorReportController }

constructor TFacturasProveedorReportController.Create;
begin
  inherited;
  FDataModule := TDataModuleFacturasProveedor.Create(Nil);
end;

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


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

procedure TFacturasProveedorReportController.Preview(const AFacturaID : String);
var
  AStream: Binary;
  AEditor : IEditorFacturasProveedorPreview;
begin
  AEditor := NIL;

  AStream := FDataModule.GetReport(AFacturaID);
  try
    CreateEditor('EditorFacturasProveedorPreview', IEditorFacturasProveedorPreview, AEditor);
    if Assigned(AEditor) then
      try
        AEditor.LoadFromStream(AStream);
        AEditor.Preview;
      finally
        AEditor.Release;
        AEditor := NIL;
      end;
  finally
    FreeAndNil(AStream);
  end;
end;

procedure TFacturasProveedorReportController.Print(const AFacturaID : String);
var
  AStream: Binary;
  AEditor : IEditorFacturasProveedorPreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AFacturaID);
    try
      CreateEditor('EditorFacturasProveedorPreview', IEditorFacturasProveedorPreview, 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.
