unit uComisionesReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleComisionesReport,
  uBizComisiones, uIntegerListUtils;

type
  IComisionesReportController = interface(IControllerBase)
  ['{60FDD06C-2B59-4276-BC24-D14EBCB87FE6}']
    procedure Preview(const AListaID : TIntegerList);
    procedure Print(const AListaID : TIntegerList);
  end;

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

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


implementation

uses
  uROTypes, uEditorRegistryUtils,
  uIEditorComisionesPreview,
  uEditorPreview, uDataModuleComisiones, uEditorBase, cxControls,
  uStringsUtils, uSistemaFunc;

{ TComisionesReportController }

constructor TComisionesReportController.Create;
begin
  inherited;
  FDataModule := TDataModuleComisiones.Create(Nil);
end;

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


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

procedure TComisionesReportController.Preview(const AListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorComisionesPreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptComisiones(AListaID);
    try
      CreateEditor('EditorComisionesPreview', IEditorComisionesPreview, AEditor);
      if Assigned(AEditor) then
        try
          AEditor.Controller := Self;
          AEditor.ListaID := AListaID;
          AEditor.LoadFromStream(AStream);
          AEditor.Preview;
        finally
          AEditor.Release;
        end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

procedure TComisionesReportController.Print(const AListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorComisionesPreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptComisiones(AListaID);
    try
      CreateEditor('EditorComisionesPreview', IEditorComisionesPreview, AEditor);
      if Assigned(AEditor) then
        try
          AEditor.Controller := Self;
          AEditor.LoadFromStream(AStream);
          AEditor.Print;
        finally
          AEditor.Release;
        end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
