unit uComisionesReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleComisionesReport,
//  uClientesController, uDetallesFacturaClienteController,
  uBizComisiones;

type
  IComisionesReportController = interface(IControllerBase)
  ['{7ABB74C2-050C-4737-BC4C-C5F20883CED8}']
    procedure Preview(const AComisionID : String);
    procedure Print(const AComisionID : String);
  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 AComisionID : String);
    procedure Print(const AComisionID : String);
  end;


implementation

uses
  Variants, uROTypes, uEditorRegistryUtils, uIEditorComisionesPreview,
  uEditorPreview, uDataModuleComisiones, uEditorBase, cxControls;

{ 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 AComisionID : String);
var
  AStream: Binary;
  AEditor : IEditorComisionesPreview;
begin
  AEditor := NIL;

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

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

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AComisionID);
    try
      CreateEditor('EditorComisionesPreview', IEditorComisionesPreview, 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.
