2011-05-19 08:46:58 +00:00
< ? php
/*
* Clase Oferta
*/
include_once ( " BD.php " );
include_once ( " Automata.php " );
include_once ( " ListaCandidatos.php " );
include_once ( " Candidato.php " );
class Oferta {
// Atributos:
/* Usuario que tiene activa la sesi<73> n. */
private $usuario ;
protected $campos = array ();
// Constructores:
function Oferta ( $oid , $usuario ) {
$this -> usuario = $usuario ;
$consulta = " SELECT oid FROM candidato_pedido WHERE oid = ' $oid ' " ;
$bd = new BD ();
$num = $bd -> numFilas ( $consulta );
if ( $num > 0 ) {
$this -> campos [ 'oid' ] = $oid ;
} else {
$error = " Oferta no encontrada. " ;
throw new Exception ( $error );
}
}
2012-03-26 17:07:21 +00:00
function esEliminable () {
return ( $oferta -> getValor ( " estado " ) == 100 );
}
2011-05-30 09:15:00 +00:00
function eliminar () {
2012-03-26 17:07:21 +00:00
return false ;
if ( $this -> esEliminable ()) {
//Antes de nada quitamos el candidato asociado a la oferta para que se haga la l<> gica que corresponda
$idCand = $this -> getValor ( " candidato " );
if ( ! empty ( $idCand )) {
if ( ! $this -> quitarCandidato ()) {
return false ;
}
}
// eliminamos en la oferta de la BD
$consulta = " DELETE FROM candidato_pedido WHERE oid = " . $this -> campos [ 'oid' ];
$bd = new BD ();
if ( ! $bd -> execQuery ( $consulta )) {
return false ;
} else {
$mensaje = " Oferta eliminada " ;
$this -> actualizarHistorial ( $mensaje );
return true ;
}
}
2011-05-30 09:15:00 +00:00
}
2011-05-19 08:46:58 +00:00
function actualizarHistorial ( $mensaje ) {
$oid = $this -> getValor ( " oid " );
$usuario = $this -> usuario -> getValor ( " nombre " );
$consulta = " INSERT INTO historial_oferta (oid_h, fecha_h, persona_h, texto_h) VALUES(' $oid ', now(), ' $usuario ', ' $mensaje ') " ;
$bd = new BD ();
return $bd -> execQuery ( $consulta );
}
2011-06-22 08:26:02 +00:00
function darNombreEstado ( $cod , $idioma ) {
$consulta = " SELECT NOMBRE from candidaturas_estados WHERE cod= \" " . $cod . " \" AND idioma= \" " . $idioma . " \" " ;
$bd = new BD ();
return $bd -> getCampo ( $consulta );
}
2011-05-19 08:46:58 +00:00
function getValor ( $nombre ) {
if ( array_key_exists ( $nombre , $this -> campos )) {
// El campo ya lo hab<61> amos recuperamos, lo mostramos
return $this -> campos [ $nombre ];
} else {
switch ( $nombre ) {
2012-03-26 17:07:21 +00:00
case " nombre_gerente " : $idGerente = $this -> getValor ( " gerente " );
$consulta = " SELECT CONCAT(nombre, \" \" ,apellidos) FROM usuarios WHERE oid= \" " . $idGerente . " \" " ;
break ;
2011-05-19 08:46:58 +00:00
case " nombre_estado " :
$idEstado = $this -> getValor ( " estado " );
$idioma = $this -> usuario -> getValor ( " idioma " );
$consulta = " SELECT nombre FROM candidaturas_estados WHERE cod= \" " . $idEstado . " \" AND idioma= \" " . $idioma . " \" " ;
break ;
case " nombre_solicitud " :
$oidPedido = $this -> getValor ( " pedido " );
$consulta = " SELECT nombre FROM pedidos WHERE oid= \" " . $oidPedido . " \" " ;
break ;
case " nombre_candidato " :
$oidCandidato = $this -> getValor ( " candidato " );
$consulta = " SELECT concat(nombre, ' ', apellidos) FROM usuarios WHERE oid= \" " . $oidCandidato . " \" " ;
break ;
default : $consulta = " SELECT " . $nombre . " FROM candidato_pedido WHERE oid= \" " . $this -> campos [ 'oid' ] . " \" " ;
break ;
}
// Lo insertamos para nosotros
$bd = new BD ();
$valor = $bd -> getCampo ( $consulta );
// Lo insertamos para nosotros
$arrayAct = array ( $nombre => $valor );
$this -> campos = $this -> campos + $arrayAct ;
return $valor ;
}
}
/**
* Acceso a los campos del pedido .
*
* @ return los campos del pedido .
*/
function getCampos () {
return $this -> campos ;
}
function getSiguientes () {
$estado = $this -> getValor ( " estado " );
$idioma = $this -> usuario -> getValor ( " idioma " );
$rol = $this -> usuario -> getValor ( " rol " );
$a = new Automata ( " candidaturas " , $idioma , $rol );
$siguientes = $a -> getSiguientes ( $estado );
return $siguientes ;
}
function transita ( $destino , $argumentos ) {
$origen = $this -> getValor ( " estado " );
$idioma = $this -> usuario -> getValor ( " idioma " );
$rol = $this -> usuario -> getValor ( " rol " );
$a = new Automata ( " candidaturas " , $idioma , $rol );
$transita = $a -> getTransicion ( $origen , $destino );
if (( $transita == " " ) || ! ( $transita >= 0 )) {
return false ;
} else {
$res = $this -> ejecutaTransicion ( $transita , $argumentos );
if ( $res ) {
$this -> setAutomatico = true ;
$this -> setCampo ( " msgEstado " , $argumentos );
$this -> setCampo ( " estado " , $destino );
$this -> setAutomatico = false ;
}
return $res ;
}
}
private function ejecutaTransicion ( $codigo , $argumentos ) {
// Si no hace nada al transitar salimos sin m<> s.
if ( $codigo == 0 )
return true ;
$funcion = " ejecutar $codigo " ;
$res = call_user_func ( array ( " Oferta " , $funcion ), $argumentos );
return $res ;
}
/**
2012-03-26 17:07:21 +00:00
* En proceso -> presentada
2011-05-19 08:46:58 +00:00
*/
2012-03-26 17:07:21 +00:00
private function ejecutar100120 () {
2011-05-19 08:46:58 +00:00
// Comprobamos que haya un candidato asignado.
$candidato = $this -> getValor ( " candidato " );
if ( ! empty ( $candidato )) {
return true ;
} else {
$error = " No se puede cambiar de estado porque no hay ning<6E> n candidato asignado a esta oferta " ;
throw new Exception ( $error );
return false ;
exit ;
}
}
2012-03-26 17:07:21 +00:00
2011-05-19 08:46:58 +00:00
function setCampo ( $nombre , $valor ) {
// PERMISOS:
/*
* Admin ( 1 ) - Todos
* Gestor ( 3 ) - Los suyos
* Otro - Excepci<EFBFBD> n
*/
2012-03-26 17:07:21 +00:00
2011-05-19 08:46:58 +00:00
$gerente = $this -> getValor ( " gerente " );
$sesion = $this -> usuario -> getValor ( " oid " );
if ( $this -> usuario -> tieneRol ( 1 ) || ( $this -> usuario -> tieneRol ( 3 ) && $sesion == $gerente ) || $this -> setAutomatico ) {
$viejo = $this -> getValor ( $nombre );
if ( $viejo != $valor ) {
// Comprobamos multivaluado y casos especiales antes del cambio:
$viejo = $this -> getValorMulti ( $nombre , $viejo );
$this -> campos [ $nombre ] = $valor ;
$oid = $this -> getValor ( " oid " );
$consulta = " UPDATE candidato_pedido SET $nombre = ' $valor ' WHERE oid=' $oid ' " ;
$bd = new BD ();
if ( $bd -> execQuery ( $consulta )) {
// Guardar en el historial
$valor = $this -> getValorMulti ( $nombre , $valor );
2011-06-22 08:26:02 +00:00
////////////////////////////////////////////////////////////////////////////////
//SOLO ACTUALIZA EL HISTORIAL LOS CAMBIOS DE ESTADO Y ASIGNACIONES DE CANDIDATOS
////////////////////////////////////////////////////////////////////////////////
if ( $nombre == " estado " ) {
$estado_viejo = $this -> darNombreEstado ( $viejo , " sp " );
$estado_nuevo = $this -> darNombreEstado ( $valor , " sp " );
$historial = " Cambio de estado ( " . $estado_viejo . " a " . $estado_nuevo . " ) " ;
$this -> actualizarHistorial ( $historial );
2012-03-26 17:07:21 +00:00
//Una vez cambiado de estado la oferta, si el nuevo estado es Rechazado o Retirada, liberamos al candidato
if (( $valor == '160' ) || ( $valor == '170' )) {
$idCand = $this -> getValor ( " candidato " );
if ( ! empty ( $idCand )) {
if ( ! $this -> quitarCandidato ()) {
return false ;
}
}
}
2011-06-22 08:26:02 +00:00
}
2011-05-19 08:46:58 +00:00
}
}
} else {
$error = " El usuario no tiene permisos para editar la solicitud de oferta. " ;
throw new Exception ( $error );
}
}
private function getValorMulti ( $nombre , $antiguo ) {
switch ( $nombre ) {
/* case " procedencia " :
$valor = nombre_procedencia ( $antiguo );
break ;
case " estado " :
$valor = nombre_estado_pedido ( $antiguo );
break ;
case " localidad " :
$valor = nombre_localidad ( $antiguo );
break ;
case " perfil " :
$valor = nombre_perfil ( $antiguo );
break ;
case " cliente " :
$valor = nombre_cliente ( $antiguo );
break ; */
default :
$valor = $antiguo ;
break ;
}
return $valor ;
}
/**
2011-12-28 11:22:48 +00:00
* Devuelve una lista de candidatos disponibles para una oferta , se cambia a los nuevos estados #723 Peticion
2011-05-19 08:46:58 +00:00
*/
function getCandidatosDisponibles () {
2011-12-28 11:22:48 +00:00
return $this -> getCandidatos ( " 520,530,540,560,600 " );
2011-05-19 08:46:58 +00:00
}
/**
* Devuelve una lista de candidatos de un estado dado
*/
private function getCandidatos ( $estado ) {
$lista = new ListaCandidatos ( $this -> usuario , " " , " " , $estado );
return $lista ;
}
function getHistorial () {
$historial = " " ;
$oid = $this -> getValor ( " oid " );
2011-06-22 08:26:02 +00:00
$consulta = " SELECT * FROM historial_oferta WHERE oid_h=' $oid ' ORDER BY id DESC " ;
2011-05-19 08:46:58 +00:00
$bd = new BD ();
$resultado = $bd -> execQuery ( $consulta );
while ( $rows = mysql_fetch_array ( $resultado )) {
$fecha = $rows [ " fecha_h " ];
$persona = $rows [ " persona_h " ];
$texto = $rows [ " texto_h " ];
$historial .= " [ $fecha ] $persona - $texto\n " ;
}
return $historial ;
}
2011-06-01 18:24:45 +00:00
function getOfertasCandidato ( $oid ) {
$consulta = " SELECT * FROM candidato_pedido WHERE candidato=' $oid ' ORDER BY fecha DESC " ;
$bd = new BD ();
$resultado = $bd -> execQuery ( $consulta );
return $resultado ;
}
2011-12-28 11:22:48 +00:00
function getNumOfertasCandidato ( $oid ) {
2012-06-19 15:50:53 +00:00
$consulta = " SELECT * FROM candidato_pedido WHERE candidato=' $oid ' and estado not in (160,170) ORDER BY fecha DESC " ;
2011-12-28 11:22:48 +00:00
$bd = new BD ();
$resultado = $bd -> execQuery ( $consulta );
return mysql_num_rows ( $resultado );
}
2011-05-19 08:46:58 +00:00
function quitarCandidato () {
$idCand = $this -> getValor ( " candidato " );
2012-03-26 17:07:21 +00:00
$this -> setCampo ( " candidato " , " " );
2011-05-19 08:46:58 +00:00
if ( ! empty ( $idCand )) {
$candidato = new Candidato ( $this -> usuario , $idCand );
$estadoCand = $candidato -> getValor ( " estado " );
2011-12-28 11:22:48 +00:00
$numOfertasCandidato = $this -> getNumOfertasCandidato ( $idCand );
2011-05-19 08:46:58 +00:00
switch ( $estadoCand ) {
2012-06-19 15:50:53 +00:00
//TODO CANDIDATO ASOCIADO A UNA OFERTA (este como este su situaci<63> n) pasar<61> a En proceso:disponible si no tiene alguna otra oferta distinta de Rechazada 160 o Retirada 170,
//el estado del candidato no cambia si est<73> asociado a alguna otra oferta que no sea Rechazada 160 o Retirada 170. se cambia a los nuevos estados #723 Peticion
2011-12-28 11:22:48 +00:00
case 600 : ;
2011-06-22 08:26:02 +00:00
case 560 : ;
2011-06-13 17:33:34 +00:00
case 510 : ;
2011-12-28 11:22:48 +00:00
case 511 : ;
2011-12-15 17:11:47 +00:00
case 520 : ;
2011-06-13 17:33:34 +00:00
case 530 : ;
2011-12-15 17:26:30 +00:00
case 540 :
2011-06-22 08:26:02 +00:00
//Modificamos historial de candidato
$mensaje = " Se quita en oferta " . $this -> getValor ( " referencia " );
$candidato -> actualizarHistorial ( $mensaje );
//Modificamos historial de la oferta
$nombre_candidato = $candidato -> getValor ( " nombre " ) . " " . $candidato -> getValor ( " apellidos " );
$mensaje = " El candidato " . $nombre_candidato . " se quita de la oferta " ;
$this -> actualizarHistorial ( $mensaje );
//Cambiamos de estado al candidato
2012-06-19 15:50:53 +00:00
if ( $numOfertasCandidato < 1 ) {
2011-06-01 18:24:45 +00:00
$candidato -> transita ( " 540 " , " " );
}
return true ;
break ;
2011-05-19 08:46:58 +00:00
default :
$error = " [quitarCandidato]. El candidato tiene un estado no permitido (' " . $estadoCand . " ') " ;
throw new Exception ( $error );
}
}
}
2011-06-01 18:24:45 +00:00
function asignarCandidato ( $idCandidato ) {
2011-06-13 17:33:34 +00:00
//Lo primero es tratar el candidato asociado actualmente antes de asignar el pasado por parametro
2011-05-19 08:46:58 +00:00
$idCand = $this -> getValor ( " candidato " );
if ( ! empty ( $idCand )) {
if ( ! $this -> quitarCandidato ()) {
return false ;
}
}
2011-06-13 17:33:34 +00:00
2011-05-19 08:46:58 +00:00
$candidato = new Candidato ( $this -> usuario , $idCandidato );
$estadoCand = $candidato -> getValor ( " estado " );
2011-12-28 11:22:48 +00:00
$numOfertasCandidato = $this -> getNumOfertasCandidato ( $idCand );
2011-06-10 15:03:20 +00:00
2011-05-19 08:46:58 +00:00
switch ( $estadoCand ) {
2011-12-28 11:22:48 +00:00
//En proceso:Disponible asignado, el estado del candidato no cambia, solo se asigna a la oferta y ya est<73> .
2011-06-10 15:03:20 +00:00
case 560 :
2011-06-01 18:24:45 +00:00
$this -> setCampo ( " candidato " , $idCandidato );
2011-06-22 08:26:02 +00:00
//Modificamos historial de candidato
$mensaje = " Asignado en oferta " . $this -> getValor ( " referencia " );
$candidato -> actualizarHistorial ( $mensaje );
//Modificamos historial de oferta
$nombre_candidato = $candidato -> getValor ( " nombre " ) . " " . $candidato -> getValor ( " apellidos " );
$mensaje = " Asignado el candidato " . $nombre_candidato ;
$this -> actualizarHistorial ( $mensaje );
2011-06-01 18:24:45 +00:00
return true ;
2011-12-28 11:22:48 +00:00
break ;
2011-06-01 18:24:45 +00:00
2011-12-28 11:22:48 +00:00
//Rechazado, Sin m<> s adelante, En proceso:Disponible, Otras provincias. se cambia a los nuevos estados #723 Peticion
//Todos los candidatos con alguno de estos estados cambiar<61> n a En proceso:Disponible asignado (560)
case 600 : ;
2011-12-15 17:11:47 +00:00
case 520 : ;
2011-12-28 11:22:48 +00:00
case 530 : ;
2011-06-10 15:03:20 +00:00
case 540 :
2011-06-01 18:24:45 +00:00
$this -> setCampo ( " candidato " , $idCandidato );
2011-06-22 08:26:02 +00:00
//Modificamos historial de candidato
$mensaje = " Asignado en oferta " . $this -> getValor ( " referencia " );
$candidato -> actualizarHistorial ( $mensaje );
//Modificamos historial de oferta
$nombre_candidato = $candidato -> getValor ( " nombre " ) . " " . $candidato -> getValor ( " apellidos " );
$mensaje = " Asignado el candidato " . $nombre_candidato ;
$this -> actualizarHistorial ( $mensaje );
//Cambiamos el estado del candidato
2011-06-01 18:24:45 +00:00
if ( $candidato -> transita ( " 560 " , " " )){
2011-05-19 08:46:58 +00:00
return true ;
2011-06-01 18:24:45 +00:00
}
else {
$error = " ERROR: El candidato no ha podido cambiar de estado " ;
throw new Exception ( $error );
}
break ;
2011-06-13 17:33:34 +00:00
2011-12-28 11:22:48 +00:00
//("")
2011-06-01 18:24:45 +00:00
default :
2011-06-13 17:33:34 +00:00
$error = " El candidato tiene un estado no permitido (' " . $candidato -> getValor ( " nombre_estado " ) . " ') " ;
2011-06-01 18:24:45 +00:00
throw new Exception ( $error );
2011-05-19 08:46:58 +00:00
}
}
}
?>