<?php
namespace App\Services;
use App\Entity\SecMensaje;
use App\Entity\TerPersona;
use Doctrine\ORM\EntityManagerInterface;
class NotificacionesInternas
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
# enviar notificaciones internas
public function notificacionInterna(TerPersona $persona,$tituloNotificacion = "Sin Titulo", $mensajeNotificacion = "Sin Mensaje",string $notificacionProgramada = null)
{
# Closure funcion interna para ejecutar fecha programada
function calcularFechaProgramada(string $parametro): ?\DateTime
{
// Limpiar espacios
$parametro = trim(strtolower($parametro));
// Validar formato: nĂºmero + letra (d, m, a)
if (!preg_match('/^(\d+)([dma])$/', $parametro, $matches)) {
return null;
}
$cantidad = (int) $matches[1];
$unidad = $matches[2];
$fecha = new \DateTime(); // Fecha actual
switch ($unidad) {
case 'd':
$fecha->modify("+{$cantidad} days");
break;
case 'm':
$fecha->modify("+{$cantidad} months");
break;
case 'a':
$fecha->modify("+{$cantidad} years");
break;
}
return $fecha;
}
$notiInterna = new SecMensaje();
$notiInterna->setProceso(
$this->entityManager->getRepository(\App\Entity\ParProceso::class)->find($persona->getPerfilCargo()->getParProceso())
);
$notiInterna->setColaborador($persona);
$notiInterna->setPerfilCargo($persona->getPerfilCargo());
$notiInterna->setEstado($this->entityManager->getRepository(\App\Entity\ParEstado::class)->find(1));
$notiInterna->setTipo(1);
$notiInterna->setTitulo($tituloNotificacion);
$notiInterna->setDescripcion($mensajeNotificacion);
$notiInterna->setLeido(false);
# Validar si se manda alguna fecha programada
if($notificacionProgramada){
$fechaProgramada = calcularFechaProgramada($notificacionProgramada);
if($fechaProgramada){
$notiInterna->setFechaProgramada($fechaProgramada); # Guardar la fecha programada
}
}
$this->entityManager->persist($notiInterna);
$this->entityManager->flush();
}
}