src/Entity/Cart.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use DateTime;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Exception;
  7. /**
  8.  * Class Cart
  9.  *
  10.  * @ORM\Entity
  11.  * @ORM\Table(name="malys_cart")
  12.  * @ORM\HasLifecycleCallbacks()
  13.  * @package App\Entity
  14.  * @ORM\Entity(repositoryClass="App\Repository\CartRepository")
  15.  */
  16. class Cart
  17. {
  18.     /**
  19.      * @var integer
  20.      *
  21.      * @ORM\Column(name="id", type="integer")
  22.      * @ORM\Id
  23.      * @ORM\GeneratedValue(strategy="AUTO")
  24.      */
  25.     protected $id;
  26.     /**
  27.      * @var Customer
  28.      *
  29.      * Many carts have one customer
  30.      * @ORM\ManyToOne(targetEntity="Customer")
  31.      * @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
  32.      */
  33.     protected $customer;
  34.     /**
  35.      * @var User
  36.      *
  37.      * Many carts have one user
  38.      *
  39.      * @ORM\ManyToOne(targetEntity="User", inversedBy="carts")
  40.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  41.      */
  42.     protected $user;
  43.     /**
  44.      * @var ArrayCollection
  45.      *
  46.      * @ORM\OneToMany(targetEntity="CartItem", mappedBy="cart", cascade={"persist", "remove"})
  47.      */
  48.     protected $items;
  49.     /**
  50.      * @var DateTime
  51.      *
  52.      * @ORM\Column(name="createdAt", type="datetime", nullable=false)
  53.      */
  54.     protected $createdAt;
  55.     /**
  56.      * @var DateTime
  57.      *
  58.      * @ORM\Column(name="updatedAt", type="datetime", nullable=true)
  59.      */
  60.     protected $updatedAt;
  61.     /**
  62.      * Order is word reserved in mysql
  63.      * @ORM\ManyToOne(targetEntity="App\Entity\Order")
  64.      * @ORM\JoinColumn(name="orderToModify", referencedColumnName="id", nullable=true)
  65.      */
  66.     protected $orderToModify;
  67.     /**
  68.      * @ORM\Column(name="comments" ,type="array", nullable=true)
  69.      */
  70.     protected $comments = [];
  71.     /**
  72.      * @ORM\Column(name="requested_delivery_dates" ,type="array", nullable=true)
  73.      */
  74.     protected $requestedDeliveryDates = [];
  75.     /******************************* FIELDS FOR VIEW ******************************/
  76.     /**
  77.      * @var boolean
  78.      */
  79.     protected $calculated;
  80.     /**
  81.      * @var float
  82.      */
  83.     protected $totalPrice;
  84.     /**
  85.      * Cart constructor.
  86.      */
  87.     public function __construct()
  88.     {
  89.         $this->items = 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 Customer
  107.      */
  108.     public function getCustomer()
  109.     {
  110.         return $this->customer;
  111.     }
  112.     /**
  113.      * @param Customer $customer
  114.      */
  115.     public function setCustomer($customer)
  116.     {
  117.         $this->customer $customer;
  118.     }
  119.     /**
  120.      * @return User
  121.      */
  122.     public function getUser()
  123.     {
  124.         return $this->user;
  125.     }
  126.     /**
  127.      * @param User $user
  128.      */
  129.     public function setUser($user)
  130.     {
  131.         $this->user $user;
  132.     }
  133.     //    /**
  134.     //     * @return SupplierCustomer
  135.     //     */
  136.     //    public function getSupplierCustomer()
  137.     //    {
  138.     //        return $this->supplierCustomer;
  139.     //    }
  140.     //
  141.     //    /**
  142.     //     * @return SupplierCustomer|null
  143.     //     */
  144.     //    public function getEnabledSupplierCustomer()
  145.     //    {
  146.     //        if ($this->supplierCustomer instanceof SupplierCustomer
  147.     //            && $this->supplierCustomer->getSupplier() instanceof Supplier
  148.     //            && $this->supplierCustomer->getSupplier()->isEnabled()) {
  149.     //            return $this->supplierCustomer;
  150.     //        }
  151.     //        return null;
  152.     //    }
  153.     //
  154.     //    /**
  155.     //     * @param SupplierCustomer $supplierCustomer
  156.     //     */
  157.     //    public function setSupplierCustomer($supplierCustomer)
  158.     //    {
  159.     //        $this->supplierCustomer = $supplierCustomer;
  160.     //    }
  161.     /**
  162.      * @return ArrayCollection
  163.      */
  164.     public function getItems()
  165.     {
  166.         return $this->items;
  167.     }
  168.     /**
  169.      * @return ArrayCollection|\Doctrine\Common\Collections\Collection
  170.      */
  171.     //    public function getActiveItems()
  172.     //    {
  173.     //        return $this->items->filter(function($entry) {
  174.     //            /** @var $entry CartItem */
  175.     //            if($entry->getProduct() instanceof Product && $entry->getProduct()->isEnabled()) {
  176.     //                return true;
  177.     //            }
  178.     //            return false;
  179.     //        });
  180.     //    }
  181.     /**
  182.      * @param ArrayCollection $items
  183.      */
  184.     public function setItems($items)
  185.     {
  186.         $this->items $items;
  187.     }
  188.     /**
  189.      * @param CartItem $item
  190.      */
  191.     public function addItem($item)
  192.     {
  193.         $this->items->add($item);
  194.     }
  195.     /**
  196.      * @param CartItem $item
  197.      */
  198.     public function removeItem($item)
  199.     {
  200.         if ($this->items->contains($item)) {
  201.             $this->items->removeElement($item);
  202.             $item->setCart(null);
  203.         }
  204.     }
  205.     /**
  206.      * @return DateTime
  207.      */
  208.     public function getCreatedAt()
  209.     {
  210.         return $this->createdAt;
  211.     }
  212.     /**
  213.      * Set createdAt
  214.      *
  215.      * @return Cart
  216.      * @ORM\PrePersist
  217.      */
  218.     public function setCreatedAt()
  219.     {
  220.         $this->createdAt = new DateTime();
  221.         $this->updatedAt = new DateTime();
  222.         return $this;
  223.     }
  224.     /**
  225.      * @return DateTime
  226.      */
  227.     public function getUpdatedAt()
  228.     {
  229.         return $this->updatedAt;
  230.     }
  231.     /**
  232.      * Set updatedAt
  233.      *
  234.      * @return Cart
  235.      * @ORM\PreUpdate
  236.      */
  237.     public function setUpdatedAt()
  238.     {
  239.         $this->updatedAt = new DateTime();
  240.         return $this;
  241.     }
  242.     /**
  243.      * @return bool
  244.      */
  245.     public function isCalculated()
  246.     {
  247.         return $this->calculated;
  248.     }
  249.     /**
  250.      * @param bool $calculated
  251.      */
  252.     public function setCalculated($calculated)
  253.     {
  254.         $this->calculated $calculated;
  255.     }
  256.     /**
  257.      * @return float
  258.      * @throws Exception
  259.      */
  260.     public function getTotalPrice()
  261.     {
  262.         if ($this->isCalculated()) {
  263.             return $this->totalPrice;
  264.         } else {
  265.             throw new Exception('Le montant n\'a pas été calculé AB');
  266.         }
  267.     }
  268.     /**
  269.      * @param float $totalPrice
  270.      */
  271.     public function setTotalPrice($totalPrice)
  272.     {
  273.         $this->totalPrice $totalPrice;
  274.     }
  275.     /**
  276.      * @return mixed
  277.      */
  278.     public function getOrderToModify()
  279.     {
  280.         return $this->orderToModify;
  281.     }
  282.     /**
  283.      * @param mixed $orderToModify
  284.      */
  285.     public function setOrderToModify($orderToModify)
  286.     {
  287.         $this->orderToModify $orderToModify;
  288.     }
  289.     /**
  290.      * @return mixed
  291.      */
  292.     public function getComments()
  293.     {
  294.         return $this->comments;
  295.     }
  296.     /**
  297.      * @param mixed $comments
  298.      */
  299.     public function setComments($comments)
  300.     {
  301.         $this->comments $comments;
  302.     }
  303.     /**
  304.      * @return mixed
  305.      */
  306.     public function getRequestedDeliveryDates()
  307.     {
  308.         return $this->requestedDeliveryDates;
  309.     }
  310.     /**
  311.      * @param mixed $requestedDeliveryDates
  312.      */
  313.     public function setRequestedDeliveryDates($requestedDeliveryDates)
  314.     {
  315.         $this->requestedDeliveryDates $requestedDeliveryDates;
  316.     }
  317. }