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. /**
  189. * @ORM\Column(type="integer", nullable=true)
  190. */
  191. private $productUserQuota;
  192. /**
  193. * @ORM\OneToMany(targetEntity=QuotaProductUser::class, mappedBy="customProduct", orphanRemoval=true)
  194. */
  195. private $quotaProductUsers;
  196. /**
  197. * @ORM\Column(type="json", nullable=true)
  198. */
  199. private $visibilityFromJobs = [];
  200. /**
  201. * @ORM\OneToMany(targetEntity=QuotaProductJob::class, mappedBy="customProduct", cascade={"persist", "remove"}, orphanRemoval=true)
  202. */
  203. private $quotaProductJobs;
  204. use DateTrait;
  205. public function __construct()
  206. {
  207. $this->fields = new ArrayCollection();
  208. $this->quotaProductUsers = new ArrayCollection();
  209. $this->quotaProductJobs = new ArrayCollection();
  210. }
  211. /**
  212. * @Serializer\VirtualProperty()
  213. * @Serializer\SerializedName("array_category_values")
  214. *
  215. * @Expose
  216. * @Groups({"customProduct:list", "customProduct:item"})
  217. */
  218. public function getArrayCategoryValues()
  219. {
  220. return json_decode( $this->categoryValues, TRUE ) ?? [];
  221. }
  222. public function modifyValueToCategory( string $slug, int $value ): CustomProduct
  223. {
  224. $newArray = $this->getArrayCategoryValues();
  225. $newArray[ $slug ] = $value;
  226. $this->categoryValues = json_encode( $newArray );
  227. return $this;
  228. }
  229. public function getId(): ?int
  230. {
  231. return $this->id;
  232. }
  233. public function getImageFile(): ?File
  234. {
  235. return $this->imageFile;
  236. }
  237. public function setImageFile( File $imageFile ): CustomProduct
  238. {
  239. $this->imageFile = $imageFile;
  240. $this->updatedAt = new DateTime();
  241. return $this;
  242. }
  243. public function getLabel(): ?string
  244. {
  245. return $this->label;
  246. }
  247. public function setLabel( string $label ): self
  248. {
  249. $this->label = $label;
  250. return $this;
  251. }
  252. public function getDescription(): ?string
  253. {
  254. return $this->description;
  255. }
  256. public function setDescription( ?string $description ): self
  257. {
  258. $this->description = $description;
  259. return $this;
  260. }
  261. public function getValue(): ?float
  262. {
  263. return $this->value;
  264. }
  265. public function setValue( ?float $value ): self
  266. {
  267. $this->value = $value;
  268. return $this;
  269. }
  270. public function isEnabled(): ?bool
  271. {
  272. return $this->enabled;
  273. }
  274. public function setEnabled( bool $enabled ): self
  275. {
  276. $this->enabled = $enabled;
  277. return $this;
  278. }
  279. public function getImage(): ?string
  280. {
  281. return $this->image;
  282. }
  283. public function setImage( ?string $image ): self
  284. {
  285. $this->image = $image;
  286. return $this;
  287. }
  288. public function getContacts(): ?array
  289. {
  290. return $this->contacts;
  291. }
  292. public function setContacts( array $contacts ): self
  293. {
  294. $this->contacts = $contacts;
  295. return $this;
  296. }
  297. /**
  298. * @return Collection<int, CustomProductField>
  299. */
  300. public function getFields(): Collection
  301. {
  302. return $this->fields;
  303. }
  304. public function addField( CustomProductField $field ): self
  305. {
  306. if ( !$this->fields->contains( $field ) ) {
  307. $this->fields[] = $field;
  308. $field->setProduct( $this );
  309. }
  310. return $this;
  311. }
  312. public function removeField( CustomProductField $field ): self
  313. {
  314. if ( $this->fields->removeElement( $field ) ) {
  315. // set the owning side to null (unless already changed)
  316. if ( $field->getProduct() === $this ) {
  317. $field->setProduct( NULL );
  318. }
  319. }
  320. return $this;
  321. }
  322. public function getType(): ?CustomProductType
  323. {
  324. return $this->type;
  325. }
  326. public function setType( ?CustomProductType $type ): self
  327. {
  328. $this->type = $type;
  329. return $this;
  330. }
  331. public function getSku(): ?string
  332. {
  333. return $this->sku;
  334. }
  335. public function setSku( ?string $sku ): self
  336. {
  337. $this->sku = $sku;
  338. return $this;
  339. }
  340. public function getStockAlert(): ?int
  341. {
  342. return $this->stockAlert;
  343. }
  344. public function setStockAlert( ?int $stockAlert ): self
  345. {
  346. $this->stockAlert = $stockAlert;
  347. return $this;
  348. }
  349. public function getStock(): ?int
  350. {
  351. return $this->stock;
  352. }
  353. public function setStock( ?int $stock ): self
  354. {
  355. $this->stock = $stock;
  356. return $this;
  357. }
  358. public function getReference(): ?string
  359. {
  360. return $this->reference;
  361. }
  362. public function setReference( ?string $reference ): self
  363. {
  364. $this->reference = $reference;
  365. return $this;
  366. }
  367. public function getCreatedBy(): ?User
  368. {
  369. return $this->createdBy;
  370. }
  371. public function setCreatedBy( ?User $createdBy ): self
  372. {
  373. $this->createdBy = $createdBy;
  374. return $this;
  375. }
  376. public function getCategoryValues(): ?string
  377. {
  378. return $this->categoryValues;
  379. }
  380. public function setCategoryValues( ?string $categoryValues ): self
  381. {
  382. $this->categoryValues = $categoryValues;
  383. return $this;
  384. }
  385. public function getFurtherInformation(): ?string
  386. {
  387. return $this->furtherInformation;
  388. }
  389. public function setFurtherInformation( ?string $furtherInformation ): self
  390. {
  391. $this->furtherInformation = $furtherInformation;
  392. return $this;
  393. }
  394. public function isAllowedMultiple(): ?bool
  395. {
  396. return $this->allowedMultiple;
  397. }
  398. public function setAllowedMultiple( bool $allowedMultiple ): self
  399. {
  400. $this->allowedMultiple = $allowedMultiple;
  401. return $this;
  402. }
  403. public function isFixedValue(): ?bool
  404. {
  405. return $this->fixedValue;
  406. }
  407. public function setFixedValue( bool $fixedValue ): self
  408. {
  409. $this->fixedValue = $fixedValue;
  410. return $this;
  411. }
  412. public function getDefaultPointCategory(): ?string
  413. {
  414. return $this->defaultPointCategory;
  415. }
  416. public function setDefaultPointCategory(?string $defaultPointCategory): self
  417. {
  418. $this->defaultPointCategory = $defaultPointCategory;
  419. return $this;
  420. }
  421. public function getProductUserQuota(): ?int
  422. {
  423. return $this->productUserQuota;
  424. }
  425. public function setProductUserQuota(?int $productUserQuota): self
  426. {
  427. $this->productUserQuota = $productUserQuota;
  428. return $this;
  429. }
  430. /**
  431. * @return Collection<int, QuotaProductUser>
  432. */
  433. public function getQuotaProductUsers(): Collection
  434. {
  435. return $this->quotaProductUsers;
  436. }
  437. public function addQuotaProductUser(QuotaProductUser $quotaProductUser): self
  438. {
  439. if (!$this->quotaProductUsers->contains($quotaProductUser)) {
  440. $this->quotaProductUsers[] = $quotaProductUser;
  441. $quotaProductUser->setCustomProduct($this);
  442. }
  443. return $this;
  444. }
  445. public function removeQuotaProductUser(QuotaProductUser $quotaProductUser): self
  446. {
  447. if ($this->quotaProductUsers->removeElement($quotaProductUser)) {
  448. // set the owning side to null (unless already changed)
  449. if ($quotaProductUser->getCustomProduct() === $this) {
  450. $quotaProductUser->setCustomProduct(null);
  451. }
  452. }
  453. return $this;
  454. }
  455. public function getVisibilityFromJobs(): ?array
  456. {
  457. return $this->visibilityFromJobs;
  458. }
  459. public function setVisibilityFromJobs(?array $visibilityFromJobs): self
  460. {
  461. $this->visibilityFromJobs = $visibilityFromJobs;
  462. return $this;
  463. }
  464. public function getQuotaProductJobs(): Collection
  465. {
  466. return $this->quotaProductJobs;
  467. }
  468. public function addQuotaProductJob(QuotaProductJob $quotaProductJob): self
  469. {
  470. if (!$this->quotaProductJobs->contains($quotaProductJob)) {
  471. $this->quotaProductJobs->add($quotaProductJob);
  472. $quotaProductJob->setCustomProduct($this);
  473. }
  474. return $this;
  475. }
  476. public function removeQuotaProductJob(QuotaProductJob $quotaProductJob): self
  477. {
  478. if ($this->quotaProductJobs->removeElement($quotaProductJob)) {
  479. if ($quotaProductJob->getCustomProduct() === $this) {
  480. $quotaProductJob->setCustomProduct(null);
  481. }
  482. }
  483. return $this;
  484. }
  485. }