unit uRecibosClienteReportController;

interface

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

type
  IRecibosClienteReportController = interface(IControllerBase)
  ['{3837DFA3-0A38-4BB2-B0DB-D0615249EA49}']
    procedure Preview(const ListaID : TIntegerList);
    function Print(const ListaID : TIntegerList): Boolean;
  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);
    function Print(const ListaID : TIntegerList): Boolean;
  end;


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorRecibosClientePreview,
  uEditorPreview, uDataModuleRecibosCliente, uEditorBase, cxControls,
  uFactuGES_App, uStringsUtils, uSistemaFunc, schRecibosClienteClient_Intf;


{ 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;

  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.Preview;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TRecibosClienteReportController.Print(const ListaID : TIntegerList): Boolean;
var
  AStream: Binary;
  AEditor : IEditorRecibosClientePreview;
begin
  Result := False;
  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.TablaImpresion := nme_RecibosCliente;
          AEditor.IdTablaImpresion := ListaID.ToString;
          Result := AEditor.Print;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
