unit uEtiquetasContactosReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uIDataModuleEtiquetasContactosReport,
  uVendedoresController, uControllerBase;

type
  IEtiquetasContactosReportController = interface(IControllerBase)
  ['{76BB35E3-554E-4D30-8D3E-54E488EF82E2}']
    procedure Preview(const AContactosID : String);
    procedure Print(const AContactosID : String);
  end;

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

    procedure Preview(const AContactosID : String);
    procedure Print(const AContactosID : String);
  end;


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorEtiquetasContactosPreview,
  uEditorPreview, uDataModuleContactos, uEditorBase, cxControls;

{ TEtiquetasContactosReportController }

constructor TEtiquetasContactosReportController.Create;
begin
  inherited;
  FDataModule := TDataModuleContactos.Create(Nil);
end;

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


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

procedure TEtiquetasContactosReportController.Preview(const AContactosID : String);
var
  AStream: Binary;
  AEditor : IEditorEtiquetasContactosPreview;
begin
  AEditor := NIL;

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

procedure TEtiquetasContactosReportController.Print(const AContactosID : String);
var
  AStream: Binary;
  AEditor : IEditorEtiquetasContactosPreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AContactosID);
    try
      CreateEditor('EditorEtiquetasContactosPreview', IEditorEtiquetasContactosPreview, 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.
