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);
    function Print(const AID : String): Boolean;
  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);
    function Print(const AID : String): Boolean;
  end;


implementation

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

{ 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, True);
    try
      CreateEditor('EditorPedidosClientePreview', IEditorPedidosClientePreview, AEditor);
      AEditor.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;

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

function TPedidosClienteReportController.Print(const AID : String): Boolean;
var
  AStream: Binary;
  AEditor : IEditorPedidosClientePreview;
begin
  Result := False;
  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
  begin
    AEditor.TablaImpresion := nme_PedidosCliente;
    AEditor.IdTablaImpresion := AID;
    Result := AEditor.Print;
  end;
end;

end.
