unit uPedidosClienteReportController;

interface

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

type
  IPedidosClienteReportController = interface(IControllerBase)
  ['{1F989C7E-9B12-4208-8CB3-C4E1B202D62A}']
    procedure Preview(const AListaID : TIntegerList);
    procedure Print(const AListaID : TIntegerList);
  end;

  TPedidosClienteReportController = class(TControllerBase, IPedidosClienteReportController)
  private
    FDataModule : IDataModulePedidosClienteReport;
    function CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;

  public
    constructor Create; override;
    destructor Destroy; override;

    procedure Preview(const AListaID : TIntegerList);
    procedure Print(const AListaID : TIntegerList);
  end;


implementation

uses
  uROTypes, Controls, uEditorRegistryUtils, uIEditorPedidosClientePreview,
  uEditorPreview, uDataModulePedidosCliente,
  uEditorBase, cxControls, uStringsUtils, uSistemaFunc, uFactuGES_App;

{ TPedidosClienteReportController }

constructor TPedidosClienteReportController.Create;
begin
  inherited;
  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 AListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorPedidosClientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptPedidos(AListaID);
    try
      CreateEditor('EditorPedidosClientePreview', IEditorPedidosClientePreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := AListaID;
          AEditor.Title := 'Pedido - ' + AppFactuGES.EmpresaActiva.NOMBRE;
          AEditor.LoadFromStream(AStream);
          AEditor.Preview;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

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

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptPedidos(AListaID);
    try
      CreateEditor('EditorPedidosClientePreview', IEditorPedidosClientePreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := AListaID;
          AEditor.Title := 'Albarán - ' + AppFactuGES.EmpresaActiva.NOMBRE;
          AEditor.LoadFromStream(AStream);
          AEditor.Print;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
