<?php
namespace App\Entity\OnlineShop;
use App\Entity\AbstractBase;
use App\Entity\MiniAbstractBase;
use App\Repository\OnlineShop\ProductSpecialPriceRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Money\Money;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
/**
* @ORM\Table(name="vulco_product_special_price")
*
* @ORM\Entity(repositoryClass=ProductSpecialPriceRepository::class)
*
* @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
*/
class ProductSpecialPrice extends AbstractBase
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\OnlineShop\Product", inversedBy="productSpecialPrices")
*
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private Product $product;
/**
* @ORM\Column(type="money")
*/
private Money $price;
/**
* @ORM\Column(type="money")
*/
private Money $costPrice;
/**
* @ORM\Column(type="datetime")
*/
private \DateTimeInterface $begin;
/**
* @ORM\Column(type="datetime")
*/
private \DateTimeInterface $end;
public function getProduct(): Product
{
return $this->product;
}
public function setProduct(Product $product): self
{
$this->product = $product;
return $this;
}
public function getPrice(): Money
{
return $this->price;
}
public function setPrice(Money $price): self
{
$this->price = $price;
return $this;
}
public function getCostPrice(): Money
{
return $this->costPrice;
}
public function setCostPrice(Money $costPrice): self
{
$this->costPrice = $costPrice;
return $this;
}
public function getBegin(): \DateTimeInterface
{
return $this->begin;
}
public function getBeginAsDateString(): string
{
return $this->getDateAsString($this->getBegin());
}
public function setBegin(\DateTimeInterface $begin): self
{
$this->begin = $begin;
return $this;
}
public function getEnd(): \DateTimeInterface
{
return $this->end;
}
public function getEndAsDateString(): string
{
return $this->getDateAsString($this->getEnd());
}
public function setEnd(\DateTimeInterface $end): self
{
$this->end = $end;
return $this;
}
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('La fecha inicial tiene que ser anterior a la fecha final.')
->atPath('begin')
->addViolation()
;
}
}
public function isActive(): bool
{
$result = false;
if ($this->getBegin() && $this->getEnd()) {
$now = (new \DateTimeImmutable())->format(MiniAbstractBase::DEFAULT_DATE_PRETTY_FORMAT);
$result = strtotime($this->getBeginAsDateString()) <= strtotime($now) && strtotime($now) <= strtotime($this->getEndAsDateString());
}
return $result;
}
}