src/Entity/Cart/Cart.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Cart;
  3. use App\Entity\Garages\Garage;
  4. use App\Entity\PointsCatalog\CatalogGiftProvider;
  5. use App\Entity\Traits\GarageTrait;
  6. use App\Repository\Cart\CartRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Gedmo\Mapping\Annotation as Gedmo;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. /**
  13.  * @ORM\Table(name="cart")
  14.  *
  15.  * @ORM\Entity(repositoryClass=CartRepository::class)
  16.  */
  17. class Cart
  18. {
  19.     use GarageTrait;
  20.     /**
  21.      * @ORM\Column(name="id", type="integer")
  22.      *
  23.      * @ORM\Id
  24.      *
  25.      * @ORM\GeneratedValue(strategy="AUTO")
  26.      */
  27.     private int $id;
  28.     /**
  29.      * @ORM\Column(name="created_at", type="datetime")
  30.      *
  31.      * @Gedmo\Timestampable(on="create")
  32.      */
  33.     private \DateTimeInterface $createdAt;
  34.     /**
  35.      * @ORM\Column(name="updated_at", type="datetime")
  36.      *
  37.      * @Gedmo\Timestampable(on="update")
  38.      */
  39.     private \DateTimeInterface $updatedAt;
  40.     /**
  41.      * @ORM\OneToMany(targetEntity="App\Entity\Cart\CartItem", mappedBy="cart", cascade={"persist", "remove"})
  42.      */
  43.     private ?Collection $cartitems;
  44.     /**
  45.      * @ORM\ManyToOne(targetEntity="App\Entity\Garages\Garage", fetch="EAGER")
  46.      *
  47.      * @ORM\JoinColumn(name="garage_id", referencedColumnName="id")
  48.      */
  49.     private ?Garage $garage null;
  50.     /**
  51.      * @ORM\ManyToOne(targetEntity="App\Entity\User", fetch="EAGER")
  52.      *
  53.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  54.      *
  55.      * User who has placed the order (could be the associated but an admin too)
  56.      */
  57.     private ?UserInterface $user null;
  58.     /**
  59.      * @ORM\Column(type="text", length=10000, nullable=true)
  60.      */
  61.     private ?string $commentUser null;
  62.     /**
  63.      * @ORM\Column(type="text", length=10000, nullable=true)
  64.      */
  65.     private ?string $commentAdmin null;
  66.     public function __construct()
  67.     {
  68.         $this->cartitems = new ArrayCollection();
  69.     }
  70.     public function getId(): int
  71.     {
  72.         return $this->id;
  73.     }
  74.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  75.     {
  76.         $this->createdAt $createdAt;
  77.         return $this;
  78.     }
  79.     public function getCreatedAt(): \DateTimeInterface
  80.     {
  81.         return $this->createdAt;
  82.     }
  83.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  84.     {
  85.         $this->updatedAt $updatedAt;
  86.         return $this;
  87.     }
  88.     public function getUpdatedAt(): \DateTimeInterface
  89.     {
  90.         return $this->updatedAt;
  91.     }
  92.     public function setCartItems(?Collection $items): self
  93.     {
  94.         $this->cartitems $items;
  95.         return $this;
  96.     }
  97.     public function addCartItem(CartItem $item): self
  98.     {
  99.         $item->setCart($this);
  100.         $this->cartitems[] = $item;
  101.         return $this;
  102.     }
  103.     public function getCartItems(): ?Collection
  104.     {
  105.         return $this->cartitems;
  106.     }
  107.     public function getCartItemsAmount(): int
  108.     {
  109.         return count($this->getCartItems());
  110.     }
  111.     public function cartItemsGroupByGiftProvider(): array
  112.     {
  113.         $cartItemsGroupByProvider = [];
  114.         foreach ($this->cartitems as $cartItem) {
  115.             $cartItemsGroupByProvider[$cartItem->getGiftItem()->firstProvider()->getId()][] = $cartItem;
  116.         }
  117.         return $cartItemsGroupByProvider;
  118.     }
  119.     public function cartItemsGroupBySupplier(): array
  120.     {
  121.         $cartItemsGroupBySupplier = [];
  122.         foreach ($this->cartitems as $cartItem) {
  123.             $item $cartItem->getItem();
  124.             $cartItemsGroupBySupplier[$item->getFamily()->getSupplier()->getId()][] = $item;
  125.         }
  126.         return $cartItemsGroupBySupplier;
  127.     }
  128.     public function getItems(): array
  129.     {
  130.         $result = [];
  131.         foreach ($this->cartitems as $cartitem) {
  132.             $result[] = $cartitem->getItem();
  133.         }
  134.         return $result;
  135.     }
  136.     public function hasItem($itemint $sizeId null): bool
  137.     {
  138.         foreach ($this->cartitems as $cartItem) {
  139.             if ($cartItem->getItem() === $item || ($cartItem->getGiftItem() === $item && (is_null($cartItem->size()) || $cartItem->size()->getId() === $sizeId))) {
  140.                 return true;
  141.             }
  142.         }
  143.         return false;
  144.     }
  145.     public function getCartItemByItem($itemint $sizeId null): ?CartItem
  146.     {
  147.         foreach ($this->cartitems as $cartItem) {
  148.             if ($cartItem->getItem() === $item || ($cartItem->getGiftItem() === $item && (is_null($cartItem->size()) || $cartItem->size()->getId() === $sizeId))) {
  149.                 return $cartItem;
  150.             }
  151.         }
  152.         return null;
  153.     }
  154.     public function getTotalQuantity(): int
  155.     {
  156.         $quantity 0;
  157.         foreach ($this->getCartItems() as $cartItem) {
  158.             $quantity += $cartItem->getQuantity();
  159.         }
  160.         return $quantity;
  161.     }
  162.     /**
  163.      * @throws \JsonException
  164.      */
  165.     public function toJson(): string
  166.     {
  167.         $result = [];
  168.         $result['totalQuantity'] = $this->getTotalQuantity();
  169.         return json_encode($resultJSON_THROW_ON_ERROR);
  170.     }
  171.     public function getUser(): ?UserInterface
  172.     {
  173.         return $this->user;
  174.     }
  175.     public function setUser(UserInterface $user): self
  176.     {
  177.         $this->user $user;
  178.         return $this;
  179.     }
  180.     public function getCommentUser(): ?string
  181.     {
  182.         return $this->commentUser;
  183.     }
  184.     public function setCommentUser(?string $commentUser): self
  185.     {
  186.         $this->commentUser $commentUser;
  187.         return $this;
  188.     }
  189.     public function getCommentAdmin(): ?string
  190.     {
  191.         return $this->commentAdmin;
  192.     }
  193.     public function setCommentAdmin(?string $commentAdmin): self
  194.     {
  195.         $this->commentAdmin $commentAdmin;
  196.         return $this;
  197.     }
  198.     public function canPlaceGiftOrder(): bool
  199.     {
  200.         if (!$this->getGarage()) {
  201.             return false;
  202.         }
  203.         if ($this->getGarage()->getOwner() && $this->totalGiftPoints() > $this->getGarage()->getOwner()->getCatalogPointsAmount()) {
  204.             return false;
  205.         }
  206.         foreach ($this->getCartItems() as $item) {
  207.             if (=== count($item->getGiftItem()->getProviders())) {
  208.                 return false;
  209.             }
  210.         }
  211.         if ($this->hasAnyProviderWithBlockedDelivery()) {
  212.             return false;
  213.         }
  214.         return true;
  215.     }
  216.     public function hasAnyProviderWithBlockedDelivery(): bool
  217.     {
  218.         foreach ($this->cartitems as $cartItem) {
  219.             if (!$cartItem->getGiftItem()) {
  220.                 continue;
  221.             }
  222.             $provider $cartItem->getGiftItem()->firstProvider();
  223.             if (!$provider) {
  224.                 continue;
  225.             }
  226.             if ($provider->hasDeliveryBlocked()) {
  227.                 return true;
  228.             }
  229.         }
  230.         return false;
  231.     }
  232.     public function providersWithBlockedDelivery(): array
  233.     {
  234.         $providers = [];
  235.         foreach ($this->cartitems as $cartItem) {
  236.             if (!$cartItem->getGiftItem()) {
  237.                 continue;
  238.             }
  239.             $provider $cartItem->getGiftItem()->firstProvider();
  240.             if (!$provider) {
  241.                 continue;
  242.             }
  243.             if ($provider->hasDeliveryBlocked()) {
  244.                 $providers[] = $provider;
  245.             }
  246.         }
  247.         return $providers;
  248.     }
  249.     public function totalGiftPoints(): int
  250.     {
  251.         $total 0;
  252.         foreach ($this->getCartItems() as $cartItem) {
  253.             $total += $cartItem->totalGiftPoints();
  254.         }
  255.         $total += $this->totalDeliveryPoints();
  256.         return $total;
  257.     }
  258.     public function distinctGiftProviders(): array
  259.     {
  260.         $giftProviders = [];
  261.         foreach ($this->cartitems as $cartItem) {
  262.             if (!$cartItem->getGiftItem()) {
  263.                 continue;
  264.             }
  265.             $provider $cartItem->getGiftItem()->firstProvider();
  266.             if ($provider && !isset($giftProviders[$provider->getId()])) {
  267.                 $giftProviders[$provider->getId()] = $provider;
  268.             }
  269.         }
  270.         return $giftProviders;
  271.     }
  272.     public function totalPointsInCartOfGiftProvider(CatalogGiftProvider $giftProvider): int
  273.     {
  274.         $totalPointsOfGiftProvider 0;
  275.         foreach ($this->cartitems as $cartItem) {
  276.             $giftItem $cartItem->getGiftItem();
  277.             if (!$giftItem) {
  278.                 continue;
  279.             }
  280.             if ($giftItem->firstProvider() !== $giftProvider) {
  281.                 continue;
  282.             }
  283.             $totalPointsOfGiftProvider += $cartItem->totalGiftPoints();
  284.         }
  285.         return $totalPointsOfGiftProvider;
  286.     }
  287.     private function totalDeliveryPoints(): int
  288.     {
  289.         $deliveryPoints 0;
  290.         foreach ($this->giftProvidersWithDeliveryLine() as $provider) {
  291.             $deliveryPoints += $provider->getDeliveryManagementPoints();
  292.         }
  293.         return $deliveryPoints;
  294.     }
  295.     public function giftProvidersWithDeliveryLine(): array
  296.     {
  297.         $providers = [];
  298.         foreach ($this->distinctGiftProviders() as $giftProvider) {
  299.             if ($giftProvider->hasMinPointsRestriction() &&
  300.                 !$giftProvider->exceedsMinPointsRestriction($this->totalPointsInCartOfGiftProvider($giftProvider))) {
  301.                 $providers[] = $giftProvider;
  302.             }
  303.         }
  304.         return $providers;
  305.     }
  306.     public function hasProviderDeliveryLine($provider): bool
  307.     {
  308.         return in_array($provider$this->giftProvidersWithDeliveryLine(), true);
  309.     }
  310.     public function containsItemsGarageHasNotAccess(): ?bool
  311.     {
  312.         if ($this->getGarage()) {
  313.             $garageOwner $this->getGarage()->getOwner();
  314.             foreach ($this->cartitems as $cartItem) {
  315.                 if (!$cartItem->getItem()->userHasAccess($garageOwner)) {
  316.                     return true;
  317.                 }
  318.             }
  319.             return false;
  320.         }
  321.         return null;
  322.     }
  323. }