<?php
namespace MedBrief\MSR\Entity;
use DH\Auditor\Provider\Doctrine\Auditing\Annotation as Audit;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection as DoctrineCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use MedBrief\MSR\Repository\LicenceRenewalTermRepository;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=LicenceRenewalTermRepository::class)
*
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
*
* @Audit\Auditable
*
* @Audit\Security(view={"ROLE_ALLOWED_TO_AUDIT"})
*
* @UniqueEntity(fields={"account", "effectiveDate"}, message="A licence renewal term with this effective date already exists.")
*/
class LicenceRenewalTerm
{
// Licence renewal period constants in months (0 = no renewal)
public const LICENCE_RENEWAL_PERIODS = [0, 6, 12, 24, 36];
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*/
private ?int $id;
/**
* @ORM\ManyToOne(targetEntity=Account::class, inversedBy="licenceRenewalTerms")
*
* @ORM\JoinColumn(nullable=false)
*/
private ?Account $account;
/**
* @ORM\Column(type="integer")
*
* @Assert\NotBlank(message="Please make a selection.")
*/
private ?int $period;
/**
* Note that the effective date is the date the licence renewal term interval starts from.
* This is distinguished from the matter's inclusion date (lastRenewalDate or createdAt).
* Hence the due date is calculated as the matter inclusion date + licence renewal term period
* and not effective date + licence renewal term period.
*
* @ORM\Column(type="datetime")
*
* @Assert\NotBlank(message="Please make a selection.")
*/
private ?\DateTime $effectiveDate;
/**
* @ORM\Column(type="datetime")
*
* @Gedmo\Timestampable(on="create")
*/
private ?\DateTime $created;
/**
* @ORM\Column(type="datetime", nullable=true)
*
* @Gedmo\Timestampable(on="update")
*/
private ?\DateTime $updated;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private ?\DateTime $deletedAt;
/**
* @ORM\ManyToOne(targetEntity=User::class)
*/
private ?User $deletedBy;
/**
* @ORM\OneToMany(targetEntity=MatterLicenceRenewalLog::class, mappedBy="licenceRenewalTerm")
*/
private ?DoctrineCollection $matterLicenceRenewalLogs;
public function __construct()
{
$this->matterLicenceRenewalLogs = new ArrayCollection();
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return Account|null
*/
public function getAccount(): ?Account
{
return $this->account;
}
/**
* @param Account|null $account
*
* @return self
*/
public function setAccount(?Account $account): self
{
$this->account = $account;
return $this;
}
/**
* @return int|null
*/
public function getPeriod(): ?int
{
return $this->period;
}
/**
* @param int|null $period
*
* @return self
*/
public function setPeriod(?int $period): self
{
$this->period = $period;
return $this;
}
/**
* Returns the date the licence renewal term interval starts from.
* Note this is not the matter's starting inclusion date which is (lastRenewalDate or createdAt).
*
* @return \DateTimeInterface|null
*/
public function getEffectiveDate(): ?\DateTimeInterface
{
return $this->effectiveDate;
}
/**
* Set the date the licence renewal term interval starts from.
* Note this is not the matter's starting inclusion date which is (lastRenewalDate or createdAt).
*
* @param \DateTimeInterface|null $effectiveDate
*
* @return self
*/
public function setEffectiveDate(?\DateTimeInterface $effectiveDate): self
{
$this->effectiveDate = $effectiveDate;
return $this;
}
/**
* @return \DateTimeInterface|null
*/
public function getCreated(): ?\DateTimeInterface
{
return $this->created;
}
/**
* @param \DateTimeInterface|null $created
*
* @return self
*/
public function setCreated(?\DateTimeInterface $created): self
{
$this->created = $created;
return $this;
}
/**
* @return \DateTimeInterface|null
*/
public function getUpdated(): ?\DateTimeInterface
{
return $this->updated;
}
/**
* @param \DateTimeInterface|null $updated
*
* @return self
*/
public function setUpdated(?\DateTimeInterface $updated): self
{
$this->updated = $updated;
return $this;
}
/**
* @return \DateTimeInterface|null
*/
public function getDeletedAt(): ?\DateTimeInterface
{
return $this->deletedAt;
}
/**
* @param \DateTimeInterface|null $deletedAt
*
* @return self
*/
public function setDeletedAt(?\DateTimeInterface $deletedAt): self
{
$this->deletedAt = $deletedAt;
return $this;
}
/**
* @return User|null
*/
public function getDeletedBy(): ?User
{
return $this->deletedBy;
}
/**
* @param User|null $deletedBy
*
* @return self
*/
public function setDeletedBy(?User $deletedBy): self
{
$this->deletedBy = $deletedBy;
return $this;
}
/**
* Function to return user friendly licence renewal period options for form usage.
*
* @return array
*/
public static function getLicenceRenewalPeriodOptions(): array
{
$licenceRenewalPeriods = [];
foreach (self::LICENCE_RENEWAL_PERIODS as $period) {
$licenceRenewalPeriods[$period] = $period === 0 ? 'No renewal' : $period . ' months';
}
return array_flip($licenceRenewalPeriods);
}
/**
* Get the current term's effective end date.
* The effective end date can be calculated by subtracting a day from the next term's effective date.
*
* @return \DateTime|null
*/
public function getEffectiveEndDate(): ?\DateTime
{
$allRenewalTerms = $this->getAccount()->getLicenceRenewalTerms();
$currentIndex = $allRenewalTerms->indexOf($this);
$nextRenewalTerm = $allRenewalTerms->get($currentIndex + 1);
// If set return the effectiveDate of the next renewal term
// subtract 1 day to get the end date of the current renewal term
if ($nextRenewalTerm !== null) {
return (clone $nextRenewalTerm->getEffectiveDate())->modify('-1 day');
}
return null;
}
/**
* @return DoctrineCollection<int, MatterLicenceRenewalLog>
*/
public function getMatterLicenceRenewalLogs(): DoctrineCollection
{
return $this->matterLicenceRenewalLogs;
}
/**
* @param MatterLicenceRenewalLog $matterLicenceRenewalLog
*
* @return self
*/
public function addMatterLicenceRenewalLog(MatterLicenceRenewalLog $matterLicenceRenewalLog): self
{
if (!$this->matterLicenceRenewalLogs->contains($matterLicenceRenewalLog)) {
$this->matterLicenceRenewalLogs[] = $matterLicenceRenewalLog;
$matterLicenceRenewalLog->setLicenceRenewalTerm($this);
}
return $this;
}
/**
* @param MatterLicenceRenewalLog $matterLicenceRenewalLog
*
* @return self
*/
public function removeMatterLicenceRenewalLog(MatterLicenceRenewalLog $matterLicenceRenewalLog): self
{
if ($this->matterLicenceRenewalLogs->removeElement($matterLicenceRenewalLog)) {
// set the owning side to null (unless already changed)
if ($matterLicenceRenewalLog->getLicenceRenewalTerm() === $this) {
$matterLicenceRenewalLog->setLicenceRenewalTerm(null);
}
}
return $this;
}
}