unit srvContactos_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:} uROXMLIntf, uROClientIntf, uROTypes, uROServer, uROServerIntf, uROSessions,
  {Required:} uRORemoteDataModule,
  {Ancestor Implementation:} DataAbstractService_Impl,
  {Used RODLs:} DataAbstract4_Intf,
  {Generated:}   uDABusinessProcessor, uDABin2DataStreamer, uDADataStreamer,
  uDAScriptingProvider, uDAClasses,
  FactuGES_Intf, uDAInterfaces;

type
  { TsrvContactos }
  TsrvContactos = class(TDataAbstractService, IsrvContactos)
    Diagrams: TDADiagrams;
    bpContactos: TDABusinessProcessor;
    bpDatosBancarios: TDABusinessProcessor;
    bpVendedores: TDABusinessProcessor;
    bpClientes: TDABusinessProcessor;
    bpProveedores: TDABusinessProcessor;
    bpDireccionesContacto: TDABusinessProcessor;
    bpClientesDescuentos: TDABusinessProcessor;
    Bin2DataStreamer: TDABin2DataStreamer;
    bpSubCuentasContacto: TDABusinessProcessor;
    schContactos: 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);
  public
    function GenerateEtiquetasReport(const ContactosID: String): Binary;
    function DarTiendaDeUsuario(const ID_Empresa: Integer; const ID_Usuario: Integer): Integer;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} FactuGES_Invk, Dialogs, uBizContactosServer,
  uDataModuleServer, uDatabaseUtils, uUsersManager,
  schContactosClient_Intf, uBizVendedoresServer,
  uBizClientesServer, uBizProveedoresServer, uRestriccionesUsuarioUtils,
  uRptEtiquetasContacto_Server;

procedure Create_srvContactos(out anInstance : IUnknown);
begin
  anInstance := TsrvContactos.Create(NIL);
end;

{ TsrvContactos }

procedure TsrvContactos.DARemoteServiceBeforeGetDatasetData(
  const Dataset: IDADataset; const IncludeSchema: Boolean;
  const MaxRecords: Integer);
begin
  if (DataSet.Name <> nme_DatosBancarios) and
     (DataSet.Name <> nme_DireccionesContacto) and
     (DataSet.Name <> nme_ProcedenciasCliente) and
     (DataSet.Name <> nme_GruposCliente) and
     (DataSet.Name <> nme_GruposProveedor) and
     (DataSet.Name <> nme_ClientesDescuentos) then
  begin
    { Aquí se asegura que el usuario sólo accede a contactos
      de las empresas a las que tiene permiso para acceder
      filtrando DataSet por ID_EMPRESA. }
    FiltrarAccesoUsuario(Session, Connection, schContactos, DataSet, fld_ContactosID_EMPRESA);
  end;
end;

procedure TsrvContactos.DARemoteServiceCreate(Sender: TObject);
begin
  SessionManager := dmServer.SessionManager;

  bpContactos.BusinessRulesID := BIZ_SERVER_CONTACTO;
  bpVendedores.BusinessRulesID := BIZ_SERVER_VENDEDOR;
  bpClientes.BusinessRulesID := BIZ_SERVER_CLIENTE;
  bpProveedores.BusinessRulesID := BIZ_SERVER_PROVEEDOR;
end;

function TsrvContactos.DarTiendaDeUsuario(const ID_Empresa: Integer;
  const ID_Usuario: Integer): Integer;
var
   ADataSet : IDADataset;
begin
  try
    ADataSet := schContactos.NewDataset(Connection, 'DarTiendaDeUsuario', ['ID_EMPRESA', 'ID_USUARIO'], [ID_Empresa, ID_Usuario]);
    ADataSet.Open;
    if ADataSet.Dataset.RecordCount > 0 then
       Result := ADataSet.Dataset.Fields[0].AsInteger
    else
       Result := 0; // 0 -> Todas las tiendas
  finally
    ADataSet.Close;
  end;
end;

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

function TsrvContactos.GenerateEtiquetasReport(const ContactosID: String): Binary;
var
  AReportGenerator : TRptEtiquetasContacto;
begin
  AReportGenerator := TRptEtiquetasContacto.Create(nil);
  try
    Result := AReportGenerator.GenerarInforme(ContactosID);
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

initialization
  TROClassFactory.Create('srvContactos', Create_srvContactos, TsrvContactos_Invoker);

finalization

end.
