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\OneToMany(targetEntity=QuotasProductFree::class, mappedBy="customProduct", cascade={"persist", "remove"}, orphanRemoval=true)
  220. *
  221. * @var Collection<int, QuotasProductFree>
  222. */
  223. private Collection $quotaProductFrees;
  224. /**
  225. * @ORM\Column(type="json", nullable=true)
  226. */
  227. private $visibilityFromJobs = [];
  228. /**
  229. * @ORM\OneToMany(targetEntity=QuotaProductJob::class, mappedBy="customProduct", cascade={"persist", "remove"}, orphanRemoval=true)
  230. */
  231. private $quotaProductJobs;
  232. /**
  233. * Indique si une transaction de débit est créé lors de la commande
  234. *
  235. * @ORM\Column(type="boolean")
  236. */
  237. private $debitAtOrder = false;
  238. /**
  239. * @ORM\OneToMany(targetEntity=CustomProductStepPrice::class, mappedBy="customProduct", cascade={"persist", "remove"}, orphanRemoval=true)
  240. *
  241. * @Expose
  242. * @Groups({"customProduct:item"})
  243. */
  244. private Collection $customProductStepPrices;
  245. /**
  246. * @ORM\ManyToMany(targetEntity=ProductCategory::class, mappedBy="products")
  247. *
  248. * @Expose
  249. * @Groups({"customProduct"})
  250. */
  251. private Collection $categories;
  252. use DateTrait;
  253. public function __construct()
  254. {
  255. $this->fields = new ArrayCollection();
  256. $this->quotaProductUsers = new ArrayCollection();
  257. $this->quotaProductFrees = new ArrayCollection();
  258. $this->quotaProductJobs = new ArrayCollection();
  259. $this->customProductStepPrices = new ArrayCollection();
  260. $this->images = new ArrayCollection();
  261. $this->categories = new ArrayCollection();
  262. }
  263. public function __toString(): string
  264. {
  265. return $this->label;
  266. }
  267. /**
  268. * @Serializer\VirtualProperty()
  269. * @Serializer\SerializedName("array_category_values")
  270. *
  271. * @Expose
  272. * @Groups({"customProduct:list", "customProduct:item"})
  273. */
  274. public function getArrayCategoryValues()
  275. {
  276. return json_decode($this->categoryValues, true) ?? [];
  277. }
  278. public function modifyValueToCategory(string $slug, int $value): CustomProduct
  279. {
  280. $newArray = $this->getArrayCategoryValues();
  281. $newArray[$slug] = $value;
  282. $this->categoryValues = json_encode($newArray);
  283. return $this;
  284. }
  285. public function getId(): ?int
  286. {
  287. return $this->id;
  288. }
  289. public function setId(int $id): self
  290. {
  291. $this->id = $id;
  292. return $this;
  293. }
  294. public function getTemplate(): ?CustomProductTemplate
  295. {
  296. return $this->template;
  297. }
  298. public function setTemplate(?CustomProductTemplate $template): self
  299. {
  300. $this->template = $template;
  301. return $this;
  302. }
  303. public function getImageFile(): ?File
  304. {
  305. return $this->imageFile;
  306. }
  307. public function setImageFile(File $imageFile): CustomProduct
  308. {
  309. $this->imageFile = $imageFile;
  310. $this->updatedAt = new DateTime();
  311. return $this;
  312. }
  313. public function getLabel(): ?string
  314. {
  315. return $this->label;
  316. }
  317. public function setLabel(string $label): self
  318. {
  319. $this->label = $label;
  320. return $this;
  321. }
  322. public function getDescription(): ?string
  323. {
  324. return $this->description;
  325. }
  326. public function setDescription(?string $description): self
  327. {
  328. $this->description = $description;
  329. return $this;
  330. }
  331. public function getValue(): ?float
  332. {
  333. return $this->value;
  334. }
  335. public function setValue(?float $value): self
  336. {
  337. $this->value = $value;
  338. return $this;
  339. }
  340. public function isEnabled(): ?bool
  341. {
  342. return $this->enabled;
  343. }
  344. public function setEnabled(bool $enabled): self
  345. {
  346. $this->enabled = $enabled;
  347. return $this;
  348. }
  349. public function getImage(): ?string
  350. {
  351. return $this->image;
  352. }
  353. public function setImage(?string $image): self
  354. {
  355. $this->image = $image;
  356. return $this;
  357. }
  358. /**
  359. * @return Collection<int, CustomProductField>
  360. */
  361. public function getImages(): Collection
  362. {
  363. return $this->images;
  364. }
  365. public function addImage(CustomProductImage $image): self
  366. {
  367. if (!$this->images->contains($image)) {
  368. $this->images[] = $image;
  369. $image->setProduct($this);
  370. }
  371. return $this;
  372. }
  373. public function removeImage(CustomProductImage $image): self
  374. {
  375. if ($this->images->removeElement($image)) {
  376. // set the owning side to null (unless already changed)
  377. if ($image->getProduct() === $this) {
  378. $image->setProduct(null);
  379. }
  380. }
  381. return $this;
  382. }
  383. public function getContacts(): ?array
  384. {
  385. return $this->contacts;
  386. }
  387. public function setContacts(array $contacts): self
  388. {
  389. $this->contacts = $contacts;
  390. return $this;
  391. }
  392. /**
  393. * @return Collection<int, CustomProductField>
  394. */
  395. public function getFields(): Collection
  396. {
  397. return $this->fields;
  398. }
  399. public function addField(CustomProductField $field): self
  400. {
  401. if (!$this->fields->contains($field)) {
  402. $this->fields[] = $field;
  403. $field->setProduct($this);
  404. }
  405. return $this;
  406. }
  407. public function removeField(CustomProductField $field): self
  408. {
  409. if ($this->fields->removeElement($field)) {
  410. // set the owning side to null (unless already changed)
  411. if ($field->getProduct() === $this) {
  412. $field->setProduct(null);
  413. }
  414. }
  415. return $this;
  416. }
  417. public function getType(): ?CustomProductType
  418. {
  419. return $this->type;
  420. }
  421. public function setType(?CustomProductType $type): self
  422. {
  423. $this->type = $type;
  424. return $this;
  425. }
  426. public function getSku(): ?string
  427. {
  428. return $this->sku;
  429. }
  430. public function setSku(?string $sku): self
  431. {
  432. $this->sku = $sku;
  433. return $this;
  434. }
  435. public function getStockAlert(): ?int
  436. {
  437. return $this->stockAlert;
  438. }
  439. public function setStockAlert(?int $stockAlert): self
  440. {
  441. $this->stockAlert = $stockAlert;
  442. return $this;
  443. }
  444. public function getStock(): ?int
  445. {
  446. return $this->stock;
  447. }
  448. public function setStock(?int $stock): self
  449. {
  450. $this->stock = $stock;
  451. return $this;
  452. }
  453. public function getReference(): ?string
  454. {
  455. return $this->reference;
  456. }
  457. public function setReference(?string $reference): self
  458. {
  459. $this->reference = $reference;
  460. return $this;
  461. }
  462. public function getCreatedBy(): ?User
  463. {
  464. return $this->createdBy;
  465. }
  466. public function setCreatedBy(?User $createdBy): self
  467. {
  468. $this->createdBy = $createdBy;
  469. return $this;
  470. }
  471. public function getCategoryValues(): ?string
  472. {
  473. return $this->categoryValues;
  474. }
  475. public function setCategoryValues(?string $categoryValues): self
  476. {
  477. $this->categoryValues = $categoryValues;
  478. return $this;
  479. }
  480. public function getFurtherInformation(): ?string
  481. {
  482. return $this->furtherInformation;
  483. }
  484. public function setFurtherInformation(?string $furtherInformation): self
  485. {
  486. $this->furtherInformation = $furtherInformation;
  487. return $this;
  488. }
  489. public function isAllowedMultiple(): ?bool
  490. {
  491. return $this->allowedMultiple;
  492. }
  493. public function setAllowedMultiple(bool $allowedMultiple): self
  494. {
  495. $this->allowedMultiple = $allowedMultiple;
  496. return $this;
  497. }
  498. public function isFixedValue(): ?bool
  499. {
  500. return $this->fixedValue;
  501. }
  502. public function setFixedValue(bool $fixedValue): self
  503. {
  504. $this->fixedValue = $fixedValue;
  505. return $this;
  506. }
  507. public function getDefaultPointCategory(): ?string
  508. {
  509. return $this->defaultPointCategory;
  510. }
  511. public function setDefaultPointCategory(?string $defaultPointCategory): self
  512. {
  513. $this->defaultPointCategory = $defaultPointCategory;
  514. return $this;
  515. }
  516. public function getProductUserQuota(): ?int
  517. {
  518. return $this->productUserQuota;
  519. }
  520. public function setProductUserQuota(?int $productUserQuota): self
  521. {
  522. $this->productUserQuota = $productUserQuota;
  523. return $this;
  524. }
  525. /**
  526. * @return Collection<int, QuotaProductUser>
  527. */
  528. public function getQuotaProductUsers(): Collection
  529. {
  530. return $this->quotaProductUsers;
  531. }
  532. public function addQuotaProductUser(QuotaProductUser $quotaProductUser): self
  533. {
  534. if (!$this->quotaProductUsers->contains($quotaProductUser)) {
  535. $this->quotaProductUsers[] = $quotaProductUser;
  536. $quotaProductUser->setCustomProduct($this);
  537. }
  538. return $this;
  539. }
  540. public function removeQuotaProductUser(QuotaProductUser $quotaProductUser): self
  541. {
  542. if ($this->quotaProductUsers->removeElement($quotaProductUser)) {
  543. // set the owning side to null (unless already changed)
  544. if ($quotaProductUser->getCustomProduct() === $this) {
  545. $quotaProductUser->setCustomProduct(null);
  546. }
  547. }
  548. return $this;
  549. }
  550. /**
  551. * @return Collection<int, QuotasProductFree>
  552. */
  553. public function getQuotaProductFrees(): Collection
  554. {
  555. return $this->quotaProductFrees;
  556. }
  557. public function addQuotaProductFree(QuotasProductFree $quotaProductFree): self
  558. {
  559. if (!$this->quotaProductFrees->contains($quotaProductFree)) {
  560. $this->quotaProductFrees->add($quotaProductFree);
  561. $quotaProductFree->setCustomProduct($this);
  562. }
  563. return $this;
  564. }
  565. public function removeQuotaProductFree(QuotasProductFree $quotaProductFree): self
  566. {
  567. if ($this->quotaProductFrees->removeElement($quotaProductFree) && $quotaProductFree->getCustomProduct() === $this) {
  568. $quotaProductFree->setCustomProduct(null);
  569. }
  570. return $this;
  571. }
  572. public function getVisibilityFromJobs(): ?array
  573. {
  574. return $this->visibilityFromJobs;
  575. }
  576. public function setVisibilityFromJobs(?array $visibilityFromJobs): self
  577. {
  578. $this->visibilityFromJobs = $visibilityFromJobs;
  579. return $this;
  580. }
  581. public function getQuotaProductJobs(): Collection
  582. {
  583. return $this->quotaProductJobs;
  584. }
  585. public function addQuotaProductJob(QuotaProductJob $quotaProductJob): self
  586. {
  587. if (!$this->quotaProductJobs->contains($quotaProductJob)) {
  588. $this->quotaProductJobs->add($quotaProductJob);
  589. $quotaProductJob->setCustomProduct($this);
  590. }
  591. return $this;
  592. }
  593. public function removeQuotaProductJob(QuotaProductJob $quotaProductJob): self
  594. {
  595. if ($this->quotaProductJobs->removeElement($quotaProductJob)) {
  596. if ($quotaProductJob->getCustomProduct() === $this) {
  597. $quotaProductJob->setCustomProduct(null);
  598. }
  599. }
  600. return $this;
  601. }
  602. public function isDebitAtOrder(): ?bool
  603. {
  604. return $this->debitAtOrder;
  605. }
  606. public function setDebitAtOrder(bool $debitAtOrder): self
  607. {
  608. $this->debitAtOrder = $debitAtOrder;
  609. return $this;
  610. }
  611. public function getCustomProductStepPrices(): Collection
  612. {
  613. return $this->customProductStepPrices;
  614. }
  615. public function addCustomProductStepPrice(CustomProductStepPrice $customProductStepPrice): self
  616. {
  617. if (!$this->customProductStepPrices->contains($customProductStepPrice)) {
  618. $this->customProductStepPrices[] = $customProductStepPrice;
  619. $customProductStepPrice->setCustomProduct($this);
  620. }
  621. return $this;
  622. }
  623. public function removeCustomProductStepPrice(CustomProductStepPrice $customProductStepPrice): self
  624. {
  625. if ($this->customProductStepPrices->removeElement($customProductStepPrice)) {
  626. // set the owning side to null (unless already changed)
  627. if ($customProductStepPrice->getCustomProduct() === $this) {
  628. $customProductStepPrice->setCustomProduct(null);
  629. }
  630. }
  631. return $this;
  632. }
  633. public function getCategories(): Collection
  634. {
  635. return $this->categories;
  636. }
  637. public function addCategory(ProductCategory $category): CustomProduct
  638. {
  639. if (!$this->categories->contains($category)) {
  640. $this->categories[] = $category;
  641. $category->addProduct($this);
  642. }
  643. return $this;
  644. }
  645. public function removeCategory(ProductCategory $category): CustomProduct
  646. {
  647. if ($this->categories->removeElement($category)) {
  648. $category->removeProduct($this);
  649. }
  650. return $this;
  651. }
  652. }