src/Entity/LicenceRenewalTerm.php line 25

Open in your IDE?
  1. <?php
  2. namespace MedBrief\MSR\Entity;
  3. use DH\Auditor\Provider\Doctrine\Auditing\Annotation as Audit;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection as DoctrineCollection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Gedmo\Mapping\Annotation as Gedmo;
  8. use MedBrief\MSR\Repository\LicenceRenewalTermRepository;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. /**
  12. * @ORM\Entity(repositoryClass=LicenceRenewalTermRepository::class)
  13. *
  14. * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
  15. *
  16. * @Audit\Auditable
  17. *
  18. * @Audit\Security(view={"ROLE_ALLOWED_TO_AUDIT"})
  19. *
  20. * @UniqueEntity(fields={"account", "effectiveDate"}, message="A licence renewal term with this effective date already exists.")
  21. */
  22. class LicenceRenewalTerm
  23. {
  24. // Licence renewal period constants in months (0 = no renewal)
  25. public const LICENCE_RENEWAL_PERIODS = [0, 6, 12, 24, 36];
  26. /**
  27. * @ORM\Id
  28. *
  29. * @ORM\GeneratedValue
  30. *
  31. * @ORM\Column(type="integer")
  32. */
  33. private ?int $id;
  34. /**
  35. * @ORM\ManyToOne(targetEntity=Account::class, inversedBy="licenceRenewalTerms")
  36. *
  37. * @ORM\JoinColumn(nullable=false)
  38. */
  39. private ?Account $account;
  40. /**
  41. * @ORM\Column(type="integer")
  42. *
  43. * @Assert\NotBlank(message="Please make a selection.")
  44. */
  45. private ?int $period;
  46. /**
  47. * Note that the effective date is the date the licence renewal term interval starts from.
  48. * This is distinguished from the matter's inclusion date (lastRenewalDate or createdAt).
  49. * Hence the due date is calculated as the matter inclusion date + licence renewal term period
  50. * and not effective date + licence renewal term period.
  51. *
  52. * @ORM\Column(type="datetime")
  53. *
  54. * @Assert\NotBlank(message="Please make a selection.")
  55. */
  56. private ?\DateTime $effectiveDate;
  57. /**
  58. * @ORM\Column(type="datetime")
  59. *
  60. * @Gedmo\Timestampable(on="create")
  61. */
  62. private ?\DateTime $created;
  63. /**
  64. * @ORM\Column(type="datetime", nullable=true)
  65. *
  66. * @Gedmo\Timestampable(on="update")
  67. */
  68. private ?\DateTime $updated;
  69. /**
  70. * @ORM\Column(type="datetime", nullable=true)
  71. */
  72. private ?\DateTime $deletedAt;
  73. /**
  74. * @ORM\ManyToOne(targetEntity=User::class)
  75. */
  76. private ?User $deletedBy;
  77. /**
  78. * @ORM\OneToMany(targetEntity=MatterLicenceRenewalLog::class, mappedBy="licenceRenewalTerm")
  79. */
  80. private ?DoctrineCollection $matterLicenceRenewalLogs;
  81. public function __construct()
  82. {
  83. $this->matterLicenceRenewalLogs = new ArrayCollection();
  84. }
  85. /**
  86. * @return int|null
  87. */
  88. public function getId(): ?int
  89. {
  90. return $this->id;
  91. }
  92. /**
  93. * @return Account|null
  94. */
  95. public function getAccount(): ?Account
  96. {
  97. return $this->account;
  98. }
  99. /**
  100. * @param Account|null $account
  101. *
  102. * @return self
  103. */
  104. public function setAccount(?Account $account): self
  105. {
  106. $this->account = $account;
  107. return $this;
  108. }
  109. /**
  110. * @return int|null
  111. */
  112. public function getPeriod(): ?int
  113. {
  114. return $this->period;
  115. }
  116. /**
  117. * @param int|null $period
  118. *
  119. * @return self
  120. */
  121. public function setPeriod(?int $period): self
  122. {
  123. $this->period = $period;
  124. return $this;
  125. }
  126. /**
  127. * Returns the date the licence renewal term interval starts from.
  128. * Note this is not the matter's starting inclusion date which is (lastRenewalDate or createdAt).
  129. *
  130. * @return \DateTimeInterface|null
  131. */
  132. public function getEffectiveDate(): ?\DateTimeInterface
  133. {
  134. return $this->effectiveDate;
  135. }
  136. /**
  137. * Set the date the licence renewal term interval starts from.
  138. * Note this is not the matter's starting inclusion date which is (lastRenewalDate or createdAt).
  139. *
  140. * @param \DateTimeInterface|null $effectiveDate
  141. *
  142. * @return self
  143. */
  144. public function setEffectiveDate(?\DateTimeInterface $effectiveDate): self
  145. {
  146. $this->effectiveDate = $effectiveDate;
  147. return $this;
  148. }
  149. /**
  150. * @return \DateTimeInterface|null
  151. */
  152. public function getCreated(): ?\DateTimeInterface
  153. {
  154. return $this->created;
  155. }
  156. /**
  157. * @param \DateTimeInterface|null $created
  158. *
  159. * @return self
  160. */
  161. public function setCreated(?\DateTimeInterface $created): self
  162. {
  163. $this->created = $created;
  164. return $this;
  165. }
  166. /**
  167. * @return \DateTimeInterface|null
  168. */
  169. public function getUpdated(): ?\DateTimeInterface
  170. {
  171. return $this->updated;
  172. }
  173. /**
  174. * @param \DateTimeInterface|null $updated
  175. *
  176. * @return self
  177. */
  178. public function setUpdated(?\DateTimeInterface $updated): self
  179. {
  180. $this->updated = $updated;
  181. return $this;
  182. }
  183. /**
  184. * @return \DateTimeInterface|null
  185. */
  186. public function getDeletedAt(): ?\DateTimeInterface
  187. {
  188. return $this->deletedAt;
  189. }
  190. /**
  191. * @param \DateTimeInterface|null $deletedAt
  192. *
  193. * @return self
  194. */
  195. public function setDeletedAt(?\DateTimeInterface $deletedAt): self
  196. {
  197. $this->deletedAt = $deletedAt;
  198. return $this;
  199. }
  200. /**
  201. * @return User|null
  202. */
  203. public function getDeletedBy(): ?User
  204. {
  205. return $this->deletedBy;
  206. }
  207. /**
  208. * @param User|null $deletedBy
  209. *
  210. * @return self
  211. */
  212. public function setDeletedBy(?User $deletedBy): self
  213. {
  214. $this->deletedBy = $deletedBy;
  215. return $this;
  216. }
  217. /**
  218. * Function to return user friendly licence renewal period options for form usage.
  219. *
  220. * @return array
  221. */
  222. public static function getLicenceRenewalPeriodOptions(): array
  223. {
  224. $licenceRenewalPeriods = [];
  225. foreach (self::LICENCE_RENEWAL_PERIODS as $period) {
  226. $licenceRenewalPeriods[$period] = $period === 0 ? 'No renewal' : $period . ' months';
  227. }
  228. return array_flip($licenceRenewalPeriods);
  229. }
  230. /**
  231. * Get the current term's effective end date.
  232. * The effective end date can be calculated by subtracting a day from the next term's effective date.
  233. *
  234. * @return \DateTime|null
  235. */
  236. public function getEffectiveEndDate(): ?\DateTime
  237. {
  238. $allRenewalTerms = $this->getAccount()->getLicenceRenewalTerms();
  239. $currentIndex = $allRenewalTerms->indexOf($this);
  240. $nextRenewalTerm = $allRenewalTerms->get($currentIndex + 1);
  241. // If set return the effectiveDate of the next renewal term
  242. // subtract 1 day to get the end date of the current renewal term
  243. if ($nextRenewalTerm !== null) {
  244. return (clone $nextRenewalTerm->getEffectiveDate())->modify('-1 day');
  245. }
  246. return null;
  247. }
  248. /**
  249. * @return DoctrineCollection<int, MatterLicenceRenewalLog>
  250. */
  251. public function getMatterLicenceRenewalLogs(): DoctrineCollection
  252. {
  253. return $this->matterLicenceRenewalLogs;
  254. }
  255. /**
  256. * @param MatterLicenceRenewalLog $matterLicenceRenewalLog
  257. *
  258. * @return self
  259. */
  260. public function addMatterLicenceRenewalLog(MatterLicenceRenewalLog $matterLicenceRenewalLog): self
  261. {
  262. if (!$this->matterLicenceRenewalLogs->contains($matterLicenceRenewalLog)) {
  263. $this->matterLicenceRenewalLogs[] = $matterLicenceRenewalLog;
  264. $matterLicenceRenewalLog->setLicenceRenewalTerm($this);
  265. }
  266. return $this;
  267. }
  268. /**
  269. * @param MatterLicenceRenewalLog $matterLicenceRenewalLog
  270. *
  271. * @return self
  272. */
  273. public function removeMatterLicenceRenewalLog(MatterLicenceRenewalLog $matterLicenceRenewalLog): self
  274. {
  275. if ($this->matterLicenceRenewalLogs->removeElement($matterLicenceRenewalLog)) {
  276. // set the owning side to null (unless already changed)
  277. if ($matterLicenceRenewalLog->getLicenceRenewalTerm() === $this) {
  278. $matterLicenceRenewalLog->setLicenceRenewalTerm(null);
  279. }
  280. }
  281. return $this;
  282. }
  283. }