src/Entity/Cart.php line 18

Open in your IDE?
  1. <?php
  2.     namespace App\Entity;
  3.     use App\Interfaces\ProductInterface;
  4.     use App\Repository\CartRepository;
  5.     use App\Traits\DateTrait;
  6.     use Doctrine\Common\Collections\ArrayCollection;
  7.     use Doctrine\Common\Collections\Collection;
  8.     use Doctrine\ORM\Mapping as ORM;
  9.     /**
  10.      * @ORM\Table(indexes={
  11.      *      @ORM\Index(columns={"serial_cookie"})
  12.      * })
  13.      * @ORM\Entity(repositoryClass=CartRepository::class)
  14.      */
  15.     class Cart
  16.     {
  17.         /**
  18.          * @ORM\Id
  19.          * @ORM\GeneratedValue
  20.          * @ORM\Column(type="integer")
  21.          */
  22.         private $id;
  23.         /**
  24.          * @ORM\Column(type="string", length=64, nullable=true)
  25.          */
  26.         private $shippingMethod;
  27.         /**
  28.          * @ORM\Column(type="string", length=64)
  29.          */
  30.         private $serialCookie;
  31.         /**
  32.          * @ORM\Column(type="boolean", options={"default": true})
  33.          */
  34.         private $sameAddressBilling TRUE;
  35.         /**
  36.          * @ORM\Column(type="integer", nullable=true)
  37.          */
  38.         private $shippingDelay;
  39.         /**
  40.          * @ORM\ManyToOne(targetEntity=User::class, inversedBy="carts", cascade={"persist"})
  41.          * @ORM\JoinColumn(nullable=true)
  42.          */
  43.         private $user;
  44.         /**
  45.          * @ORM\Column(type="boolean", options={"default": false})
  46.          */
  47.         private $multiShipping FALSE;
  48.         /**
  49.          * @ORM\Column(type="decimal", precision=12, scale=4, nullable=true)
  50.          */
  51.         private $bonusTotalUsed;
  52.         /**
  53.          * @ORM\OneToOne(targetEntity=CartAddress::class, cascade={"persist", "remove"}, orphanRemoval=true)
  54.          */
  55.         private $shippingAddress;
  56.         /**
  57.          * @ORM\OneToOne(targetEntity=CartAddress::class, cascade={"persist", "remove"}, orphanRemoval=true)
  58.          */
  59.         private $billingAddress;
  60.         /**
  61.          * @ORM\OneToMany(targetEntity=CartItem::class, mappedBy="cart", cascade={"remove"})
  62.          */
  63.         private $items;
  64.         /**
  65.          * @ORM\OneToOne(targetEntity=SaleOrder::class, mappedBy="cart", cascade={"persist", "remove"})
  66.          */
  67.         private $saleOrder;
  68.         use DateTrait;
  69.         public function __construct()
  70.         {
  71.             $this->items = new ArrayCollection();
  72.         }
  73.         /*
  74.          * ============================================================================================
  75.          * =============================== FONCTIONS CUSTOM ===========================================
  76.          * ============================================================================================
  77.          */
  78.         public function getReference()
  79.         {
  80.             return str_pad$this->getId(), 8'0'STR_PAD_LEFT );
  81.         }
  82.         // ITEMS
  83.         public function getId(): ?int
  84.         {
  85.             return $this->id;
  86.         }
  87.         // @TODO check product, dans la 2.8 ca ne fct pas non  plus
  88.         //        public function getTotalHt()
  89.         //        {
  90.         //            $total = NULL;
  91.         //            /* @var $item CartItem */
  92.         //            foreach ( $this->items as $item ) {
  93.         //                $total += $item->getProduct()->getPriceHT() * $item->getQuantity();
  94.         //            }
  95.         //            return $total;
  96.         //        }
  97.         //
  98.         //
  99.         //        public function getTotalTTC()
  100.         //        {
  101.         //            $total = NULL;
  102.         //            /** @var CartItem $item */
  103.         //            foreach ( $this->items as $item ) {
  104.         //                $total += $item->getProduct()->getPriceTTC() * $item->getQuantity();
  105.         //            }
  106.         //            return $total;
  107.         //        }
  108.         public function getItemsCount()
  109.         {
  110.             return count$this->items );
  111.         }
  112.         /**
  113.          * @param ProductInterface $product
  114.          *
  115.          * @return CartItem|false
  116.          */
  117.         public function getCartItemByProductProductInterface $product )
  118.         {
  119.             foreach ( $this->getItems() as $item ) {
  120.                 if ( $item->getSku() == $product->getSku() ) {
  121.                     return $item;
  122.                 }
  123.             }
  124.             return FALSE;
  125.         }
  126.         /**
  127.          * @return Collection|CartItem[]
  128.          */
  129.         public function getItems(): Collection
  130.         {
  131.             return $this->items;
  132.         }
  133.         public function getAddressTypeAddress $address ): string
  134.         {
  135.             if ( $this->billingAddress == $address ) {
  136.                 return Address::TYPE_BILLING_ADDRESS;
  137.             }
  138.             elseif ( $this->shippingAddress == $address ) {
  139.                 return Address::TYPE_SHIPPING_ADDRESS;
  140.             }
  141.             return Address::TYPE_BILLING_ADDRESS;
  142.         }
  143.         /*
  144.          * ============================================================================================
  145.          * ============================== FIN FONCTIONS CUSTOM ========================================
  146.          * ============================================================================================
  147.          */
  148.         public function getCartItemById$id )
  149.         {
  150.             foreach ( $this->getItems() as $item ) {
  151.                 if ( $item->getId() == $id ) {
  152.                     return $item;
  153.                 }
  154.             }
  155.             return FALSE;
  156.         }
  157.         public function totalQuantity()
  158.         {
  159.             $items = [];
  160.             foreach ( $this->getItems() as $item ) {
  161.                 $items[] = $item->getQuantity();
  162.             }
  163.             return ( $this->getItems() && count$this->getItems() ) > ) ? array_sum$items ) : 0;
  164.         }
  165.         public function getShippingMethod(): ?string
  166.         {
  167.             return $this->shippingMethod;
  168.         }
  169.         public function setShippingMethod( ?string $shippingMethod ): self
  170.         {
  171.             $this->shippingMethod $shippingMethod;
  172.             return $this;
  173.         }
  174.         public function getSerialCookie(): ?string
  175.         {
  176.             return $this->serialCookie;
  177.         }
  178.         public function setSerialCookiestring $serialCookie ): self
  179.         {
  180.             $this->serialCookie $serialCookie;
  181.             return $this;
  182.         }
  183.         public function getSameAddressBilling(): ?bool
  184.         {
  185.             return $this->sameAddressBilling;
  186.         }
  187.         public function setSameAddressBillingbool $sameAddressBilling ): self
  188.         {
  189.             $this->sameAddressBilling $sameAddressBilling;
  190.             return $this;
  191.         }
  192.         public function getShippingDelay(): ?int
  193.         {
  194.             return $this->shippingDelay;
  195.         }
  196.         public function setShippingDelay( ?int $shippingDelay ): self
  197.         {
  198.             $this->shippingDelay $shippingDelay;
  199.             return $this;
  200.         }
  201.         public function getUser(): ?User
  202.         {
  203.             return $this->user;
  204.         }
  205.         public function setUser( ?User $user ): self
  206.         {
  207.             $this->user $user;
  208.             return $this;
  209.         }
  210.         public function getMultiShipping(): ?bool
  211.         {
  212.             return $this->multiShipping;
  213.         }
  214.         public function setMultiShippingbool $multiShipping ): self
  215.         {
  216.             $this->multiShipping $multiShipping;
  217.             return $this;
  218.         }
  219.         public function getBonusTotalUsed(): ?string
  220.         {
  221.             return $this->bonusTotalUsed;
  222.         }
  223.         public function setBonusTotalUsed( ?string $bonusTotalUsed ): self
  224.         {
  225.             $this->bonusTotalUsed $bonusTotalUsed;
  226.             return $this;
  227.         }
  228.         public function getShippingAddress(): ?CartAddress
  229.         {
  230.             return $this->shippingAddress;
  231.         }
  232.         public function setShippingAddress( ?CartAddress $shippingAddress ): self
  233.         {
  234.             $this->shippingAddress $shippingAddress;
  235.             return $this;
  236.         }
  237.         public function getBillingAddress(): ?CartAddress
  238.         {
  239.             return $this->billingAddress;
  240.         }
  241.         public function setBillingAddress( ?CartAddress $billingAddress ): self
  242.         {
  243.             $this->billingAddress $billingAddress;
  244.             return $this;
  245.         }
  246.         public function addItemCartItem $item ): self
  247.         {
  248.             if ( !$this->items->contains$item ) ) {
  249.                 $this->items[] = $item;
  250.                 $item->setCart$this );
  251.             }
  252.             return $this;
  253.         }
  254.         public function removeItemCartItem $item ): self
  255.         {
  256.             if ( $this->items->removeElement$item ) ) {
  257.                 // set the owning side to null (unless already changed)
  258.                 if ( $item->getCart() === $this ) {
  259.                     $item->setCartNULL );
  260.                 }
  261.             }
  262.             return $this;
  263.         }
  264.         public function getSaleOrder(): ?SaleOrder
  265.         {
  266.             return $this->saleOrder;
  267.         }
  268.         public function setSaleOrder( ?SaleOrder $saleOrder ): self
  269.         {
  270.             // unset the owning side of the relation if necessary
  271.             if ( $saleOrder === NULL && $this->saleOrder !== NULL ) {
  272.                 $this->saleOrder->setCartNULL );
  273.             }
  274.             // set the owning side of the relation if necessary
  275.             if ( $saleOrder !== NULL && $saleOrder->getCart() !== $this ) {
  276.                 $saleOrder->setCart$this );
  277.             }
  278.             $this->saleOrder $saleOrder;
  279.             return $this;
  280.         }
  281.     }