<?php
namespace App\Entity;
use App\Repository\PointTransactionRepository;
use App\Traits\DateTrait;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\Expose;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=PointTransactionRepository::class)
*
* @Serializer\ExclusionPolicy("ALL")
*/
class PointTransaction
{
use DateTrait;
public CONST TRANSACTION_FEES_TYPE_FEES = 'fees';
public CONST TRANSACTION_FEES_TYPE_SHIPPING_PRICE = 'shipping_price';
public CONST USER_VALIDATION_TRANSACTION_TYPE = 'user_validation';
/** Solde des commandes */
private int $orderSolde = 0;
/** Solde des points de fidélités */
private int $fidelitySolde = 0;
/**
* Identifiant unique auto-incrémenté
*
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @Expose
* @Serializer\Groups({"point_transaction:id"})
*/
private ?int $id = NULL;
/**
* Utilisateur lié à la transaction
*
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="pointTransactions")
* @ORM\JoinColumn(nullable=false)
*
* @Expose
* @Serializer\Groups({"point_transaction:user"})
*/
private ?User $user = NULL;
/**
* Commande liée à la transaction
*
* @ORM\ManyToOne(targetEntity=SaleOrder::class, inversedBy="pointTransactions")
*/
private ?SaleOrder $saleOrder = NULL;
/**
* Valeur de la transaction
*
* @ORM\Column(type="float")
*
* @Expose
* @Serializer\Groups({"point_transaction:value"})
*/
private ?float $value = NULL;
/**
* Label de la transaction
*
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank
*
* @Expose
* @Serializer\Groups({"point_transaction:label"})
*/
private ?string $label = NULL;
/**
* Référence du paiement par carte
*
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Expose
* @Serializer\Groups({"point_transaction:paymentReference"})
*/
private ?string $paymentReference = NULL;
/**
* Future valeur de la transaction lors d'un paiement par carte
* Ce montant sera attribué à la valeur réelle après le retour IPN de la banque
*
* @ORM\Column(type="float", nullable=true)
*
* @Expose
* @Serializer\Groups({"point_transaction:value"})
*/
private ?float $paymentValue = NULL;
/**
* Informations reçues lors du retour IPN de la banque
*
* @ORM\Column(type="json", nullable=true)
*
* @Expose
* @Serializer\Groups({"point_transaction:paymentInformations"})
*/
private ?array $paymentInformations = [];
/**
* Date d'expiration de la transaction
*
* @ORM\Column(type="datetime", nullable=true)
*/
private ?DateTime $expiredAt = NULL;
/**
* Transaction liée à une autre transaction
*
* @ORM\ManyToOne(
* targetEntity=PointTransaction::class,
* inversedBy="childrenPointTransactions",
* cascade={"persist", "remove"}
* )
*/
private ?PointTransaction $linkedPointTransaction = NULL;
/**
* Liste des transactions enfants liées à la transaction
*
* @ORM\OneToMany(targetEntity=PointTransaction::class, mappedBy="linkedPointTransaction", orphanRemoval=true)
*/
private ?Collection $childrenPointTransactions;
/**
* Type de transaction
*
* @ORM\ManyToOne(targetEntity=PointTransactionType::class, inversedBy="pointTransactions")
* @ORM\JoinColumn(nullable=false)
*/
private ?PointTransactionType $transactionType = NULL;
/**
* Déclaration d'achat liée à la transaction
*
* @ORM\ManyToOne(targetEntity=Purchase::class, inversedBy="pointTransactions")
*/
private ?Purchase $purchase = NULL;
/**
* Historique d'import lié à la transaction
*
* @ORM\ManyToOne(
* targetEntity=PointTransactionImportHistory::class,
* inversedBy="pointTransactions",
* cascade={"persist"}
* )
*/
private ?PointTransactionImportHistory $importHistory = NULL;
/**
* Date d'effet de la transaction
*
* @ORM\Column(type="datetime", nullable=true)
*/
private ?DateTimeInterface $effectiveAt = NULL;
/**
* Méthode d'import de la transaction
*
* @ORM\Column(type="string", length=32, nullable=true)
*/
private ?string $importMethod = NULL;
/**
* Commande de produit personnalisé liée à la transaction
*
* @ORM\ManyToOne(targetEntity=CustomProductOrder::class)
*/
private ?CustomProductOrder $customProductOrder = NULL;
/**
* Résultat d'activité lié à la transaction
*
* @ORM\OneToOne(
* targetEntity=UserBusinessResult::class,
* inversedBy="pointTransaction",
* cascade={"persist", "remove"}
* )
*/
private ?UserBusinessResult $businessResult = NULL;
/**
* Catégorie de la transaction
*
* @ORM\Column(type="string", length=64, nullable=true)
*/
private ?string $category = NULL;
/**
* On lie un SaleOrderItem a une transaction seulement à l'annulation, pour pouvoir lier un remboursement a un produit
*
* @ORM\ManyToOne(targetEntity=SaleOrderItem::class, inversedBy="pointTransaction", cascade={"persist"})
*/
private ?SaleOrderItem $saleOrderItem = null;
/**
* Panier lié à la transaction quand elle est validée (retour de l'utilisateur depuis l'interface de la banque)
* même si elle n'est pas encore confirmée par la banque (retour automatique IPN)
* quand elle est confirmée, on lui attribue le montant correspondant, sinon il reste à 0
*
* @ORM\ManyToOne(targetEntity=Cart::class,inversedBy="paidPointTransactions")
* @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
*/
private ?Cart $paidCart = NULL;
/**
* Panier lié à la transaction, dans le cadre d'un paiement par carte avant que la transaction ne soit validée
*
* @ORM\ManyToOne(targetEntity=Cart::class,inversedBy="pointTransactions")
* @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
*/
private ?Cart $cart = NULL;
/**
* @ORM\ManyToMany(targetEntity=Invoice::class, mappedBy="transactionPoints")
*/
private Collection $invoices;
/**
* Type de frais de la transaction
*
* @ORM\Column(type="string", length=64, nullable=true)
*/
private ?string $subtype = NULL;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $eventKey;
public function __construct()
{
$this->childrenPointTransactions = new ArrayCollection();
$this->invoices = new ArrayCollection();
}
public function __toString()
{
return $this->getValue() . ' - ' . $this->getLabel();
}
// MÉTHODES NÉCESSAIRES POUR LE CALCUL DE POINT : NE PAS EFFACER
public function setOrderSolde($orderSolde): PointTransaction
{
$this->orderSolde += $orderSolde;
return $this;
}
public function getOrderSolde(): int
{
return $this->orderSolde;
}
public function setFidelitySolde($fidelitySolde): PointTransaction
{
$this->fidelitySolde += $fidelitySolde;
return $this;
}
public function getFidelitySolde(): int
{
return $this->fidelitySolde;
}
// END METHODES
public function getId(): ?int
{
return $this->id;
}
public function getValue(): ?float
{
return $this->value;
}
public function setValue(float $value): self
{
$this->value = $value;
return $this;
}
public function getLabel(): ?string
{
$label = $this->label;
if($this->getTransactionType(true) === PointTransactionType::PAYMENT) $label .= ' ('.$this->getPaymentReference().')';
return $label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getExpiredAt(): ?DateTimeInterface
{
return $this->expiredAt;
}
public function setExpiredAt(?DateTime $expiredAt): self
{
$this->expiredAt = $expiredAt;
return $this;
}
public function getEffectiveAt(): ?DateTimeInterface
{
return $this->effectiveAt;
}
public function setEffectiveAt(?DateTimeInterface $effectiveAt): self
{
$this->effectiveAt = $effectiveAt;
return $this;
}
public function getImportMethod(): ?string
{
return $this->importMethod;
}
public function setImportMethod(?string $importMethod): self
{
$this->importMethod = $importMethod;
return $this;
}
public function getCategory(): ?string
{
return $this->category;
}
public function setCategory(?string $category): self
{
$this->category = $category;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getSaleOrder(): ?SaleOrder
{
return $this->saleOrder;
}
public function setSaleOrder(?SaleOrder $saleOrder): self
{
if($this->saleOrder === $saleOrder) return $this;
if($this->saleOrder !== null) $this->saleOrder->removePointTransaction($this);
$this->saleOrder = $saleOrder;
if($saleOrder !== null && !$saleOrder->getPointTransactions()->contains($this)) $saleOrder->addPointTransaction($this);
return $this;
}
public function getLinkedPointTransaction(): ?self
{
return $this->linkedPointTransaction;
}
public function setLinkedPointTransaction(?self $linkedPointTransaction): self
{
$this->linkedPointTransaction = $linkedPointTransaction;
return $this;
}
/**
* @return Collection<int, PointTransaction>
*/
public function getChildrenPointTransactions(): Collection
{
return $this->childrenPointTransactions;
}
public function addChildrenPointTransaction(PointTransaction $childrenPointTransaction): self
{
if (!$this->childrenPointTransactions->contains($childrenPointTransaction)) {
$this->childrenPointTransactions[] = $childrenPointTransaction;
$childrenPointTransaction->setLinkedPointTransaction($this);
}
return $this;
}
public function removeChildrenPointTransaction(PointTransaction $childrenPointTransaction): self
{
if ($this->childrenPointTransactions->removeElement($childrenPointTransaction)) {
// set the owning side to null (unless already changed)
if ($childrenPointTransaction->getLinkedPointTransaction() === $this) {
$childrenPointTransaction->setLinkedPointTransaction(NULL);
}
}
return $this;
}
/**
* @param bool $getSlug
* @return PointTransactionType|null|string
*/
public function getTransactionType(bool $getSlug = false)
{
if(!$getSlug || !$this->transactionType) return $this->transactionType;
return $this->transactionType->getSlug();
}
public function setTransactionType(?PointTransactionType $transactionType): self
{
$this->transactionType = $transactionType;
return $this;
}
public function getPurchase(): ?Purchase
{
return $this->purchase;
}
public function setPurchase(?Purchase $purchase): self
{
$this->purchase = $purchase;
return $this;
}
public function getImportHistory(): ?PointTransactionImportHistory
{
return $this->importHistory;
}
public function setImportHistory(?PointTransactionImportHistory $importHistory): self
{
$this->importHistory = $importHistory;
return $this;
}
public function getCustomProductOrder(): ?CustomProductOrder
{
return $this->customProductOrder;
}
public function setCustomProductOrder(?CustomProductOrder $customProductOrder): self
{
$this->customProductOrder = $customProductOrder;
return $this;
}
public function getBusinessResult(): ?UserBusinessResult
{
return $this->businessResult;
}
public function setBusinessResult(?UserBusinessResult $businessResult): self
{
$this->businessResult = $businessResult;
return $this;
}
public function getSaleOrderItem(): ?SaleOrderItem
{
return $this->saleOrderItem;
}
public function setSaleOrderItem(?SaleOrderItem $saleOrderItem): self
{
$this->saleOrderItem = $saleOrderItem;
return $this;
}
/**
* @return string|null
*/
public function getPaymentReference(): ?string
{
return $this->paymentReference;
}
/**
* @param string|null $paymentReference
*
* @return PointTransaction
*/
public function setPaymentReference(?string $paymentReference): PointTransaction
{
$this->paymentReference = $paymentReference;
return $this;
}
/**
* @return array
*/
public function getPaymentInformations(): array
{
if($this->paymentInformations === null) $this->paymentInformations = [];
return $this->paymentInformations;
}
/**
* @param array|null $paymentInformations
*
* @return PointTransaction
*/
public function setPaymentInformations(?array $paymentInformations = []): PointTransaction
{
if($paymentInformations === null) $paymentInformations = [];
$this->paymentInformations = $paymentInformations;
return $this;
}
/**
* @param string $key
* @param $value
*
* @return $this
*/
public function addPaymentInformation(string $key, $value): PointTransaction
{
$this->paymentInformations[$key] = $value;
return $this;
}
/**
* @param string $key
*
* @return $this
*/
public function removePaymentInformation(string $key): PointTransaction
{
unset($this->paymentInformations[$key]);
return $this;
}
/**
* @return float|null
*/
public function getPaymentValue(): ?float
{
return $this->paymentValue;
}
/**
* @param float|null $paymentValue
*
* @return PointTransaction
*/
public function setPaymentValue(?float $paymentValue): PointTransaction
{
$this->paymentValue = $paymentValue;
return $this;
}
/**
* @return Cart|null
*/
public function getPaidCart(): ?Cart
{
return $this->paidCart;
}
/**
* @param Cart|null $paidCart
*
* @return PointTransaction
*/
public function setPaidCart(?Cart $paidCart = null): self
{
if($this->paidCart === $paidCart) return $this;
if($this->paidCart !== null) $this->paidCart->removePaidPointTransaction($this);
$this->paidCart = $paidCart;
if($paidCart !== null && !$paidCart->getPaidPointTransactions()->contains($this)) $paidCart->addPaidPointTransaction($this);
return $this;
}
/**
* @return Cart|null
*/
public function getCart(): ?Cart
{
return $this->cart;
}
/**
* @param Cart|null $cart
*
* @return PointTransaction
*/
public function setCart(?Cart $cart = null): self
{
if($this->cart === $cart) return $this;
if($this->cart !== null) $this->cart->removePointTransaction($this);
$this->cart = $cart;
if($cart !== null && !$cart->getPointTransactions()->contains($this)) $cart->addPointTransaction($this);
return $this;
}
public function getSubtype(): ?string
{
return $this->subtype;
}
public function setSubtype(?string $subtype): self
{
$this->subtype = $subtype;
return $this;
}
/**
* @return Collection<int, Invoice>
*/
public function getInvoices(): Collection
{
return $this->invoices;
}
public function addInvoice(Invoice $invoice): self
{
if (!$this->invoices->contains($invoice)) {
$this->invoices[] = $invoice;
$invoice->addPointTransaction($this);
}
return $this;
}
public function removeInvoice(Invoice $invoice): self
{
if ($this->invoices->removeElement($invoice)) {
$invoice->removePointTransaction($this);
}
return $this;
}
public function getEventKey(): ?string
{
return $this->eventKey;
}
public function setEventKey(?string $eventKey): self
{
$this->eventKey = $eventKey;
return $this;
}
}