unit uRecibosClienteReportController;

interface

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

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

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

    procedure Preview(const ListaID : TIntegerList);
    procedure Print(const ListaID : TIntegerList);
  end;


implementation

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

{ TRecibosClienteReportController }

constructor TRecibosClienteReportController.Create;
begin
  inherited;
  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 ListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorRecibosClientePreview;
begin
  AEditor := NIL;

  AStream := FDataModule.GetReport(ListaID);
  try
    CreateEditor('EditorRecibosClientePreview', IEditorRecibosClientePreview, AEditor);
    if Assigned(AEditor) then
    begin
      try
        AEditor.Controller := Self;
        AEditor.ListaID := ListaID;
        AEditor.LoadFromStream(AStream);
        AEditor.Preview;
      finally
        AEditor.Release;
      end;
    end;
  finally
    FreeAndNil(AStream);
    AEditor := Nil;
  end;
end;

procedure TRecibosClienteReportController.Print(const ListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorRecibosClientePreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(ListaID);
    try
      CreateEditor('EditorRecibosClientePreview', IEditorRecibosClientePreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := ListaID;
          AEditor.LoadFromStream(AStream);
          AEditor.Print;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
