src/Entity/Cart.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Interfaces\ProductInterface;
  4. use App\Model\CartInfo;
  5. use App\Repository\CartRepository;
  6. use App\Traits\DateTrait;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. /**
  11. * @ORM\Table(indexes={
  12. * @ORM\Index(columns={"serial_cookie"})
  13. * })
  14. * @ORM\Entity(repositoryClass=CartRepository::class)
  15. */
  16. class Cart
  17. {
  18. /**
  19. * @ORM\Id
  20. * @ORM\GeneratedValue
  21. * @ORM\Column(type="integer")
  22. */
  23. private ?int $id;
  24. /**
  25. * @ORM\Column(type="string", length=64, nullable=true)
  26. */
  27. private ?string $shippingMethod;
  28. /**
  29. * @ORM\Column(type="string", length=64)
  30. */
  31. private ?string $serialCookie;
  32. /**
  33. * @ORM\Column(type="boolean", options={"default": true})
  34. */
  35. private bool $sameAddressBilling = TRUE;
  36. /**
  37. * @ORM\Column(type="integer", nullable=true)
  38. */
  39. private ?int $shippingDelay;
  40. /**
  41. * @ORM\ManyToOne(targetEntity=User::class, inversedBy="carts", cascade={"persist"})
  42. * @ORM\JoinColumn(nullable=true)
  43. */
  44. private ?User $user;
  45. /**
  46. * @ORM\Column(type="boolean", options={"default": false})
  47. */
  48. private bool $multiShipping = FALSE;
  49. /**
  50. * @ORM\Column(type="decimal", precision=12, scale=4, nullable=true)
  51. */
  52. private ?string $bonusTotalUsed;
  53. /**
  54. * @ORM\OneToOne(targetEntity=CartAddress::class, cascade={"persist", "remove"}, orphanRemoval=true)
  55. */
  56. private ?CartAddress $shippingAddress;
  57. /**
  58. * @ORM\OneToOne(targetEntity=CartAddress::class, cascade={"persist", "remove"}, orphanRemoval=true)
  59. */
  60. private ?CartAddress $billingAddress;
  61. /**
  62. * @ORM\OneToMany(targetEntity=CartItem::class, mappedBy="cart", cascade={"remove"})
  63. */
  64. private Collection $items;
  65. /**
  66. * @ORM\OneToOne(targetEntity=SaleOrder::class, mappedBy="cart", cascade={"persist", "remove"})
  67. */
  68. private ?SaleOrder $saleOrder;
  69. /**
  70. * @ORM\OneToMany(targetEntity=PointTransaction::class, mappedBy="paidCart")
  71. */
  72. private Collection $paidPointTransactions;
  73. /**
  74. * @ORM\OneToMany(targetEntity=PointTransaction::class, mappedBy="cart")
  75. */
  76. private Collection $pointTransactions;
  77. public ?CartInfo $cartInfo = null;
  78. use DateTrait;
  79. public function __construct()
  80. {
  81. $this->items = new ArrayCollection();
  82. $this->paidPointTransactions = new ArrayCollection();
  83. $this->pointTransactions = new ArrayCollection();
  84. }
  85. public function __toString()
  86. {
  87. return 'Panier '.$this->getId();
  88. }
  89. /*
  90. * ============================================================================================
  91. * =============================== FONCTIONS CUSTOM ===========================================
  92. * ============================================================================================
  93. */
  94. public function getReference(): string
  95. {
  96. $id = strval($this->getId());
  97. return str_pad($id, 8, '0', STR_PAD_LEFT);
  98. }
  99. // ITEMS
  100. public function getId(): ?int
  101. {
  102. return $this->id;
  103. }
  104. // @TODO check product, dans la 2.8 ca ne fct pas non plus
  105. // public function getTotalHt()
  106. // {
  107. // $total = NULL;
  108. // /* @var $item CartItem */
  109. // foreach ( $this->items as $item ) {
  110. // $total += $item->getProduct()->getPriceHT() * $item->getQuantity();
  111. // }
  112. // return $total;
  113. // }
  114. //
  115. //
  116. // public function getTotalTTC()
  117. // {
  118. // $total = NULL;
  119. // /** @var CartItem $item */
  120. // foreach ( $this->items as $item ) {
  121. // $total += $item->getProduct()->getPriceTTC() * $item->getQuantity();
  122. // }
  123. // return $total;
  124. // }
  125. public function getItemsCount(): int
  126. {
  127. return count($this->items);
  128. }
  129. /**
  130. * @param ProductInterface $product
  131. *
  132. * @return CartItem|false
  133. */
  134. public function getCartItemByProduct(ProductInterface $product)
  135. {
  136. foreach ($this->getItems() as $item) {
  137. if ($item->getSku() == $product->getSku()) {
  138. return $item;
  139. }
  140. }
  141. return FALSE;
  142. }
  143. /**
  144. * @return Collection|CartItem[]
  145. */
  146. public function getItems(): Collection
  147. {
  148. return $this->items;
  149. }
  150. public function getAddressType(Address $address): string
  151. {
  152. if ($this->billingAddress == $address) {
  153. return Address::TYPE_BILLING_ADDRESS;
  154. } elseif ($this->shippingAddress == $address) {
  155. return Address::TYPE_SHIPPING_ADDRESS;
  156. }
  157. return Address::TYPE_BILLING_ADDRESS;
  158. }
  159. /*
  160. * ============================================================================================
  161. * ============================== FIN FONCTIONS CUSTOM ========================================
  162. * ============================================================================================
  163. */
  164. public function getCartItemById($id)
  165. {
  166. foreach ($this->getItems() as $item) {
  167. if ($item->getId() == $id) {
  168. return $item;
  169. }
  170. }
  171. return FALSE;
  172. }
  173. public function totalQuantity()
  174. {
  175. $totalQuantity = 0;
  176. foreach ($this->getItems() as $item) {
  177. $totalQuantity += $item->getQuantity();
  178. }
  179. return $totalQuantity;
  180. }
  181. public function getShippingMethod(): ?string
  182. {
  183. return $this->shippingMethod;
  184. }
  185. public function setShippingMethod(?string $shippingMethod): self
  186. {
  187. $this->shippingMethod = $shippingMethod;
  188. return $this;
  189. }
  190. public function getSerialCookie(): ?string
  191. {
  192. return $this->serialCookie;
  193. }
  194. public function setSerialCookie(string $serialCookie): self
  195. {
  196. $this->serialCookie = $serialCookie;
  197. return $this;
  198. }
  199. public function getSameAddressBilling(): ?bool
  200. {
  201. return $this->sameAddressBilling;
  202. }
  203. public function setSameAddressBilling(bool $sameAddressBilling): self
  204. {
  205. $this->sameAddressBilling = $sameAddressBilling;
  206. return $this;
  207. }
  208. public function getShippingDelay(): ?int
  209. {
  210. return $this->shippingDelay;
  211. }
  212. public function setShippingDelay(?int $shippingDelay): self
  213. {
  214. $this->shippingDelay = $shippingDelay;
  215. return $this;
  216. }
  217. public function getUser(): ?User
  218. {
  219. return $this->user;
  220. }
  221. public function setUser(?User $user): self
  222. {
  223. $this->user = $user;
  224. return $this;
  225. }
  226. public function getMultiShipping(): ?bool
  227. {
  228. return $this->multiShipping;
  229. }
  230. public function setMultiShipping(bool $multiShipping): self
  231. {
  232. $this->multiShipping = $multiShipping;
  233. return $this;
  234. }
  235. public function getBonusTotalUsed(): ?string
  236. {
  237. return $this->bonusTotalUsed;
  238. }
  239. public function setBonusTotalUsed(?string $bonusTotalUsed): self
  240. {
  241. $this->bonusTotalUsed = $bonusTotalUsed;
  242. return $this;
  243. }
  244. public function getShippingAddress(): ?CartAddress
  245. {
  246. return $this->shippingAddress;
  247. }
  248. public function setShippingAddress(?CartAddress $shippingAddress): self
  249. {
  250. $this->shippingAddress = $shippingAddress;
  251. return $this;
  252. }
  253. public function getBillingAddress(): ?CartAddress
  254. {
  255. return $this->billingAddress;
  256. }
  257. public function setBillingAddress(?CartAddress $billingAddress): self
  258. {
  259. $this->billingAddress = $billingAddress;
  260. return $this;
  261. }
  262. public function addItem(CartItem $item): self
  263. {
  264. if (!$this->items->contains($item)) {
  265. $this->items[] = $item;
  266. $item->setCart($this);
  267. }
  268. return $this;
  269. }
  270. public function removeItem(CartItem $item): self
  271. {
  272. if ($this->items->removeElement($item)) {
  273. // set the owning side to null (unless already changed)
  274. if ($item->getCart() === $this) {
  275. $item->setCart(NULL);
  276. }
  277. }
  278. return $this;
  279. }
  280. public function getSaleOrder(): ?SaleOrder
  281. {
  282. return $this->saleOrder;
  283. }
  284. public function setSaleOrder(?SaleOrder $saleOrder): self
  285. {
  286. // unset the owning side of the relation if necessary
  287. if ($saleOrder === NULL && $this->saleOrder !== NULL) {
  288. $this->saleOrder->setCart(NULL);
  289. }
  290. // set the owning side of the relation if necessary
  291. if ($saleOrder !== NULL && $saleOrder->getCart() !== $this) {
  292. $saleOrder->setCart($this);
  293. }
  294. $this->saleOrder = $saleOrder;
  295. return $this;
  296. }
  297. /**
  298. * @return Collection|PointTransaction[]
  299. */
  300. public function getAllPointTransactions(): Collection
  301. {
  302. $all = $this->pointTransactions;
  303. foreach($this->paidPointTransactions as $transaction)
  304. {
  305. if($all->contains($transaction)) continue;
  306. $all->add($transaction);
  307. }
  308. return $all;
  309. }
  310. /**
  311. * @return Collection|PointTransaction[]
  312. */
  313. public function getPaidPointTransactions(): Collection
  314. {
  315. return $this->paidPointTransactions;
  316. }
  317. /**
  318. * @param PointTransaction $transaction
  319. *
  320. * @return $this
  321. */
  322. public function addPaidPointTransaction(PointTransaction $transaction): self
  323. {
  324. if(!$this->paidPointTransactions->contains($transaction))
  325. {
  326. $this->paidPointTransactions[] = $transaction;
  327. if($transaction->getPaidCart() !== $this) $transaction->setPaidCart($this);
  328. }
  329. return $this;
  330. }
  331. /**
  332. * @param PointTransaction $transaction
  333. *
  334. * @return $this
  335. */
  336. public function removePaidPointTransaction(PointTransaction $transaction): self
  337. {
  338. if($this->paidPointTransactions->removeElement($transaction) && $transaction->getPaidCart() === $this)
  339. {
  340. $transaction->setPaidCart();
  341. }
  342. return $this;
  343. }
  344. /**
  345. * @param iterable|PointTransaction[] $transactions
  346. *
  347. * @return $this
  348. */
  349. public function setPaidPointTransactions(iterable $transactions): self
  350. {
  351. foreach($this->paidPointTransactions as $transaction)
  352. {
  353. $this->removePaidPointTransaction($transaction);
  354. }
  355. foreach($transactions as $transaction)
  356. {
  357. $this->addPaidPointTransaction($transaction);
  358. }
  359. return $this;
  360. }
  361. /**
  362. * @param bool $excludeValids
  363. * @param bool $onlyValids
  364. * @param string|null $onlyValids
  365. *
  366. * @return float
  367. */
  368. public function getTotalPaidPointTransactions(bool $excludeValids = false, bool $onlyValids = false, ?string $pointCategory = null): float
  369. {
  370. $points = 0;
  371. foreach($this->getPaidPointTransactions() as $transaction)
  372. {
  373. if($excludeValids && $transaction->getValue() != 0) continue;
  374. if($onlyValids && $transaction->getValue() == 0) continue;
  375. if ($pointCategory !== null && $transaction->getCategory() !== $pointCategory) continue;
  376. $points += $transaction->getPaymentValue();
  377. }
  378. return round($points, 2);
  379. }
  380. /**
  381. * @return Collection|PointTransaction[]
  382. */
  383. public function getPointTransactions(): Collection
  384. {
  385. return $this->pointTransactions;
  386. }
  387. /**
  388. * @param PointTransaction $transaction
  389. *
  390. * @return $this
  391. */
  392. public function addPointTransaction(PointTransaction $transaction): self
  393. {
  394. if(!$this->pointTransactions->contains($transaction))
  395. {
  396. $this->pointTransactions[] = $transaction;
  397. if($transaction->getCart() !== $this) $transaction->setCart($this);
  398. }
  399. return $this;
  400. }
  401. /**
  402. * @param PointTransaction $transaction
  403. *
  404. * @return $this
  405. */
  406. public function removePointTransaction(PointTransaction $transaction): self
  407. {
  408. if($this->pointTransactions->removeElement($transaction) && $transaction->getCart() === $this)
  409. {
  410. $transaction->setCart(null);
  411. }
  412. return $this;
  413. }
  414. /**
  415. * @param iterable|PointTransaction[] $transactions
  416. *
  417. * @return $this
  418. */
  419. public function setPointTransactions(iterable $transactions): self
  420. {
  421. foreach($this->pointTransactions as $transaction)
  422. {
  423. $this->removePointTransaction($transaction);
  424. }
  425. foreach($transactions as $transaction)
  426. {
  427. $this->addPointTransaction($transaction);
  428. }
  429. return $this;
  430. }
  431. }