src/Entity/Cart/Cart.php line 21

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