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