src/Entity/ECommerce/CartItem.php line 605

Open in your IDE?
  1. <?php
  2. namespace App\Entity\ECommerce;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use JMS\Serializer\Annotation as JMS;
  7. use App\Entity\ECommerce\Traits\DatesInterface;
  8. use App\Entity\ECommerce\Traits\DatesTrait;
  9. use Symfony\Component\Validator\Constraints;
  10. /**
  11. * CartItem
  12. *
  13. * @ORM\Table(name="cart_item")
  14. * @ORM\Entity(repositoryClass="App\Repository\ECommerce\CartItemRepository")
  15. */
  16. class CartItem implements DatesInterface
  17. {
  18. public const SHIPPED_STATUS = Invoice::SHIPPED_STATUS;
  19. public const PARTIALLY_SHIPPED_STATUS = Invoice::PARTIALLY_SHIPPED_STATUS;
  20. public const UNSHIPPED_STATUS = Invoice::UNSHIPPED_STATUS;
  21. /**
  22. * Money calculations rounding precision
  23. */
  24. public const ROUNDING_PRECISION = Cart::ROUNDING_PRECISION;
  25. /**
  26. * @var int
  27. *
  28. * @ORM\Column(name="id", type="integer")
  29. * @ORM\Id
  30. * @ORM\GeneratedValue(strategy="AUTO")
  31. *
  32. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  33. */
  34. private $id;
  35. /**
  36. * @var float
  37. *
  38. * @ORM\Column(name="product_base_price", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "product default price"})
  39. *
  40. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  41. */
  42. private $productBasePrice;
  43. /**
  44. * @var float
  45. *
  46. * @ORM\Column(name="product_base_price_converted", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "product default price converted in clients currency"})
  47. *
  48. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  49. */
  50. private $productBasePriceConverted;
  51. /**
  52. * @var float
  53. *
  54. * @ORM\Column(name="product_price", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "product discount price"})
  55. *
  56. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  57. */
  58. private $productPrice;
  59. /**
  60. * @var float
  61. *
  62. * @ORM\Column(name="product_price_converted", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "product discount price converted in clients currency"})
  63. *
  64. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  65. */
  66. private $productPriceConverted;
  67. /**
  68. * @var float
  69. *
  70. * @ORM\Column(name="product_tax", type="decimal", precision=18, scale=4, nullable=true)
  71. *
  72. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  73. */
  74. private $productTax;
  75. /**
  76. * @var float
  77. *
  78. * @ORM\Column(name="product_tax_converted", type="decimal", precision=18, scale=4, nullable=true)
  79. *
  80. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  81. */
  82. private $productTaxConverted;
  83. /**
  84. * @var int
  85. *
  86. * @ORM\Column(name="quantity", type="integer")
  87. *
  88. * @Constraints\GreaterThanOrEqual(1)
  89. *
  90. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  91. */
  92. private $quantity=1;
  93. /**
  94. * @var float
  95. *
  96. * @ORM\Column(name="rebate_percent", type="decimal", precision=5, scale=2, nullable=true)
  97. *
  98. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  99. */
  100. private $rebatePercent;
  101. /**
  102. * @var float
  103. *
  104. * @ORM\Column(name="rebate", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "discount"})
  105. *
  106. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  107. */
  108. private $rebate;
  109. /**
  110. * @var float
  111. *
  112. * @ORM\Column(name="rebate_converted", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "discount converted in clients currency"})
  113. *
  114. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  115. */
  116. private $rebateConverted;
  117. /**
  118. * @var float
  119. *
  120. * @ORM\Column(name="sum", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "product price with rebate multiplied with quantity"})
  121. *
  122. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  123. */
  124. private $sum;
  125. /**
  126. * @var float
  127. *
  128. * @ORM\Column(name="sum_converted", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "sum value converted in clients currency"})
  129. *
  130. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  131. */
  132. private $sumConverted;
  133. /**
  134. * @var float
  135. *
  136. * @ORM\Column(name="tax_percent", type="decimal", precision=7, scale=4, nullable=true)
  137. *
  138. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  139. */
  140. private $taxPercent;
  141. /**
  142. * @var float
  143. *
  144. * @ORM\Column(name="tax", type="decimal", precision=18, scale=4, nullable=true)
  145. *
  146. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  147. */
  148. private $tax;
  149. /**
  150. * @var float
  151. *
  152. * @ORM\Column(name="tax_converted", type="decimal", precision=18, scale=4, nullable=true)
  153. *
  154. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  155. */
  156. private $taxConverted;
  157. /**
  158. * @var float
  159. *
  160. * @ORM\Column(name="total", type="decimal", precision=18, scale=4, nullable=true)
  161. *
  162. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  163. */
  164. private $total;
  165. /**
  166. * @var float
  167. *
  168. * @ORM\Column(name="total_converted", type="decimal", precision=18, scale=4, nullable=true)
  169. *
  170. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  171. */
  172. private $totalConverted;
  173. /**
  174. * @var \DateTime
  175. *
  176. * @ORM\Column(name="added_at", type="datetime", nullable=false)
  177. *
  178. * @JMS\Groups(groups={"cart_private", "view_cart_item"})
  179. */
  180. private $addedAt;
  181. /**
  182. * @var \DateTime
  183. *
  184. * @ORM\Column(name="deliver_date", type="datetime", nullable=true)
  185. *
  186. * @Constraints\GreaterThanOrEqual(propertyPath="minDeliveryDate", groups={"CheckOut"})
  187. * @Constraints\NotNull(groups={"CheckOut"})
  188. */
  189. private $deliverDate;
  190. /**
  191. * @var \DateTime
  192. */
  193. protected $minDeliveryDate;
  194. /**
  195. * @var Cart
  196. *
  197. * @ORM\ManyToOne(targetEntity="App\Entity\ECommerce\Cart", inversedBy="items")
  198. */
  199. private $cart;
  200. /**
  201. * @var Depot
  202. *
  203. * @ORM\ManyToOne(targetEntity="App\Entity\ECommerce\Depot")
  204. * @ORM\JoinColumn(name="depot", referencedColumnName="id", nullable=true)
  205. */
  206. private $depot;
  207. /**
  208. * @var Product
  209. *
  210. * @ORM\ManyToOne(targetEntity="App\Entity\ECommerce\Product")
  211. * @ORM\JoinColumn(name="product", referencedColumnName="id")
  212. *
  213. * @JMS\Groups(groups={})
  214. */
  215. private $product;
  216. /**
  217. * @deprecated use \App\Repository\ECommerce\DepotProductRepository::getByDepotAndProduct(Depot $depot, Product $product) method
  218. */
  219. private $depotProduct = null;
  220. /**
  221. * @var Collection
  222. *
  223. * @ORM\OneToMany(targetEntity="App\Entity\ECommerce\BillItem", mappedBy="cartItem")
  224. */
  225. private $billItems;
  226. use DatesTrait {
  227. DatesTrait::__construct as __DTConstruct;
  228. }
  229. /**
  230. * CartItem constructor.
  231. */
  232. public function __construct(Cart $cart, Product $product, Depot $depot)
  233. {
  234. $this->billItems = new ArrayCollection();
  235. $this->setCart($cart)
  236. ->setProduct($product)
  237. ->setDepot($depot)
  238. ->setAddedAt();
  239. $this->__DTConstruct();
  240. }
  241. /**
  242. * Get id
  243. *
  244. * @return int
  245. */
  246. public function getId(): ?int
  247. {
  248. return $this->id;
  249. }
  250. /**
  251. * Get id
  252. */
  253. public function setId(int $id): self
  254. {
  255. $this->id = $id;
  256. return $this;
  257. }
  258. /**
  259. * Set quantity
  260. *
  261. *
  262. */
  263. public function setQuantity(int $quantity): CartItem
  264. {
  265. $this->quantity = max($quantity, 1);
  266. return $this;
  267. }
  268. /**
  269. * Get quantity
  270. *
  271. * @return int
  272. */
  273. public function getQuantity(): ?int
  274. {
  275. return $this->quantity;
  276. }
  277. /**
  278. * Set product
  279. *
  280. *
  281. */
  282. public function setProduct(?Product $product = null): CartItem
  283. {
  284. $this->product = $product;
  285. return $this;
  286. }
  287. /**
  288. * Get product
  289. */
  290. public function getProduct(): Product
  291. {
  292. return $this->product;
  293. }
  294. /**
  295. * Set productPrice
  296. *
  297. * @param float $productPrice
  298. */
  299. public function setProductPrice($productPrice): CartItem
  300. {
  301. $this->productPrice = $productPrice;
  302. return $this;
  303. }
  304. /**
  305. * Get productPrice
  306. *
  307. * @return float
  308. */
  309. public function getProductPrice(): ?float
  310. {
  311. return $this->productPrice;
  312. }
  313. /**
  314. * Set productPriceConverted
  315. *
  316. * @param string $productPriceConverted
  317. */
  318. public function setProductPriceConverted($productPriceConverted): CartItem
  319. {
  320. $this->productPriceConverted = $productPriceConverted;
  321. return $this;
  322. }
  323. /**
  324. * Get productPriceConverted
  325. *
  326. * @return float
  327. */
  328. public function getProductPriceConverted(): ?float
  329. {
  330. return $this->productPriceConverted;
  331. }
  332. /**
  333. * Set productTax
  334. *
  335. * @param float $productTax
  336. */
  337. public function setProductTax($productTax): CartItem
  338. {
  339. $this->productTax = $productTax;
  340. return $this;
  341. }
  342. /**
  343. * Get productTax
  344. *
  345. * @return float
  346. */
  347. public function getProductTax(): ?float
  348. {
  349. return $this->productTax;
  350. }
  351. /**
  352. * Set productTaxConverted
  353. *
  354. * @param string $productTaxConverted
  355. */
  356. public function setProductTaxConverted($productTaxConverted): CartItem
  357. {
  358. $this->productTaxConverted = $productTaxConverted;
  359. return $this;
  360. }
  361. /**
  362. * Get productTaxConverted
  363. *
  364. * @return float
  365. */
  366. public function getProductTaxConverted(): ?float
  367. {
  368. return $this->productTaxConverted;
  369. }
  370. /**
  371. * Set tax
  372. *
  373. * @param float $tax
  374. */
  375. public function setTax($tax): CartItem
  376. {
  377. $this->tax = $tax;
  378. return $this;
  379. }
  380. /**
  381. * Get tax
  382. *
  383. * @return float
  384. */
  385. public function getTax(): ?float
  386. {
  387. return $this->tax;
  388. }
  389. /**
  390. * Set taxConverted
  391. *
  392. * @param string $taxConverted
  393. */
  394. public function setTaxConverted($taxConverted): CartItem
  395. {
  396. $this->taxConverted = $taxConverted;
  397. return $this;
  398. }
  399. /**
  400. * Get taxConverted
  401. *
  402. * @return float
  403. */
  404. public function getTaxConverted(): ?float
  405. {
  406. return $this->taxConverted;
  407. }
  408. /**
  409. * Set sum
  410. *
  411. * @param float $sum
  412. */
  413. public function setSum($sum): CartItem
  414. {
  415. $this->sum = $sum;
  416. return $this;
  417. }
  418. /**
  419. * Get sum
  420. *
  421. * @return float
  422. */
  423. public function getSum(): ?float
  424. {
  425. return $this->sum;
  426. }
  427. /**
  428. * Set sumConverted
  429. *
  430. * @param string $sumConverted
  431. */
  432. public function setSumConverted($sumConverted): CartItem
  433. {
  434. $this->sumConverted = $sumConverted;
  435. return $this;
  436. }
  437. /**
  438. * Get sumConverted
  439. *
  440. * @return float
  441. */
  442. public function getSumConverted(): ?float
  443. {
  444. return $this->sumConverted;
  445. }
  446. /**
  447. * Set total
  448. *
  449. * @param string $total
  450. */
  451. public function setTotal($total): CartItem
  452. {
  453. $this->total = $total;
  454. return $this;
  455. }
  456. /**
  457. * Get total
  458. *
  459. * @return float
  460. */
  461. public function getTotal(): ?float
  462. {
  463. return $this->total;
  464. }
  465. /**
  466. * Set totalConverted
  467. *
  468. * @param string $totalConverted
  469. */
  470. public function setTotalConverted($totalConverted): CartItem
  471. {
  472. $this->totalConverted = $totalConverted;
  473. return $this;
  474. }
  475. /**
  476. * Get totalConverted
  477. *
  478. * @return float
  479. */
  480. public function getTotalConverted(): ?float
  481. {
  482. return $this->totalConverted;
  483. }
  484. /**
  485. * Set addedAt
  486. *
  487. * @param \DateTime $addedAt
  488. */
  489. public function setAddedAt($addedAt=null): CartItem
  490. {
  491. $this->addedAt = $addedAt ?? new \DateTime();
  492. return $this;
  493. }
  494. /**
  495. * Get addedAt
  496. *
  497. * @return \DateTime
  498. */
  499. public function getAddedAt(): ?\DateTime
  500. {
  501. return $this->addedAt;
  502. }
  503. /**
  504. * Set cart
  505. *
  506. *
  507. */
  508. public function setCart(Cart $cart = null): CartItem
  509. {
  510. $this->cart = $cart;
  511. return $this;
  512. }
  513. /**
  514. * Get cart
  515. */
  516. public function getCart(): Cart
  517. {
  518. return $this->cart;
  519. }
  520. /**
  521. * Set depot
  522. *
  523. *
  524. */
  525. public function setDepot(Depot $depot = null): CartItem
  526. {
  527. $this->depot = $depot;
  528. return $this;
  529. }
  530. /**
  531. * Get depot
  532. */
  533. public function getDepot(): Depot
  534. {
  535. return $this->depot;
  536. }
  537. /**
  538. * Set rebatePercent
  539. *
  540. * @param string $rebatePercent
  541. */
  542. public function setRebatePercent($rebatePercent): CartItem
  543. {
  544. $this->rebatePercent = $rebatePercent;
  545. return $this;
  546. }
  547. /**
  548. * Get rebatePercent
  549. *
  550. * @return float
  551. */
  552. public function getRebatePercent(): ?float
  553. {
  554. return $this->rebatePercent;
  555. }
  556. /**
  557. * Set rebate
  558. *
  559. * @param string $rebate
  560. */
  561. public function setRebate($rebate): CartItem
  562. {
  563. $this->rebate = $rebate;
  564. return $this;
  565. }
  566. /**
  567. * Get rebate
  568. *
  569. * @return float
  570. */
  571. public function getRebate(): ?float
  572. {
  573. return $this->rebate;
  574. }
  575. /**
  576. * Set rebateConverted
  577. *
  578. * @param string $rebateConverted
  579. */
  580. public function setRebateConverted($rebateConverted): CartItem
  581. {
  582. $this->rebateConverted = $rebateConverted;
  583. return $this;
  584. }
  585. /**
  586. * Get rebateConverted
  587. *
  588. * @return float
  589. */
  590. public function getRebateConverted(): ?float
  591. {
  592. return $this->rebateConverted;
  593. }
  594. /**
  595. * Set taxPercent
  596. *
  597. * @param string $taxPercent
  598. */
  599. public function setTaxPercent($taxPercent): CartItem
  600. {
  601. $this->taxPercent = $taxPercent;
  602. return $this;
  603. }
  604. /**
  605. * Get taxPercent
  606. *
  607. * @return float
  608. */
  609. public function getTaxPercent(): ?float
  610. {
  611. return $this->taxPercent;
  612. }
  613. /**
  614. * Set deliverDate
  615. *
  616. * @param \DateTime $deliverDate
  617. */
  618. public function setDeliverDate($deliverDate): CartItem
  619. {
  620. $this->deliverDate = $deliverDate;
  621. return $this;
  622. }
  623. /**
  624. * Get deliverDate
  625. *
  626. * @return \DateTime
  627. */
  628. public function getDeliverDate(): ?\DateTime
  629. {
  630. return $this->deliverDate;
  631. }
  632. public function getMinDeliveryDate(): \DateTime
  633. {
  634. $deliveryAt = $this->getDepot()->getDeliverAt();
  635. $this->minDeliveryDate = new \DateTime($deliveryAt);
  636. return $this->minDeliveryDate;
  637. }
  638. public function setMinDeliveryDate(\DateTime $minDeliveryDate): CartItem
  639. {
  640. $this->minDeliveryDate = $minDeliveryDate;
  641. return $this;
  642. }
  643. /**
  644. * Add billItem.
  645. *
  646. *
  647. */
  648. public function addBillItem(BillItem $billItem): CartItem
  649. {
  650. $this->billItems[] = $billItem;
  651. return $this;
  652. }
  653. /**
  654. * Remove billItem.
  655. *
  656. *
  657. * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  658. */
  659. public function removeBillItem(BillItem $billItem): bool
  660. {
  661. return $this->billItems->removeElement($billItem);
  662. }
  663. /**
  664. * Get billItems.
  665. */
  666. public function getBillItems(): Collection
  667. {
  668. return $this->billItems;
  669. }
  670. public function getStatus(): string
  671. {
  672. $statuses = $this->billItems->map(fn(BillItem $billItem): int => $billItem->getBill()->getShipped() ? $billItem->getQuantity() : 0)->toArray();
  673. $sum =array_sum($statuses);
  674. if ($sum === 0) {
  675. return self::UNSHIPPED_STATUS;
  676. }
  677. if ($this->quantity === $sum) {
  678. return self::SHIPPED_STATUS;
  679. }
  680. return self::PARTIALLY_SHIPPED_STATUS;
  681. }
  682. /**
  683. * Set productBasePrice.
  684. *
  685. * @param string|null $productBasePrice
  686. */
  687. public function setProductBasePrice($productBasePrice = null): CartItem
  688. {
  689. $this->productBasePrice = $productBasePrice;
  690. return $this;
  691. }
  692. /**
  693. * Get productBasePrice.
  694. *
  695. * @return float
  696. */
  697. public function getProductBasePrice(): ?float
  698. {
  699. return $this->productBasePrice;
  700. }
  701. /**
  702. * Set productBasePriceConverted.
  703. *
  704. * @param string|null $productBasePriceConverted
  705. */
  706. public function setProductBasePriceConverted($productBasePriceConverted = null): CartItem
  707. {
  708. $this->productBasePriceConverted = $productBasePriceConverted;
  709. return $this;
  710. }
  711. /**
  712. * Get productBasePriceConverted.
  713. *
  714. * @return float
  715. */
  716. public function getProductBasePriceConverted(): ?float
  717. {
  718. return $this->productBasePriceConverted;
  719. }
  720. }