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