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) $label .= ' (' . $this->getPaymentReference() . ')';
  271. return $label;
  272. }
  273. public function setLabel(string $label): self
  274. {
  275. $this->label = $label;
  276. return $this;
  277. }
  278. public function getExpiredAt(): ?DateTimeInterface
  279. {
  280. return $this->expiredAt;
  281. }
  282. public function setExpiredAt(?DateTime $expiredAt): self
  283. {
  284. $this->expiredAt = $expiredAt;
  285. return $this;
  286. }
  287. public function getEffectiveAt(): ?DateTimeInterface
  288. {
  289. return $this->effectiveAt;
  290. }
  291. public function setEffectiveAt(?DateTimeInterface $effectiveAt): self
  292. {
  293. $this->effectiveAt = $effectiveAt;
  294. return $this;
  295. }
  296. public function getImportMethod(): ?string
  297. {
  298. return $this->importMethod;
  299. }
  300. public function setImportMethod(?string $importMethod): self
  301. {
  302. $this->importMethod = $importMethod;
  303. return $this;
  304. }
  305. public function getCategory(): ?string
  306. {
  307. return $this->category;
  308. }
  309. public function setCategory(?string $category): self
  310. {
  311. $this->category = $category;
  312. return $this;
  313. }
  314. public function getUser(): ?User
  315. {
  316. return $this->user;
  317. }
  318. public function setUser(?User $user): self
  319. {
  320. $this->user = $user;
  321. return $this;
  322. }
  323. public function getSaleOrder(): ?SaleOrder
  324. {
  325. return $this->saleOrder;
  326. }
  327. public function setSaleOrder(?SaleOrder $saleOrder): self
  328. {
  329. if($this->saleOrder === $saleOrder) return $this;
  330. if($this->saleOrder !== null) $this->saleOrder->removePointTransaction($this);
  331. $this->saleOrder = $saleOrder;
  332. if($saleOrder !== null && !$saleOrder->getPointTransactions()
  333. ->contains($this)) $saleOrder->addPointTransaction($this);
  334. return $this;
  335. }
  336. public function getLinkedPointTransaction(): ?self
  337. {
  338. return $this->linkedPointTransaction;
  339. }
  340. public function setLinkedPointTransaction(?self $linkedPointTransaction): self
  341. {
  342. $this->linkedPointTransaction = $linkedPointTransaction;
  343. return $this;
  344. }
  345. /**
  346. * @return Collection<int, PointTransaction>
  347. */
  348. public function getChildrenPointTransactions(): Collection
  349. {
  350. return $this->childrenPointTransactions;
  351. }
  352. public function addChildrenPointTransaction(PointTransaction $childrenPointTransaction): self
  353. {
  354. if(!$this->childrenPointTransactions->contains($childrenPointTransaction))
  355. {
  356. $this->childrenPointTransactions[] = $childrenPointTransaction;
  357. $childrenPointTransaction->setLinkedPointTransaction($this);
  358. }
  359. return $this;
  360. }
  361. public function removeChildrenPointTransaction(PointTransaction $childrenPointTransaction): self
  362. {
  363. if($this->childrenPointTransactions->removeElement($childrenPointTransaction))
  364. {
  365. // set the owning side to null (unless already changed)
  366. if($childrenPointTransaction->getLinkedPointTransaction() === $this)
  367. {
  368. $childrenPointTransaction->setLinkedPointTransaction(NULL);
  369. }
  370. }
  371. return $this;
  372. }
  373. /**
  374. * @param bool $getSlug
  375. * @return PointTransactionType|null|string
  376. */
  377. public function getTransactionType(bool $getSlug = false)
  378. {
  379. if(!$getSlug || !$this->transactionType) return $this->transactionType;
  380. return $this->transactionType->getSlug();
  381. }
  382. public function setTransactionType(?PointTransactionType $transactionType): self
  383. {
  384. $this->transactionType = $transactionType;
  385. return $this;
  386. }
  387. public function getPurchase(): ?Purchase
  388. {
  389. return $this->purchase;
  390. }
  391. public function setPurchase(?Purchase $purchase): self
  392. {
  393. $this->purchase = $purchase;
  394. return $this;
  395. }
  396. public function getImportHistory(): ?PointTransactionImportHistory
  397. {
  398. return $this->importHistory;
  399. }
  400. public function setImportHistory(?PointTransactionImportHistory $importHistory): self
  401. {
  402. $this->importHistory = $importHistory;
  403. return $this;
  404. }
  405. public function getCustomProductOrder(): ?CustomProductOrder
  406. {
  407. return $this->customProductOrder;
  408. }
  409. public function setCustomProductOrder(?CustomProductOrder $customProductOrder): self
  410. {
  411. $this->customProductOrder = $customProductOrder;
  412. return $this;
  413. }
  414. public function getBusinessResult(): ?UserBusinessResult
  415. {
  416. return $this->businessResult;
  417. }
  418. public function setBusinessResult(?UserBusinessResult $businessResult): self
  419. {
  420. $this->businessResult = $businessResult;
  421. return $this;
  422. }
  423. public function getSaleOrderItem(): ?SaleOrderItem
  424. {
  425. return $this->saleOrderItem;
  426. }
  427. public function setSaleOrderItem(?SaleOrderItem $saleOrderItem): self
  428. {
  429. if($this->saleOrderItem === $saleOrderItem)
  430. {
  431. return $this;
  432. }
  433. if($this->saleOrderItem !== null)
  434. {
  435. $this->saleOrderItem->removePointTransaction($this);
  436. }
  437. $this->saleOrderItem = $saleOrderItem;
  438. if($saleOrderItem !== null && !$saleOrderItem->getPointTransactions()->contains($this))
  439. {
  440. $saleOrderItem->addPointTransaction($this);
  441. }
  442. return $this;
  443. }
  444. /**
  445. * @return string|null
  446. */
  447. public function getPaymentReference(): ?string
  448. {
  449. return $this->paymentReference;
  450. }
  451. /**
  452. * @param string|null $paymentReference
  453. *
  454. * @return PointTransaction
  455. */
  456. public function setPaymentReference(?string $paymentReference): PointTransaction
  457. {
  458. $this->paymentReference = $paymentReference;
  459. return $this;
  460. }
  461. /**
  462. * @return array
  463. */
  464. public function getPaymentInformations(): array
  465. {
  466. if($this->paymentInformations === null) $this->paymentInformations = [];
  467. return $this->paymentInformations;
  468. }
  469. /**
  470. * @param array|null $paymentInformations
  471. *
  472. * @return PointTransaction
  473. */
  474. public function setPaymentInformations(?array $paymentInformations = []): PointTransaction
  475. {
  476. if($paymentInformations === null) $paymentInformations = [];
  477. $this->paymentInformations = $paymentInformations;
  478. return $this;
  479. }
  480. /**
  481. * @param string $key
  482. * @param $value
  483. *
  484. * @return $this
  485. */
  486. public function addPaymentInformation(string $key, $value): PointTransaction
  487. {
  488. $this->paymentInformations[$key] = $value;
  489. return $this;
  490. }
  491. /**
  492. * @param string $key
  493. *
  494. * @return $this
  495. */
  496. public function removePaymentInformation(string $key): PointTransaction
  497. {
  498. unset($this->paymentInformations[$key]);
  499. return $this;
  500. }
  501. /**
  502. * @return float|null
  503. */
  504. public function getPaymentValue(): ?float
  505. {
  506. return $this->paymentValue;
  507. }
  508. /**
  509. * @param float|null $paymentValue
  510. *
  511. * @return PointTransaction
  512. */
  513. public function setPaymentValue(?float $paymentValue): PointTransaction
  514. {
  515. $this->paymentValue = $paymentValue;
  516. return $this;
  517. }
  518. /**
  519. * @return Cart|null
  520. */
  521. public function getPaidCart(): ?Cart
  522. {
  523. return $this->paidCart;
  524. }
  525. /**
  526. * @param Cart|null $paidCart
  527. *
  528. * @return PointTransaction
  529. */
  530. public function setPaidCart(?Cart $paidCart = null): self
  531. {
  532. if($this->paidCart === $paidCart) return $this;
  533. if($this->paidCart !== null) $this->paidCart->removePaidPointTransaction($this);
  534. $this->paidCart = $paidCart;
  535. if($paidCart !== null && !$paidCart->getPaidPointTransactions()
  536. ->contains($this)) $paidCart->addPaidPointTransaction($this);
  537. return $this;
  538. }
  539. /**
  540. * @return Cart|null
  541. */
  542. public function getCart(): ?Cart
  543. {
  544. return $this->cart;
  545. }
  546. /**
  547. * @param Cart|null $cart
  548. *
  549. * @return PointTransaction
  550. */
  551. public function setCart(?Cart $cart = null): self
  552. {
  553. if($this->cart === $cart) return $this;
  554. if($this->cart !== null) $this->cart->removePointTransaction($this);
  555. $this->cart = $cart;
  556. if($cart !== null && !$cart->getPointTransactions()->contains($this)) $cart->addPointTransaction($this);
  557. return $this;
  558. }
  559. public function getSubtype(): ?string
  560. {
  561. return $this->subtype;
  562. }
  563. public function setSubtype(?string $subtype): self
  564. {
  565. $this->subtype = $subtype;
  566. return $this;
  567. }
  568. /**
  569. * @return Collection<int, Invoice>
  570. */
  571. public function getInvoices(): Collection
  572. {
  573. return $this->invoices;
  574. }
  575. public function addInvoice(Invoice $invoice): self
  576. {
  577. if(!$this->invoices->contains($invoice))
  578. {
  579. $this->invoices[] = $invoice;
  580. $invoice->addPointTransaction($this);
  581. }
  582. return $this;
  583. }
  584. public function removeInvoice(Invoice $invoice): self
  585. {
  586. if($this->invoices->removeElement($invoice))
  587. {
  588. $invoice->removePointTransaction($this);
  589. }
  590. return $this;
  591. }
  592. public function getEventKey(): ?string
  593. {
  594. return $this->eventKey;
  595. }
  596. public function setEventKey(?string $eventKey): self
  597. {
  598. $this->eventKey = $eventKey;
  599. return $this;
  600. }
  601. }