src/ECommerceBundle/Entity/Cart.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\ECommerceBundle\Entity;
  3. use App\NewShippingBundle\Entity\Zone;
  4. use App\OnlinePaymentBundle\Entity\PaymentMethod;
  5. use App\ShippingBundle\Entity\ShippingAddress;
  6. use App\UserBundle\Entity\User;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. /**
  11. * @ORM\Table("cart")
  12. * @ORM\HasLifecycleCallbacks
  13. * @ORM\Entity(repositoryClass="App\ECommerceBundle\Repository\CartRepository")
  14. */
  15. class Cart
  16. {
  17. /**
  18. * @ORM\Column(name="id", type="integer")
  19. * @ORM\Id
  20. * @ORM\GeneratedValue(strategy="AUTO")
  21. */
  22. private ?int $id = null;
  23. /**
  24. * @ORM\ManyToOne(targetEntity="App\OnlinePaymentBundle\Entity\PaymentMethod", cascade={"persist"})
  25. */
  26. private ?PaymentMethod $paymentMethod = null;
  27. /**
  28. * @ORM\ManyToOne(targetEntity="Coupon", cascade={"persist"})
  29. */
  30. private ?Coupon $coupon = null;
  31. /**
  32. * @ORM\ManyToOne(targetEntity="App\ShippingBundle\Entity\ShippingAddress")
  33. */
  34. private ?ShippingAddress $shippingAddress = null;
  35. /**
  36. * @ORM\Column(name="created", type="datetime")
  37. */
  38. private ?\DateTimeInterface $created = null;
  39. /**
  40. * @ORM\OneToMany(targetEntity="CartHasProductPrice", mappedBy="cart", cascade={"persist", "remove"}, orphanRemoval=true)
  41. * */
  42. private Collection $cartHasProductPrices;
  43. /**
  44. * @ORM\OneToOne(targetEntity="CartHasUser", mappedBy="cart", cascade={"persist", "remove"})
  45. */
  46. private ?CartHasUser $cartHasUser = null;
  47. /**
  48. * @ORM\OneToOne(targetEntity="CartHasCookie", mappedBy="cart", cascade={"persist", "remove"})
  49. */
  50. private ?CartHasCookie $cartHasCookie = null;
  51. /**
  52. * @ORM\OneToOne(targetEntity="CartGuestData", mappedBy="cart", cascade={"persist", "remove"})
  53. */
  54. private ?CartGuestData $cartGuestData = null;
  55. private ?float $subTotal = null;
  56. private ?float $shippingFees = null;
  57. private ?float $discount = null;
  58. private ?float $extraFees = null;
  59. private ?float $grandTotal = null;
  60. public function getUser(): ?User
  61. {
  62. if ($this->getCartHasUser() === null) {
  63. return null;
  64. }
  65. if ($this->getCartHasUser()->getUser() instanceof User) {
  66. return $this->getCartHasUser()->getUser();
  67. }
  68. return null;
  69. }
  70. public function getBuyerName(): ?string
  71. {
  72. if ($this->getUser() instanceof User) {
  73. return $this->getUser()->getFullName();
  74. } elseif ($this->getCartGuestData() instanceof CartGuestData) {
  75. return $this->getCartGuestData()->getName();
  76. }
  77. return null;
  78. }
  79. public function getBuyerAddress(): ?string
  80. {
  81. if ($this->getUser() instanceof User and $this->getShippingAddress() instanceof ShippingAddress) {
  82. return $this->getShippingAddress()->getFormattedFullAddress(false);
  83. } elseif ($this->getCartGuestData() instanceof CartGuestData) {
  84. return $this->getCartGuestData()->getFormattedFullAddress(false);
  85. }
  86. return null;
  87. }
  88. public function getBuyerAddressZone(): ?Zone
  89. {
  90. if ($this->getUser() instanceof User and $this->getShippingAddress() instanceof ShippingAddress) {
  91. return $this->getShippingAddress()->getZone();
  92. } elseif ($this->getCartGuestData() instanceof CartGuestData) {
  93. return $this->getCartGuestData()->getZone();
  94. }
  95. return null;
  96. }
  97. public function getBuyerEmail(): ?string
  98. {
  99. if ($this->getUser() instanceof User) {
  100. return $this->getUser()->getEmail();
  101. } elseif ($this->getCartGuestData() instanceof CartGuestData) {
  102. return $this->getCartGuestData()->getEmail();
  103. }
  104. return null;
  105. }
  106. public function getBuyerMobileNumber(): ?string
  107. {
  108. if ($this->getUser() instanceof User and $this->getShippingAddress() instanceof ShippingAddress) {
  109. return $this->getShippingAddress()->getMobileNumber();
  110. } elseif ($this->getCartGuestData() instanceof CartGuestData) {
  111. return $this->getCartGuestData()->getMobileNumber();
  112. }
  113. return null;
  114. }
  115. public function getCookieHash(): ?string
  116. {
  117. if ($this->getCartHasCookie() === null) {
  118. return null;
  119. }
  120. if ($this->getCartHasCookie()->getCookie() instanceof User) {
  121. return $this->getCartHasCookie()->getCookie();
  122. }
  123. return null;
  124. }
  125. /**
  126. * @ORM\PrePersist()
  127. */
  128. public function updatedTimestamps(): void
  129. {
  130. $this->setCreated(new \DateTime(date('Y-m-d H:i:s')));
  131. }
  132. public function __construct()
  133. {
  134. $this->cartHasProductPrices = new ArrayCollection();
  135. }
  136. public function __clone(): void
  137. {
  138. $this->id = null;
  139. }
  140. public function getObj(): array
  141. {
  142. $couponObj = null;
  143. $userId = null;
  144. if ($this->getCartHasUser()) {
  145. $userId = ($this->getCartHasUser()->getUser()) ? $this->getCartHasUser()->getUser()->getId() : null;
  146. } elseif ($this->getCartHasCookie()) {
  147. $userId = $this->getCartHasCookie()->getCookie();
  148. }
  149. if ($this->getCoupon()) {
  150. $coupon = $this->getCoupon();
  151. $couponObj = [
  152. "code" => $coupon->getCode(),
  153. "description" => $coupon->getDescription(),
  154. ];
  155. }
  156. return [
  157. "id" => $this->getId(),
  158. "userId" => $userId,
  159. "noOfItems" => $this->getItemNo(),
  160. "coupon" => $couponObj, // object
  161. "shippingAddress" => ($this->getShippingAddress()) ? $this->getShippingAddress()->getObj() : null,
  162. "paymentMethod" => ($this->getPaymentMethod()) ? $this->getPaymentMethod()->getObj() : null,
  163. "created" => ($this->getCreated()) ? $this->getCreated()->format("Y-m-d H:i:s") : null,
  164. ];
  165. }
  166. public function getProductPricesHash(): ?string
  167. {
  168. $productPricesArray = [];
  169. if ($this->getCartHasProductPrices()->count() == 0) {
  170. return null;
  171. }
  172. foreach ($this->getCartHasProductPrices() as $cartHasProductPrice) {
  173. $productPrice = $cartHasProductPrice->getProductPrice();
  174. $productPricesArray[] = $productPrice->getId();
  175. }
  176. sort($productPricesArray); // sort ascending
  177. $productPricesConcatenate = implode("-", $productPricesArray);
  178. return md5($productPricesConcatenate);
  179. }
  180. public function getItemNo(): int
  181. {
  182. $itemNo = 0;
  183. foreach ($this->getCartHasProductPrices() as $cartHasProductPrice) {
  184. $itemNo += $cartHasProductPrice->getQty();
  185. }
  186. return $itemNo;
  187. }
  188. public function emptyCartHasProductPrices(): self
  189. {
  190. $this->cartHasProductPrices->clear();
  191. return $this;
  192. }
  193. public function getSubTotal(): ?float
  194. {
  195. return $this->subTotal;
  196. }
  197. public function setSubTotal(?float $subTotal): self
  198. {
  199. $this->subTotal = $subTotal;
  200. return $this;
  201. }
  202. public function getShippingFees(): ?float
  203. {
  204. return $this->shippingFees;
  205. }
  206. public function setShippingFees(?float $shippingFees): self
  207. {
  208. $this->shippingFees = $shippingFees;
  209. return $this;
  210. }
  211. public function getDiscount(): ?float
  212. {
  213. return $this->discount;
  214. }
  215. public function setDiscount(?float $discount): self
  216. {
  217. $this->discount = $discount;
  218. return $this;
  219. }
  220. public function getExtraFees(): ?float
  221. {
  222. return $this->extraFees;
  223. }
  224. public function setExtraFees(?float $extraFees): self
  225. {
  226. $this->extraFees = $extraFees;
  227. return $this;
  228. }
  229. public function getGrandTotal(): ?float
  230. {
  231. return $this->grandTotal;
  232. }
  233. /**
  234. * @param float|null $grandTotal
  235. * @return Cart
  236. */
  237. public function setGrandTotal(?float $grandTotal): self
  238. {
  239. $this->grandTotal = $grandTotal;
  240. return $this;
  241. }
  242. /**
  243. * @return Collection|CartHasProductPrice[]
  244. */
  245. public function getCartHasProductPrices(): Collection
  246. {
  247. return $this->cartHasProductPrices;
  248. }
  249. public function getId(): ?int
  250. {
  251. return $this->id;
  252. }
  253. public function getCreated(): ?\DateTimeInterface
  254. {
  255. return $this->created;
  256. }
  257. public function setCreated(\DateTimeInterface $created): self
  258. {
  259. $this->created = $created;
  260. return $this;
  261. }
  262. public function getPaymentMethod(): ?PaymentMethod
  263. {
  264. return $this->paymentMethod;
  265. }
  266. public function setPaymentMethod(?PaymentMethod $paymentMethod): self
  267. {
  268. $this->paymentMethod = $paymentMethod;
  269. return $this;
  270. }
  271. public function getCoupon(): ?Coupon
  272. {
  273. return $this->coupon;
  274. }
  275. public function setCoupon(?Coupon $coupon): self
  276. {
  277. $this->coupon = $coupon;
  278. return $this;
  279. }
  280. public function getShippingAddress(): ?ShippingAddress
  281. {
  282. return $this->shippingAddress;
  283. }
  284. public function setShippingAddress(?ShippingAddress $shippingAddress): self
  285. {
  286. $this->shippingAddress = $shippingAddress;
  287. return $this;
  288. }
  289. public function addCartHasProductPrice(CartHasProductPrice $cartHasProductPrice): self
  290. {
  291. if (!$this->cartHasProductPrices->contains($cartHasProductPrice)) {
  292. $this->cartHasProductPrices->add($cartHasProductPrice);
  293. $cartHasProductPrice->setCart($this);
  294. }
  295. return $this;
  296. }
  297. public function removeCartHasProductPrice(CartHasProductPrice $cartHasProductPrice): self
  298. {
  299. if ($this->cartHasProductPrices->removeElement($cartHasProductPrice)) {
  300. // set the owning side to null (unless already changed)
  301. if ($cartHasProductPrice->getCart() === $this) {
  302. $cartHasProductPrice->setCart(null);
  303. }
  304. }
  305. return $this;
  306. }
  307. public function getCartHasUser(): ?CartHasUser
  308. {
  309. return $this->cartHasUser;
  310. }
  311. public function setCartHasUser(?CartHasUser $cartHasUser): self
  312. {
  313. // unset the owning side of the relation if necessary
  314. if ($cartHasUser === null && $this->cartHasUser !== null) {
  315. $this->cartHasUser->setCart(null);
  316. }
  317. // set the owning side of the relation if necessary
  318. if ($cartHasUser !== null && $cartHasUser->getCart() !== $this) {
  319. $cartHasUser->setCart($this);
  320. }
  321. $this->cartHasUser = $cartHasUser;
  322. return $this;
  323. }
  324. public function getCartHasCookie(): ?CartHasCookie
  325. {
  326. return $this->cartHasCookie;
  327. }
  328. public function setCartHasCookie(?CartHasCookie $cartHasCookie): self
  329. {
  330. // unset the owning side of the relation if necessary
  331. if ($cartHasCookie === null && $this->cartHasCookie !== null) {
  332. $this->cartHasCookie->setCart(null);
  333. }
  334. // set the owning side of the relation if necessary
  335. if ($cartHasCookie !== null && $cartHasCookie->getCart() !== $this) {
  336. $cartHasCookie->setCart($this);
  337. }
  338. $this->cartHasCookie = $cartHasCookie;
  339. return $this;
  340. }
  341. public function getCartGuestData(): ?CartGuestData
  342. {
  343. return $this->cartGuestData;
  344. }
  345. public function setCartGuestData(?CartGuestData $cartGuestData): self
  346. {
  347. // unset the owning side of the relation if necessary
  348. if ($cartGuestData === null && $this->cartGuestData !== null) {
  349. $this->cartGuestData->setCart(null);
  350. }
  351. // set the owning side of the relation if necessary
  352. if ($cartGuestData !== null && $cartGuestData->getCart() !== $this) {
  353. $cartGuestData->setCart($this);
  354. }
  355. $this->cartGuestData = $cartGuestData;
  356. return $this;
  357. }
  358. }