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. {
  273. $sum += $pointTransaction->getValue();
  274. }
  275. return $sum;
  276. }
  277. /**
  278. * @return float|int
  279. * @deprecated
  280. * $value returns points acquired including boosters
  281. * This method returns points without booster effects
  282. */
  283. public function getBulkValue()
  284. {
  285. $value = 0;
  286. foreach($this->items as $item)
  287. {
  288. $value += $item->getProduct()->getValue() * $item->getQuantity();
  289. }
  290. return $value;
  291. }
  292. /**
  293. * @return int|null
  294. * @deprecated Il y a une relation entre PointTransaction et Purchase {@see Purchase::$pointTransactions}
  295. */
  296. public function getValue(): ?int
  297. {
  298. return $this->value;
  299. }
  300. /**
  301. * @param int|null $value
  302. *
  303. * @return $this
  304. * @deprecated Il y a une relation entre PointTransaction et Purchase {@see Purchase::$pointTransactions}
  305. */
  306. public function setValue(?int $value): self
  307. {
  308. $this->value = $value;
  309. return $this;
  310. }
  311. /**
  312. * @return mixed
  313. */
  314. public function getImageFile()
  315. {
  316. return $this->imageFile;
  317. }
  318. public function getArrayCategoryValues()
  319. {
  320. $categoryValues = [];
  321. /** @var PurchaseProductItem $item */
  322. foreach($this->items as $item)
  323. {
  324. $product = $item->getProduct();
  325. $productCV = $product->getArrayCategoryValues();
  326. if($productCV !== NULL)
  327. {
  328. if($categoryValues === [])
  329. {
  330. $categoryValues = $productCV;
  331. }
  332. else
  333. {
  334. $categoryValues = array_combine(array_keys($categoryValues), array_map(function($a, $b)
  335. {
  336. return $a + $b;
  337. }, $categoryValues, $productCV));
  338. }
  339. }
  340. }
  341. return $categoryValues;
  342. }
  343. /*
  344. * ============================================================================================
  345. * ============================== FIN FONCTIONS CUSTOM ========================================
  346. * ============================================================================================
  347. */
  348. /**
  349. * @param $imageFile
  350. *
  351. * @return $this
  352. */
  353. public function setImageFile($imageFile): Purchase
  354. {
  355. $this->imageFile = $imageFile;
  356. return $this;
  357. }
  358. /**
  359. * @param array $files
  360. *
  361. * @return $this
  362. */
  363. public function addFiles(array $files): Purchase
  364. {
  365. $this->files = new ArrayCollection(array_merge($this->files->toArray(), $files));
  366. return $this;
  367. }
  368. public function getId(): ?int
  369. {
  370. return $this->id;
  371. }
  372. public function getInvoiceNumber(): ?string
  373. {
  374. return $this->invoiceNumber;
  375. }
  376. public function setInvoiceNumber(string $invoiceNumber): self
  377. {
  378. $this->invoiceNumber = $invoiceNumber;
  379. return $this;
  380. }
  381. public function getInvoiceDate(): ?DateTimeInterface
  382. {
  383. return $this->invoiceDate;
  384. }
  385. public function setInvoiceDate(?DateTimeInterface $invoiceDate): self
  386. {
  387. $this->invoiceDate = $invoiceDate;
  388. return $this;
  389. }
  390. public function getStatus(): ?string
  391. {
  392. return $this->status;
  393. }
  394. public function setStatus(?string $status): self
  395. {
  396. $this->status = $status;
  397. return $this;
  398. }
  399. public function getLastStatusReason(): ?string
  400. {
  401. return $this->lastStatusReason;
  402. }
  403. public function setLastStatusReason(?string $lastStatusReason): self
  404. {
  405. $this->lastStatusReason = $lastStatusReason;
  406. return $this;
  407. }
  408. /**
  409. * @return string|null
  410. * @deprecated Relation sur {@see Distributor}
  411. */
  412. public function getDistributorName(): ?string
  413. {
  414. return $this->distributorName;
  415. }
  416. /**
  417. * @param string|null $distributorName
  418. *
  419. * @return $this
  420. * @deprecated Relation sur {@see Distributor}
  421. */
  422. public function setDistributorName(?string $distributorName): self
  423. {
  424. $this->distributorName = $distributorName;
  425. return $this;
  426. }
  427. /**
  428. * @return string|null
  429. * @deprecated Relation sur {@see Distributor}
  430. */
  431. public function getDistributorPostalCode(): ?string
  432. {
  433. return $this->distributorPostalCode;
  434. }
  435. /**
  436. * @param string|null $distributorPostalCode
  437. *
  438. * @return $this
  439. * @deprecated Relation sur {@see Distributor}
  440. */
  441. public function setDistributorPostalCode(?string $distributorPostalCode): self
  442. {
  443. $this->distributorPostalCode = $distributorPostalCode;
  444. return $this;
  445. }
  446. /**
  447. * @return string|null
  448. * @deprecated Relation sur {@see Distributor}
  449. */
  450. public function getDistributorCity(): ?string
  451. {
  452. return $this->distributorCity;
  453. }
  454. /**
  455. * @param string|null $distributorCity
  456. *
  457. * @return $this
  458. * @deprecated Relation sur {@see Distributor}
  459. */
  460. public function setDistributorCity(?string $distributorCity): self
  461. {
  462. $this->distributorCity = $distributorCity;
  463. return $this;
  464. }
  465. /**
  466. * @return string|null
  467. * @deprecated Relation sur {@see Distributor}
  468. */
  469. public function getDistributorCountry(): ?string
  470. {
  471. return $this->distributorCountry;
  472. }
  473. /**
  474. * @param string|null $distributorCountry
  475. *
  476. * @return $this
  477. * @deprecated Relation sur {@see Distributor}
  478. */
  479. public function setDistributorCountry(?string $distributorCountry): self
  480. {
  481. $this->distributorCountry = $distributorCountry;
  482. return $this;
  483. }
  484. public function getValidator(): ?User
  485. {
  486. return $this->validator;
  487. }
  488. public function setValidator(?User $validator): self
  489. {
  490. $this->validator = $validator;
  491. return $this;
  492. }
  493. public function getUser(): ?User
  494. {
  495. return $this->user;
  496. }
  497. public function setUser(?User $user): self
  498. {
  499. $this->user = $user;
  500. return $this;
  501. }
  502. public function getDistributor(): ?Distributor
  503. {
  504. return $this->distributor;
  505. }
  506. public function setDistributor(?Distributor $distributor): self
  507. {
  508. $this->distributor = $distributor;
  509. return $this;
  510. }
  511. /**
  512. * @return Collection|PurchaseHistory[]
  513. */
  514. public function getPurchaseHistories(): Collection
  515. {
  516. return $this->purchaseHistories;
  517. }
  518. public function addPurchaseHistory(PurchaseHistory $purchaseHistory): self
  519. {
  520. if(!$this->purchaseHistories->contains($purchaseHistory))
  521. {
  522. $this->purchaseHistories[] = $purchaseHistory;
  523. $purchaseHistory->setPurchase($this);
  524. }
  525. return $this;
  526. }
  527. public function removePurchaseHistory(PurchaseHistory $purchaseHistory): self
  528. {
  529. if($this->purchaseHistories->removeElement($purchaseHistory))
  530. {
  531. // set the owning side to null (unless already changed)
  532. if($purchaseHistory->getPurchase() === $this)
  533. {
  534. $purchaseHistory->setPurchase(NULL);
  535. }
  536. }
  537. return $this;
  538. }
  539. /**
  540. * @return Collection|PurchaseFile[]
  541. */
  542. public function getFiles(): Collection
  543. {
  544. return $this->files;
  545. }
  546. public function addFile(PurchaseFile $file): self
  547. {
  548. if(!$this->files->contains($file))
  549. {
  550. $this->files[] = $file;
  551. $file->setPurchase($this);
  552. }
  553. return $this;
  554. }
  555. public function removeFile(PurchaseFile $file): self
  556. {
  557. if($this->files->removeElement($file))
  558. {
  559. // set the owning side to null (unless already changed)
  560. if($file->getPurchase() === $this)
  561. {
  562. $file->setPurchase(NULL);
  563. }
  564. }
  565. return $this;
  566. }
  567. /**
  568. * @return Collection|PurchaseProductItem[]
  569. */
  570. public function getItems(): Collection
  571. {
  572. return $this->items;
  573. }
  574. public function addItem(PurchaseProductItem $item): self
  575. {
  576. if(!$this->items->contains($item))
  577. {
  578. $this->items[] = $item;
  579. $item->setPurchase($this);
  580. }
  581. return $this;
  582. }
  583. public function removeItem(PurchaseProductItem $item): self
  584. {
  585. if($this->items->removeElement($item))
  586. {
  587. // set the owning side to null (unless already changed)
  588. if($item->getPurchase() === $this)
  589. {
  590. $item->setPurchase(NULL);
  591. }
  592. }
  593. return $this;
  594. }
  595. /**
  596. * @return Collection|PointTransaction[]
  597. */
  598. public function getPointTransactions(): Collection
  599. {
  600. return $this->pointTransactions;
  601. }
  602. public function addPointTransaction(PointTransaction $pointTransaction): self
  603. {
  604. if(!$this->pointTransactions->contains($pointTransaction))
  605. {
  606. $this->pointTransactions[] = $pointTransaction;
  607. $pointTransaction->setPurchase($this);
  608. }
  609. return $this;
  610. }
  611. public function removePointTransaction(PointTransaction $pointTransaction): self
  612. {
  613. if($this->pointTransactions->removeElement($pointTransaction))
  614. {
  615. // set the owning side to null (unless already changed)
  616. if($pointTransaction->getPurchase() === $this)
  617. {
  618. $pointTransaction->setPurchase(NULL);
  619. }
  620. }
  621. return $this;
  622. }
  623. public function getValidationDate(): ?DateTimeInterface
  624. {
  625. return $this->validationDate;
  626. }
  627. public function setValidationDate(?DateTimeInterface $validationDate): self
  628. {
  629. $this->validationDate = $validationDate;
  630. return $this;
  631. }
  632. public function getDistributorAddress1(): ?string
  633. {
  634. return $this->distributorAddress1;
  635. }
  636. public function setDistributorAddress1(?string $distributorAddress1): self
  637. {
  638. $this->distributorAddress1 = $distributorAddress1;
  639. return $this;
  640. }
  641. public function getCustomerFirstName(): ?string
  642. {
  643. return $this->customerFirstName;
  644. }
  645. public function setCustomerFirstName(?string $customerFirstName): self
  646. {
  647. $this->customerFirstName = $customerFirstName;
  648. return $this;
  649. }
  650. public function getCustomerLastName(): ?string
  651. {
  652. return $this->customerLastName;
  653. }
  654. public function setCustomerLastName(?string $customerLastName): self
  655. {
  656. $this->customerLastName = $customerLastName;
  657. return $this;
  658. }
  659. public function getCustomerAddress1(): ?string
  660. {
  661. return $this->customerAddress1;
  662. }
  663. public function setCustomerAddress1(?string $customerAddress1): self
  664. {
  665. $this->customerAddress1 = $customerAddress1;
  666. return $this;
  667. }
  668. public function getCustomerCity(): ?string
  669. {
  670. return $this->customerCity;
  671. }
  672. public function setCustomerCity(?string $customerCity): self
  673. {
  674. $this->customerCity = $customerCity;
  675. return $this;
  676. }
  677. public function getCustomerPostcode(): ?string
  678. {
  679. return $this->customerPostcode;
  680. }
  681. public function setCustomerPostcode(?string $customerPostcode): self
  682. {
  683. $this->customerPostcode = $customerPostcode;
  684. return $this;
  685. }
  686. public function getCustomerAddress2(): ?string
  687. {
  688. return $this->customerAddress2;
  689. }
  690. public function setCustomerAddress2(?string $customerAddress2): self
  691. {
  692. $this->customerAddress2 = $customerAddress2;
  693. return $this;
  694. }
  695. public function getCommercialName(): ?string
  696. {
  697. return $this->commercialName;
  698. }
  699. public function setCommercialName(?string $commercialName): self
  700. {
  701. $this->commercialName = $commercialName;
  702. return $this;
  703. }
  704. }