unit srvFacturasCliente_Impl;

{----------------------------------------------------------------------------}
{ This unit was automatically generated by the RemObjects SDK after reading  }
{ the RODL file associated with this project .                               }
{                                                                            }
{ This is where you are supposed to code the implementation of your objects. }
{----------------------------------------------------------------------------}

interface

uses
  {vcl:} Classes, SysUtils, 
  {RemObjects:} uROClientIntf, uROTypes, uROServer, uROServerIntf, uROSessions,
  {Ancestor Implementation:} DataAbstractService_Impl,
  {Used RODLs:} DataAbstract4_Intf,
  {Generated:}   uDABusinessProcessor, uDABin2DataStreamer, uDADataStreamer,
  uDAScriptingProvider, uDAClasses,
  FactuGES_Intf, uDAInterfaces;

type
  { TsrvFacturasCliente }
  TsrvFacturasCliente = class(TDataAbstractService, IsrvFacturasCliente)
    Diagrams: TDADiagrams;
    Bin2DataStreamer: TDABin2DataStreamer;
    bpFacturasCliente: TDABusinessProcessor;
    bpFacturasClienteDetalles: TDABusinessProcessor;
    schFacturasCliente: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DARemoteServiceBeforeGetDatasetData(const Dataset: IDADataset;
      const IncludeSchema: Boolean; const MaxRecords: Integer);
    procedure DARemoteServiceCreate(Sender: TObject);
    procedure DataAbstractServiceBeforeAcquireConnection(aSender: TObject; var aConnectionName: string);
  private
  protected
    { IsrvFacturasCliente methods }
    function GenerarInforme(const ListaID: TIntegerArray; const VerSello: Boolean = True): Binary;
    function GenerarInformeEnWord(const ID: Integer; const VerSello: Boolean = True): Binary;
    function GenerarInformeEnPDF(const ListaID: TIntegerArray; const VerSello: Boolean = True): Binary;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} FactuGES_Invk, Dialogs, uSistemaFunc,
  uDataModuleServer, uDatabaseUtils, uUsersManager, schFacturasClienteClient_Intf,
  uRestriccionesUsuarioUtils,
//  uRptFacturasCliente_Server,
  uBizFacturasClienteServer, uRptWordFacturaCliente, uRptFacturasCliente_Server;

procedure Create_srvFacturasCliente(out anInstance : IUnknown);
begin
  anInstance := TsrvFacturasCliente.Create(NIL);
end;

{ srvFacturasCliente }

procedure TsrvFacturasCliente.DARemoteServiceBeforeGetDatasetData(
  const Dataset: IDADataset; const IncludeSchema: Boolean;
  const MaxRecords: Integer);
begin
{
  if DataSet.Name = nme_FacturasCliente then
  begin
    { Aquí se asegura que el usuario sólo accede a facturas
      de las empresas a las que tiene permiso para acceder
      filtrando DataSet por ID_EMPRESA. }
{    FiltrarAccesoUsuario(Session, Connection, schFacturasCliente, DataSet, fld_FacturasClienteID_EMPRESA);
  end;
}
end;

procedure TsrvFacturasCliente.DARemoteServiceCreate(Sender: TObject);
begin
  SessionManager := dmServer.SessionManager;
  bpFacturasCliente.BusinessRulesID := BIZ_SERVER_FACTURAS_CLIENTE;
end;

procedure TsrvFacturasCliente.DataAbstractServiceBeforeAcquireConnection(
  aSender: TObject; var aConnectionName: string);
begin
  ConnectionName := dmServer.ConnectionName;
end;

function TsrvFacturasCliente.GenerarInformeEnPDF(const ListaID: TIntegerArray; const VerSello: Boolean = True): Binary;
var
  AReportGenerator : TRptFacturasCliente;
begin
  AReportGenerator := TRptFacturasCliente.Create(nil);
  try
    Result := AReportGenerator.GenerarFacturaEnPDF(ListaID, VerSello);
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

function TsrvFacturasCliente.GenerarInforme(const ListaID: TIntegerArray; const VerSello: Boolean = True): Binary;
var
  AReportGenerator : TRptFacturasCliente;
begin
  AReportGenerator := TRptFacturasCliente.Create(nil);
  try
    Result := AReportGenerator.GenerarFactura(ListaID, VerSello);
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

function TsrvFacturasCliente.GenerarInformeEnWord(const ID: Integer; const VerSello: Boolean = True): Binary;
var
  AReportGenerator : TRptWordFacturaCliente;
  AFicheroTMP : TFileName;
begin
  Result := NIL;
  AFicheroTMP := DarFicheroTemporal;
  AReportGenerator := TRptWordFacturaCliente.Create(nil);
  try
    if AReportGenerator.Exportar(IntToStr(ID), AFicheroTMP, VerSello) then
    begin
      Result := Binary.Create;
      Result.LoadFromFile(AFicheroTMP);
      SysUtils.DeleteFile(AFicheroTMP)
    end;
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

initialization
  TROClassFactory.Create('srvFacturasCliente', Create_srvFacturasCliente, TsrvFacturasCliente_Invoker);

finalization

end.
