src/Entity/Purchase.php line 22

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