<?php
namespace App\Entity;
use App\Repository\PromoCodeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=PromoCodeRepository::class)
*/
class PromoCode
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $content;
/**
* @ORM\OneToMany(targetEntity=PromoCodeUsage::class, mappedBy="promoCode")
*/
private $promoCodeUsages;
/**
* @ORM\ManyToOne(targetEntity=Commission::class)
*/
private $commission;
/**
* @ORM\ManyToOne(targetEntity=PercentOff::class)
*/
private $percentOff;
/**
* @ORM\Column(type="boolean", options={"default"=1})
*/
private $isActivo;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $createdAt;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $venceDias;
/**
* @ORM\Column(type="boolean", options={"default" = 0})
*/
private $isSendgrid;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $expiresAt;
/**
* @ORM\Column(type="boolean", options={"default" = 0})
*/
private $ofTheDay;
/**
* @ORM\Column(type="boolean", options={"default" = 0})
*/
private $isExpired;
/**
* @ORM\OneToMany(targetEntity=User::class, mappedBy="PromoCode")
*/
private $users;
/**
* @ORM\OneToMany(targetEntity=User::class, mappedBy="usedPromoCode")
*/
private $usedUsers;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="promoCodes")
*/
private $user;
public function __construct()
{
$this->promoCodeUsages = new ArrayCollection();
if(!$this->createdAt) $this->createdAt = new \DateTimeImmutable();
$this->content = strtoupper($this->content);
$this->users = new ArrayCollection();
$this->usedUsers = new ArrayCollection();
if(!$this->isSendgrid) $this->isSendgrid = 0;
if(!$this->ofTheDay) $this->ofTheDay = 0;
if(!$this->isExpired) $this->isExpired = 0;
}
public function getId(): ?int
{
return $this->id;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = strtoupper($content);
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
/**
* @return Collection<int, PromoCodeUsage>
*/
public function getPromoCodeUsages(): Collection
{
return $this->promoCodeUsages;
}
public function addPromoCodeUsage(PromoCodeUsage $promoCodeUsage): self
{
if (!$this->promoCodeUsages->contains($promoCodeUsage)) {
$this->promoCodeUsages[] = $promoCodeUsage;
$promoCodeUsage->setPromoCode($this);
}
return $this;
}
public function removePromoCodeUsage(PromoCodeUsage $promoCodeUsage): self
{
if ($this->promoCodeUsages->removeElement($promoCodeUsage)) {
// set the owning side to null (unless already changed)
if ($promoCodeUsage->getPromoCode() === $this) {
$promoCodeUsage->setPromoCode(null);
}
}
return $this;
}
public function __toString()
{
return $this->content;
}
public function getUnpaidCommission()
{
$r = 0; $total = 0;
$us = $this->getPromoCodeUsages();
if($us){
foreach ($us as $u){
// $u = new PromoCodeUsage();
if($u->getCurrentPlace() == 'Unpaid'){
++$r;
}
}
$total = $r * $this->getCommission()->getValor();
}
return $total;
}
public function getCommission(): ?Commission
{
return $this->commission;
}
public function setCommission(?Commission $commission): self
{
$this->commission = $commission;
return $this;
}
public function getPercentOff(): ?PercentOff
{
return $this->percentOff;
}
public function setPercentOff(?PercentOff $percentOff): self
{
$this->percentOff = $percentOff;
return $this;
}
public function getIsActivo(): ?bool
{
return $this->isActivo;
}
public function setIsActivo(bool $isActivo): self
{
$this->isActivo = $isActivo;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getVenceDias(): ?int
{
return $this->venceDias;
}
public function setVenceDias(?int $venceDias): self
{
$this->venceDias = $venceDias;
return $this;
}
public function getIsSendgrid(): ?bool
{
return $this->isSendgrid;
}
public function setIsSendgrid(?bool $isSendgrid): self
{
$this->isSendgrid = $isSendgrid;
return $this;
}
public function getExpiresAt(): ?\DateTimeImmutable
{
return $this->expiresAt;
}
public function setExpiresAt(?\DateTimeImmutable $expiresAt): self
{
$this->expiresAt = $expiresAt;
return $this;
}
public function isOfTheDay(): ?bool
{
return $this->ofTheDay;
}
public function setOfTheDay(bool $ofTheDay): self
{
$this->ofTheDay = $ofTheDay;
return $this;
}
public function getIsExpired(): ?bool
{
return $this->isExpired;
}
public function setIsExpired(bool $isExpired): self
{
$this->isExpired = $isExpired;
return $this;
}
/**
* @return Collection<int, User>
*/
public function getUsedUsers(): Collection
{
return $this->usedUsers;
}
public function addUsedUser(User $usedUser): self
{
if (!$this->usedUsers->contains($usedUser)) {
$this->usedUsers[] = $usedUser;
$usedUser->setUsedPromoCode($this);
}
return $this;
}
public function removeUsedUser(User $usedUser): self
{
if ($this->usedUsers->removeElement($usedUser)) {
// set the owning side to null (unless already changed)
if ($usedUser->getUsedPromoCode() === $this) {
$usedUser->setUsedPromoCode(null);
}
}
return $this;
}
}