src/Entity/PointTransaction.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\PointTransactionRepository;
  4. use App\Traits\DateTrait;
  5. use DateTime;
  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 Symfony\Component\Validator\Constraints as Assert;
  13. /**
  14. * @ORM\Entity(repositoryClass=PointTransactionRepository::class)
  15. *
  16. * @Serializer\ExclusionPolicy("ALL")
  17. */
  18. class PointTransaction
  19. {
  20. use DateTrait;
  21. public const TRANSACTION_FEES_TYPE_FEES = 'fees';
  22. public const TRANSACTION_FEES_TYPE_SHIPPING_PRICE = 'shipping_price';
  23. public const USER_VALIDATION_TRANSACTION_TYPE = 'user_validation';
  24. public const TRANSACTION_GOD_CHILD = 'god_child';
  25. public const TRANSACTION_GOD_FATHER = 'god_father';
  26. public const TRANSACTION_ANIMATION = 'animation';
  27. public const TRANSACTION_HIGHLIGHT = 'highlight';
  28. public const TRANSACTION_BONUS_STATUS = 'bonus_status';
  29. public const TRANSACTION_BONUS_BOOSTER = 'bonus_booster';
  30. public const TRANSACTION_RESET = 'reset';
  31. /** Solde des commandes */
  32. private int $orderSolde = 0;
  33. /** Solde des points de fidélités */
  34. private int $fidelitySolde = 0;
  35. /**
  36. * Identifiant unique auto-incrémenté
  37. *
  38. * @ORM\Id
  39. * @ORM\GeneratedValue
  40. * @ORM\Column(type="integer")
  41. *
  42. * @Expose
  43. * @Serializer\Groups({"point_transaction:id"})
  44. */
  45. private ?int $id = null;
  46. /**
  47. * Utilisateur lié à la transaction
  48. *
  49. * @ORM\ManyToOne(targetEntity=User::class, inversedBy="pointTransactions")
  50. * @ORM\JoinColumn(nullable=false)
  51. *
  52. * @Expose
  53. * @Serializer\Groups({"point_transaction:user"})
  54. */
  55. private ?User $user = null;
  56. /**
  57. * Commande liée à la transaction
  58. *
  59. * @ORM\ManyToOne(targetEntity=SaleOrder::class, inversedBy="pointTransactions")
  60. */
  61. private ?SaleOrder $saleOrder = null;
  62. /**
  63. * Valeur de la transaction
  64. *
  65. * @ORM\Column(type="float")
  66. *
  67. * @Expose
  68. * @Serializer\Groups({"point_transaction:value"})
  69. */
  70. private ?float $value = null;
  71. /**
  72. * Label de la transaction
  73. *
  74. * @ORM\Column(type="string", length=255)
  75. *
  76. * @Assert\NotBlank
  77. *
  78. * @Expose
  79. * @Serializer\Groups({"point_transaction:label"})
  80. */
  81. private ?string $label = null;
  82. /**
  83. * Référence du paiement par carte
  84. *
  85. * @ORM\Column(type="string", length=255, nullable=true)
  86. *
  87. * @Expose
  88. * @Serializer\Groups({"point_transaction:paymentReference"})
  89. */
  90. private ?string $paymentReference = null;
  91. /**
  92. * Future valeur de la transaction lors d'un paiement par carte
  93. * Ce montant sera attribué à la valeur réelle après le retour IPN de la banque
  94. *
  95. * @ORM\Column(type="float", nullable=true)
  96. *
  97. * @Expose
  98. * @Serializer\Groups({"point_transaction:value"})
  99. */
  100. private ?float $paymentValue = null;
  101. /**
  102. * Informations reçues lors du retour IPN de la banque
  103. *
  104. * @ORM\Column(type="json", nullable=true)
  105. *
  106. * @Expose
  107. * @Serializer\Groups({"point_transaction:paymentInformations"})
  108. */
  109. private ?array $paymentInformations = [];
  110. /**
  111. * Date d'expiration de la transaction
  112. *
  113. * @ORM\Column(type="datetime", nullable=true)
  114. */
  115. private ?DateTime $expiredAt = null;
  116. /**
  117. * Transaction liée à une autre transaction
  118. *
  119. * @ORM\ManyToOne(
  120. * targetEntity=PointTransaction::class,
  121. * inversedBy="childrenPointTransactions",
  122. * cascade={"persist", "remove"}
  123. * )
  124. */
  125. private ?PointTransaction $linkedPointTransaction = null;
  126. /**
  127. * Liste des transactions enfants liées à la transaction
  128. *
  129. * @ORM\OneToMany(targetEntity=PointTransaction::class, mappedBy="linkedPointTransaction", orphanRemoval=true)
  130. */
  131. private ?Collection $childrenPointTransactions = null;
  132. /**
  133. * Type de transaction
  134. *
  135. * @ORM\ManyToOne(targetEntity=PointTransactionType::class, inversedBy="pointTransactions")
  136. * @ORM\JoinColumn(nullable=false)
  137. */
  138. private ?PointTransactionType $transactionType = null;
  139. /**
  140. * Déclaration d'achat liée à la transaction
  141. *
  142. * @ORM\ManyToOne(targetEntity=Purchase::class, inversedBy="pointTransactions")
  143. */
  144. private ?Purchase $purchase = null;
  145. /**
  146. * Historique d'import lié à la transaction
  147. *
  148. * @ORM\ManyToOne(
  149. * targetEntity=PointTransactionImportHistory::class,
  150. * inversedBy="pointTransactions",
  151. * cascade={"persist"}
  152. * )
  153. */
  154. private ?PointTransactionImportHistory $importHistory = null;
  155. /**
  156. * Date d'effet de la transaction
  157. *
  158. * @ORM\Column(type="datetime", nullable=true)
  159. */
  160. private ?DateTimeInterface $effectiveAt = null;
  161. /**
  162. * Méthode d'import de la transaction
  163. *
  164. * @ORM\Column(type="string", length=32, nullable=true)
  165. */
  166. private ?string $importMethod = null;
  167. /**
  168. * Commande de produit personnalisé liée à la transaction
  169. *
  170. * @ORM\ManyToOne(targetEntity=CustomProductOrder::class)
  171. */
  172. private ?CustomProductOrder $customProductOrder = null;
  173. /**
  174. * Résultat d'activité lié à la transaction
  175. *
  176. * @ORM\OneToOne(
  177. * targetEntity=UserBusinessResult::class,
  178. * inversedBy="pointTransaction",
  179. * cascade={"persist", "remove"}
  180. * )
  181. */
  182. private ?UserBusinessResult $businessResult = null;
  183. /**
  184. * Catégorie de la transaction
  185. *
  186. * @ORM\Column(type="string", length=64, nullable=true)
  187. */
  188. private ?string $category = null;
  189. /**
  190. * On lie un SaleOrderItem a une transaction seulement à l'annulation, pour pouvoir lier un remboursement a un produit
  191. *
  192. * @ORM\ManyToOne(targetEntity=SaleOrderItem::class, inversedBy="pointTransactions", cascade={"persist"})
  193. */
  194. private ?SaleOrderItem $saleOrderItem = null;
  195. /**
  196. * Panier lié à la transaction quand elle est validée (retour de l'utilisateur depuis l'interface de la banque)
  197. * même si elle n'est pas encore confirmée par la banque (retour automatique IPN)
  198. * quand elle est confirmée, on lui attribue le montant correspondant, sinon il reste à 0
  199. *
  200. * @ORM\ManyToOne(targetEntity=Cart::class,inversedBy="paidPointTransactions")
  201. * @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
  202. */
  203. private ?Cart $paidCart = null;
  204. /**
  205. * Panier lié à la transaction, dans le cadre d'un paiement par carte avant que la transaction ne soit validée
  206. *
  207. * @ORM\ManyToOne(targetEntity=Cart::class,inversedBy="pointTransactions")
  208. * @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
  209. */
  210. private ?Cart $cart = null;
  211. /**
  212. * @ORM\ManyToMany(targetEntity=Invoice::class, mappedBy="transactionPoints")
  213. */
  214. private Collection $invoices;
  215. /**
  216. * Type de frais de la transaction
  217. *
  218. * @ORM\Column(type="string", length=64, nullable=true)
  219. */
  220. private ?string $subtype = null;
  221. /**
  222. * @ORM\Column(type="string", length=255, nullable=true)
  223. */
  224. private $eventKey;
  225. public function __construct()
  226. {
  227. $this->childrenPointTransactions = new ArrayCollection();
  228. $this->invoices = new ArrayCollection();
  229. }
  230. public function __toString()
  231. {
  232. return $this->getValue() . ' - ' . $this->getLabel();
  233. }
  234. // MÉTHODES NÉCESSAIRES POUR LE CALCUL DE POINT : NE PAS EFFACER
  235. public function setOrderSolde($orderSolde): PointTransaction
  236. {
  237. $this->orderSolde += $orderSolde;
  238. return $this;
  239. }
  240. public function getOrderSolde(): int
  241. {
  242. return $this->orderSolde;
  243. }
  244. public function setFidelitySolde($fidelitySolde): PointTransaction
  245. {
  246. $this->fidelitySolde += $fidelitySolde;
  247. return $this;
  248. }
  249. public function getFidelitySolde(): int
  250. {
  251. return $this->fidelitySolde;
  252. }
  253. // END METHODES
  254. public function getId(): ?int
  255. {
  256. return $this->id;
  257. }
  258. public function getValue(): ?float
  259. {
  260. return $this->value;
  261. }
  262. public function setValue(float $value): self
  263. {
  264. $this->value = $value;
  265. return $this;
  266. }
  267. public function getLabel(): ?string
  268. {
  269. $label = $this->label;
  270. if ($this->getTransactionType(true) === PointTransactionType::PAYMENT) {
  271. $label .= ' (' . $this->getPaymentReference() . ')';
  272. }
  273. return $label;
  274. }
  275. public function setLabel(string $label): self
  276. {
  277. $this->label = $label;
  278. return $this;
  279. }
  280. public function getExpiredAt(): ?DateTimeInterface
  281. {
  282. return $this->expiredAt;
  283. }
  284. public function setExpiredAt(?DateTime $expiredAt): self
  285. {
  286. $this->expiredAt = $expiredAt;
  287. return $this;
  288. }
  289. public function getEffectiveAt(): ?DateTimeInterface
  290. {
  291. return $this->effectiveAt;
  292. }
  293. public function setEffectiveAt(?DateTimeInterface $effectiveAt): self
  294. {
  295. $this->effectiveAt = $effectiveAt;
  296. return $this;
  297. }
  298. public function getImportMethod(): ?string
  299. {
  300. return $this->importMethod;
  301. }
  302. public function setImportMethod(?string $importMethod): self
  303. {
  304. $this->importMethod = $importMethod;
  305. return $this;
  306. }
  307. public function getCategory(): ?string
  308. {
  309. return $this->category;
  310. }
  311. public function setCategory(?string $category): self
  312. {
  313. $this->category = $category;
  314. return $this;
  315. }
  316. public function getUser(): ?User
  317. {
  318. return $this->user;
  319. }
  320. public function setUser(?User $user): self
  321. {
  322. $this->user = $user;
  323. return $this;
  324. }
  325. public function getSaleOrder(): ?SaleOrder
  326. {
  327. return $this->saleOrder;
  328. }
  329. public function setSaleOrder(?SaleOrder $saleOrder): self
  330. {
  331. if ($this->saleOrder === $saleOrder) {
  332. return $this;
  333. }
  334. if ($this->saleOrder !== null) {
  335. $this->saleOrder->removePointTransaction($this);
  336. }
  337. $this->saleOrder = $saleOrder;
  338. if ($saleOrder !== null && !$saleOrder->getPointTransactions()
  339. ->contains($this)) {
  340. $saleOrder->addPointTransaction($this);
  341. }
  342. return $this;
  343. }
  344. public function getLinkedPointTransaction(): ?self
  345. {
  346. return $this->linkedPointTransaction;
  347. }
  348. public function setLinkedPointTransaction(?self $linkedPointTransaction): self
  349. {
  350. $this->linkedPointTransaction = $linkedPointTransaction;
  351. return $this;
  352. }
  353. /**
  354. * @return Collection<int, PointTransaction>
  355. */
  356. public function getChildrenPointTransactions(): Collection
  357. {
  358. return $this->childrenPointTransactions;
  359. }
  360. public function addChildrenPointTransaction(PointTransaction $childrenPointTransaction): self
  361. {
  362. if (!$this->childrenPointTransactions->contains($childrenPointTransaction)) {
  363. $this->childrenPointTransactions[] = $childrenPointTransaction;
  364. $childrenPointTransaction->setLinkedPointTransaction($this);
  365. }
  366. return $this;
  367. }
  368. public function removeChildrenPointTransaction(PointTransaction $childrenPointTransaction): self
  369. {
  370. if ($this->childrenPointTransactions->removeElement($childrenPointTransaction)) {
  371. // set the owning side to null (unless already changed)
  372. if ($childrenPointTransaction->getLinkedPointTransaction() === $this) {
  373. $childrenPointTransaction->setLinkedPointTransaction(null);
  374. }
  375. }
  376. return $this;
  377. }
  378. /**
  379. * @param bool $getSlug
  380. * @return PointTransactionType|null|string
  381. */
  382. public function getTransactionType(bool $getSlug = false)
  383. {
  384. if (!$getSlug || !$this->transactionType) {
  385. return $this->transactionType;
  386. }
  387. return $this->transactionType->getSlug();
  388. }
  389. public function setTransactionType(?PointTransactionType $transactionType): self
  390. {
  391. $this->transactionType = $transactionType;
  392. return $this;
  393. }
  394. public function getPurchase(): ?Purchase
  395. {
  396. return $this->purchase;
  397. }
  398. public function setPurchase(?Purchase $purchase): self
  399. {
  400. $this->purchase = $purchase;
  401. return $this;
  402. }
  403. public function getImportHistory(): ?PointTransactionImportHistory
  404. {
  405. return $this->importHistory;
  406. }
  407. public function setImportHistory(?PointTransactionImportHistory $importHistory): self
  408. {
  409. $this->importHistory = $importHistory;
  410. return $this;
  411. }
  412. public function getCustomProductOrder(): ?CustomProductOrder
  413. {
  414. return $this->customProductOrder;
  415. }
  416. public function setCustomProductOrder(?CustomProductOrder $customProductOrder): self
  417. {
  418. $this->customProductOrder = $customProductOrder;
  419. return $this;
  420. }
  421. public function getBusinessResult(): ?UserBusinessResult
  422. {
  423. return $this->businessResult;
  424. }
  425. public function setBusinessResult(?UserBusinessResult $businessResult): self
  426. {
  427. $this->businessResult = $businessResult;
  428. return $this;
  429. }
  430. public function getSaleOrderItem(): ?SaleOrderItem
  431. {
  432. return $this->saleOrderItem;
  433. }
  434. public function setSaleOrderItem(?SaleOrderItem $saleOrderItem): self
  435. {
  436. if ($this->saleOrderItem === $saleOrderItem) {
  437. return $this;
  438. }
  439. if ($this->saleOrderItem !== null) {
  440. $this->saleOrderItem->removePointTransaction($this);
  441. }
  442. $this->saleOrderItem = $saleOrderItem;
  443. if ($saleOrderItem !== null && !$saleOrderItem->getPointTransactions()->contains($this)) {
  444. $saleOrderItem->addPointTransaction($this);
  445. }
  446. return $this;
  447. }
  448. /**
  449. * @return string|null
  450. */
  451. public function getPaymentReference(): ?string
  452. {
  453. return $this->paymentReference;
  454. }
  455. /**
  456. * @param string|null $paymentReference
  457. *
  458. * @return PointTransaction
  459. */
  460. public function setPaymentReference(?string $paymentReference): PointTransaction
  461. {
  462. $this->paymentReference = $paymentReference;
  463. return $this;
  464. }
  465. /**
  466. * @return array
  467. */
  468. public function getPaymentInformations(): array
  469. {
  470. if ($this->paymentInformations === null) {
  471. $this->paymentInformations = [];
  472. }
  473. return $this->paymentInformations;
  474. }
  475. /**
  476. * @param array|null $paymentInformations
  477. *
  478. * @return PointTransaction
  479. */
  480. public function setPaymentInformations(?array $paymentInformations = []): PointTransaction
  481. {
  482. if ($paymentInformations === null) {
  483. $paymentInformations = [];
  484. }
  485. $this->paymentInformations = $paymentInformations;
  486. return $this;
  487. }
  488. /**
  489. * @param string $key
  490. * @param $value
  491. *
  492. * @return $this
  493. */
  494. public function addPaymentInformation(string $key, $value): PointTransaction
  495. {
  496. $this->paymentInformations[$key] = $value;
  497. return $this;
  498. }
  499. /**
  500. * @param string $key
  501. *
  502. * @return $this
  503. */
  504. public function removePaymentInformation(string $key): PointTransaction
  505. {
  506. unset($this->paymentInformations[$key]);
  507. return $this;
  508. }
  509. /**
  510. * @return float|null
  511. */
  512. public function getPaymentValue(): ?float
  513. {
  514. return $this->paymentValue;
  515. }
  516. /**
  517. * @param float|null $paymentValue
  518. *
  519. * @return PointTransaction
  520. */
  521. public function setPaymentValue(?float $paymentValue): PointTransaction
  522. {
  523. $this->paymentValue = $paymentValue;
  524. return $this;
  525. }
  526. /**
  527. * @return Cart|null
  528. */
  529. public function getPaidCart(): ?Cart
  530. {
  531. return $this->paidCart;
  532. }
  533. /**
  534. * @param Cart|null $paidCart
  535. *
  536. * @return PointTransaction
  537. */
  538. public function setPaidCart(?Cart $paidCart = null): self
  539. {
  540. if ($this->paidCart === $paidCart) {
  541. return $this;
  542. }
  543. if ($this->paidCart !== null) {
  544. $this->paidCart->removePaidPointTransaction($this);
  545. }
  546. $this->paidCart = $paidCart;
  547. if ($paidCart !== null && !$paidCart->getPaidPointTransactions()
  548. ->contains($this)) {
  549. $paidCart->addPaidPointTransaction($this);
  550. }
  551. return $this;
  552. }
  553. /**
  554. * @return Cart|null
  555. */
  556. public function getCart(): ?Cart
  557. {
  558. return $this->cart;
  559. }
  560. /**
  561. * @param Cart|null $cart
  562. *
  563. * @return PointTransaction
  564. */
  565. public function setCart(?Cart $cart = null): self
  566. {
  567. if ($this->cart === $cart) {
  568. return $this;
  569. }
  570. if ($this->cart !== null) {
  571. $this->cart->removePointTransaction($this);
  572. }
  573. $this->cart = $cart;
  574. if ($cart !== null && !$cart->getPointTransactions()->contains($this)) {
  575. $cart->addPointTransaction($this);
  576. }
  577. return $this;
  578. }
  579. public function getSubtype(): ?string
  580. {
  581. return $this->subtype;
  582. }
  583. public function setSubtype(?string $subtype): self
  584. {
  585. $this->subtype = $subtype;
  586. return $this;
  587. }
  588. /**
  589. * @return Collection<int, Invoice>
  590. */
  591. public function getInvoices(): Collection
  592. {
  593. return $this->invoices;
  594. }
  595. public function addInvoice(Invoice $invoice): self
  596. {
  597. if (!$this->invoices->contains($invoice)) {
  598. $this->invoices[] = $invoice;
  599. $invoice->addPointTransaction($this);
  600. }
  601. return $this;
  602. }
  603. public function removeInvoice(Invoice $invoice): self
  604. {
  605. if ($this->invoices->removeElement($invoice)) {
  606. $invoice->removePointTransaction($this);
  607. }
  608. return $this;
  609. }
  610. public function getEventKey(): ?string
  611. {
  612. return $this->eventKey;
  613. }
  614. public function setEventKey(?string $eventKey): self
  615. {
  616. $this->eventKey = $eventKey;
  617. return $this;
  618. }
  619. }