src/Entity/CustomProduct.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CustomProductRepository;
  4. use App\Traits\DateTrait;
  5. use DateTime;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use JMS\Serializer\Annotation as Serializer;
  10. use JMS\Serializer\Annotation\Expose;
  11. use JMS\Serializer\Annotation\Groups;
  12. use Symfony\Component\HttpFoundation\File\File;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  15. /**
  16. * Produit personnalisé
  17. *
  18. * @ORM\Entity(repositoryClass=CustomProductRepository::class)
  19. *
  20. * @Serializer\ExclusionPolicy("ALL")
  21. * @Vich\Uploadable
  22. */
  23. class CustomProduct
  24. {
  25. /**
  26. * @ORM\Id
  27. * @ORM\GeneratedValue
  28. * @ORM\Column(type="integer")
  29. *
  30. * @Expose
  31. * @Groups({"customProduct:list", "customProduct:item"})
  32. */
  33. private ?int $id = NULL;
  34. /**
  35. * Label du produit
  36. *
  37. * @ORM\Column(type="string", length=255)
  38. *
  39. * @Assert\Length(
  40. * min = 2,
  41. * max = 255,
  42. * )
  43. *
  44. * @Expose
  45. * @Groups({"customProduct:list", "customProduct:item"})
  46. */
  47. private ?string $label = NULL;
  48. /**
  49. * Description du produit
  50. *
  51. * @ORM\Column(type="text", nullable=true)
  52. *
  53. * @Expose
  54. * @Groups({"customProduct:item"})
  55. */
  56. private ?string $description = NULL;
  57. /**
  58. * Valeur (prix) du produit
  59. *
  60. * @ORM\Column(type="float", nullable=false)
  61. *
  62. * @Assert\PositiveOrZero
  63. *
  64. * @Expose
  65. * @Groups({"customProduct:list", "customProduct:item"})
  66. */
  67. private ?float $value = NULL;
  68. /**
  69. * Indique si le produit est actif
  70. *
  71. * @ORM\Column(type="boolean")
  72. *
  73. * @Expose
  74. * @Groups({"customProduct:list", "customProduct:item"})
  75. */
  76. private bool $enabled = TRUE;
  77. /**
  78. * Image du produit
  79. *
  80. * @ORM\Column(type="string", length=255, nullable=true)
  81. *
  82. * @Expose
  83. * @Groups({"customProduct:list", "customProduct:item"})
  84. */
  85. private ?string $image = NULL;
  86. /**
  87. * Vich Uploader
  88. *
  89. * @Vich\UploadableField(mapping="custom_product_images", fileNameProperty="image")
  90. * @var null|File
  91. */
  92. private ?File $imageFile = NULL;
  93. /**
  94. * Liste des contacts (email) quand il y a une demande pour ce produit
  95. *
  96. * @ORM\Column(type="array", nullable=true)
  97. *
  98. * @Expose
  99. * @Groups({"customProduct:item"})
  100. */
  101. private array $contacts = [];
  102. /**
  103. * Liste des champs pour commander ce produit
  104. *
  105. * @ORM\OneToMany(
  106. * targetEntity=CustomProductField::class,
  107. * mappedBy="product",
  108. * orphanRemoval=true,
  109. * cascade={"persist"}
  110. * )
  111. */
  112. private $fields;
  113. /**
  114. * Type de produit
  115. *
  116. * @ORM\ManyToOne(targetEntity=CustomProductType::class, inversedBy="templates")
  117. * @ORM\JoinColumn(nullable=false, name="type", referencedColumnName="slug")
  118. *
  119. * @Expose
  120. * @Groups({"customProduct:list", "customProduct:item"})
  121. */
  122. private ?CustomProductType $type = NULL;
  123. /**
  124. * @ORM\Column(type="string", length=10, nullable=true)
  125. *
  126. * @Expose
  127. * @Groups({"customProduct:list", "customProduct:item"})
  128. */
  129. private $sku;
  130. /**
  131. * @ORM\Column(type="integer", nullable=true)
  132. */
  133. private $stockAlert;
  134. /**
  135. * @ORM\Column(type="string", length=255, nullable=true)
  136. */
  137. private $reference;
  138. /**
  139. * @ORM\ManyToOne(targetEntity=User::class, inversedBy="customProducts")
  140. */
  141. private $createdBy;
  142. /**
  143. * @ORM\Column(type="text", nullable=true)
  144. */
  145. private $categoryValues;
  146. /**
  147. * @ORM\Column(type="text", nullable=true)
  148. */
  149. private $furtherInformation;
  150. /**
  151. * Stock du produit
  152. *
  153. * @ORM\Column(type="integer", nullable=true)
  154. *
  155. * @Assert\PositiveOrZero
  156. *
  157. * @Expose
  158. * @Groups({"customProduct:list", "customProduct:item"})
  159. */
  160. private ?int $stock = NULL;
  161. /**
  162. * Indique si on peut commander plusieurs fois le produit par commande
  163. *
  164. * @ORM\Column(type="boolean")
  165. *
  166. * @Expose
  167. * @Groups({"customProduct:list", "customProduct:item"})
  168. */
  169. private bool $allowedMultiple = TRUE;
  170. /**
  171. * Indique si le prix du produit est fixe
  172. *
  173. * @ORM\Column(type="boolean")
  174. *
  175. * @Expose
  176. * @Groups({"customProduct:list", "customProduct:item"})
  177. */
  178. private bool $fixedValue = TRUE;
  179. /**
  180. * Indique la catégorie de point par defaut
  181. *
  182. * @ORM\Column(type="string", nullable=true)
  183. *
  184. * @Expose
  185. * @Groups({"customProduct:list", "customProduct:item"})
  186. */
  187. private ?string $defaultPointCategory = NULL;
  188. use DateTrait;
  189. public function __construct()
  190. {
  191. $this->fields = new ArrayCollection();
  192. }
  193. /**
  194. * @Serializer\VirtualProperty()
  195. * @Serializer\SerializedName("array_category_values")
  196. *
  197. * @Expose
  198. * @Groups({"customProduct:list", "customProduct:item"})
  199. */
  200. public function getArrayCategoryValues()
  201. {
  202. return json_decode( $this->categoryValues, TRUE ) ?? [];
  203. }
  204. public function modifyValueToCategory( string $slug, int $value ): CustomProduct
  205. {
  206. $newArray = $this->getArrayCategoryValues();
  207. $newArray[ $slug ] = $value;
  208. $this->categoryValues = json_encode( $newArray );
  209. return $this;
  210. }
  211. public function getId(): ?int
  212. {
  213. return $this->id;
  214. }
  215. public function getImageFile(): ?File
  216. {
  217. return $this->imageFile;
  218. }
  219. public function setImageFile( File $imageFile ): CustomProduct
  220. {
  221. $this->imageFile = $imageFile;
  222. $this->updatedAt = new DateTime();
  223. return $this;
  224. }
  225. public function getLabel(): ?string
  226. {
  227. return $this->label;
  228. }
  229. public function setLabel( string $label ): self
  230. {
  231. $this->label = $label;
  232. return $this;
  233. }
  234. public function getDescription(): ?string
  235. {
  236. return $this->description;
  237. }
  238. public function setDescription( ?string $description ): self
  239. {
  240. $this->description = $description;
  241. return $this;
  242. }
  243. public function getValue(): ?float
  244. {
  245. return $this->value;
  246. }
  247. public function setValue( ?float $value ): self
  248. {
  249. $this->value = $value;
  250. return $this;
  251. }
  252. public function isEnabled(): ?bool
  253. {
  254. return $this->enabled;
  255. }
  256. public function setEnabled( bool $enabled ): self
  257. {
  258. $this->enabled = $enabled;
  259. return $this;
  260. }
  261. public function getImage(): ?string
  262. {
  263. return $this->image;
  264. }
  265. public function setImage( ?string $image ): self
  266. {
  267. $this->image = $image;
  268. return $this;
  269. }
  270. public function getContacts(): ?array
  271. {
  272. return $this->contacts;
  273. }
  274. public function setContacts( array $contacts ): self
  275. {
  276. $this->contacts = $contacts;
  277. return $this;
  278. }
  279. /**
  280. * @return Collection<int, CustomProductField>
  281. */
  282. public function getFields(): Collection
  283. {
  284. return $this->fields;
  285. }
  286. public function addField( CustomProductField $field ): self
  287. {
  288. if ( !$this->fields->contains( $field ) ) {
  289. $this->fields[] = $field;
  290. $field->setProduct( $this );
  291. }
  292. return $this;
  293. }
  294. public function removeField( CustomProductField $field ): self
  295. {
  296. if ( $this->fields->removeElement( $field ) ) {
  297. // set the owning side to null (unless already changed)
  298. if ( $field->getProduct() === $this ) {
  299. $field->setProduct( NULL );
  300. }
  301. }
  302. return $this;
  303. }
  304. public function getType(): ?CustomProductType
  305. {
  306. return $this->type;
  307. }
  308. public function setType( ?CustomProductType $type ): self
  309. {
  310. $this->type = $type;
  311. return $this;
  312. }
  313. public function getSku(): ?string
  314. {
  315. return $this->sku;
  316. }
  317. public function setSku( ?string $sku ): self
  318. {
  319. $this->sku = $sku;
  320. return $this;
  321. }
  322. public function getStockAlert(): ?int
  323. {
  324. return $this->stockAlert;
  325. }
  326. public function setStockAlert( ?int $stockAlert ): self
  327. {
  328. $this->stockAlert = $stockAlert;
  329. return $this;
  330. }
  331. public function getStock(): ?int
  332. {
  333. return $this->stock;
  334. }
  335. public function setStock( ?int $stock ): self
  336. {
  337. $this->stock = $stock;
  338. return $this;
  339. }
  340. public function getReference(): ?string
  341. {
  342. return $this->reference;
  343. }
  344. public function setReference( ?string $reference ): self
  345. {
  346. $this->reference = $reference;
  347. return $this;
  348. }
  349. public function getCreatedBy(): ?User
  350. {
  351. return $this->createdBy;
  352. }
  353. public function setCreatedBy( ?User $createdBy ): self
  354. {
  355. $this->createdBy = $createdBy;
  356. return $this;
  357. }
  358. public function getCategoryValues(): ?string
  359. {
  360. return $this->categoryValues;
  361. }
  362. public function setCategoryValues( ?string $categoryValues ): self
  363. {
  364. $this->categoryValues = $categoryValues;
  365. return $this;
  366. }
  367. public function getFurtherInformation(): ?string
  368. {
  369. return $this->furtherInformation;
  370. }
  371. public function setFurtherInformation( ?string $furtherInformation ): self
  372. {
  373. $this->furtherInformation = $furtherInformation;
  374. return $this;
  375. }
  376. public function isAllowedMultiple(): ?bool
  377. {
  378. return $this->allowedMultiple;
  379. }
  380. public function setAllowedMultiple( bool $allowedMultiple ): self
  381. {
  382. $this->allowedMultiple = $allowedMultiple;
  383. return $this;
  384. }
  385. public function isFixedValue(): ?bool
  386. {
  387. return $this->fixedValue;
  388. }
  389. public function setFixedValue( bool $fixedValue ): self
  390. {
  391. $this->fixedValue = $fixedValue;
  392. return $this;
  393. }
  394. public function getDefaultPointCategory(): ?string
  395. {
  396. return $this->defaultPointCategory;
  397. }
  398. public function setDefaultPointCategory(?string $defaultPointCategory): self
  399. {
  400. $this->defaultPointCategory = $defaultPointCategory;
  401. return $this;
  402. }
  403. }