src/Entity/CartItem.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Product\Product;
  4. use DateTime;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Exception;
  8. /**
  9.  * Class Cart Item
  10.  *
  11.  * @ORM\Entity
  12.  * @ORM\Table(name="malys_cart_item")
  13.  * @ORM\HasLifecycleCallbacks()
  14.  * @package App\Entity
  15.  * @ORM\Entity(repositoryClass="App\Repository\CartItemRepository")
  16.  */
  17. class CartItem
  18. {
  19.     /**
  20.      * @var integer
  21.      *
  22.      * @ORM\Column(name="id", type="integer")
  23.      * @ORM\Id
  24.      * @ORM\GeneratedValue(strategy="AUTO")
  25.      */
  26.     protected $id;
  27.     /**
  28.      * @var Cart
  29.      *
  30.      * @ORM\ManyToOne(targetEntity="Cart", inversedBy="items")
  31.      * @ORM\JoinColumn(name="cart_id", referencedColumnName="id")
  32.      */
  33.     protected $cart;
  34.     /**
  35.      * @var integer
  36.      *
  37.      * @ORM\Column(name="quantity", type="integer", nullable=false)
  38.      */
  39.     protected $quantity 1;
  40.     /**
  41.      * @var float $bundling
  42.      * @ORM\Column(name="bundling", type="float", nullable=true)
  43.      */
  44.     protected $bundling;
  45.     /**
  46.      * @var Product
  47.      *
  48.      * @ORM\ManyToOne(targetEntity="App\Entity\Product\Product")
  49.      * @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
  50.      */
  51.     protected $product;
  52.     /**
  53.      *
  54.      * @ORM\ManyToOne(targetEntity="ProductVariation")
  55.      * @ORM\JoinColumn(name="product_variation_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
  56.      */
  57.     protected $productVariation;
  58.     /**
  59.      * @ORM\ManyToMany(targetEntity="App\Entity\ProductService")
  60.      * @ORM\JoinTable(name="malys_cart_item_services")
  61.      */
  62.     protected $services;
  63.     /**
  64.      * @var DateTime
  65.      *
  66.      * @ORM\Column(name="createdAt", type="datetime", nullable=false)
  67.      */
  68.     protected $createdAt;
  69.     /**
  70.      * @var DateTime
  71.      *
  72.      * @ORM\Column(name="updatedAt", type="datetime", nullable=true)
  73.      */
  74.     protected $updatedAt;
  75.     /******************************* FIELDS FOR VIEW ******************************/
  76.     /**
  77.      * @var boolean
  78.      */
  79.     protected $calculated;
  80.     /**
  81.      * @var float
  82.      */
  83.     protected $totalPrice;
  84.     /**
  85.      * CartItem constructor.
  86.      */
  87.     public function __construct()
  88.     {
  89.         $this->services = new ArrayCollection();
  90.     }
  91.     /**
  92.      * @return int
  93.      */
  94.     public function getId()
  95.     {
  96.         return $this->id;
  97.     }
  98.     /**
  99.      * @param int $id
  100.      */
  101.     public function setId($id)
  102.     {
  103.         $this->id $id;
  104.     }
  105.     /**
  106.      * @return Cart
  107.      */
  108.     public function getCart()
  109.     {
  110.         return $this->cart;
  111.     }
  112.     /**
  113.      * @param Cart $cart
  114.      */
  115.     public function setCart($cart)
  116.     {
  117.         $this->cart $cart;
  118.     }
  119.     /**
  120.      * @return int
  121.      */
  122.     public function getQuantity()
  123.     {
  124.         return $this->quantity;
  125.     }
  126.     /**
  127.      * @param int $quantity
  128.      */
  129.     public function setQuantity($quantity)
  130.     {
  131.         $this->quantity $quantity;
  132.     }
  133.     /**
  134.      * @return Product
  135.      */
  136.     public function getProduct()
  137.     {
  138.         return $this->product;
  139.     }
  140.     /**
  141.      * @param Product $product
  142.      */
  143.     public function setProduct($product)
  144.     {
  145.         $this->product $product;
  146.     }
  147.     /**
  148.      * @return DateTime
  149.      */
  150.     public function getCreatedAt()
  151.     {
  152.         return $this->createdAt;
  153.     }
  154.     /**
  155.      * Set createdAt
  156.      *
  157.      * @return CartItem
  158.      * @ORM\PrePersist
  159.      * @throws Exception
  160.      */
  161.     public function setCreatedAt()
  162.     {
  163.         $this->createdAt = new DateTime();
  164.         $this->updatedAt = new DateTime();
  165.         return $this;
  166.     }
  167.     /**
  168.      * @return DateTime
  169.      */
  170.     public function getUpdatedAt()
  171.     {
  172.         return $this->updatedAt;
  173.     }
  174.     /**
  175.      * Set updatedAt
  176.      *
  177.      * @return CartItem
  178.      * @ORM\PreUpdate
  179.      * @throws Exception
  180.      */
  181.     public function setUpdatedAt()
  182.     {
  183.         $this->updatedAt = new DateTime();
  184.         return $this;
  185.     }
  186.     /**
  187.      * @return bool
  188.      */
  189.     public function isCalculated()
  190.     {
  191.         return $this->calculated;
  192.     }
  193.     /**
  194.      * @param bool $calculated
  195.      */
  196.     public function setCalculated($calculated)
  197.     {
  198.         $this->calculated $calculated;
  199.     }
  200.     /**
  201.      * @return float
  202.      * @throws Exception
  203.      */
  204.     public function getTotalPrice()
  205.     {
  206.         if ($this->isCalculated()) {
  207.             return $this->totalPrice;
  208.         } else {
  209.             throw new Exception('Le montant n\'a pas été calculé AA');
  210.         }
  211.     }
  212.     /**
  213.      * @param float $totalPrice
  214.      */
  215.     public function setTotalPrice($totalPrice)
  216.     {
  217.         $this->totalPrice $totalPrice;
  218.     }
  219.     /**
  220.      * @return float
  221.      */
  222.     public function getBundling()
  223.     {
  224.         return $this->bundling;
  225.     }
  226.     /**
  227.      * @param float $bundling
  228.      */
  229.     public function setBundling($bundling)
  230.     {
  231.         $this->bundling $bundling;
  232.     }
  233.     public function getPositiveBundling()
  234.     {
  235.         if (!empty($this->bundling) && $this->bundling 0) {
  236.             $bundling $this->bundling;
  237.         } else {
  238.             $bundling 1;
  239.         }
  240.         return $bundling;
  241.     }
  242.     /**
  243.      * @return mixed
  244.      */
  245.     public function getProductVariation()
  246.     {
  247.         return $this->productVariation;
  248.     }
  249.     /**
  250.      * @param mixed $productVariation
  251.      */
  252.     public function setProductVariation($productVariation)
  253.     {
  254.         $this->productVariation $productVariation;
  255.     }
  256.     /**
  257.      * @return mixed
  258.      */
  259.     public function getServices()
  260.     {
  261.         return $this->services;
  262.     }
  263.     /**
  264.      * @param mixed $services
  265.      */
  266.     public function setServices($services)
  267.     {
  268.         $this->services $services;
  269.     }
  270.     public function addService(ProductService $productService)
  271.     {
  272.         if (!$this->services->contains($productService)) {
  273.             $this->services->add($productService);
  274.         }
  275.         return $this;
  276.     }
  277.     public function removeService(ProductService $productService)
  278.     {
  279.         $this->services->removeElement($productService);
  280.         return $this;
  281.     }
  282. }