src/Entity/CustomProductOrder.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CustomProductOrderRepository;
  4. use App\Traits\DateTrait;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use JMS\Serializer\Annotation as Serializer;
  7. use JMS\Serializer\Annotation\Expose;
  8. use JMS\Serializer\Annotation\Groups;
  9. use JMS\Serializer\Annotation\SerializedName;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. /**
  12. * Commande d'un utilisateur pour un produit personnalisé
  13. *
  14. * @ORM\Entity(repositoryClass=CustomProductOrderRepository::class)
  15. *
  16. * @Serializer\ExclusionPolicy("ALL")
  17. */
  18. class CustomProductOrder
  19. {
  20. public const PENDING = 0;
  21. public const PROCESSING = 1;
  22. public const DONE = 2;
  23. public const CANCELED = 3;
  24. public const STATUS = [self::PENDING, self::PROCESSING, self::DONE, self::CANCELED];
  25. /**
  26. * @ORM\Id
  27. * @ORM\GeneratedValue
  28. * @ORM\Column(type="integer")
  29. *
  30. * @Expose
  31. * @Groups({"customProductOrder:list"})
  32. */
  33. private ?int $id = null;
  34. /**
  35. * Utilisateur qui passe la commande
  36. *
  37. * @ORM\ManyToOne(targetEntity=User::class, inversedBy="customProductOrders")
  38. * @ORM\JoinColumn(nullable=false)
  39. *
  40. * @Expose
  41. * @Groups({"customProductOrder:list"})
  42. */
  43. private ?User $user = null;
  44. /**
  45. * Quantité commandée
  46. *
  47. * @ORM\Column(type="integer")
  48. *
  49. * @Assert\Positive
  50. *
  51. * @Expose
  52. * @Groups({"customProductOrder:list"})
  53. */
  54. private ?int $quantity = null;
  55. /**
  56. * Statut de la commande
  57. *
  58. * @ORM\Column(type="integer")
  59. *
  60. * @Assert\Choice(choices=CustomProductOrder::STATUS, message="Sélectionnez un statut valide")
  61. *
  62. * @Expose
  63. * @Groups({"customProductOrder:list"})
  64. */
  65. private int $status = 0;
  66. /**
  67. * Donnée du formulaire rempli par l'utilisateur
  68. *
  69. * @ORM\Column(type="json")
  70. *
  71. */
  72. private ?array $formData = null;
  73. /**
  74. * Produit commandé (figé à l'instant de la commande stocké en JSON)
  75. *
  76. * @ORM\Column(type="json")
  77. *
  78. */
  79. private ?array $product = null;
  80. /**
  81. * Donnée le l'utilisateur au moment de la commande (figé à l'instant de la commande stocké en JSON)
  82. *
  83. * @ORM\Column(type="json")
  84. *
  85. */
  86. private ?array $userData = null;
  87. /**
  88. * Type de produit
  89. *
  90. * @ORM\ManyToOne(targetEntity=CustomProductType::class, inversedBy="templates")
  91. * @ORM\JoinColumn(nullable=false, name="type", referencedColumnName="slug")
  92. *
  93. * @Expose
  94. * @Groups({"customProductOrder:list"})
  95. */
  96. private ?CustomProductType $type = null;
  97. /**
  98. * @ORM\OneToMany(targetEntity=CustomProductField::class, mappedBy="order", cascade={"persist","remove"})
  99. */
  100. private $fields;
  101. /**
  102. * @ORM\Column(type="text")
  103. */
  104. private $value;
  105. use DateTrait;
  106. public function getProductToArray(): ?array
  107. {
  108. return $this->product;
  109. }
  110. public function getFormDataToArray(): array
  111. {
  112. return $this->formData;
  113. }
  114. /**
  115. * Label du produit
  116. *
  117. * @Serializer\VirtualProperty()
  118. * @SerializedName ("productLabel")
  119. * @Groups ({"customProductOrder:list"})
  120. *
  121. * @return string
  122. */
  123. public function getProductLabel(): string
  124. {
  125. return $this->getProductToArray()['label'];
  126. }
  127. /**
  128. * Description du produit
  129. *
  130. * @return string|null
  131. */
  132. public function getProductDescription(): ?string
  133. {
  134. return $this->getProductToArray()['description'];
  135. }
  136. /**
  137. * Valeur (prix) du produit
  138. *
  139. * @return float
  140. */
  141. public function getProductValue(): float
  142. {
  143. $tiers = $this->getProductToArray()['customProductStepPrices'] ?? [];
  144. if (empty($tiers)) {
  145. return $this->getProductToArray()['value'] ?? 0;
  146. }
  147. // Trier par minQuantity
  148. usort($tiers, fn($a, $b) => $a['minQuantity'] <=> $b['minQuantity']);
  149. foreach ($tiers as $tier) {
  150. $min = $tier['minQuantity'];
  151. $max = $tier['maxQuantity'];
  152. if ($this->quantity >= $min && ($max === null || $this->quantity <= $max)) {
  153. return $tier["value"];
  154. }
  155. }
  156. return $this->getProductToArray()['value'] ?? 0;
  157. }
  158. /**
  159. * Images du produit
  160. *
  161. * @return array|null
  162. */
  163. public function getProductImage(): ?array
  164. {
  165. return $this->getProductToArray()['images'] ?? null;
  166. }
  167. /**
  168. * Liste des contacts (email) quand il y a une demande pour ce produit
  169. *
  170. * @return array
  171. */
  172. public function getProductContacts(): array
  173. {
  174. return $this->getProductToArray()['contacts'];
  175. }
  176. public function getUserDataFullName(): string
  177. {
  178. $userData = $this->getUserData();
  179. $fullName = ucfirst(trim($userData['firstName'])) . ' ' . ucfirst(trim($userData['lastName']));
  180. if (empty($fullName)) {
  181. return $userData['email'];
  182. }
  183. return $fullName;
  184. }
  185. public function getUserDataFirstName(): string
  186. {
  187. return ucfirst(trim($this->getUserData()['firstName']));
  188. }
  189. public function getUserDataLastName(): string
  190. {
  191. return ucfirst(trim($this->getUserData()['lastName']));
  192. }
  193. public function getUserDataEmail(): string
  194. {
  195. return $this->getUserData()['email'];
  196. }
  197. public function getUserDataRecipient(): array
  198. {
  199. return [
  200. 'name' => $this->getUserDataFullName(),
  201. 'email' => $this->getUserDataEmail(),
  202. ];
  203. }
  204. public function getHumanizedStatus(): string
  205. {
  206. $labels = [
  207. 'En attente de traitement',
  208. 'En cours de traitement',
  209. 'Traitée',
  210. 'Annulée',
  211. ];
  212. return $labels[$this->status];
  213. }
  214. public function getId(): ?int
  215. {
  216. return $this->id;
  217. }
  218. public function getUser(): ?User
  219. {
  220. return $this->user;
  221. }
  222. public function setUser(?User $user): self
  223. {
  224. $this->user = $user;
  225. return $this;
  226. }
  227. public function getQuantity(): ?int
  228. {
  229. return $this->quantity;
  230. }
  231. public function setQuantity(int $quantity): self
  232. {
  233. $this->quantity = $quantity;
  234. return $this;
  235. }
  236. public function getStatus(): int
  237. {
  238. return $this->status;
  239. }
  240. public function setStatus(int $status): self
  241. {
  242. $this->status = $status;
  243. return $this;
  244. }
  245. public function getFormData(): ?array
  246. {
  247. return $this->formData;
  248. }
  249. public function setFormData(array $formData): self
  250. {
  251. $this->formData = $formData;
  252. return $this;
  253. }
  254. public function getProduct(): ?array
  255. {
  256. return $this->product;
  257. }
  258. public function setProduct(array $product): self
  259. {
  260. $this->product = $product;
  261. return $this;
  262. }
  263. public function getUserData(): ?array
  264. {
  265. return $this->userData;
  266. }
  267. public function setUserData(?array $userData): self
  268. {
  269. $this->userData = $userData;
  270. return $this;
  271. }
  272. public function getType(): ?CustomProductType
  273. {
  274. return $this->type;
  275. }
  276. public function setType(?CustomProductType $type): self
  277. {
  278. $this->type = $type;
  279. return $this;
  280. }
  281. public function getValue(): ?string
  282. {
  283. return $this->value;
  284. }
  285. public function setValue(string $value): self
  286. {
  287. $this->value = $value;
  288. return $this;
  289. }
  290. }