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. /**
  79. * @ORM\Column(type="boolean", options={"default": false})
  80. */
  81. private bool $isExpressDelivery = false;
  82. use DateTrait;
  83. public function __construct()
  84. {
  85. $this->items = new ArrayCollection();
  86. $this->paidPointTransactions = new ArrayCollection();
  87. $this->pointTransactions = new ArrayCollection();
  88. }
  89. public function __toString()
  90. {
  91. return 'Panier '.$this->getId();
  92. }
  93. /*
  94. * ============================================================================================
  95. * =============================== FONCTIONS CUSTOM ===========================================
  96. * ============================================================================================
  97. */
  98. public function getReference(): string
  99. {
  100. $id = strval($this->getId());
  101. return str_pad($id, 8, '0', STR_PAD_LEFT);
  102. }
  103. // ITEMS
  104. public function getId(): ?int
  105. {
  106. return $this->id;
  107. }
  108. // @TODO check product, dans la 2.8 ca ne fct pas non plus
  109. // public function getTotalHt()
  110. // {
  111. // $total = NULL;
  112. // /* @var $item CartItem */
  113. // foreach ( $this->items as $item ) {
  114. // $total += $item->getProduct()->getPriceHT() * $item->getQuantity();
  115. // }
  116. // return $total;
  117. // }
  118. //
  119. //
  120. // public function getTotalTTC()
  121. // {
  122. // $total = NULL;
  123. // /** @var CartItem $item */
  124. // foreach ( $this->items as $item ) {
  125. // $total += $item->getProduct()->getPriceTTC() * $item->getQuantity();
  126. // }
  127. // return $total;
  128. // }
  129. public function getItemsCount(): int
  130. {
  131. return count($this->items);
  132. }
  133. /**
  134. * @param ProductInterface $product
  135. *
  136. * @return CartItem|false
  137. */
  138. public function getCartItemByProduct(ProductInterface $product)
  139. {
  140. foreach ($this->getItems() as $item) {
  141. if ($item->getSku() == $product->getSku()) {
  142. return $item;
  143. }
  144. }
  145. return FALSE;
  146. }
  147. /**
  148. * @return Collection|CartItem[]
  149. */
  150. public function getItems(): Collection
  151. {
  152. return $this->items;
  153. }
  154. public function getAddressType(Address $address): string
  155. {
  156. if ($this->billingAddress == $address) {
  157. return Address::TYPE_BILLING_ADDRESS;
  158. } elseif ($this->shippingAddress == $address) {
  159. return Address::TYPE_SHIPPING_ADDRESS;
  160. }
  161. return Address::TYPE_BILLING_ADDRESS;
  162. }
  163. /*
  164. * ============================================================================================
  165. * ============================== FIN FONCTIONS CUSTOM ========================================
  166. * ============================================================================================
  167. */
  168. public function getCartItemById($id)
  169. {
  170. foreach ($this->getItems() as $item) {
  171. if ($item->getId() == $id) {
  172. return $item;
  173. }
  174. }
  175. return FALSE;
  176. }
  177. public function totalQuantity()
  178. {
  179. $totalQuantity = 0;
  180. foreach ($this->getItems() as $item) {
  181. $totalQuantity += $item->getQuantity();
  182. }
  183. return $totalQuantity;
  184. }
  185. public function getShippingMethod(): ?string
  186. {
  187. return $this->shippingMethod;
  188. }
  189. public function setShippingMethod(?string $shippingMethod): self
  190. {
  191. $this->shippingMethod = $shippingMethod;
  192. return $this;
  193. }
  194. public function getSerialCookie(): ?string
  195. {
  196. return $this->serialCookie;
  197. }
  198. public function setSerialCookie(string $serialCookie): self
  199. {
  200. $this->serialCookie = $serialCookie;
  201. return $this;
  202. }
  203. public function getSameAddressBilling(): ?bool
  204. {
  205. return $this->sameAddressBilling;
  206. }
  207. public function setSameAddressBilling(bool $sameAddressBilling): self
  208. {
  209. $this->sameAddressBilling = $sameAddressBilling;
  210. return $this;
  211. }
  212. public function getShippingDelay(): ?int
  213. {
  214. return $this->shippingDelay;
  215. }
  216. public function setShippingDelay(?int $shippingDelay): self
  217. {
  218. $this->shippingDelay = $shippingDelay;
  219. return $this;
  220. }
  221. public function getUser(): ?User
  222. {
  223. return $this->user;
  224. }
  225. public function setUser(?User $user): self
  226. {
  227. $this->user = $user;
  228. return $this;
  229. }
  230. public function getMultiShipping(): ?bool
  231. {
  232. return $this->multiShipping;
  233. }
  234. public function setMultiShipping(bool $multiShipping): self
  235. {
  236. $this->multiShipping = $multiShipping;
  237. return $this;
  238. }
  239. public function getBonusTotalUsed(): ?string
  240. {
  241. return $this->bonusTotalUsed;
  242. }
  243. public function setBonusTotalUsed(?string $bonusTotalUsed): self
  244. {
  245. $this->bonusTotalUsed = $bonusTotalUsed;
  246. return $this;
  247. }
  248. public function getShippingAddress(): ?CartAddress
  249. {
  250. return $this->shippingAddress;
  251. }
  252. public function setShippingAddress(?CartAddress $shippingAddress): self
  253. {
  254. $this->shippingAddress = $shippingAddress;
  255. return $this;
  256. }
  257. public function getBillingAddress(): ?CartAddress
  258. {
  259. return $this->billingAddress;
  260. }
  261. public function setBillingAddress(?CartAddress $billingAddress): self
  262. {
  263. $this->billingAddress = $billingAddress;
  264. return $this;
  265. }
  266. public function addItem(CartItem $item): self
  267. {
  268. if (!$this->items->contains($item)) {
  269. $this->items[] = $item;
  270. $item->setCart($this);
  271. }
  272. return $this;
  273. }
  274. public function removeItem(CartItem $item): self
  275. {
  276. if ($this->items->removeElement($item)) {
  277. // set the owning side to null (unless already changed)
  278. if ($item->getCart() === $this) {
  279. $item->setCart(NULL);
  280. }
  281. }
  282. return $this;
  283. }
  284. public function getSaleOrder(): ?SaleOrder
  285. {
  286. return $this->saleOrder;
  287. }
  288. public function setSaleOrder(?SaleOrder $saleOrder): self
  289. {
  290. // unset the owning side of the relation if necessary
  291. if ($saleOrder === NULL && $this->saleOrder !== NULL) {
  292. $this->saleOrder->setCart(NULL);
  293. }
  294. // set the owning side of the relation if necessary
  295. if ($saleOrder !== NULL && $saleOrder->getCart() !== $this) {
  296. $saleOrder->setCart($this);
  297. }
  298. $this->saleOrder = $saleOrder;
  299. return $this;
  300. }
  301. /**
  302. * @return Collection|PointTransaction[]
  303. */
  304. public function getAllPointTransactions(): Collection
  305. {
  306. $all = $this->pointTransactions;
  307. foreach($this->paidPointTransactions as $transaction)
  308. {
  309. if($all->contains($transaction)) continue;
  310. $all->add($transaction);
  311. }
  312. return $all;
  313. }
  314. /**
  315. * @return Collection|PointTransaction[]
  316. */
  317. public function getPaidPointTransactions(): Collection
  318. {
  319. return $this->paidPointTransactions;
  320. }
  321. /**
  322. * @param PointTransaction $transaction
  323. *
  324. * @return $this
  325. */
  326. public function addPaidPointTransaction(PointTransaction $transaction): self
  327. {
  328. if(!$this->paidPointTransactions->contains($transaction))
  329. {
  330. $this->paidPointTransactions[] = $transaction;
  331. if($transaction->getPaidCart() !== $this) $transaction->setPaidCart($this);
  332. }
  333. return $this;
  334. }
  335. /**
  336. * @param PointTransaction $transaction
  337. *
  338. * @return $this
  339. */
  340. public function removePaidPointTransaction(PointTransaction $transaction): self
  341. {
  342. if($this->paidPointTransactions->removeElement($transaction) && $transaction->getPaidCart() === $this)
  343. {
  344. $transaction->setPaidCart();
  345. }
  346. return $this;
  347. }
  348. /**
  349. * @param iterable|PointTransaction[] $transactions
  350. *
  351. * @return $this
  352. */
  353. public function setPaidPointTransactions(iterable $transactions): self
  354. {
  355. foreach($this->paidPointTransactions as $transaction)
  356. {
  357. $this->removePaidPointTransaction($transaction);
  358. }
  359. foreach($transactions as $transaction)
  360. {
  361. $this->addPaidPointTransaction($transaction);
  362. }
  363. return $this;
  364. }
  365. /**
  366. * @param bool $excludeValids
  367. * @param bool $onlyValids
  368. * @param string|null $onlyValids
  369. *
  370. * @return float
  371. */
  372. public function getTotalPaidPointTransactions(bool $excludeValids = false, bool $onlyValids = false, ?string $pointCategory = null): float
  373. {
  374. $points = 0;
  375. foreach($this->getPaidPointTransactions() as $transaction)
  376. {
  377. if($excludeValids && $transaction->getValue() != 0) continue;
  378. if($onlyValids && $transaction->getValue() == 0) continue;
  379. if ($pointCategory !== null && $transaction->getCategory() !== $pointCategory) continue;
  380. $points += $transaction->getPaymentValue();
  381. }
  382. return round($points, 2);
  383. }
  384. /**
  385. * @return Collection|PointTransaction[]
  386. */
  387. public function getPointTransactions(): Collection
  388. {
  389. return $this->pointTransactions;
  390. }
  391. /**
  392. * @param PointTransaction $transaction
  393. *
  394. * @return $this
  395. */
  396. public function addPointTransaction(PointTransaction $transaction): self
  397. {
  398. if(!$this->pointTransactions->contains($transaction))
  399. {
  400. $this->pointTransactions[] = $transaction;
  401. if($transaction->getCart() !== $this) $transaction->setCart($this);
  402. }
  403. return $this;
  404. }
  405. /**
  406. * @param PointTransaction $transaction
  407. *
  408. * @return $this
  409. */
  410. public function removePointTransaction(PointTransaction $transaction): self
  411. {
  412. if($this->pointTransactions->removeElement($transaction) && $transaction->getCart() === $this)
  413. {
  414. $transaction->setCart(null);
  415. }
  416. return $this;
  417. }
  418. /**
  419. * @param iterable|PointTransaction[] $transactions
  420. *
  421. * @return $this
  422. */
  423. public function setPointTransactions(iterable $transactions): self
  424. {
  425. foreach($this->pointTransactions as $transaction)
  426. {
  427. $this->removePointTransaction($transaction);
  428. }
  429. foreach($transactions as $transaction)
  430. {
  431. $this->addPointTransaction($transaction);
  432. }
  433. return $this;
  434. }
  435. public function isExpressDelivery(): ?bool
  436. {
  437. return $this->isExpressDelivery;
  438. }
  439. public function setExpressDelivery(bool $isExpressDelivery): self
  440. {
  441. $this->isExpressDelivery = $isExpressDelivery;
  442. return $this;
  443. }
  444. }