src/Entity/CartItem.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
  5. use App\Helpers\Encoder;
  6. /**
  7. * CustomersBasket
  8. *
  9. * @ORM\Table(name="carts_items")
  10. * @ORM\Entity
  11. * @ORM\HasLifecycleCallbacks()
  12. */
  13. class CartItem
  14. {
  15. /**
  16. * @var int
  17. *
  18. * @ORM\Column(name="id", type="integer", nullable=false)
  19. * @ORM\Id
  20. * @ORM\GeneratedValue(strategy="IDENTITY")
  21. */
  22. private $id;
  23. /**
  24. * @var \App\Entity\Cart
  25. *
  26. * @ORM\ManyToOne(targetEntity="App\Entity\Cart", inversedBy="items")
  27. * @ORM\JoinColumn(name="cart_id", referencedColumnName="id", onDelete="cascade")
  28. */
  29. private $cart;
  30. /**
  31. * @var \App\Entity\Product
  32. *
  33. * @ORM\ManyToOne(targetEntity="App\Entity\Product")
  34. * @ORM\JoinColumn(name="product_id", referencedColumnName="products_id", onDelete="cascade")
  35. */
  36. private $product;
  37. /**
  38. * @var int
  39. *
  40. * @ORM\Column(name="quantity", type="integer", nullable=false)
  41. */
  42. private $quantity = '0';
  43. /**
  44. * @var string
  45. *
  46. * @ORM\Column(name="final_price", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  47. */
  48. private $unitPrice = '0.0000';
  49. /**
  50. * @var string
  51. *
  52. * @ORM\Column(name="total_price", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
  53. */
  54. private $totalPrice = '0.0000';
  55. /**
  56. * @var ?MarketingRule
  57. */
  58. private $marketingRule = null;
  59. /**
  60. * @var ?string
  61. */
  62. private $discountType = null;
  63. /**
  64. * @var \DateTime|null
  65. *
  66. * @ORM\Column(name="created_at", type="datetime", nullable=true)
  67. */
  68. private $createdAt;
  69. /**
  70. * @var \DateTime|null
  71. *
  72. * @ORM\Column(name="updated_at", type="datetime", nullable=true)
  73. */
  74. private $updatedAt;
  75. private $quantityOffered = 0;
  76. public function __construct(Product $product, $qty) {
  77. $this->product = $product;
  78. $this->quantity = $qty;
  79. }
  80. public function getId() {
  81. return $this->id;
  82. }
  83. public function getCart(): \App\Entity\Cart {
  84. return $this->cart;
  85. }
  86. public function getQuantity() {
  87. return $this->quantity;
  88. }
  89. public function getUnitPrice($withEcotax = true) {
  90. if($withEcotax === false){
  91. return $this->unitPrice - $this->product->getEcotax();
  92. }
  93. return $this->unitPrice;
  94. }
  95. public function getPrice($withEcotax = true) {
  96. return $this->getUnitPrice($withEcotax, $this->getCart()->getPriceGroup()) * $this->getQuantity();
  97. }
  98. public function getNormalPrice($withEcotax = true) {
  99. return $this->getProduct()->getPrice($withEcotax, $this->getCart()->getPriceGroup()) * $this->getQuantity();
  100. }
  101. public function getCreatedAt() {
  102. return $this->createdAt;
  103. }
  104. public function getUpdatedAt(): ?\DateTime {
  105. return $this->updatedAt;
  106. }
  107. public function getProduct() {
  108. return $this->product;
  109. }
  110. public function getReminded() {
  111. return $this->reminded;
  112. }
  113. public function hasDiscount() {
  114. return (Encoder::priceFormat($this->getPrice(true, $this->getCart()->getPriceGroup())) != Encoder::priceFormat($this->getTotalPrice())) ||
  115. (Encoder::priceFormat($this->getUnitPrice(false)) != Encoder::priceFormat($this->getProduct()->getPrice(false, $this->getCart()->getPriceGroup())));
  116. }
  117. public function getDiscount() {
  118. return $this->getUnitPrice(false) - $this->getProduct()->getPrice(false, $this->getCart()->getPriceGroup());
  119. }
  120. public function getPercentDiscount() {
  121. return round($this->getDiscount(false) / $this->getProduct()->getPrice(false, $this->getCart()->getPriceGroup()),2)*100;
  122. }
  123. public function getTotalPrice() {
  124. return $this->totalPrice;
  125. }
  126. public function getMarketingRule(): ?MarketingRule {
  127. return $this->marketingRule;
  128. }
  129. public function getDiscountType(): ?string {
  130. return $this->discountType;
  131. }
  132. public function getQuantityOffered() {
  133. return $this->quantityOffered;
  134. }
  135. public function setId($id) {
  136. $this->id = $id;
  137. }
  138. public function setCart(\App\Entity\Cart $cart): void {
  139. $this->cart = $cart;
  140. }
  141. public function setQuantity($quantity) {
  142. $this->quantity = $quantity;
  143. }
  144. public function setUnitPrice($finalPrice) {
  145. $this->unitPrice = $finalPrice;
  146. }
  147. public function setCreatedAt($createdAt) {
  148. $this->createdAt = $createdAt;
  149. }
  150. public function setUpdatedAt(?\DateTime $updatedAt): void {
  151. $this->updatedAt = $updatedAt;
  152. }
  153. public function setProduct(\App\Entity\Product $product) {
  154. $this->product = $product;
  155. }
  156. public function setReminded(\DateTime $date) {
  157. $this->reminded = $date;
  158. }
  159. public function setTotalPrice($totalPrice): void {
  160. $this->totalPrice = $totalPrice;
  161. }
  162. public function setMarketingRule(?MarketingRule $marketingRule): void {
  163. $this->marketingRule = $marketingRule;
  164. }
  165. public function setDiscountType(?string $discountType): void {
  166. if($discountType == 'soleil'){
  167. $this->discountType = $this->product->isNonSoleil() ? 'non-soleil' : $discountType;
  168. }else{
  169. $this->discountType = $discountType;
  170. }
  171. }
  172. public function setQuantityOffered($quantityOffered): void {
  173. $this->quantityOffered = $quantityOffered;
  174. }
  175. public function toArray()
  176. {
  177. return [
  178. 'id'=>$this->getProduct()->getId(),
  179. 'name'=>$this->getProduct()->getName(),
  180. 'reference'=>$this->getProduct()->getModel(),
  181. 'unitPrice'=>$this->getUnitPrice(),
  182. 'normalUnitPrice'=>$this->getProduct()->getPrice(false, $this->getCart()->getPriceGroup()),
  183. 'price'=>$this->getPrice(false, $this->getCart()->getPriceGroup()),
  184. 'normalPrice'=>$this->getNormalPrice(),
  185. 'totalPrice'=>$this->getTotalPrice(),
  186. 'quantity'=>$this->getQuantity(),
  187. 'product'=>$this->getProduct()->toArray(),
  188. ];
  189. }
  190. /**
  191. * @ORM\PostLoad
  192. */
  193. public function postLoad(\Doctrine\ORM\Event\LifecycleEventArgs $arg) {
  194. }
  195. /**
  196. * @ORM\PrePersist()
  197. */
  198. public function prePersist(\Doctrine\ORM\Event\LifecycleEventArgs $args) {
  199. $this->setCreatedAt(new \DateTime());
  200. }
  201. /**
  202. * @ORM\PreUpdate()
  203. */
  204. public function preUpdate(\Doctrine\ORM\Event\LifecycleEventArgs $args) {
  205. $this->setUpdatedAt(new \DateTime());
  206. }
  207. }