src/Entity/Purchase.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Constants\Purchase as PurchaseStatus;
  4. use App\Repository\PurchaseRepository;
  5. use App\Traits\DateTrait;
  6. use DateTimeInterface;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use JMS\Serializer\Annotation as Serializer;
  11. use JMS\Serializer\Annotation\Expose;
  12. use JMS\Serializer\Annotation\Groups;
  13. /**
  14. * @ORM\Entity(repositoryClass=PurchaseRepository::class)
  15. *
  16. * @Serializer\ExclusionPolicy("ALL")
  17. */
  18. class Purchase
  19. {
  20. // Un nombre de points est attribué à chaque référence
  21. public const POINT_WITH_REFERENCE = 'point_with_reference';
  22. // On se référence au prix total payé par l’installateur
  23. public const POINT_PRICE_PAID_TOTAL = 'point_price_paid_total';
  24. // On se référence au prix unitaire par produit payé par l’installateur
  25. public const POINT_PRICE_PAID_PER_UNIT = 'point_price_paid_per_unit';
  26. // On se refere à la taille en m² (expert-fenetre.rehau) pour calculer les points multiplier par un coefficient
  27. public const POINT_PAID_PER_COEFFICIENT = 'point_paid_per_coefficient';
  28. public const STATUS_PENDING = PurchaseStatus::STATUS_PENDING;
  29. public const STATUS_VALIDATED = PurchaseStatus::STATUS_VALIDATED;
  30. public const STATUS_RETURNED = PurchaseStatus::STATUS_RETURNED;
  31. public const STATUS_REJECTED = PurchaseStatus::STATUS_REJECTED;
  32. /**
  33. * @ORM\Id
  34. * @ORM\GeneratedValue
  35. * @ORM\Column(type="integer")
  36. *
  37. * @Expose
  38. * @Groups({"purchase", "export_purchase_declaration_datatable"})
  39. */
  40. private ?int $id = null;
  41. /**
  42. * Numéro de facture
  43. *
  44. * @ORM\Column(type="string", length=64)
  45. *
  46. * @Expose
  47. * @Groups({"purchase", "export_purchase_declaration_datatable"})
  48. */
  49. private $invoiceNumber;
  50. /**
  51. * Date de facture
  52. *
  53. * @ORM\Column(type="date", nullable=true)
  54. *
  55. * @Expose
  56. * @Groups({"purchase", "export_purchase_declaration_datatable"})
  57. */
  58. private $invoiceDate;
  59. /**
  60. * Status de la déclaration
  61. *
  62. * @ORM\Column(type="integer", options={"default": self::STATUS_PENDING})
  63. *
  64. * @Expose
  65. * @Groups({"purchase", "export_purchase_declaration_datatable"})
  66. */
  67. private $status = self::STATUS_PENDING;
  68. /**
  69. * Raison du statut
  70. *
  71. * @ORM\Column(type="string", length=512, nullable=true)
  72. */
  73. private ?string $lastStatusReason = null;
  74. /**
  75. * Nom du distributeur
  76. *
  77. * @deprecated Relation sur {@see Distributor}
  78. *
  79. * @ORM\Column(type="string", length=255, nullable=true)
  80. */
  81. private ?string $distributorName = null;
  82. /**
  83. * Code postal du distributeur
  84. *
  85. * @deprecated Relation sur {@see Distributor}
  86. *
  87. * @ORM\Column(type="string", length=255, nullable=true)
  88. */
  89. private ?string $distributorPostalCode = null;
  90. /**
  91. * Ville du distributeur
  92. *
  93. * @deprecated Relation sur {@see Distributor}
  94. *
  95. * @ORM\Column(type="string", length=255, nullable=true)
  96. */
  97. private ?string $distributorCity = null;
  98. /**
  99. * Pays du distributeur
  100. *
  101. * @deprecated Relation sur {@see Distributor}
  102. *
  103. * @ORM\Column(type="string", length=255, nullable=true)
  104. */
  105. private ?string $distributorCountry = null;
  106. /**
  107. * Valeur de la déclaration
  108. *
  109. * @deprecated Il y a une relation entre PointTransaction et Purchase {@see Purchase::$pointTransactions}
  110. *
  111. * @ORM\Column(type="integer", nullable=true)
  112. */
  113. private ?int $value = null;
  114. /**
  115. * Personne qui a validé la déclaration
  116. *
  117. * @ORM\ManyToOne(targetEntity=User::class,inversedBy="purchasesIHaveProcessed")
  118. * @ORM\JoinColumn(onDelete="SET NULL")
  119. *
  120. * @Expose
  121. * @Groups({"purchase"})
  122. */
  123. private $validator;
  124. /**
  125. * Utilisateur qui a fait la déclaration
  126. *
  127. * @ORM\ManyToOne(targetEntity=User::class,inversedBy="purchases")
  128. *
  129. * @Expose
  130. * @Groups({"purchase", "export_purchase_declaration_datatable"})
  131. */
  132. private $user;
  133. /**
  134. * Distributeur rattaché à la déclaration
  135. *
  136. * @ORM\ManyToOne(targetEntity=Distributor::class, inversedBy="purchases")
  137. *
  138. * @Expose()
  139. * @Groups({"export_purchase_declaration_datatable"})
  140. */
  141. private $distributor;
  142. /**
  143. * @TODO Check que toujours utilse
  144. */
  145. private $imageFile;
  146. /**
  147. * @ORM\OneToMany(
  148. * targetEntity=PurchaseHistory::class,
  149. * mappedBy="purchase",
  150. * cascade={"persist", "remove"}
  151. * )
  152. */
  153. private $purchaseHistories;
  154. /**
  155. * @ORM\OneToMany(
  156. * targetEntity=PurchaseFile::class,
  157. * mappedBy="purchase",
  158. * cascade={"remove", "persist"}
  159. * )
  160. */
  161. private $files;
  162. /**
  163. * @ORM\OneToMany(
  164. * targetEntity=PurchaseProductItem::class,
  165. * mappedBy="purchase",
  166. * cascade={"remove", "persist"},
  167. * orphanRemoval=true
  168. * )
  169. *
  170. * @Expose()
  171. * @Groups({"export_purchase_declaration_datatable"})
  172. */
  173. private $items;
  174. /**
  175. * @ORM\OneToMany(targetEntity=PointTransaction::class, mappedBy="purchase")
  176. */
  177. private Collection $pointTransactions;
  178. /**
  179. * Date de la validation
  180. *
  181. * @ORM\Column(type="datetime", nullable=true)
  182. */
  183. private $validationDate;
  184. /**
  185. * @ORM\Column(type="string", length=255, nullable=true)
  186. */
  187. private ?string $distributorAddress1 = null;
  188. /**
  189. * Prénom du client
  190. *
  191. * @ORM\Column(type="string", length=255, nullable=true)
  192. *
  193. * @Expose
  194. * @Groups({"purchase", "export_purchase_declaration_datatable"})
  195. */
  196. private ?string $customerFirstName = null;
  197. /**
  198. * Nom du client
  199. *
  200. * @ORM\Column(type="string", length=255, nullable=true)
  201. *
  202. * @Expose
  203. * @Groups({"purchase", "export_purchase_declaration_datatable"})
  204. */
  205. private ?string $customerLastName = null;
  206. /**
  207. * Adresse du client
  208. *
  209. * @ORM\Column(type="string", length=255, nullable=true)
  210. *
  211. * @Expose
  212. * @Groups({"purchase", "export_purchase_declaration_datatable"})
  213. */
  214. private ?string $customerAddress1 = null;
  215. /**
  216. * Ville du client
  217. *
  218. * @ORM\Column(type="string", length=255, nullable=true)
  219. *
  220. * @Expose
  221. * @Groups({"purchase", "export_purchase_declaration_datatable"})
  222. */
  223. private ?string $customerCity = null;
  224. /**
  225. * Code postal du client
  226. *
  227. * @ORM\Column(type="string", length=255, nullable=true)
  228. *
  229. * @Expose
  230. * @Groups({"purchase", "export_purchase_declaration_datatable"})
  231. */
  232. private ?string $customerPostcode = null;
  233. /**
  234. * Complément d'adresse du client
  235. *
  236. * @ORM\Column(type="text", nullable=true)
  237. *
  238. * @Expose
  239. * @Groups({"purchase", "export_purchase_declaration_datatable"})
  240. */
  241. private ?string $customerAddress2 = null;
  242. /**
  243. * @ORM\Column(type="string", length=255, nullable=true)
  244. */
  245. private ?string $commercialName = null;
  246. public function __construct()
  247. {
  248. $this->purchaseHistories = new ArrayCollection();
  249. $this->files = new ArrayCollection();
  250. $this->items = new ArrayCollection();
  251. $this->pointTransactions = new ArrayCollection();
  252. }
  253. use DateTrait;
  254. /*
  255. * ============================================================================================
  256. * =============================== FONCTIONS CUSTOM ===========================================
  257. * ============================================================================================
  258. */
  259. /**
  260. * @return float|int|null
  261. * @Serializer\VirtualProperty()
  262. * @Serializer\SerializedName("get_sum_values")
  263. *
  264. * @Expose()
  265. * @Groups({"purchase"})
  266. */
  267. public function getSumValues()
  268. {
  269. $sum = 0;
  270. /** @var PointTransaction $pointTransaction */
  271. foreach ($this->pointTransactions as $pointTransaction) {
  272. $sum += $pointTransaction->getValue();
  273. }
  274. return $sum;
  275. }
  276. /**
  277. * @return float|int
  278. * @deprecated
  279. * $value returns points acquired including boosters
  280. * This method returns points without booster effects
  281. */
  282. public function getBulkValue()
  283. {
  284. $value = 0;
  285. foreach ($this->items as $item) {
  286. $value += $item->getProduct()->getValue() * $item->getQuantity();
  287. }
  288. return $value;
  289. }
  290. /**
  291. * @return int|null
  292. * @deprecated Il y a une relation entre PointTransaction et Purchase {@see Purchase::$pointTransactions}
  293. */
  294. public function getValue(): ?int
  295. {
  296. return $this->value;
  297. }
  298. /**
  299. * @param int|null $value
  300. *
  301. * @return $this
  302. * @deprecated Il y a une relation entre PointTransaction et Purchase {@see Purchase::$pointTransactions}
  303. */
  304. public function setValue(?int $value): self
  305. {
  306. $this->value = $value;
  307. return $this;
  308. }
  309. /**
  310. * @return mixed
  311. */
  312. public function getImageFile()
  313. {
  314. return $this->imageFile;
  315. }
  316. public function getArrayCategoryValues()
  317. {
  318. $categoryValues = [];
  319. /** @var PurchaseProductItem $item */
  320. foreach ($this->items as $item) {
  321. $product = $item->getProduct();
  322. $productCV = $product->getArrayCategoryValues();
  323. if ($productCV !== null) {
  324. if ($categoryValues === []) {
  325. $categoryValues = $productCV;
  326. } else {
  327. $categoryValues = array_combine(array_keys($categoryValues), array_map(function ($a, $b) {
  328. return $a + $b;
  329. }, $categoryValues, $productCV));
  330. }
  331. }
  332. }
  333. return $categoryValues;
  334. }
  335. /*
  336. * ============================================================================================
  337. * ============================== FIN FONCTIONS CUSTOM ========================================
  338. * ============================================================================================
  339. */
  340. /**
  341. * @param $imageFile
  342. *
  343. * @return $this
  344. */
  345. public function setImageFile($imageFile): Purchase
  346. {
  347. $this->imageFile = $imageFile;
  348. return $this;
  349. }
  350. /**
  351. * @param array $files
  352. *
  353. * @return $this
  354. */
  355. public function addFiles(array $files): Purchase
  356. {
  357. $this->files = new ArrayCollection(array_merge($this->files->toArray(), $files));
  358. return $this;
  359. }
  360. public function getId(): ?int
  361. {
  362. return $this->id;
  363. }
  364. public function getInvoiceNumber(): ?string
  365. {
  366. return $this->invoiceNumber;
  367. }
  368. public function setInvoiceNumber(string $invoiceNumber): self
  369. {
  370. $this->invoiceNumber = $invoiceNumber;
  371. return $this;
  372. }
  373. public function getInvoiceDate(): ?DateTimeInterface
  374. {
  375. return $this->invoiceDate;
  376. }
  377. public function setInvoiceDate(?DateTimeInterface $invoiceDate): self
  378. {
  379. $this->invoiceDate = $invoiceDate;
  380. return $this;
  381. }
  382. public function getStatus(): ?string
  383. {
  384. return $this->status;
  385. }
  386. public function setStatus(?string $status): self
  387. {
  388. $this->status = $status;
  389. return $this;
  390. }
  391. public function getLastStatusReason(): ?string
  392. {
  393. return $this->lastStatusReason;
  394. }
  395. public function setLastStatusReason(?string $lastStatusReason): self
  396. {
  397. $this->lastStatusReason = $lastStatusReason;
  398. return $this;
  399. }
  400. /**
  401. * @return string|null
  402. * @deprecated Relation sur {@see Distributor}
  403. */
  404. public function getDistributorName(): ?string
  405. {
  406. return $this->distributorName;
  407. }
  408. /**
  409. * @param string|null $distributorName
  410. *
  411. * @return $this
  412. * @deprecated Relation sur {@see Distributor}
  413. */
  414. public function setDistributorName(?string $distributorName): self
  415. {
  416. $this->distributorName = $distributorName;
  417. return $this;
  418. }
  419. /**
  420. * @return string|null
  421. * @deprecated Relation sur {@see Distributor}
  422. */
  423. public function getDistributorPostalCode(): ?string
  424. {
  425. return $this->distributorPostalCode;
  426. }
  427. /**
  428. * @param string|null $distributorPostalCode
  429. *
  430. * @return $this
  431. * @deprecated Relation sur {@see Distributor}
  432. */
  433. public function setDistributorPostalCode(?string $distributorPostalCode): self
  434. {
  435. $this->distributorPostalCode = $distributorPostalCode;
  436. return $this;
  437. }
  438. /**
  439. * @return string|null
  440. * @deprecated Relation sur {@see Distributor}
  441. */
  442. public function getDistributorCity(): ?string
  443. {
  444. return $this->distributorCity;
  445. }
  446. /**
  447. * @param string|null $distributorCity
  448. *
  449. * @return $this
  450. * @deprecated Relation sur {@see Distributor}
  451. */
  452. public function setDistributorCity(?string $distributorCity): self
  453. {
  454. $this->distributorCity = $distributorCity;
  455. return $this;
  456. }
  457. /**
  458. * @return string|null
  459. * @deprecated Relation sur {@see Distributor}
  460. */
  461. public function getDistributorCountry(): ?string
  462. {
  463. return $this->distributorCountry;
  464. }
  465. /**
  466. * @param string|null $distributorCountry
  467. *
  468. * @return $this
  469. * @deprecated Relation sur {@see Distributor}
  470. */
  471. public function setDistributorCountry(?string $distributorCountry): self
  472. {
  473. $this->distributorCountry = $distributorCountry;
  474. return $this;
  475. }
  476. public function getValidator(): ?User
  477. {
  478. return $this->validator;
  479. }
  480. public function setValidator(?User $validator): self
  481. {
  482. $this->validator = $validator;
  483. return $this;
  484. }
  485. public function getUser(): ?User
  486. {
  487. return $this->user;
  488. }
  489. public function setUser(?User $user): self
  490. {
  491. $this->user = $user;
  492. return $this;
  493. }
  494. public function getDistributor(): ?Distributor
  495. {
  496. return $this->distributor;
  497. }
  498. public function setDistributor(?Distributor $distributor): self
  499. {
  500. $this->distributor = $distributor;
  501. return $this;
  502. }
  503. /**
  504. * @return Collection|PurchaseHistory[]
  505. */
  506. public function getPurchaseHistories(): Collection
  507. {
  508. return $this->purchaseHistories;
  509. }
  510. public function addPurchaseHistory(PurchaseHistory $purchaseHistory): self
  511. {
  512. if (!$this->purchaseHistories->contains($purchaseHistory)) {
  513. $this->purchaseHistories[] = $purchaseHistory;
  514. $purchaseHistory->setPurchase($this);
  515. }
  516. return $this;
  517. }
  518. public function removePurchaseHistory(PurchaseHistory $purchaseHistory): self
  519. {
  520. if ($this->purchaseHistories->removeElement($purchaseHistory)) {
  521. // set the owning side to null (unless already changed)
  522. if ($purchaseHistory->getPurchase() === $this) {
  523. $purchaseHistory->setPurchase(null);
  524. }
  525. }
  526. return $this;
  527. }
  528. /**
  529. * @return Collection|PurchaseFile[]
  530. */
  531. public function getFiles(): Collection
  532. {
  533. return $this->files;
  534. }
  535. public function addFile(PurchaseFile $file): self
  536. {
  537. if (!$this->files->contains($file)) {
  538. $this->files[] = $file;
  539. $file->setPurchase($this);
  540. }
  541. return $this;
  542. }
  543. public function removeFile(PurchaseFile $file): self
  544. {
  545. if ($this->files->removeElement($file)) {
  546. // set the owning side to null (unless already changed)
  547. if ($file->getPurchase() === $this) {
  548. $file->setPurchase(null);
  549. }
  550. }
  551. return $this;
  552. }
  553. /**
  554. * @return Collection|PurchaseProductItem[]
  555. */
  556. public function getItems(): Collection
  557. {
  558. return $this->items;
  559. }
  560. public function addItem(PurchaseProductItem $item): self
  561. {
  562. if (!$this->items->contains($item)) {
  563. $this->items[] = $item;
  564. $item->setPurchase($this);
  565. }
  566. return $this;
  567. }
  568. public function removeItem(PurchaseProductItem $item): self
  569. {
  570. if ($this->items->removeElement($item)) {
  571. // set the owning side to null (unless already changed)
  572. if ($item->getPurchase() === $this) {
  573. $item->setPurchase(null);
  574. }
  575. }
  576. return $this;
  577. }
  578. /**
  579. * @return Collection|PointTransaction[]
  580. */
  581. public function getPointTransactions(): Collection
  582. {
  583. return $this->pointTransactions;
  584. }
  585. public function addPointTransaction(PointTransaction $pointTransaction): self
  586. {
  587. if (!$this->pointTransactions->contains($pointTransaction)) {
  588. $this->pointTransactions[] = $pointTransaction;
  589. $pointTransaction->setPurchase($this);
  590. }
  591. return $this;
  592. }
  593. public function removePointTransaction(PointTransaction $pointTransaction): self
  594. {
  595. if ($this->pointTransactions->removeElement($pointTransaction)) {
  596. // set the owning side to null (unless already changed)
  597. if ($pointTransaction->getPurchase() === $this) {
  598. $pointTransaction->setPurchase(null);
  599. }
  600. }
  601. return $this;
  602. }
  603. public function getValidationDate(): ?DateTimeInterface
  604. {
  605. return $this->validationDate;
  606. }
  607. public function setValidationDate(?DateTimeInterface $validationDate): self
  608. {
  609. $this->validationDate = $validationDate;
  610. return $this;
  611. }
  612. public function getDistributorAddress1(): ?string
  613. {
  614. return $this->distributorAddress1;
  615. }
  616. public function setDistributorAddress1(?string $distributorAddress1): self
  617. {
  618. $this->distributorAddress1 = $distributorAddress1;
  619. return $this;
  620. }
  621. public function getCustomerFirstName(): ?string
  622. {
  623. return $this->customerFirstName;
  624. }
  625. public function setCustomerFirstName(?string $customerFirstName): self
  626. {
  627. $this->customerFirstName = $customerFirstName;
  628. return $this;
  629. }
  630. public function getCustomerLastName(): ?string
  631. {
  632. return $this->customerLastName;
  633. }
  634. public function setCustomerLastName(?string $customerLastName): self
  635. {
  636. $this->customerLastName = $customerLastName;
  637. return $this;
  638. }
  639. public function getCustomerAddress1(): ?string
  640. {
  641. return $this->customerAddress1;
  642. }
  643. public function setCustomerAddress1(?string $customerAddress1): self
  644. {
  645. $this->customerAddress1 = $customerAddress1;
  646. return $this;
  647. }
  648. public function getCustomerCity(): ?string
  649. {
  650. return $this->customerCity;
  651. }
  652. public function setCustomerCity(?string $customerCity): self
  653. {
  654. $this->customerCity = $customerCity;
  655. return $this;
  656. }
  657. public function getCustomerPostcode(): ?string
  658. {
  659. return $this->customerPostcode;
  660. }
  661. public function setCustomerPostcode(?string $customerPostcode): self
  662. {
  663. $this->customerPostcode = $customerPostcode;
  664. return $this;
  665. }
  666. public function getCustomerAddress2(): ?string
  667. {
  668. return $this->customerAddress2;
  669. }
  670. public function setCustomerAddress2(?string $customerAddress2): self
  671. {
  672. $this->customerAddress2 = $customerAddress2;
  673. return $this;
  674. }
  675. public function getCommercialName(): ?string
  676. {
  677. return $this->commercialName;
  678. }
  679. public function setCommercialName(?string $commercialName): self
  680. {
  681. $this->commercialName = $commercialName;
  682. return $this;
  683. }
  684. }