unit srvAlbaranesCliente_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:} Windows, 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, uROClient;

type
  { TsrvAlbaranesCliente }
  TsrvAlbaranesCliente = class(TDataAbstractService, IsrvAlbaranesCliente)
    bpAlbaranesCliente: TDABusinessProcessor;
    bpAlbaranesCliente_Detalles: TDABusinessProcessor;
    Bin2DataStreamer: TDABin2DataStreamer;
    bpAlbaranesCliente_DetalleColor: TDABusinessProcessor;
    schAlbaranesCliente: TDASchema;
    DADataDictionary: TDADataDictionary;
    procedure DataAbstractServiceBeforeAcquireConnection(aSender: TObject; var aConnectionName: string);
    procedure DARemoteServiceBeforeGetDatasetData(const Dataset: IDADataset; const IncludeSchema: Boolean; const MaxRecords: Integer);
    procedure DARemoteServiceCreate(Sender: TObject);
  protected
    { IsrvAlbaranesCliente methods }
    function GenerarInforme(const ListaID: TIntegerArray): Binary;
    function GenerarInformeEtiquetas(const ListaID: TIntegerArray; const AMercancia: Variant; const ABultos: Integer): Binary;
    function GenerarInformeEnWord(const ID: Integer): Binary;
    function GenerarInformeEnPDF(const ListaID: TIntegerArray): Binary;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} FactuGES_Invk, uDataModuleServer, uDatabaseUtils,
  schAlbaranesClienteClient_Intf, uRestriccionesUsuarioUtils,
  uRptAlbaranesCliente_Server, uBizAlbaranClienteServer, uRptWordAlbaranCliente,
  uSistemaFunc;

procedure Create_srvAlbaranesCliente(out anInstance : IUnknown);
begin
  anInstance := TsrvAlbaranesCliente.Create(NIL);
end;

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

procedure TsrvAlbaranesCliente.DARemoteServiceCreate(Sender: TObject);
begin
  //SessionManager := dmServer.SessionManager;
  bpAlbaranesCliente.BusinessRulesID := BIZ_SERVER_ALBARAN_CLIENTE;
end;

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

function TsrvAlbaranesCliente.GenerarInformeEnPDF(
  const ListaID: TIntegerArray): Binary;
var
  AReportGenerator : TRptAlbaranesCliente;
begin
  AReportGenerator := TRptAlbaranesCliente.Create(nil);
  try
    Result := AReportGenerator.GenerarAlbaranEnPDF(ListaID);
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

function TsrvAlbaranesCliente.GenerarInformeEtiquetas(const ListaID : TIntegerArray; const AMercancia: Variant; const ABultos: Integer): Binary;
var
  AReportGenerator : TRptAlbaranesCliente;
begin
  AReportGenerator := TRptAlbaranesCliente.Create(nil);
  try
    Result := AReportGenerator.GenerarEtiquetas(ListaID, AMercancia, ABultos);
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

function TsrvAlbaranesCliente.GenerarInforme(const ListaID: TIntegerArray): Binary;
var
  AReportGenerator : TRptAlbaranesCliente;
begin
  AReportGenerator := TRptAlbaranesCliente.Create(nil);
  try
    Result := AReportGenerator.GenerarAlbaran(ListaID);
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

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

initialization
  TROClassFactory.Create('srvAlbaranesCliente', Create_srvAlbaranesCliente, TsrvAlbaranesCliente_Invoker);

finalization

end.
