unit uListadosContactosReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uIDataModuleListadosContactosReport,
  uVendedoresController, uControllerBase, uIntegerListUtils;

type
  IListadosContactosReportController = interface(IControllerBase)
  ['{76BB35E3-554E-4D30-8D3E-54E488EF82E2}']
    procedure PreviewInformeEtiquetas(const ListaID : TIntegerList);
    procedure PrintInformeEtiquetas(const ListaID : TIntegerList);
    procedure PreviewInformeListaDatosContacto(const ListaID : TIntegerList);
    procedure PrintInformeListaDatosContacto(const ListaID : TIntegerList);
  end;

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

    procedure PreviewInformeEtiquetas(const ListaID : TIntegerList);
    procedure PrintInformeEtiquetas(const ListaID : TIntegerList);
    procedure PreviewInformeListaDatosContacto(const ListaID : TIntegerList);
    procedure PrintInformeListaDatosContacto(const ListaID : TIntegerList);

  end;


implementation

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

{ TEtiquetasContactosReportController }

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

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


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

procedure TListadosContactosReportController.PreviewInformeEtiquetas(const ListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorListadosContactosPreview;
begin
  AEditor := NIL;

  AStream := FDataModule.GetReportInformeEtiquetas(ListaID);
  try
    CreateEditor('EditorListadosContactosPreview', IEditorListadosContactosPreview, AEditor);
    if Assigned(AEditor) then
    begin
      try
        AEditor.Controller := Self;
        AEditor.ListaID := ListaID;
        AEditor.LoadFromStream(AStream);
        AEditor.Preview;
      finally
        AEditor.Release;
      end;
    end;
  finally
    FreeAndNil(AStream);
    AEditor := Nil;
  end;
end;

procedure TListadosContactosReportController.PreviewInformeListaDatosContacto(
  const ListaID: TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorListadosContactosPreview;
begin
  AEditor := NIL;

  AStream := FDataModule.GetReportInformeListaDatosContacto(ListaID);
  try
    CreateEditor('EditorListadosContactosPreview', IEditorListadosContactosPreview, AEditor);
    if Assigned(AEditor) then
    begin
      try
        AEditor.Controller := Self;
        AEditor.ListaID := ListaID;
        AEditor.LoadFromStream(AStream);
        AEditor.Preview;
      finally
        AEditor.Release;
      end;
    end;
  finally
    FreeAndNil(AStream);
    AEditor := Nil;
  end;
end;

procedure TListadosContactosReportController.PrintInformeEtiquetas(const ListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorListadosContactosPreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReportInformeEtiquetas(ListaID);
    try
      CreateEditor('EditorListadosContactosPreview', IEditorListadosContactosPreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := ListaID;
          AEditor.LoadFromStream(AStream);
          AEditor.Print;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

procedure TListadosContactosReportController.PrintInformeListaDatosContacto(
  const ListaID: TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorListadosContactosPreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReportInformeListaDatosContacto(ListaID);
    try
      CreateEditor('EditorListadosContactosPreview', IEditorListadosContactosPreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := ListaID;
          AEditor.LoadFromStream(AStream);
          AEditor.Print;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
