src/Entity/OnlineShop/ProductSpecialPrice.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity\OnlineShop;
  3. use App\Entity\AbstractBase;
  4. use App\Entity\MiniAbstractBase;
  5. use App\Repository\OnlineShop\ProductSpecialPriceRepository;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Gedmo\Mapping\Annotation as Gedmo;
  8. use Money\Money;
  9. use Symfony\Component\Validator\Constraints\Callback;
  10. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  11. use Symfony\Component\Validator\Mapping\ClassMetadata;
  12. /**
  13.  * @ORM\Table(name="vulco_product_special_price")
  14.  *
  15.  * @ORM\Entity(repositoryClass=ProductSpecialPriceRepository::class)
  16.  *
  17.  * @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
  18.  */
  19. class ProductSpecialPrice extends AbstractBase
  20. {
  21.     /**
  22.      * @ORM\ManyToOne(targetEntity="App\Entity\OnlineShop\Product", inversedBy="productSpecialPrices")
  23.      *
  24.      * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
  25.      */
  26.     private Product $product;
  27.     /**
  28.      * @ORM\Column(type="money")
  29.      */
  30.     private Money $price;
  31.     /**
  32.      * @ORM\Column(type="money")
  33.      */
  34.     private Money $costPrice;
  35.     /**
  36.      * @ORM\Column(type="datetime")
  37.      */
  38.     private \DateTimeInterface $begin;
  39.     /**
  40.      * @ORM\Column(type="datetime")
  41.      */
  42.     private \DateTimeInterface $end;
  43.     public function getProduct(): Product
  44.     {
  45.         return $this->product;
  46.     }
  47.     public function setProduct(Product $product): self
  48.     {
  49.         $this->product $product;
  50.         return $this;
  51.     }
  52.     public function getPrice(): Money
  53.     {
  54.         return $this->price;
  55.     }
  56.     public function setPrice(Money $price): self
  57.     {
  58.         $this->price $price;
  59.         return $this;
  60.     }
  61.     public function getCostPrice(): Money
  62.     {
  63.         return $this->costPrice;
  64.     }
  65.     public function setCostPrice(Money $costPrice): self
  66.     {
  67.         $this->costPrice $costPrice;
  68.         return $this;
  69.     }
  70.     public function getBegin(): \DateTimeInterface
  71.     {
  72.         return $this->begin;
  73.     }
  74.     public function getBeginAsDateString(): string
  75.     {
  76.         return $this->getDateAsString($this->getBegin());
  77.     }
  78.     public function setBegin(\DateTimeInterface $begin): self
  79.     {
  80.         $this->begin $begin;
  81.         return $this;
  82.     }
  83.     public function getEnd(): \DateTimeInterface
  84.     {
  85.         return $this->end;
  86.     }
  87.     public function getEndAsDateString(): string
  88.     {
  89.         return $this->getDateAsString($this->getEnd());
  90.     }
  91.     public function setEnd(\DateTimeInterface $end): self
  92.     {
  93.         $this->end $end;
  94.         return $this;
  95.     }
  96.     public static function loadValidatorMetadata(ClassMetadata $metadata): void
  97.     {
  98.         $metadata->addConstraint(new Callback('validate'));
  99.     }
  100.     public function validate(ExecutionContextInterface $context): void
  101.     {
  102.         if ($this->getBegin()->getTimestamp() > $this->getEnd()->getTimestamp()) {
  103.             $context->buildViolation('La fecha inicial tiene que ser anterior a la fecha final.')
  104.                 ->atPath('begin')
  105.                 ->addViolation()
  106.             ;
  107.         }
  108.     }
  109.     public function isActive(): bool
  110.     {
  111.         $result false;
  112.         if ($this->getBegin() && $this->getEnd()) {
  113.             $now = (new \DateTimeImmutable())->format(MiniAbstractBase::DEFAULT_DATE_PRETTY_FORMAT);
  114.             $result strtotime($this->getBeginAsDateString()) <= strtotime($now) && strtotime($now) <= strtotime($this->getEndAsDateString());
  115.         }
  116.         return $result;
  117.     }
  118. }