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