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:} uROClientIntf, uROTypes, uROServer, uROServerIntf, uROSessions,
  {Ancestor Implementation:} DARemoteService_Impl,
  {Used RODLs:} DataAbstract_Intf,
  {Generated:} FactuGES_Intf, uDADataTable, uDABINAdapter, uDAClasses,
  uDAInterfaces, uDAScriptingProvider,
  uDABusinessProcessor;

type
  { TsrvContactos }
  TsrvContactos = class(TDARemoteService, IsrvContactos)
    BINAdapter: TDABINAdapter;
    Diagrams: TDADiagrams;
    bpContactos: TDABusinessProcessor;
    bpDatosBancarios: TDABusinessProcessor;
    bpCategoriasContactos: TDABusinessProcessor;
    bpAgentes: TDABusinessProcessor;
    bpClientes: TDABusinessProcessor;
    bpProveedores: TDABusinessProcessor;
    bpDireccionesContacto: TDABusinessProcessor;
    bpAgentesComisiones: TDABusinessProcessor;
    bpClientesDescuentos: TDABusinessProcessor;
    schContactos: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DARemoteServiceBeforeAcquireConnection(Sender: TDARemoteService;
      var ConnectionName: string);
    procedure DARemoteServiceBeforeGetDatasetData(const Dataset: IDADataset;
      const IncludeSchema: Boolean; const MaxRecords: Integer);
    procedure DARemoteServiceCreate(Sender: TObject);
  public
    function GetNextAutoInc(const GeneratorName: string): Integer;
    function GenerateEtiquetas(const ID_Contactos: string): Binary;
    function ClienteEliminable(const ID: Integer): Boolean;    
  end;

implementation

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

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

{ TsrvContactos }
function TsrvContactos.ClienteEliminable(const ID: Integer): Boolean;
var
  dsDataSet: IDADataset;
  AConnection: IDAConnection;
begin
   AConnection:= dmServer.DarNuevaConexion;
   AConnection.BeginTransaction;
   try
     dsDataSet := schContactos.NewDataset(AConnection, 'ClienteEliminable', ['ID'], [ID]);
     dsDataSet.Active := True;
     Result :=  not dsDataSet.IsEmpty;
   finally
     AConnection.RollbackTransaction;
     dsDataSet := Nil;
     AConnection := Nil;
   end;
end;

procedure TsrvContactos.DARemoteServiceBeforeAcquireConnection(
  Sender: TDARemoteService; var ConnectionName: string);
begin
  ConnectionName := dmServer.ConnectionName;
end;

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_GruposCliente) and
     (DataSet.Name <> nme_AgentesComisiones) and
     (DataSet.Name <> nme_ClientesDescuentos) and     
     (DataSet.Name <> nme_ContactosCategorias) 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;
  bpAgentes.BusinessRulesID := BIZ_SERVER_AGENTE;
  bpClientes.BusinessRulesID := BIZ_SERVER_CLIENTE;
  bpProveedores.BusinessRulesID := BIZ_SERVER_PROVEEDOR;  
end;

function TsrvContactos.GenerateEtiquetas(const ID_Contactos: string): Binary;
var
  AReportGenerator : TRptEtiquetasContactos;
begin
  AReportGenerator := TRptEtiquetasContactos.Create(nil);
  try
    Result := AReportGenerator.GenerarEtiquetas(ID_Contactos);
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

function TsrvContactos.GetNextAutoInc(const GeneratorName: string): Integer;
begin
  Result := uDatabaseUtils.GetNextAutoInc(GeneratorName)
end;

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

finalization

end.
