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. return $this->getProductToArray()[ 'value' ] ?? 0;
  144. }
  145. /**
  146. * Image du produit
  147. *
  148. * @return string|null
  149. */
  150. public function getProductImage(): ?string
  151. {
  152. return $this->getProductToArray()[ 'image' ];
  153. }
  154. /**
  155. * Liste des contacts (email) quand il y a une demande pour ce produit
  156. *
  157. * @return array
  158. */
  159. public function getProductContacts(): array
  160. {
  161. return $this->getProductToArray()[ 'contacts' ];
  162. }
  163. public function getUserDataFullName(): string
  164. {
  165. $userData = $this->getUserData();
  166. $fullName = ucfirst(trim($userData['firstName'])) . ' ' . ucfirst(trim($userData['lastName']));
  167. if (empty($fullName)) {
  168. return $userData['email'];
  169. }
  170. return $fullName;
  171. }
  172. public function getUserDataFirstName(): string
  173. {
  174. return ucfirst(trim($this->getUserData()['firstName']));
  175. }
  176. public function getUserDataLastName(): string
  177. {
  178. return ucfirst(trim($this->getUserData()['lastName']));
  179. }
  180. public function getUserDataEmail(): string
  181. {
  182. return $this->getUserData()['email'];
  183. }
  184. public function getUserDataRecipient(): array
  185. {
  186. return [
  187. 'name' => $this->getUserDataFullName(),
  188. 'email' => $this->getUserDataEmail(),
  189. ];
  190. }
  191. public function getHumanizedStatus(): string
  192. {
  193. $labels = [
  194. 'En attente de traitement',
  195. 'En cours de traitement',
  196. 'Traitée',
  197. 'Annulée',
  198. ];
  199. return $labels[ $this->status ];
  200. }
  201. public function getId(): ?int
  202. {
  203. return $this->id;
  204. }
  205. public function getUser(): ?User
  206. {
  207. return $this->user;
  208. }
  209. public function setUser(?User $user): self
  210. {
  211. $this->user = $user;
  212. return $this;
  213. }
  214. public function getQuantity(): ?int
  215. {
  216. return $this->quantity;
  217. }
  218. public function setQuantity(int $quantity): self
  219. {
  220. $this->quantity = $quantity;
  221. return $this;
  222. }
  223. public function getStatus(): int
  224. {
  225. return $this->status;
  226. }
  227. public function setStatus(int $status): self
  228. {
  229. $this->status = $status;
  230. return $this;
  231. }
  232. public function getFormData(): ?array
  233. {
  234. return $this->formData;
  235. }
  236. public function setFormData(array $formData): self
  237. {
  238. $this->formData = $formData;
  239. return $this;
  240. }
  241. public function getProduct(): ?array
  242. {
  243. return $this->product;
  244. }
  245. public function setProduct(array $product): self
  246. {
  247. $this->product = $product;
  248. return $this;
  249. }
  250. public function getUserData(): ?array
  251. {
  252. return $this->userData;
  253. }
  254. public function setUserData(?array $userData): self
  255. {
  256. $this->userData = $userData;
  257. return $this;
  258. }
  259. public function getType(): ?CustomProductType
  260. {
  261. return $this->type;
  262. }
  263. public function setType(?CustomProductType $type): self
  264. {
  265. $this->type = $type;
  266. return $this;
  267. }
  268. public function getValue(): ?string
  269. {
  270. return $this->value;
  271. }
  272. public function setValue(string $value): self
  273. {
  274. $this->value = $value;
  275. return $this;
  276. }
  277. }