src/Entity/CartItem.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CartItemRepository;
  4. use App\Traits\DateTrait;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. /**
  10. * @ORM\Entity(repositoryClass=CartItemRepository::class)
  11. */
  12. class CartItem
  13. {
  14. /**
  15. * @ORM\Id
  16. * @ORM\GeneratedValue
  17. * @ORM\Column(type="integer")
  18. */
  19. private $id;
  20. /**
  21. * SKU : Identifiant d'unité de stock
  22. * @ORM\Column(type="string", length=255)
  23. */
  24. private $sku;
  25. /**
  26. * @ORM\Column(type="integer")
  27. */
  28. private $quantity;
  29. /**
  30. * @ORM\ManyToOne(targetEntity=Cart::class, inversedBy="items")
  31. * @ORM\JoinColumn(nullable=false, referencedColumnName="id")
  32. */
  33. private $cart;
  34. /**
  35. * @ORM\OneToMany(targetEntity=CartItemOption::class, mappedBy="item",cascade={ "remove"})
  36. */
  37. private $options;
  38. /**
  39. * @ORM\OneToMany(targetEntity=CartItemShipping::class, mappedBy="item", cascade={"persist", "remove"})
  40. * @ORM\JoinColumn(nullable=true)
  41. */
  42. private $shipments;
  43. /**
  44. * @ORM\OneToMany(targetEntity=CartItemParticipant::class, mappedBy="cartItem", cascade={"persist","remove"})
  45. * @ORM\JoinColumn(nullable=true)
  46. */
  47. private $participants;
  48. /**
  49. * @ORM\Column(type="datetime", nullable=true)
  50. */
  51. private $wishDateEvasion;
  52. /**
  53. * provenance du produit
  54. * @ORM\Column(type="string", length=255)
  55. */
  56. private ?string $catalogue;
  57. use DateTrait;
  58. public function __construct()
  59. {
  60. $this->options = new ArrayCollection();
  61. $this->shipments = new ArrayCollection();
  62. $this->participants = new ArrayCollection();
  63. }
  64. /*
  65. * ============================================================================================
  66. * =============================== FONCTIONS CUSTOM ===========================================
  67. * ============================================================================================
  68. */
  69. /**
  70. * @Assert\IsFalse(message = "Les quantités sont incorrectes, merci de rectifier")
  71. * @return boolean
  72. */
  73. public function isQuantityByItemShipping(): bool
  74. {
  75. $quantity = 0;
  76. foreach ( $this->getShipments() as $shipment ) {
  77. if ( $shipment instanceof CartItemShipping ) {
  78. $quantity += $shipment->getQuantity();
  79. }
  80. }
  81. if ( $quantity <= $this->getQuantity() ) {
  82. return FALSE;
  83. }
  84. return TRUE;
  85. }
  86. /**
  87. * @return Collection|CartItemShipping[]
  88. */
  89. public function getShipments(): Collection
  90. {
  91. return $this->shipments;
  92. }
  93. public function getQuantity(): ?int
  94. {
  95. return $this->quantity;
  96. }
  97. /*
  98. * ============================================================================================
  99. * ============================== FIN FONCTIONS CUSTOM ========================================
  100. * ============================================================================================
  101. */
  102. public function setQuantity( int $quantity ): self
  103. {
  104. $this->quantity = $quantity;
  105. return $this;
  106. }
  107. public function getInitItemShipping()
  108. {
  109. foreach ( $this->getShipments() as $shipment ) {
  110. if ( $shipment instanceof CartItemShipping && $shipment->getInit() ) {
  111. return $shipment;
  112. }
  113. }
  114. return NULL;
  115. }
  116. public function getItemsINotShipping(): bool
  117. {
  118. foreach ( $this->getShipments() as $shipment ) {
  119. if ( $shipment instanceof CartItemShipping && NULL === $shipment->getShippingAddress() ) {
  120. return TRUE;
  121. }
  122. }
  123. return FALSE;
  124. }
  125. public function getId(): ?int
  126. {
  127. return $this->id;
  128. }
  129. public function getCatalogue(): ?string
  130. {
  131. return $this->catalogue;
  132. }
  133. public function setCatalogue( string $catalogue ): self
  134. {
  135. $this->catalogue = $catalogue;
  136. return $this;
  137. }
  138. public function getSku(): ?string
  139. {
  140. return $this->sku;
  141. }
  142. public function setSku( string $sku ): self
  143. {
  144. $this->sku = $sku;
  145. return $this;
  146. }
  147. public function getCart(): ?Cart
  148. {
  149. return $this->cart;
  150. }
  151. public function setCart( ?Cart $cart ): self
  152. {
  153. $this->cart = $cart;
  154. return $this;
  155. }
  156. /**
  157. * @return Collection|CartItemOption[]
  158. */
  159. public function getOptions(): Collection
  160. {
  161. return $this->options;
  162. }
  163. public function addOption( CartItemOption $option ): self
  164. {
  165. if ( !$this->options->contains( $option ) ) {
  166. $this->options[] = $option;
  167. $option->setItem( $this );
  168. }
  169. return $this;
  170. }
  171. public function removeOption( CartItemOption $option ): self
  172. {
  173. if ( $this->options->removeElement( $option ) ) {
  174. // set the owning side to null (unless already changed)
  175. if ( $option->getItem() === $this ) {
  176. $option->setItem( NULL );
  177. }
  178. }
  179. return $this;
  180. }
  181. public function addQuantity( $quantity ): CartItem
  182. {
  183. if ( is_null( $this->quantity ) ) {
  184. $this->quantity = 0;
  185. }
  186. $this->quantity += $quantity;
  187. return $this;
  188. }
  189. public function editQuantity( $quantity ): CartItem
  190. {
  191. $this->quantity = $quantity;
  192. return $this;
  193. }
  194. public function removeQuantity( $quantity ): CartItem
  195. {
  196. $this->quantity -= $quantity;
  197. if ( $this->quantity < 0 ) {
  198. $this->quantity = 0;
  199. }
  200. return $this;
  201. }
  202. public function addShipment( CartItemShipping $shipment ): self
  203. {
  204. if ( !$this->shipments->contains( $shipment ) ) {
  205. $this->shipments[] = $shipment;
  206. $shipment->setItem( $this );
  207. }
  208. return $this;
  209. }
  210. public function removeShipment( CartItemShipping $shipment ): self
  211. {
  212. if ( $this->shipments->removeElement( $shipment ) ) {
  213. // set the owning side to null (unless already changed)
  214. if ( $shipment->getItem() === $this ) {
  215. $shipment->setItem( NULL );
  216. }
  217. }
  218. return $this;
  219. }
  220. /**
  221. * @return Collection|CartItemParticipant[]
  222. */
  223. public function getParticipants(): Collection
  224. {
  225. return $this->participants;
  226. }
  227. public function addParticipant( CartItemParticipant $participant ): self
  228. {
  229. if ( !$this->participants->contains( $participant ) ) {
  230. $this->participants[] = $participant;
  231. $participant->setCartItem( $this );
  232. }
  233. return $this;
  234. }
  235. public function removeParticipant( CartItemParticipant $participant ): self
  236. {
  237. if ( $this->participants->removeElement( $participant ) ) {
  238. // set the owning side to null (unless already changed)
  239. if ( $participant->getCartItem() === $this ) {
  240. $participant->setCartItem( NULL );
  241. }
  242. }
  243. return $this;
  244. }
  245. public function getWishDateEvasion()
  246. {
  247. return $this->wishDateEvasion;
  248. }
  249. public function setWishDateEvasion($wishDateEvasion)
  250. {
  251. $this->wishDateEvasion = $wishDateEvasion;
  252. }
  253. }