src/Entity/PointsCatalog/CatalogOrderGift.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Entity\PointsCatalog;
  3. use App\Annotation\SiteAware;
  4. use App\Entity\AbstractBase;
  5. use App\Entity\Interfaces\SiteInterface;
  6. use App\Entity\MiniAbstractBase;
  7. use App\Entity\Traits\SiteTrait;
  8. use App\Entity\User;
  9. use App\Enum\CatalogOrderStatusEnum;
  10. use App\Exception\CatalogOrderLineCannotBeCancelled;
  11. use App\Exception\PointsLimitException;
  12. use App\Repository\PointsCatalog\CatalogOrderGiftRepository;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Gedmo\Mapping\Annotation as Gedmo;
  15. use Money\Currency;
  16. use Money\Money;
  17. /**
  18.  * @ORM\Table(name="vulco_points_catalog_order_gift", indexes={@ORM\Index(name="catalog_order_gift_site_idx", columns={"site"})})
  19.  * @ORM\Entity(repositoryClass=CatalogOrderGiftRepository::class)
  20.  * @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
  21.  * @SiteAware(siteFieldName="site")
  22.  */
  23. class CatalogOrderGift extends AbstractBase implements SiteInterface
  24. {
  25.     use SiteTrait;
  26.     /**
  27.      * @ORM\ManyToOne(targetEntity="App\Entity\PointsCatalog\CatalogOrder", inversedBy="gifts")
  28.      * @ORM\JoinColumn(name="order_id", referencedColumnName="id")
  29.      */
  30.     private CatalogOrder $catalogOrder;
  31.     /**
  32.      * @ORM\ManyToOne(targetEntity="App\Entity\PointsCatalog\CatalogGift")
  33.      * @ORM\JoinColumn(name="gift_id", referencedColumnName="id")
  34.      */
  35.     private ?CatalogGift $gift;
  36.     /**
  37.      * @ORM\ManyToOne(targetEntity="App\Entity\PointsCatalog\CatalogGiftProvider")
  38.      * @ORM\JoinColumn(name="provider_id", referencedColumnName="id")
  39.      */
  40.     private ?CatalogGiftProvider $provider;
  41.     /**
  42.      * @ORM\ManyToOne(targetEntity="App\Entity\PointsCatalog\CatalogSize")
  43.      * @ORM\JoinColumn(name="size_id", referencedColumnName="id")
  44.      */
  45.     private ?CatalogSize $size;
  46.     /**
  47.      * @ORM\Column(type="integer")
  48.      */
  49.     private int $unitPointsValue;
  50.     /**
  51.      * @ORM\Column(type="integer")
  52.      */
  53.     private int $units;
  54.     /**
  55.      * @ORM\Column(type="money", nullable=true)
  56.      */
  57.     private ?Money $unitPrice;
  58.     /**
  59.      * @ORM\Column(type="integer")
  60.      */
  61.     private int $state;
  62.     /**
  63.      * @ORM\Column(type="datetime", nullable=true)
  64.      */
  65.     protected ?\DateTimeInterface $processedAt null;
  66.     /**
  67.      * @ORM\Column(type="datetime", nullable=true)
  68.      */
  69.     protected ?\DateTimeInterface $cancelledAt null;
  70.     public function __construct(CatalogOrder $orderint $unitsint $unitPointsValueCatalogGift $gift nullCatalogSize $size nullCatalogGiftProvider $provider null$unitPrice null$state CatalogOrderStatusEnum::PENDING)
  71.     {
  72.         $this->catalogOrder $order;
  73.         $this->gift $gift;
  74.         $this->unitPointsValue $unitPointsValue;
  75.         $this->units $units;
  76.         $this->unitPrice $unitPrice;
  77.         $this->size $size;
  78.         $this->provider $provider;
  79.         $this->state $state;
  80.     }
  81.     public static function makeCatalogGiftOrderLine(CatalogOrder $orderCatalogGift $giftint $unitsCatalogSize $size null): self
  82.     {
  83.         return new self($order$units$gift->getPointsValue(), $gift$size);
  84.     }
  85.     public static function makeDeliveryOrderLine(CatalogOrder $orderCatalogGiftProvider $provider): self
  86.     {
  87.         return new self($order1$provider->getDeliveryManagementPoints(), nullnull$provider);
  88.     }
  89.     public function name(): string
  90.     {
  91.         $result '';
  92.         if ($this->getGift()) {
  93.             $result $this->getGift()->getName();
  94.             if ($this->getSize()) {
  95.                 $result .= sprintf(' (%s)'$this->getSize()->getName());
  96.             }
  97.         } elseif (isset($this->provider)) {
  98.             $result sprintf('Portes %s'$this->getProvider()->getName());
  99.         }
  100.         return $result;
  101.     }
  102.     public function getName(): string
  103.     {
  104.         return $this->name();
  105.     }
  106.     public function size(): ?CatalogSize
  107.     {
  108.         return $this->size;
  109.     }
  110.     public function getSize(): ?CatalogSize
  111.     {
  112.         return $this->size();
  113.     }
  114.     public function setSize(?CatalogSize $size): self
  115.     {
  116.         $this->size $size;
  117.         return $this;
  118.     }
  119.     public function units(): int
  120.     {
  121.         return $this->units;
  122.     }
  123.     public function getUnits(): int
  124.     {
  125.         return $this->units();
  126.     }
  127.     public function setUnits(int $units): self
  128.     {
  129.         $this->units $units;
  130.         return $this;
  131.     }
  132.     public function unitPointsValue(): int
  133.     {
  134.         return $this->unitPointsValue;
  135.     }
  136.     public function getUnitPointsValue(): int
  137.     {
  138.         return $this->unitPointsValue();
  139.     }
  140.     public function setUnitPointsValue(int $unitPointsValue): self
  141.     {
  142.         $this->unitPointsValue $unitPointsValue;
  143.         return $this;
  144.     }
  145.     public function unitPrice(): Money
  146.     {
  147.         $result = new Money(0, new Currency(MiniAbstractBase::DEFAULT_EURO_CURRENCY));
  148.         if (isset($this->unitPrice)) {
  149.             $result $result->add($this->unitPrice);
  150.         } elseif ($this->getGift()) {
  151.             $result $result->add($this->getGift()->price());
  152.         }
  153.         return $result;
  154.     }
  155.     public function fixUnitPrice(): self
  156.     {
  157.         if ($this->getGift()) {
  158.             $this->unitPrice $this->getGift()->price();
  159.         }
  160.         return $this;
  161.     }
  162.     public function getState(): int
  163.     {
  164.         return $this->state;
  165.     }
  166.     public function setState(int $state): self
  167.     {
  168.         $this->state $state;
  169.         return $this;
  170.     }
  171.     public function gift(): ?CatalogGift
  172.     {
  173.         return $this->gift;
  174.     }
  175.     public function getGift(): ?CatalogGift
  176.     {
  177.         return $this->gift();
  178.     }
  179.     public function setGift(?CatalogGift $gift): self
  180.     {
  181.         $this->gift $gift;
  182.         return $this;
  183.     }
  184.     public function provider(): ?CatalogGiftProvider
  185.     {
  186.         if ($this->provider) {
  187.             return $this->provider;
  188.         }
  189.         return $this->getGift() ? $this->getGift()->firstProvider() : null;
  190.     }
  191.     public function getProvider(): ?CatalogGiftProvider
  192.     {
  193.         return $this->provider();
  194.     }
  195.     public function setProvider(?CatalogGiftProvider $provider): self
  196.     {
  197.         $this->provider $provider;
  198.         return $this;
  199.     }
  200.     public function catalogOrder(): CatalogOrder
  201.     {
  202.         return $this->catalogOrder;
  203.     }
  204.     public function getCatalogOrder(): CatalogOrder
  205.     {
  206.         return $this->catalogOrder();
  207.     }
  208.     public function setCatalogOrder(CatalogOrder $catalogOrder): self
  209.     {
  210.         $this->catalogOrder $catalogOrder;
  211.         return $this;
  212.     }
  213.     public function totalPoints(): int
  214.     {
  215.         return $this->units() * $this->unitPointsValue();
  216.     }
  217.     public function totalPrice(): Money
  218.     {
  219.         return $this->unitPrice()->multiply($this->units());
  220.     }
  221.     public function isDeliveryOrderLine(): bool
  222.     {
  223.         return null !== $this->provider;
  224.     }
  225.     public function isPending(): bool
  226.     {
  227.         return CatalogOrderStatusEnum::PENDING === $this->state;
  228.     }
  229.     public function canBeProcessed(): bool
  230.     {
  231.         return !$this->isDeliveryOrderLine() && $this->isPending();
  232.     }
  233.     public function processedAt(): ?\DateTimeInterface
  234.     {
  235.         return $this->processedAt;
  236.     }
  237.     public function getProcessedAt(): ?\DateTimeInterface
  238.     {
  239.         return $this->processedAt();
  240.     }
  241.     public function setProcessedAt(?\DateTimeInterface $processedAt): self
  242.     {
  243.         $this->processedAt $processedAt;
  244.         return $this;
  245.     }
  246.     public function process(): void
  247.     {
  248.         if (!$this->isProcessed()) {
  249.             $this->fixUnitPrice();
  250.             $this->processedAt = new \DateTimeImmutable();
  251.             $this->state CatalogOrderStatusEnum::PROCESSED;
  252.         }
  253.     }
  254.     public function isProcessed(): bool
  255.     {
  256.         return CatalogOrderStatusEnum::PROCESSED === $this->state;
  257.     }
  258.     /**
  259.      * @throws CatalogOrderLineCannotBeCancelled|PointsLimitException
  260.      */
  261.     public function cancel(string $reasonToCancelDescription): CatalogPointMovement
  262.     {
  263.         if (!$this->canBeCancelled()) {
  264.             throw new CatalogOrderLineCannotBeCancelled();
  265.         }
  266.         $this->state CatalogOrderStatusEnum::CANCELLED;
  267.         $this->cancelledAt = new \DateTimeImmutable();
  268.         return $this->createReturnPointMovementWithDescriptionAndPoints($reasonToCancelDescription);
  269.     }
  270.     public function canBeCancelled(): bool
  271.     {
  272.         return CatalogOrderStatusEnum::PENDING === $this->state;
  273.     }
  274.     /**
  275.      * @throws PointsLimitException
  276.      */
  277.     public function createReturnPointMovementWithDescriptionAndPoints(string $description): CatalogPointMovement
  278.     {
  279.         $pointMovement = new CatalogPointMovement();
  280.         $pointMovement
  281.             ->setGarage($this->catalogOrder()->garage())
  282.             ->setDescription($description)
  283.             ->setDate($this->getCreatedAt())
  284.             ->setUser($this->catalogOrder()->garage()->getOwner())
  285.             ->setProvider($this->provider())
  286.             ->setReturnMovement(true)
  287.             ->setPoints($this->totalPoints(), true// en las devoluciones de puntos no hay que controlar el límite de puntos (por que son puntos que ya habia ganado antes)
  288.         ;
  289.         return $pointMovement;
  290.     }
  291.     public function isCancelled(): bool
  292.     {
  293.         return CatalogOrderStatusEnum::CANCELLED === $this->state;
  294.     }
  295.     public function cancelledAt(): ?\DateTimeInterface
  296.     {
  297.         return $this->cancelledAt;
  298.     }
  299.     public function getCancelledAt(): ?\DateTimeInterface
  300.     {
  301.         return $this->cancelledAt();
  302.     }
  303.     public function setCancelledAt(?\DateTimeInterface $cancelledAt): self
  304.     {
  305.         $this->cancelledAt $cancelledAt;
  306.         return $this;
  307.     }
  308.     public function owner(): User
  309.     {
  310.         return $this->catalogOrder()->owner();
  311.     }
  312.     public function __toString(): string
  313.     {
  314.         return 'ID'.$this->id.' · OID'.$this->catalogOrder->id.' · GID'.$this->gift->id.' · '.$this->units;
  315.     }
  316. }