<?php
namespace App\Entity\System;
use App\Repository\System\NotificationRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=NotificationRepository::class)
*
* @ORM\Table(name="notification")
*/
class Notification
{
public const ID_WALLET_LOW_BALANCE = 1;
public const ID_PACK_EXPIRED = 2;
public const ID_PACK_ABOUT_TO_EXPIRE = 3;
public const ID_SHOP_ENTER_DATA = 4;
public const ID_SHOP_WRONG_DATA = 5;
public const ID_RETURN_PENDING_ADDRESS = 6;
public const ID_RETURN_PENDING_REVISION = 7;
public const ID_ORDER_PENDING_INVOICE = 8;
public const ID_ORDER_PENDING_BANKWIRE = 9;
public const ID_RETURN_NEW_COMMENT = 10;
public const ID_RETURN_DOWNLOAD_TAG = 11;
public const ID_ADD_PAYMENT_METHOD = 12;
public const ID_SHOP_FINISHED = 13;
public const ID_SUSPENDED_SHOP = 14;
public const ID_RETURN_PAID = 15;
public const ID_RETURN_APPROVED_FOR_PAYMENT = 16;
/**
* @var int
*
* @ORM\Id
*
* @ORM\GeneratedValue(strategy="AUTO")
*
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var \DateTime|null
*
* @ORM\Column(type="datetime", nullable=true)
*/
private $expirationDate;
/**
* @var \DateTime
*
* @ORM\Column(type="datetime", nullable=false)
*/
private $creationDate;
/**
* @var Employee
*
* @ORM\ManyToOne(targetEntity="App\Entity\System\Employee")
*
* @ORM\JoinColumn(name="creation_employee_id", referencedColumnName="id_employee", nullable=false)
*/
private $creationEmployee;
/**
* @var \DateTime|null
*
* @ORM\Column(type="datetime", nullable=true)
*/
private $notificationDate;
/**
* @var Employee|null
*
* @ORM\ManyToOne(targetEntity="App\Entity\System\Employee")
*
* @ORM\JoinColumn(name="notification_employee_id", referencedColumnName="id_employee")
*/
private $notificationEmployee;
/**
* @var Employee|null
*
* @ORM\ManyToOne(targetEntity="App\Entity\System\Employee")
*
* @ORM\JoinColumn(name="deactivation_employee_id", referencedColumnName="id_employee")
*/
private $deactivationEmployee;
/**
* @var \DateTime|null
*
* @ORM\Column(type="datetime", nullable=true)
*/
private $deactivationDate;
/**
* @var NotificationType
*
* @ORM\ManyToOne(targetEntity="NotificationType")
*
* @ORM\JoinColumn(name="type_id", referencedColumnName="id", nullable=false)
*/
private $type;
/**
* @var NotificationProduct
*
* @ORM\ManyToOne(targetEntity="NotificationProduct")
*
* @ORM\JoinColumn(name="notification_product_id", referencedColumnName="id", nullable=false)
*/
private $notificationProduct;
/**
* @var NotificationSection|null
*
* @ORM\ManyToOne(targetEntity="NotificationSection")
*
* @ORM\JoinColumn(name="section_id", referencedColumnName="id", nullable=true)
*/
private $section;
/**
* @var bool
*
* @ORM\Column(type="boolean")
*/
private $active;
/**
* @var NotificationLang[]|Collection
*
* @ORM\OneToMany(targetEntity="NotificationLang", mappedBy="notification", cascade={"persist"})
*/
private $translations;
/**
* @var NotificationHistory[]|Collection
*
* @ORM\OneToMany(targetEntity="NotificationHistory", mappedBy="notification", cascade={"persist"})
*/
private $history;
/**
* @param Employee $employee
* @param \DateTime $creationDate
* @param \DateTime|null $expirationDate
* @param NotificationType $type
* @param NotificationProduct $product
*
* @return self
*/
public static function createFromValues(Employee $employee, \DateTime $creationDate, ?\DateTime $expirationDate, NotificationType $type, NotificationProduct $product, ?NotificationSection $section): self
{
$notification = new self();
$notification->creationEmployee = $employee;
$notification->creationDate = $creationDate;
$notification->active = true;
$notification->expirationDate = $expirationDate;
$notification->type = $type;
$notification->notificationProduct = $product;
$notification->section = $section;
$notification->translations = new ArrayCollection();
$notification->history = new ArrayCollection();
return $notification;
}
public function notify(Employee $employee, \DateTime $currentDateTime): bool
{
if (null === $this->notificationDate) {
$this->notificationDate = $currentDateTime;
$this->notificationEmployee = $employee;
return true;
}
return false;
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @return \DateTime|null
*/
public function getExpirationDate(): ?\DateTime
{
return $this->expirationDate;
}
/**
* @return \DateTime
*/
public function getCreationDate(): \DateTime
{
return $this->creationDate;
}
/**
* @return Employee
*/
public function getCreationEmployee(): Employee
{
return $this->creationEmployee;
}
/**
* @return \DateTime|null
*/
public function getNotificationDate(): ?\DateTime
{
return $this->notificationDate;
}
/**
* @return Employee|null
*/
public function getNotificationEmployee(): ?Employee
{
return $this->notificationEmployee;
}
/**
* @return Employee|null
*/
public function getDeactivationEmployee(): ?Employee
{
return $this->deactivationEmployee;
}
/**
* @return \DateTime|null
*/
public function getDeactivationDate(): ?\DateTime
{
return $this->deactivationDate;
}
/**
* @return NotificationType
*/
public function getType(): NotificationType
{
return $this->type;
}
/**
* @return NotificationProduct
*/
public function getNotificationProduct(): NotificationProduct
{
return $this->notificationProduct;
}
/**
* @return NotificationSection|null
*/
public function getSection(): ?NotificationSection
{
return $this->section;
}
/**
* @return bool
*/
public function isActive(): bool
{
return $this->active;
}
/**
* @return NotificationLang[]|Collection
*/
public function getTranslations()
{
return $this->translations;
}
/**
* @param \DateTime $deactivationDate
* @param Employee $employee
*
* @return void
*/
public function deactivate(\DateTime $deactivationDate, Employee $employee): void
{
$this->deactivationDate = $deactivationDate;
$this->deactivationEmployee = $employee;
$this->active = false;
}
public function addTranslation(NotificationLang $notificationLang): void
{
$this->translations->add($notificationLang);
}
/**
* @param \DateTime|null $expirationDate
*/
public function setExpirationDate(?\DateTime $expirationDate): void
{
$this->expirationDate = $expirationDate;
}
/**
* @param Language $language
* @param Employee $employee
* @param \DateTime $currentDateTime
* @param string|null $title
* @param string|null $description
* @param string|null $link
* @param string|null $linkText
*
* @return void
*/
public function updateTranslation(Language $language, Employee $employee, \DateTime $currentDateTime, ?string $title, ?string $description, ?string $link, ?string $linkText): void
{
/** @var NotificationLang $notificationLang */
$notificationLang = $this->translations->filter(function (NotificationLang $notificationLang) use ($language) {
return $notificationLang->getLanguage()->getId() === $language->getId();
})->first();
if ($notificationLang) {
$this->addHistoryIfChangesDetected($notificationLang, $employee, $currentDateTime, $title, $description);
$notificationLang->updateFromValues($title, $description, $link, $linkText);
}
}
private function addHistoryIfChangesDetected(NotificationLang $notificationLang, Employee $employee, \DateTime $currentDateTime, ?string $title, ?string $description): void
{
$historyItem = NotificationHistory::createFromNotificationFormTranslation(
$this,
$notificationLang,
$employee,
$currentDateTime,
$title,
$description
);
if ($historyItem) {
$this->history->add($historyItem);
}
}
}