src/Entity/InvitacionEnviada.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\Doctrine\InvitacionEnviadaRepository;
  4. use Doctrine\DBAL\Types\Types;
  5. use Doctrine\ORM\Mapping as ORM;
  6. /**
  7. * @ORM\Entity(repositoryClass=InvitacionEnviadaRepository::class)
  8. * @ORM\Table(name="invitacion_enviada")
  9. * @ORM\HasLifecycleCallbacks()
  10. */
  11. class InvitacionEnviada extends BaseEntity
  12. {
  13. /**
  14. * @ORM\Id
  15. * @ORM\GeneratedValue
  16. * @ORM\Column(type="integer")
  17. */
  18. private ?int $id = null;
  19. /**
  20. * @ORM\ManyToOne(targetEntity=ConsorciPropietarioContenido::class)
  21. * @ORM\JoinColumn(nullable=false)
  22. */
  23. private ?ConsorciPropietarioContenido $remitente = null;
  24. /**
  25. * @ORM\Column(type="json")
  26. */
  27. private array $destinatariosInfo = [];
  28. /**
  29. * @ORM\Column(type="string", length=255)
  30. */
  31. private ?string $asunto = null;
  32. /**
  33. * @ORM\Column(type="text")
  34. */
  35. private ?string $cuerpo = null;
  36. /**
  37. * @ORM\Column(type="datetime")
  38. */
  39. private ?\DateTimeInterface $fechaEnvio = null;
  40. /**
  41. * @ORM\Column(type="integer")
  42. */
  43. private int $totalDestinatarios = 0;
  44. /**
  45. * @ORM\Column(type="integer")
  46. */
  47. private int $enviosExitosos = 0;
  48. /**
  49. * @ORM\Column(type="integer")
  50. */
  51. private int $enviosFallidos = 0;
  52. public function getId(): ?int
  53. {
  54. return $this->id;
  55. }
  56. public function getRemitente(): ?ConsorciPropietarioContenido
  57. {
  58. return $this->remitente;
  59. }
  60. public function setRemitente(?ConsorciPropietarioContenido $remitente): self
  61. {
  62. $this->remitente = $remitente;
  63. return $this;
  64. }
  65. public function getDestinatariosInfo(): array
  66. {
  67. return $this->destinatariosInfo;
  68. }
  69. public function setDestinatariosInfo(array $destinatariosInfo): self
  70. {
  71. $this->destinatariosInfo = $destinatariosInfo;
  72. return $this;
  73. }
  74. public function addDestinatarioInfo(array $destinatarioInfo): self
  75. {
  76. $this->destinatariosInfo[] = $destinatarioInfo;
  77. return $this;
  78. }
  79. public function getAsunto(): ?string
  80. {
  81. return $this->asunto;
  82. }
  83. public function setAsunto(string $asunto): self
  84. {
  85. $this->asunto = $asunto;
  86. return $this;
  87. }
  88. public function getCuerpo(): ?string
  89. {
  90. return $this->cuerpo;
  91. }
  92. public function setCuerpo(string $cuerpo): self
  93. {
  94. $this->cuerpo = $cuerpo;
  95. return $this;
  96. }
  97. public function getFechaEnvio(): ?\DateTimeInterface
  98. {
  99. return $this->fechaEnvio;
  100. }
  101. public function setFechaEnvio(\DateTimeInterface $fechaEnvio): self
  102. {
  103. $this->fechaEnvio = $fechaEnvio;
  104. return $this;
  105. }
  106. public function getTotalDestinatarios(): int
  107. {
  108. return $this->totalDestinatarios;
  109. }
  110. public function setTotalDestinatarios(int $totalDestinatarios): self
  111. {
  112. $this->totalDestinatarios = $totalDestinatarios;
  113. return $this;
  114. }
  115. public function getEnviosExitosos(): int
  116. {
  117. return $this->enviosExitosos;
  118. }
  119. public function setEnviosExitosos(int $enviosExitosos): self
  120. {
  121. $this->enviosExitosos = $enviosExitosos;
  122. return $this;
  123. }
  124. public function getEnviosFallidos(): int
  125. {
  126. return $this->enviosFallidos;
  127. }
  128. public function setEnviosFallidos(int $enviosFallidos): self
  129. {
  130. $this->enviosFallidos = $enviosFallidos;
  131. return $this;
  132. }
  133. /**
  134. * @ORM\PrePersist
  135. */
  136. public function onPrePersist(): void
  137. {
  138. if ($this->fechaEnvio === null) {
  139. $this->fechaEnvio = new \DateTime();
  140. }
  141. // Calcular totales a partir del array de destinatarios
  142. $this->totalDestinatarios = count($this->destinatariosInfo);
  143. }
  144. /**
  145. * Calcula las estadĂ­sticas de envĂ­o
  146. */
  147. public function calcularEstadisticas(): array
  148. {
  149. $exitosos = 0;
  150. $fallidos = 0;
  151. foreach ($this->destinatariosInfo as $destinatario) {
  152. if (isset($destinatario['envioExitoso']) && $destinatario['envioExitoso']) {
  153. $exitosos++;
  154. } else {
  155. $fallidos++;
  156. }
  157. }
  158. $this->enviosExitosos = $exitosos;
  159. $this->enviosFallidos = $fallidos;
  160. return [
  161. 'total' => $this->totalDestinatarios,
  162. 'exitosos' => $this->enviosExitosos,
  163. 'fallidos' => $this->enviosFallidos,
  164. 'porcentajeExito' => $this->totalDestinatarios > 0 ? round(($exitosos / $this->totalDestinatarios) * 100, 2) : 0
  165. ];
  166. }
  167. /**
  168. * Obtiene los destinatarios exitosos
  169. */
  170. public function getDestinatariosExitosos(): array
  171. {
  172. return array_filter($this->destinatariosInfo, function($destinatario) {
  173. return isset($destinatario['envioExitoso']) && $destinatario['envioExitoso'];
  174. });
  175. }
  176. /**
  177. * Obtiene los destinatarios fallidos
  178. */
  179. public function getDestinatariosFallidos(): array
  180. {
  181. return array_filter($this->destinatariosInfo, function($destinatario) {
  182. return !isset($destinatario['envioExitoso']) || !$destinatario['envioExitoso'];
  183. });
  184. }
  185. }