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;
    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
  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;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AComisionID);
    try
      CreateEditor('EditorComisionesPreview', IEditorComisionesPreview, AEditor);
      if Assigned(AEditor) then
         with AEditor do
         begin
           LoadFromStream(AStream);
           Preview;
           Release;
         end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  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
         with AEditor do
         begin
           LoadFromStream(AStream);
           Print;
           Release;
         end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
