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. * @Assert\Json( message = "Vous avez entré un Json invalide" )
  72. */
  73. private ?string $formData = NULL;
  74. /**
  75. * Produit commandé (figé à l'instant de la commande stocké en JSON)
  76. *
  77. * @ORM\Column(type="json")
  78. *
  79. * @Assert\Json( message = "Vous avez entré un Json invalide" )
  80. *
  81. */
  82. private ?string $product = NULL;
  83. /**
  84. * Type de produit
  85. *
  86. * @ORM\ManyToOne(targetEntity=CustomProductType::class, inversedBy="templates")
  87. * @ORM\JoinColumn(nullable=false, name="type", referencedColumnName="slug")
  88. *
  89. * @Expose
  90. * @Groups({"customProductOrder:list"})
  91. */
  92. private ?CustomProductType $type = NULL;
  93. /**
  94. * @ORM\OneToMany(targetEntity=CustomProductField::class, mappedBy="order", cascade={"persist","remove"})
  95. */
  96. private $fields;
  97. /**
  98. * @ORM\Column(type="text")
  99. */
  100. private $value;
  101. use DateTrait;
  102. public function getProductToArray(): array
  103. {
  104. return json_decode($this->product, TRUE);
  105. }
  106. public function getFormDataToArray(): array
  107. {
  108. return json_decode($this->formData, TRUE);
  109. }
  110. /**
  111. * Label du type de produit
  112. *
  113. * @Serializer\VirtualProperty()
  114. * @SerializedName ("orderTypeLabel")
  115. * @Groups ({"customProductOrder:list"})
  116. *
  117. * @return string
  118. */
  119. public function getOrderTypeLabel()
  120. {
  121. return $this->getType()->getName();
  122. }
  123. /**
  124. * Label du produit
  125. *
  126. * @Serializer\VirtualProperty()
  127. * @SerializedName ("productLabel")
  128. * @Groups ({"customProductOrder:list"})
  129. *
  130. * @return string
  131. */
  132. public function getProductLabel(): string
  133. {
  134. return $this->getProductToArray()[ 'label' ];
  135. }
  136. /**
  137. * Description du produit
  138. *
  139. * @return string|null
  140. */
  141. public function getProductDescription(): ?string
  142. {
  143. return $this->getProductToArray()[ 'description' ];
  144. }
  145. /**
  146. * Valeur (prix) du produit
  147. *
  148. * @return float
  149. */
  150. public function getProductValue(): float
  151. {
  152. return $this->getProductToArray()[ 'value' ] ?? 0;
  153. }
  154. /**
  155. * Image du produit
  156. *
  157. * @return string|null
  158. */
  159. public function getProductImage(): ?string
  160. {
  161. return $this->getProductToArray()[ 'image' ];
  162. }
  163. /**
  164. * Liste des contacts (email) quand il y a une demande pour ce produit
  165. *
  166. * @return array
  167. */
  168. public function getProductContacts(): array
  169. {
  170. return $this->getProductToArray()[ 'contacts' ];
  171. }
  172. public function getHumanizedStatus(): string
  173. {
  174. $labels = [
  175. 'En attente de traitement',
  176. 'En cours de traitement',
  177. 'Traitée',
  178. 'Annulée',
  179. ];
  180. return $labels[ $this->status ];
  181. }
  182. public function getId(): ?int
  183. {
  184. return $this->id;
  185. }
  186. public function getUser(): ?User
  187. {
  188. return $this->user;
  189. }
  190. public function setUser(?User $user): self
  191. {
  192. $this->user = $user;
  193. return $this;
  194. }
  195. public function getQuantity(): ?int
  196. {
  197. return $this->quantity;
  198. }
  199. public function setQuantity(int $quantity): self
  200. {
  201. $this->quantity = $quantity;
  202. return $this;
  203. }
  204. public function getStatus(): int
  205. {
  206. return $this->status;
  207. }
  208. public function setStatus(int $status): self
  209. {
  210. $this->status = $status;
  211. return $this;
  212. }
  213. public function getFormData(): ?string
  214. {
  215. return $this->formData;
  216. }
  217. public function setFormData(string $formData): self
  218. {
  219. $this->formData = $formData;
  220. return $this;
  221. }
  222. public function getProduct(): ?string
  223. {
  224. return $this->product;
  225. }
  226. public function setProduct(string $product): self
  227. {
  228. $this->product = $product;
  229. return $this;
  230. }
  231. public function getType(): ?CustomProductType
  232. {
  233. return $this->type;
  234. }
  235. public function setType(?CustomProductType $type): self
  236. {
  237. $this->type = $type;
  238. return $this;
  239. }
  240. public function getValue(): ?string
  241. {
  242. return $this->value;
  243. }
  244. public function setValue(string $value): self
  245. {
  246. $this->value = $value;
  247. return $this;
  248. }
  249. }