src/Entity/ECommerce/Invoice.php line 229

Open in your IDE?
  1. <?php
  2. namespace App\Entity\ECommerce;
  3. use DateTime;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use JMS\Serializer\Annotation as JMS;
  8. use App\Entity\ECommerce\Traits\DatesInterface;
  9. use App\Entity\ECommerce\Traits\DatesTrait;
  10. use App\Entity\ECommerce\Traits\RemoteInterface;
  11. use App\Entity\ECommerce\Traits\RemoteTrait;
  12. /**
  13. * Invoice
  14. *
  15. * @ORM\Table(
  16. * name="invoice",
  17. * uniqueConstraints={@ORM\UniqueConstraint(name="invoice_number", columns={"number", "year"})}
  18. * )
  19. * @ORM\Entity(repositoryClass="App\Repository\ECommerce\InvoiceRepository")
  20. */
  21. class Invoice implements RemoteInterface, DatesInterface
  22. {
  23. public const SHIPPED_STATUS = 'shipped';
  24. public const PARTIALLY_SHIPPED_STATUS = 'partially';
  25. public const UNSHIPPED_STATUS = 'not-shipped';
  26. /**
  27. *
  28. * @ORM\Column(name="id", type="string", length=8)
  29. * @ORM\Id
  30. */
  31. private string $id;
  32. /**
  33. * @var int
  34. *
  35. * @ORM\Column(name="number", type="integer")
  36. *
  37. * @JMS\Expose()
  38. * @JMS\Groups({"cart_private"})
  39. */
  40. private $number;
  41. /**
  42. *
  43. * @ORM\Column(name="year", type="integer")
  44. *
  45. * @JMS\Expose()
  46. * @JMS\Groups({"cart_private"})
  47. */
  48. private int $year;
  49. /**
  50. * @var DateTime
  51. *
  52. * @ORM\Column(name="due_date", type="date", nullable=true)
  53. *
  54. * @JMS\Expose()
  55. * @JMS\Groups({"cart_private"})
  56. */
  57. private $dueDate;
  58. /**
  59. * @var Cart
  60. *
  61. * @ORM\OneToMany(targetEntity="App\Entity\ECommerce\Bill", mappedBy="invoice")
  62. */
  63. private $bills;
  64. use RemoteTrait;
  65. /**
  66. * Dates trait
  67. * createdAt, updatedAt, IndexAt, syncedAt
  68. */
  69. use DatesTrait {
  70. DatesTrait::__construct as __DT__construct;
  71. }
  72. /**
  73. * Invoice constructor.
  74. */
  75. public function __construct(/**
  76. *
  77. * @ORM\OneToOne(targetEntity="App\Entity\ECommerce\Cart", inversedBy="invoice", cascade={"persist"})
  78. * @ORM\JoinColumn(name="cart", referencedColumnName="id")
  79. */
  80. private Cart $cart)
  81. {
  82. $this->bills = new ArrayCollection();
  83. $this->year = (integer) (new DateTime())->format('Y');
  84. $this->id = hash('crc32b', $this->cart . $this->year);
  85. $this->__DT__construct();
  86. }
  87. /**
  88. * Get id
  89. */
  90. public function getId(): string
  91. {
  92. return $this->id;
  93. }
  94. /**
  95. * Set number
  96. *
  97. *
  98. */
  99. public function setNumber(int $number): self
  100. {
  101. $this->number = $number;
  102. return $this;
  103. }
  104. /**
  105. * Get number
  106. *
  107. * @return integer
  108. */
  109. public function getNumber(): ?int
  110. {
  111. return $this->number;
  112. }
  113. /**
  114. * Set year
  115. */
  116. public function setYear(int $year): self
  117. {
  118. $this->year = $year;
  119. return $this;
  120. }
  121. /**
  122. * Get year
  123. *
  124. * @return int
  125. */
  126. public function getYear():? int
  127. {
  128. return $this->year;
  129. }
  130. /**
  131. * Set carts
  132. *
  133. * @param Cart|null $cart
  134. */
  135. public function setCart(Cart $cart = null): self
  136. {
  137. $this->cart = $cart;
  138. return $this;
  139. }
  140. /**
  141. * Get carts
  142. */
  143. public function getCart(): Cart
  144. {
  145. return $this->cart;
  146. }
  147. /**
  148. * Set id
  149. *
  150. *
  151. */
  152. public function setId(string $id): self
  153. {
  154. $this->id = $id;
  155. return $this;
  156. }
  157. public function isNew(): bool
  158. {
  159. return $this->createdAt->diff(new DateTime())->days < 1;
  160. }
  161. public function getInvoiceNumber(): string
  162. {
  163. return $this->number . '-' . $this->year;
  164. }
  165. /**
  166. * Add bill.
  167. *
  168. *
  169. */
  170. public function addBill(Bill $bill): self
  171. {
  172. $this->bills[] = $bill;
  173. return $this;
  174. }
  175. /**
  176. * Remove bill.
  177. *
  178. *
  179. * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  180. */
  181. public function removeBill(Bill $bill): bool
  182. {
  183. return $this->bills->removeElement($bill);
  184. }
  185. /**
  186. * Get bills.
  187. */
  188. public function getBills(): Collection
  189. {
  190. return $this->bills;
  191. }
  192. /**
  193. * Set dueDate.
  194. *
  195. * @param DateTime|null $dueDate
  196. */
  197. public function setDueDate(DateTime $dueDate = null): self
  198. {
  199. $this->dueDate = $dueDate;
  200. return $this;
  201. }
  202. /**
  203. * Get dueDate.
  204. */
  205. public function getDueDate(): ?DateTime
  206. {
  207. return $this->dueDate;
  208. }
  209. public function getStatus(): string
  210. {
  211. $quantity = $this->cart->getItems()->map(fn(CartItem $item): ?int => $item->getQuantity())->toArray();
  212. $quantity = array_sum($quantity);
  213. $billItems = $this->bills->map(fn(Bill $bill) => $bill->getItems()->toArray())->toArray();
  214. if (empty($billItems)) {
  215. return self::UNSHIPPED_STATUS;
  216. }
  217. $billItems = call_user_func(array_merge(...), ...$billItems);
  218. $statuses = array_map(fn(BillItem $billItem): int => $billItem->getBill()->getShipped() ? $billItem->getQuantity() : 0,$billItems);
  219. $sum =array_sum($statuses);
  220. if ($sum === 0) {
  221. return self::UNSHIPPED_STATUS;
  222. }
  223. if ($quantity === $sum) {
  224. return self::SHIPPED_STATUS;
  225. }
  226. return self::PARTIALLY_SHIPPED_STATUS;
  227. }
  228. }