<?php
namespace App\Entity\Promotions;
use App\Annotation\SiteAware;
use App\Entity\AbstractBase;
use App\Entity\Garages\Garage;
use App\Entity\Interfaces\SiteInterface;
use App\Entity\Traits\GarageTrait;
use App\Entity\Traits\NameTrait;
use App\Entity\Traits\PublishedTrait;
use App\Entity\Traits\SiteTrait;
use App\Enum\LocalPromotionTypeEnum;
use App\Enum\SiteEnum;
use App\Repository\Promotions\LocalPromotionRepository;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
/**
* @ORM\Table(name="vulco_local_promotion_v2", indexes={@ORM\Index(name="local_promotion_v2_site_idx", columns={"site"})})
*
* @ORM\Entity(repositoryClass=LocalPromotionRepository::class)
*
* @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
*
* @SiteAware(siteFieldName="site")
*/
class LocalPromotion extends AbstractBase implements SiteInterface
{
use GarageTrait;
use NameTrait;
use SiteTrait;
use PublishedTrait;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private string $name;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Garages\Garage", inversedBy="localPromotions")
*
* @ORM\JoinColumn(name="garage_id", referencedColumnName="id")
*/
private Garage $garage;
/**
* @ORM\Column(type="integer", nullable=false, options={"default": 0})
*/
private int $type = 0;
/**
* @ORM\Column(type="datetime")
*/
private \DateTimeInterface $begin;
/**
* @ORM\Column(type="datetime")
*/
private \DateTimeInterface $end;
/**
* @ORM\Column(type="boolean", nullable=false, options={"default": 0})
*/
private bool $featured = false;
/**
* @ORM\Column(type="boolean", nullable=false, options={"default": 0})
*/
private bool $approved = false;
/**
* @ORM\Column(type="boolean", nullable=false, options={"default": 0})
*/
private bool $approvalPending = false;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $url = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $text1 = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $text2 = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $text3 = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $text4 = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $text5 = null;
/**
* @ORM\Column(type="boolean", nullable=false, options={"default": 1})
*/
private bool $showConditions = true;
public function __construct()
{
}
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addConstraint(new Callback('validate'));
}
public function validate(ExecutionContextInterface $context): void
{
if ($this->getBegin()->getTimestamp() > $this->getEnd()->getTimestamp()) {
$context->buildViolation(SiteEnum::SITE_STR_ES === $this->getSite() ? 'La fecha inicial tiene que ser anterior a la fecha final.' : 'A data inicial deve ser antes da data final.')
->atPath('begin')
->addViolation()
;
}
}
public function getType(): int
{
return $this->type;
}
public function getTypeAsTranlationKeyString(): string
{
return LocalPromotionTypeEnum::getTranslatedEnumArray()[$this->getType()];
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
public function getBegin(): \DateTimeInterface
{
return $this->begin;
}
public function setBegin(\DateTimeInterface $begin): self
{
$this->begin = $begin;
return $this;
}
public function getEnd(): \DateTimeInterface
{
return $this->end;
}
public function setEnd(\DateTimeInterface $end): self
{
$this->end = $end;
return $this;
}
public function isFeatured(): bool
{
return $this->featured;
}
public function setFeatured(bool $featured): self
{
$this->featured = $featured;
return $this;
}
public function isApproved(): bool
{
return $this->approved;
}
public function setApproved(bool $approved): self
{
$this->approved = $approved;
return $this;
}
public function isApprovalPending(): bool
{
return $this->approvalPending;
}
public function setApprovalPending(bool $approvalPending): self
{
$this->approvalPending = $approvalPending;
return $this;
}
public function getUrl(): ?string
{
return $this->url;
}
public function setUrl(?string $url): self
{
$this->url = $url;
return $this;
}
public function getText1(): ?string
{
return $this->text1;
}
public function setText1(?string $text1): self
{
$this->text1 = $text1;
return $this;
}
public function getText2(): ?string
{
return $this->text2;
}
public function setText2(?string $text2): self
{
$this->text2 = $text2;
return $this;
}
public function getText3(): ?string
{
return $this->text3;
}
public function setText3(?string $text3): self
{
$this->text3 = $text3;
return $this;
}
public function getText4(): ?string
{
return $this->text4;
}
public function setText4(?string $text4): self
{
$this->text4 = $text4;
return $this;
}
public function getText5(): ?string
{
return $this->text5;
}
public function setText5(?string $text5): self
{
$this->text5 = $text5;
return $this;
}
public function isShowConditions(): bool
{
return $this->showConditions;
}
public function setShowConditions(bool $showConditions): self
{
$this->showConditions = $showConditions;
return $this;
}
public function isActive(): bool
{
$now = new \DateTime();
$nowStr = $now->format('d-m-Y');
if ($this->getBegin()) {
$beginStr = $this->getBegin()->format('d-m-Y');
} else {
return false;
}
if ($this->getEnd()) {
$endStr = $this->getEnd()->format('d-m-Y');
} else {
return false;
}
return strtotime($beginStr) <= strtotime($nowStr) && strtotime($nowStr) <= strtotime($endStr) && $this->isApproved() && $this->isPublished();
}
public function hasToBeValidated(): bool
{
return !$this->isApproved() && !$this->isApprovalPending();
}
}