src/Entity/CustomProduct.php line 33

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. use App\Validator\NoOverlappingStepPrices;
  16. use App\Validator\ValueEqualFirstStepPrices;
  17. use App\Validator\NoGapStepPrices;
  18. /**
  19. * Produit personnalisé
  20. *
  21. * @ORM\Entity(repositoryClass=CustomProductRepository::class)
  22. *
  23. * @Serializer\ExclusionPolicy("ALL")
  24. * @Vich\Uploadable
  25. *
  26. * @NoOverlappingStepPrices
  27. * @ValueEqualFirstStepPrices
  28. * @NoGapStepPrices
  29. */
  30. class CustomProduct
  31. {
  32. /**
  33. * @ORM\Id
  34. * @ORM\GeneratedValue
  35. * @ORM\Column(type="integer")
  36. *
  37. * @Expose
  38. * @Groups({"customProduct:list", "customProduct:item", "customProductCategory:list"})
  39. */
  40. private ?int $id = null;
  41. /**
  42. * Label du produit
  43. *
  44. * @ORM\Column(type="string", length=255)
  45. *
  46. * @Assert\Length(
  47. * min = 2,
  48. * max = 255,
  49. * )
  50. *
  51. * @Expose
  52. * @Groups({"customProduct:list", "customProduct:item", "customProductCategory:list"})
  53. */
  54. private ?string $label = null;
  55. /**
  56. * Description du produit
  57. *
  58. * @ORM\Column(type="text", nullable=true)
  59. *
  60. * @Expose
  61. * @Groups({"customProduct:item"})
  62. */
  63. private ?string $description = null;
  64. /**
  65. * Valeur (prix) du produit
  66. *
  67. * @ORM\Column(type="float", nullable=false)
  68. *
  69. * @Assert\PositiveOrZero
  70. *
  71. * @Expose
  72. * @Groups({"customProduct:list", "customProduct:item"})
  73. */
  74. private ?float $value = null;
  75. /**
  76. * Indique si le produit est actif
  77. *
  78. * @ORM\Column(type="boolean")
  79. *
  80. * @Expose
  81. * @Groups({"customProduct:list", "customProduct:item"})
  82. */
  83. private bool $enabled = true;
  84. /**
  85. * Image du produit (ancien systeme)
  86. *
  87. * @ORM\Column(type="string", length=255, nullable=true)
  88. */
  89. private ?string $image = null;
  90. /**
  91. * Vich Uploader
  92. *
  93. * @Vich\UploadableField(mapping="custom_product_images", fileNameProperty="image")
  94. * @var null|File
  95. */
  96. private ?File $imageFile = null;
  97. /**
  98. * Liste des contacts (email) quand il y a une demande pour ce produit
  99. *
  100. * @ORM\Column(type="array", nullable=true)
  101. *
  102. * @Expose
  103. * @Groups({"customProduct:item"})
  104. */
  105. private array $contacts = [];
  106. /**
  107. * Liste des champs pour commander ce produit
  108. *
  109. * @ORM\OneToMany(
  110. * targetEntity=CustomProductField::class,
  111. * mappedBy="product",
  112. * orphanRemoval=true,
  113. * cascade={"persist","remove"}
  114. * )
  115. */
  116. private $fields;
  117. /**
  118. * Liste des images
  119. *
  120. * @ORM\OneToMany(
  121. * targetEntity=CustomProductImage::class,
  122. * mappedBy="product",
  123. * orphanRemoval=true,
  124. * cascade={"persist","remove"}
  125. * )
  126. *
  127. * @Expose
  128. * @Groups({"customProduct:list", "customProduct:item"})
  129. */
  130. private ?Collection $images;
  131. /**
  132. * @ORM\ManyToOne(targetEntity=CustomProductTemplate::class)
  133. */
  134. private ?CustomProductTemplate $template = null;
  135. /**
  136. * Type de produit
  137. *
  138. * @ORM\ManyToOne(targetEntity=CustomProductType::class, inversedBy="templates")
  139. * @ORM\JoinColumn(nullable=false, name="type", referencedColumnName="slug")
  140. *
  141. * @Expose
  142. * @Groups({"customProduct:list", "customProduct:item"})
  143. */
  144. private ?CustomProductType $type = null;
  145. /**
  146. * @ORM\Column(type="string", length=10, nullable=true)
  147. *
  148. * @Expose
  149. * @Groups({"customProduct:list", "customProduct:item"})
  150. */
  151. private $sku;
  152. /**
  153. * @ORM\Column(type="integer", nullable=true)
  154. */
  155. private $stockAlert;
  156. /**
  157. * @ORM\Column(type="string", length=255, nullable=true)
  158. */
  159. private $reference;
  160. /**
  161. * @ORM\ManyToOne(targetEntity=User::class, inversedBy="customProducts")
  162. */
  163. private $createdBy;
  164. /**
  165. * @ORM\Column(type="text", nullable=true)
  166. */
  167. private $categoryValues;
  168. /**
  169. * @ORM\Column(type="text", nullable=true)
  170. */
  171. private $furtherInformation;
  172. /**
  173. * Stock du produit
  174. *
  175. * @ORM\Column(type="integer", nullable=true)
  176. *
  177. * @Assert\PositiveOrZero
  178. *
  179. * @Expose
  180. * @Groups({"customProduct:list", "customProduct:item"})
  181. */
  182. private ?int $stock = null;
  183. /**
  184. * Indique si on peut commander plusieurs fois le produit par commande
  185. *
  186. * @ORM\Column(type="boolean")
  187. *
  188. * @Expose
  189. * @Groups({"customProduct:list", "customProduct:item"})
  190. */
  191. private bool $allowedMultiple = true;
  192. /**
  193. * Indique si le prix du produit est fixe
  194. *
  195. * @ORM\Column(type="boolean")
  196. *
  197. * @Expose
  198. * @Groups({"customProduct:list", "customProduct:item"})
  199. */
  200. private bool $fixedValue = true;
  201. /**
  202. * Indique la catégorie de point par defaut
  203. *
  204. * @ORM\Column(type="string", nullable=true)
  205. *
  206. * @Expose
  207. * @Groups({"customProduct:list", "customProduct:item"})
  208. */
  209. private ?string $defaultPointCategory = null;
  210. /**
  211. * @ORM\Column(type="integer", nullable=true)
  212. */
  213. private $productUserQuota;
  214. /**
  215. * @ORM\OneToMany(targetEntity=QuotaProductUser::class, mappedBy="customProduct", orphanRemoval=true)
  216. */
  217. private $quotaProductUsers;
  218. /**
  219. * @ORM\Column(type="json", nullable=true)
  220. */
  221. private $visibilityFromJobs = [];
  222. /**
  223. * @ORM\OneToMany(targetEntity=QuotaProductJob::class, mappedBy="customProduct", cascade={"persist", "remove"}, orphanRemoval=true)
  224. */
  225. private $quotaProductJobs;
  226. /**
  227. * Indique si une transaction de débit est créé lors de la commande
  228. *
  229. * @ORM\Column(type="boolean")
  230. */
  231. private $debitAtOrder = false;
  232. /**
  233. * @ORM\OneToMany(targetEntity=CustomProductStepPrice::class, mappedBy="customProduct", cascade={"persist", "remove"}, orphanRemoval=true)
  234. *
  235. * @Expose
  236. * @Groups({"customProduct:item"})
  237. */
  238. private Collection $customProductStepPrices;
  239. /**
  240. * @ORM\ManyToMany(targetEntity=CustomProductCategory::class, mappedBy="products")
  241. *
  242. * @Expose
  243. * @Groups({"customProduct"})
  244. */
  245. private Collection $categories;
  246. use DateTrait;
  247. public function __construct()
  248. {
  249. $this->fields = new ArrayCollection();
  250. $this->quotaProductUsers = new ArrayCollection();
  251. $this->quotaProductJobs = new ArrayCollection();
  252. $this->customProductStepPrices = new ArrayCollection();
  253. $this->images = new ArrayCollection();
  254. $this->categories = new ArrayCollection();
  255. }
  256. public function __toString(): string
  257. {
  258. return $this->label;
  259. }
  260. /**
  261. * @Serializer\VirtualProperty()
  262. * @Serializer\SerializedName("array_category_values")
  263. *
  264. * @Expose
  265. * @Groups({"customProduct:list", "customProduct:item"})
  266. */
  267. public function getArrayCategoryValues()
  268. {
  269. return json_decode($this->categoryValues, true) ?? [];
  270. }
  271. public function modifyValueToCategory(string $slug, int $value): CustomProduct
  272. {
  273. $newArray = $this->getArrayCategoryValues();
  274. $newArray[$slug] = $value;
  275. $this->categoryValues = json_encode($newArray);
  276. return $this;
  277. }
  278. public function getId(): ?int
  279. {
  280. return $this->id;
  281. }
  282. public function setId(int $id): self
  283. {
  284. $this->id = $id;
  285. return $this;
  286. }
  287. public function getTemplate(): ?CustomProductTemplate
  288. {
  289. return $this->template;
  290. }
  291. public function setTemplate(?CustomProductTemplate $template): self
  292. {
  293. $this->template = $template;
  294. return $this;
  295. }
  296. public function getImageFile(): ?File
  297. {
  298. return $this->imageFile;
  299. }
  300. public function setImageFile(File $imageFile): CustomProduct
  301. {
  302. $this->imageFile = $imageFile;
  303. $this->updatedAt = new DateTime();
  304. return $this;
  305. }
  306. public function getLabel(): ?string
  307. {
  308. return $this->label;
  309. }
  310. public function setLabel(string $label): self
  311. {
  312. $this->label = $label;
  313. return $this;
  314. }
  315. public function getDescription(): ?string
  316. {
  317. return $this->description;
  318. }
  319. public function setDescription(?string $description): self
  320. {
  321. $this->description = $description;
  322. return $this;
  323. }
  324. public function getValue(): ?float
  325. {
  326. return $this->value;
  327. }
  328. public function setValue(?float $value): self
  329. {
  330. $this->value = $value;
  331. return $this;
  332. }
  333. public function isEnabled(): ?bool
  334. {
  335. return $this->enabled;
  336. }
  337. public function setEnabled(bool $enabled): self
  338. {
  339. $this->enabled = $enabled;
  340. return $this;
  341. }
  342. public function getImage(): ?string
  343. {
  344. return $this->image;
  345. }
  346. public function setImage(?string $image): self
  347. {
  348. $this->image = $image;
  349. return $this;
  350. }
  351. /**
  352. * @return Collection<int, CustomProductField>
  353. */
  354. public function getImages(): Collection
  355. {
  356. return $this->images;
  357. }
  358. public function addImage(CustomProductImage $image): self
  359. {
  360. if (!$this->images->contains($image)) {
  361. $this->images[] = $image;
  362. $image->setProduct($this);
  363. }
  364. return $this;
  365. }
  366. public function removeImage(CustomProductImage $image): self
  367. {
  368. if ($this->images->removeElement($image)) {
  369. // set the owning side to null (unless already changed)
  370. if ($image->getProduct() === $this) {
  371. $image->setProduct(null);
  372. }
  373. }
  374. return $this;
  375. }
  376. public function getContacts(): ?array
  377. {
  378. return $this->contacts;
  379. }
  380. public function setContacts(array $contacts): self
  381. {
  382. $this->contacts = $contacts;
  383. return $this;
  384. }
  385. /**
  386. * @return Collection<int, CustomProductField>
  387. */
  388. public function getFields(): Collection
  389. {
  390. return $this->fields;
  391. }
  392. public function addField(CustomProductField $field): self
  393. {
  394. if (!$this->fields->contains($field)) {
  395. $this->fields[] = $field;
  396. $field->setProduct($this);
  397. }
  398. return $this;
  399. }
  400. public function removeField(CustomProductField $field): self
  401. {
  402. if ($this->fields->removeElement($field)) {
  403. // set the owning side to null (unless already changed)
  404. if ($field->getProduct() === $this) {
  405. $field->setProduct(null);
  406. }
  407. }
  408. return $this;
  409. }
  410. public function getType(): ?CustomProductType
  411. {
  412. return $this->type;
  413. }
  414. public function setType(?CustomProductType $type): self
  415. {
  416. $this->type = $type;
  417. return $this;
  418. }
  419. public function getSku(): ?string
  420. {
  421. return $this->sku;
  422. }
  423. public function setSku(?string $sku): self
  424. {
  425. $this->sku = $sku;
  426. return $this;
  427. }
  428. public function getStockAlert(): ?int
  429. {
  430. return $this->stockAlert;
  431. }
  432. public function setStockAlert(?int $stockAlert): self
  433. {
  434. $this->stockAlert = $stockAlert;
  435. return $this;
  436. }
  437. public function getStock(): ?int
  438. {
  439. return $this->stock;
  440. }
  441. public function setStock(?int $stock): self
  442. {
  443. $this->stock = $stock;
  444. return $this;
  445. }
  446. public function getReference(): ?string
  447. {
  448. return $this->reference;
  449. }
  450. public function setReference(?string $reference): self
  451. {
  452. $this->reference = $reference;
  453. return $this;
  454. }
  455. public function getCreatedBy(): ?User
  456. {
  457. return $this->createdBy;
  458. }
  459. public function setCreatedBy(?User $createdBy): self
  460. {
  461. $this->createdBy = $createdBy;
  462. return $this;
  463. }
  464. public function getCategoryValues(): ?string
  465. {
  466. return $this->categoryValues;
  467. }
  468. public function setCategoryValues(?string $categoryValues): self
  469. {
  470. $this->categoryValues = $categoryValues;
  471. return $this;
  472. }
  473. public function getFurtherInformation(): ?string
  474. {
  475. return $this->furtherInformation;
  476. }
  477. public function setFurtherInformation(?string $furtherInformation): self
  478. {
  479. $this->furtherInformation = $furtherInformation;
  480. return $this;
  481. }
  482. public function isAllowedMultiple(): ?bool
  483. {
  484. return $this->allowedMultiple;
  485. }
  486. public function setAllowedMultiple(bool $allowedMultiple): self
  487. {
  488. $this->allowedMultiple = $allowedMultiple;
  489. return $this;
  490. }
  491. public function isFixedValue(): ?bool
  492. {
  493. return $this->fixedValue;
  494. }
  495. public function setFixedValue(bool $fixedValue): self
  496. {
  497. $this->fixedValue = $fixedValue;
  498. return $this;
  499. }
  500. public function getDefaultPointCategory(): ?string
  501. {
  502. return $this->defaultPointCategory;
  503. }
  504. public function setDefaultPointCategory(?string $defaultPointCategory): self
  505. {
  506. $this->defaultPointCategory = $defaultPointCategory;
  507. return $this;
  508. }
  509. public function getProductUserQuota(): ?int
  510. {
  511. return $this->productUserQuota;
  512. }
  513. public function setProductUserQuota(?int $productUserQuota): self
  514. {
  515. $this->productUserQuota = $productUserQuota;
  516. return $this;
  517. }
  518. /**
  519. * @return Collection<int, QuotaProductUser>
  520. */
  521. public function getQuotaProductUsers(): Collection
  522. {
  523. return $this->quotaProductUsers;
  524. }
  525. public function addQuotaProductUser(QuotaProductUser $quotaProductUser): self
  526. {
  527. if (!$this->quotaProductUsers->contains($quotaProductUser)) {
  528. $this->quotaProductUsers[] = $quotaProductUser;
  529. $quotaProductUser->setCustomProduct($this);
  530. }
  531. return $this;
  532. }
  533. public function removeQuotaProductUser(QuotaProductUser $quotaProductUser): self
  534. {
  535. if ($this->quotaProductUsers->removeElement($quotaProductUser)) {
  536. // set the owning side to null (unless already changed)
  537. if ($quotaProductUser->getCustomProduct() === $this) {
  538. $quotaProductUser->setCustomProduct(null);
  539. }
  540. }
  541. return $this;
  542. }
  543. public function getVisibilityFromJobs(): ?array
  544. {
  545. return $this->visibilityFromJobs;
  546. }
  547. public function setVisibilityFromJobs(?array $visibilityFromJobs): self
  548. {
  549. $this->visibilityFromJobs = $visibilityFromJobs;
  550. return $this;
  551. }
  552. public function getQuotaProductJobs(): Collection
  553. {
  554. return $this->quotaProductJobs;
  555. }
  556. public function addQuotaProductJob(QuotaProductJob $quotaProductJob): self
  557. {
  558. if (!$this->quotaProductJobs->contains($quotaProductJob)) {
  559. $this->quotaProductJobs->add($quotaProductJob);
  560. $quotaProductJob->setCustomProduct($this);
  561. }
  562. return $this;
  563. }
  564. public function removeQuotaProductJob(QuotaProductJob $quotaProductJob): self
  565. {
  566. if ($this->quotaProductJobs->removeElement($quotaProductJob)) {
  567. if ($quotaProductJob->getCustomProduct() === $this) {
  568. $quotaProductJob->setCustomProduct(null);
  569. }
  570. }
  571. return $this;
  572. }
  573. public function isDebitAtOrder(): ?bool
  574. {
  575. return $this->debitAtOrder;
  576. }
  577. public function setDebitAtOrder(bool $debitAtOrder): self
  578. {
  579. $this->debitAtOrder = $debitAtOrder;
  580. return $this;
  581. }
  582. public function getCustomProductStepPrices(): Collection
  583. {
  584. return $this->customProductStepPrices;
  585. }
  586. public function addCustomProductStepPrice(CustomProductStepPrice $customProductStepPrice): self
  587. {
  588. if (!$this->customProductStepPrices->contains($customProductStepPrice)) {
  589. $this->customProductStepPrices[] = $customProductStepPrice;
  590. $customProductStepPrice->setCustomProduct($this);
  591. }
  592. return $this;
  593. }
  594. public function removeCustomProductStepPrice(CustomProductStepPrice $customProductStepPrice): self
  595. {
  596. if ($this->customProductStepPrices->removeElement($customProductStepPrice)) {
  597. // set the owning side to null (unless already changed)
  598. if ($customProductStepPrice->getCustomProduct() === $this) {
  599. $customProductStepPrice->setCustomProduct(null);
  600. }
  601. }
  602. return $this;
  603. }
  604. public function getCategories(): Collection
  605. {
  606. return $this->categories;
  607. }
  608. public function addCategory(CustomProductCategory $category): CustomProduct
  609. {
  610. if (!$this->categories->contains($category)) {
  611. $this->categories[] = $category;
  612. $category->addProduct($this);
  613. }
  614. return $this;
  615. }
  616. public function removeCategory(CustomProductCategory $category): CustomProduct
  617. {
  618. if ($this->categories->removeElement($category)) {
  619. $category->removeProduct($this);
  620. }
  621. return $this;
  622. }
  623. }