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