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;
    bpEmpleados: TDABusinessProcessor;
    bpClientes: TDABusinessProcessor;
    bpProveedores: TDABusinessProcessor;
    bpDireccionesContacto: TDABusinessProcessor;
    bpClientesDescuentos: TDABusinessProcessor;
    Bin2DataStreamer: TDABin2DataStreamer;
    bpPersonalContacto: TDABusinessProcessor;
    bpAgentes: TDABusinessProcessor;
    bpAgentesComisiones: 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 GenerarInformeFichaEmpleado(const ListaID: TIntegerArray): Binary;
    function GenerarInformeEtiquetas(const ListaID: TIntegerArray): Binary;
    function DarListaContratosEmpleados: String;
    function DarListaDescripcionesProveedores: String;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} FactuGES_Invk, Dialogs, uBizContactosServer,
  uDataModuleServer, uDatabaseUtils, uUsersManager,
  schContactosClient_Intf, uBizEmpleadosServer, uBizAgentesServer,
  uBizClientesServer, uBizProveedoresServer, uRestriccionesUsuarioUtils,
  uStringsUtils, Variants,
  uRptFichasEmpleado_Server, 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 Assigned(Dataset.FindField(fld_ContactosID_EMPRESA)) 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;
  bpEmpleados.BusinessRulesID := BIZ_SERVER_EMPLEADO;
  bpAgentes.BusinessRulesID := BIZ_SERVER_AGENTE;
  bpClientes.BusinessRulesID := BIZ_SERVER_CLIENTE;
  bpProveedores.BusinessRulesID := BIZ_SERVER_PROVEEDOR;
end;

function TsrvContactos.DarListaContratosEmpleados: String;
var
  dsContratos : IDADataset;
  Aux : TStringList;
begin
  Aux := TStringList.Create;

  dsContratos := schContactos.NewDataset(Connection, 'ContratosEmpleados', '', True);
  try
    while not dsContratos.EOF do
    begin
      if not VarIsNull(dsContratos.FieldValues[0]) then
        Aux.Add(dsContratos.FieldValues[0]);
      dsContratos.Next;
    end;
    Result := StringsToString(Aux, '|');
  finally
    dsContratos.Close;
    dsContratos := NIL;
  end;
end;

function TsrvContactos.DarListaDescripcionesProveedores: String;
var
  dsDescripciones : IDADataset;
  Aux : TStringList;
begin
  Aux := TStringList.Create;

  dsDescripciones := schContactos.NewDataset(Connection, 'DescripcionesProveedores', '', True);
  try
    while not dsDescripciones.EOF do
    begin
      if not VarIsNull(dsDescripciones.FieldValues[0]) then
        Aux.Add(dsDescripciones.FieldValues[0]);
      dsDescripciones.Next;
    end;
    Result := StringsToString(Aux, '|');
  finally
    dsDescripciones.Close;
    dsDescripciones := NIL;
  end;
end;

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

function TsrvContactos.GenerarInformeEtiquetas(const ListaID: TIntegerArray): Binary;
var
  AReportGenerator : TRptEtiquetasContacto;
begin
  AReportGenerator := TRptEtiquetasContacto.Create(nil);
  try
    Result := AReportGenerator.GenerarInforme(ListaID);
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

function TsrvContactos.GenerarInformeFichaEmpleado(const ListaID: TIntegerArray): Binary;
var
  AReportGenerator : TRptFichasEmpleado;
begin
  AReportGenerator := TRptFichasEmpleado.Create(nil);
  try
    Result := AReportGenerator.GenerarFicha(ListaID);
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

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

finalization

end.
