unit uPedidosClienteReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModulePedidosClienteReport,
  uClientesController, uPedidosClienteController, uDetallesPedidoClienteController,
  uBizPedidosCliente;

type
  IPedidosClienteReportController = interface
  ['{D0686358-251C-43C4-9927-6112F2F4D3B8}']
    procedure Preview(const AID : String);
    procedure Print(const AID : String);
  end;

  TPedidosClienteReportController = class(TInterfacedObject, IPedidosClienteReportController)
  private
    FDataModule : IDataModulePedidosClienteReport;
    function CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;
  public
    constructor Create;
    destructor Destroy; override;

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


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorPedidosClientePreview,
  uEditorPreview, uDataModulePedidosCliente, uEditorBase, cxControls;

{ TPedidosClienteReportController }

constructor TPedidosClienteReportController.Create;
begin
  FDataModule := TDataModulePedidosCliente.Create(Nil);
end;

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


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

procedure TPedidosClienteReportController.Preview(const AID : String);
var
  AStream: Binary;
  AEditor : IEditorPedidosClientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AID);
    try
      CreateEditor('EditorPedidosClientePreview', IEditorPedidosClientePreview, AEditor);
      AEditor.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;

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

procedure TPedidosClienteReportController.Print(const AID : String);
var
  AStream: Binary;
  AEditor : IEditorPedidosClientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
  AStream := FDataModule.GetReport(AID);
    try
      CreateEditor('EditorPedidosClientePreview', IEditorPedidosClientePreview, AEditor);
      AEditor.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    AEditor.Print
end;

end.
