<?php
namespace App\Entity\Stat;
use App\Entity\HearingBrand;
use App\Entity\Store;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity(repositoryClass="App\Repository\Stat\ReportRepository")
* @ORM\Table(name="stat_report")
* @ORM\HasLifecycleCallbacks()
*/
class Report
{
const TYPE_MONTHLY = 'monthly';
const TYPE_YEARLY = 'yearly';
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=190, unique=true)
*/
private $token;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="update")
*/
private $generatedDate;
/**
* @ORM\Column(type="string", length=25)
*/
private $type;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Store")
*/
private $store;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\HearingBrand", inversedBy="reports")
*/
private $brand;
/**
* @ORM\Column(type="date")
*/
private $startDate;
/**
* @ORM\Column(type="date")
*/
private $endDate;
/**
* @ORM\Column(type="integer")
*/
private $nbDisplay;
/**
* @ORM\Column(type="integer")
*/
private $nbPhoneDisplay;
/**
* @ORM\Column(type="integer")
*/
private $nbCheckup;
/**
* @ORM\Column(type="integer")
*/
private $nbAppointement;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Stat\Report", inversedBy="nextReport")
*/
private $previousReport;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Stat\Report", mappedBy="previousReport")
*/
private $nextReport;
public static function getAvailableTypes(){
return [
self::TYPE_MONTHLY=>self::TYPE_MONTHLY,
self::TYPE_YEARLY=>self::TYPE_YEARLY,
];
}
/**
* Représente l'entité dans le BO
* @return string
*/
public function getLabel()
{
return 'Rapport de ' . $this->getStartDate()->format('m/y');
}
public function getId(): ?int
{
return $this->id;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getStore(): ?Store
{
return $this->store;
}
public function setStore(?Store $store): self
{
$this->store = $store;
return $this;
}
public function getStartDate(): ?\DateTimeInterface
{
return $this->startDate;
}
public function setStartDate(\DateTimeInterface $startDate): self
{
$this->startDate = $startDate;
return $this;
}
public function getEndDate(): ?\DateTimeInterface
{
return $this->endDate;
}
public function setEndDate(\DateTimeInterface $endDate): self
{
$this->endDate = $endDate;
return $this;
}
public function getNbDisplay(): ?int
{
return $this->nbDisplay;
}
public function setNbDisplay(int $nbDisplay): self
{
$this->nbDisplay = $nbDisplay;
return $this;
}
public function getNbPhoneDisplay(): ?int
{
return $this->nbPhoneDisplay;
}
public function setNbPhoneDisplay(int $nbPhoneDisplay): self
{
$this->nbPhoneDisplay = $nbPhoneDisplay;
return $this;
}
public function getNbCheckup(): ?int
{
return $this->nbCheckup;
}
public function setNbCheckup(int $nbCheckup): self
{
$this->nbCheckup = $nbCheckup;
return $this;
}
public function getNbAppointement(): ?int
{
return $this->nbAppointement;
}
public function setNbAppointement(int $nbAppointement): self
{
$this->nbAppointement = $nbAppointement;
return $this;
}
/**
* @return mixed
*/
public function getGeneratedDate()
{
return $this->generatedDate;
}
/**
* @param mixed $generatedDate
*/
public function setGeneratedDate($generatedDate): void
{
$this->generatedDate = $generatedDate;
}
/**
* @return mixed
*/
public function getBrand()
{
return $this->brand;
}
/**
* @param mixed $brand
*/
public function setBrand($brand): void
{
$this->brand = $brand;
}
public function hasExpired()
{
$now = new DateTime();
//if report has been generated afetr its enddate, not expired
if ($this->getEndDate() > $this->getGeneratedDate()) {
//if generated less than an hour ago, still valid
$diff = $now->diff($this->getGeneratedDate());
$hours = $diff->h;
$hours = $hours + ($diff->days * 24);
if ($hours >= 1) {
return true;
}
}
return false;
}
/**
* @return mixed
*/
public function getToken()
{
return $this->token;
}
/**
* @ORM\PrePersist
*/
public function initToken(): void
{
$token = time() . '-' . rand(1, 1000);
if ($this->getBrand()) {
$token .= 'b' . $this->getBrand()->getId();
} elseif ($this->getStore()) {
$token .= 's' . $this->getStore()->getId();
}
$this->token = md5($token);
}
/**
* @return mixed
*/
public function getPreviousReport()
{
return $this->previousReport;
}
/**
* @param mixed $previousReport
*/
public function setPreviousReport($previousReport): void
{
$this->previousReport = $previousReport;
}
/**
* @return mixed
*/
public function getNextReport()
{
return $this->nextReport;
}
/**
* @param mixed $nextReport
*/
public function setNextReport($nextReport): void
{
$this->nextReport = $nextReport;
}
public function setToken(string $token): self
{
$this->token = $token;
return $this;
}
}