unit uAlbaranesProveedorReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleAlbaranesProveedorReport,
  uAlbaranesProveedorController, uDetallesAlbaranProveedorController,
  uBizAlbaranesProveedor;

type
  IAlbaranesProveedorReportController = interface(IControllerBase)
  ['{AAE252CE-D407-4FF4-AF2A-350C5A421C89}']
    procedure Preview(const AID : String);
    procedure Print(const AID : String);
  end;

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

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


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorAlbaranesProveedorPreview,
  uEditorPreview, uDataModuleAlbaranesProveedor, uEditorBase, cxControls;

{ TAlbaranesProveedorReportController }

constructor TAlbaranesProveedorReportController.Create;
begin
  inherited;
  FDataModule := TDataModuleAlbaranesProveedor.Create(Nil);
end;

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


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

procedure TAlbaranesProveedorReportController.Preview(const AID : String);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesProveedorPreview;
begin
  AEditor := NIL;

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

procedure TAlbaranesProveedorReportController.Print(const AID : String);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesProveedorPreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AID);
    try
      CreateEditor('EditorAlbaranesProveedorPreview', IEditorAlbaranesProveedorPreview, 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.
