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 = null;
  24. /**
  25. * @ORM\Column(type="string", length=64, nullable=true)
  26. */
  27. private ?string $shippingMethod = null;
  28. /**
  29. * @ORM\Column(type="string", length=64)
  30. */
  31. private ?string $serialCookie = null;
  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 = null;
  40. /**
  41. * @ORM\ManyToOne(targetEntity=User::class, inversedBy="carts", cascade={"persist"})
  42. * @ORM\JoinColumn(nullable=true)
  43. */
  44. private ?User $user = null;
  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 = null;
  53. /**
  54. * @ORM\OneToOne(targetEntity=CartAddress::class, cascade={"persist", "remove"}, orphanRemoval=true)
  55. */
  56. private ?CartAddress $shippingAddress = null;
  57. /**
  58. * @ORM\OneToOne(targetEntity=CartAddress::class, cascade={"persist", "remove"}, orphanRemoval=true)
  59. */
  60. private ?CartAddress $billingAddress = null;
  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 = null;
  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. if ($all->contains($transaction)) {
  309. continue;
  310. }
  311. $all->add($transaction);
  312. }
  313. return $all;
  314. }
  315. /**
  316. * @return Collection|PointTransaction[]
  317. */
  318. public function getPaidPointTransactions(): Collection
  319. {
  320. return $this->paidPointTransactions;
  321. }
  322. /**
  323. * @param PointTransaction $transaction
  324. *
  325. * @return $this
  326. */
  327. public function addPaidPointTransaction(PointTransaction $transaction): self
  328. {
  329. if (!$this->paidPointTransactions->contains($transaction)) {
  330. $this->paidPointTransactions[] = $transaction;
  331. if ($transaction->getPaidCart() !== $this) {
  332. $transaction->setPaidCart($this);
  333. }
  334. }
  335. return $this;
  336. }
  337. /**
  338. * @param PointTransaction $transaction
  339. *
  340. * @return $this
  341. */
  342. public function removePaidPointTransaction(PointTransaction $transaction): self
  343. {
  344. if ($this->paidPointTransactions->removeElement($transaction) && $transaction->getPaidCart() === $this) {
  345. $transaction->setPaidCart();
  346. }
  347. return $this;
  348. }
  349. /**
  350. * @param iterable|PointTransaction[] $transactions
  351. *
  352. * @return $this
  353. */
  354. public function setPaidPointTransactions(iterable $transactions): self
  355. {
  356. foreach ($this->paidPointTransactions as $transaction) {
  357. $this->removePaidPointTransaction($transaction);
  358. }
  359. foreach ($transactions as $transaction) {
  360. $this->addPaidPointTransaction($transaction);
  361. }
  362. return $this;
  363. }
  364. /**
  365. * @param bool $excludeValids
  366. * @param bool $onlyValids
  367. * @param string|null $onlyValids
  368. *
  369. * @return float
  370. */
  371. public function getTotalPaidPointTransactions(
  372. bool $excludeValids = false,
  373. bool $onlyValids = false,
  374. ?string $pointCategory = null
  375. ): float {
  376. $points = 0;
  377. foreach ($this->getPaidPointTransactions() as $transaction) {
  378. if ($excludeValids && $transaction->getValue() != 0) {
  379. continue;
  380. }
  381. if ($onlyValids && $transaction->getValue() == 0) {
  382. continue;
  383. }
  384. if ($pointCategory !== null && $transaction->getCategory() !== $pointCategory) {
  385. continue;
  386. }
  387. $points += $transaction->getPaymentValue();
  388. }
  389. return round($points, 2);
  390. }
  391. /**
  392. * @return Collection|PointTransaction[]
  393. */
  394. public function getPointTransactions(): Collection
  395. {
  396. return $this->pointTransactions;
  397. }
  398. /**
  399. * @param PointTransaction $transaction
  400. *
  401. * @return $this
  402. */
  403. public function addPointTransaction(PointTransaction $transaction): self
  404. {
  405. if (!$this->pointTransactions->contains($transaction)) {
  406. $this->pointTransactions[] = $transaction;
  407. if ($transaction->getCart() !== $this) {
  408. $transaction->setCart($this);
  409. }
  410. }
  411. return $this;
  412. }
  413. /**
  414. * @param PointTransaction $transaction
  415. *
  416. * @return $this
  417. */
  418. public function removePointTransaction(PointTransaction $transaction): self
  419. {
  420. if ($this->pointTransactions->removeElement($transaction) && $transaction->getCart() === $this) {
  421. $transaction->setCart(null);
  422. }
  423. return $this;
  424. }
  425. /**
  426. * @param iterable|PointTransaction[] $transactions
  427. *
  428. * @return $this
  429. */
  430. public function setPointTransactions(iterable $transactions): self
  431. {
  432. foreach ($this->pointTransactions as $transaction) {
  433. $this->removePointTransaction($transaction);
  434. }
  435. foreach ($transactions as $transaction) {
  436. $this->addPointTransaction($transaction);
  437. }
  438. return $this;
  439. }
  440. public function isExpressDelivery(): ?bool
  441. {
  442. return $this->isExpressDelivery;
  443. }
  444. public function setExpressDelivery(bool $isExpressDelivery): self
  445. {
  446. $this->isExpressDelivery = $isExpressDelivery;
  447. return $this;
  448. }
  449. }