<?php
namespace App\Entity;
use App\Repository\UserSharedRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Blameable\Traits\BlameableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: UserSharedRepository::class)]
#[UniqueEntity('token')]
#[UniqueEntity(fields: ["influencer", "product"], errorPath: 'influencer', groups: ['influencerShared'])]
class UserShared
{
use BlameableEntity; //Hook blameable behaviour. Updates createdBy, updatedBy fields
use TimestampableEntity; //Hook timestampable behaviour. Updates createdAt, updatedAt fields
const TOKEN_NAME = 'UserSharedPublicToken';
const REFERRAL_USER_TYPE = 'REFERRAL_USER_TYPE';
const REFERRAL_INFLUENCER_TYPE = 'REFERRAL_INFLUENCER_TYPE';
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "IDENTITY")]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: '2')]
#[Assert\NotNull(groups: ['influencerShared'])]
#[Assert\PositiveOrZero(groups: ['influencerShared'])]
private ?float $referredPoints = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: '2')]
#[Assert\NotNull()]
#[Assert\PositiveOrZero()]
private ?float $extraReferredPoints = null;
#[ORM\Column(length: 50, unique: true)]
#[Assert\NotNull()]
#[Assert\Length(max: 50)]
private ?string $token = null;
#[ORM\ManyToOne]
#[Assert\NotNull(groups: ['influencerShared'])]
private ?User $influencer = null;
#[ORM\ManyToOne(inversedBy: 'influencerShareds')]
private ?Product $product = null;
#[ORM\OneToOne(inversedBy: 'originUserShared', cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(nullable: true)]
private ?OrderDetail $originOrderDetail = null;
#[ORM\OneToMany(mappedBy: 'destinationUserShared', targetEntity: OrderDetail::class)]
private Collection $destitationOrderDetails;
#[ORM\Column]
private ?bool $isActive = true;
public function __construct()
{
$this->destitationOrderDetails = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTotalReferredPoints(): ?string
{
return (
($this->getReferredPoints() ?? 0) +
($this->getExtraReferredPoints() ?? 0)
);
}
public function getReferredPoints(): ?string
{
return $this->referredPoints;
}
public function setReferredPoints(string $referredPoints): self
{
$this->referredPoints = $referredPoints;
return $this;
}
public function getExtraReferredPoints(): ?string
{
return $this->extraReferredPoints;
}
public function setExtraReferredPoints(string $extraReferredPoints): self
{
$this->extraReferredPoints = $extraReferredPoints;
return $this;
}
public function getToken(): ?string
{
return $this->token;
}
public function setToken(string $token): self
{
$this->token = $token;
return $this;
}
public function getReferralUserType(): string
{
//Check Influencer Token
if (
$this->getInfluencer()
) {
return self::REFERRAL_INFLUENCER_TYPE;
}
//Check Influencer Token
if ($this->getOriginOrderDetail()) {
return self::REFERRAL_USER_TYPE;
}
return null;
}
public function getInfluencer(): ?User
{
return $this->influencer;
}
public function setInfluencer(?User $influencer): self
{
$this->influencer = $influencer;
return $this;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
return $this;
}
public function getOriginOrderDetail(): ?OrderDetail
{
return $this->originOrderDetail;
}
public function setOriginOrderDetail(OrderDetail $originOrderDetail): self
{
$this->originOrderDetail = $originOrderDetail;
return $this;
}
/**
* @return Collection<int, OrderDetail>
*/
public function getDestitationOrderDetails(): Collection
{
return $this->destitationOrderDetails;
}
public function addDestitationOrderDetail(OrderDetail $destitationOrderDetail): self
{
if (!$this->destitationOrderDetails->contains($destitationOrderDetail)) {
$this->destitationOrderDetails->add($destitationOrderDetail);
$destitationOrderDetail->setDestinationUserShared($this);
}
return $this;
}
public function removeDestitationOrderDetail(OrderDetail $destitationOrderDetail): self
{
if ($this->destitationOrderDetails->removeElement($destitationOrderDetail)) {
// set the owning side to null (unless already changed)
if ($destitationOrderDetail->getDestinationUserShared() === $this) {
$destitationOrderDetail->setDestinationUserShared(null);
}
}
return $this;
}
public function isActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
}