unit uComisionesReportController;

interface

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

type
  IComisionesReportController = interface
  ['{7ABB74C2-050C-4737-BC4C-C5F20883CED8}']
    procedure Preview(const AComisionID : Variant; const DesglosadoProv: Boolean);
    procedure Print(const AComisionID : Variant; const DesglosadoProv: Boolean);
  end;

  TComisionesReportController = class(TInterfacedObject, 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 : Variant; const DesglosadoProv: Boolean);
    procedure Print(const AComisionID : Variant; const DesglosadoProv: Boolean);
  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 : Variant; const DesglosadoProv: Boolean);
var
  AStream: Binary;
  AEditor : IEditorComisionesPreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AComisionID, DesglosadoProv,'', '', Null);
    try
      CreateEditor('EditorComisionesPreview', IEditorComisionesPreview, AEditor);
      AEditor.DataModule := FDataModule;
      AEditor.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    AEditor.Preview;
end;

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

  ShowHourglassCursor;
  try
  AStream := FDataModule.GetReport(AComisionID, DesglosadoProv, '', '', Null);
    try
      CreateEditor('EditorComisionesPreview', IEditorComisionesPreview, AEditor);
      AEditor.DataModule := FDataModule;
      AEditor.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    AEditor.Print
end;

end.
