<?php
namespace App\Entity\Purse;
use App\Entity\Ean\Ean;
use App\Entity\Participation\Participation;
use App\Repository\Purse\ItemRepository;
use DateInterval;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiProperty;
/**
* @ApiResource(
* attributes={
* "normalization_context"={"groups"={"Purse:io","Purse:output"}},
* "denormalization_context"={"groups"={"Purse:io","Purse:input"}}
* },
* collectionOperations={
* "get"={
* "method"="GET",
* "access_control"="is_granted('ROLE_USER')",
* },
* },
* itemOperations={
* "get"={
* "method"="GET",
* "access_control"="is_granted('ROLE_USER')",
* },
* }
* )
* @ORM\Entity(repositoryClass=ItemRepository::class)
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({
* "item" = "Item",
* "puntos" = "ItemPuntos",
* "step" = "ItemStep"
* })
*/
class Item
{
const STATUS_NEW = 'new';
const STATUS_AVAILABLE = 'available';
const STATUS_EXPIRED = 'expired';
const STATUS_EXPIRING = 'purse_expiring';
const STATUS_REFUND_ASKED = 'refund_asked';
const STATUS_REFUND_PURSE_EXPIRING_ASK = 'refund_purse_expiring_asked';
const STATUS_REFUND_SENT = 'refund_sent';
const STATUS_REFUNDED = 'refunded';
const STATUS_BURNED = 'burned';
const DEFAULT_CONFIG_ITEM_DURATION_IN_DAYS = 365;
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* @ORM\Id
* @ApiProperty(identifier=true)
* @Groups({"Purse:output"})
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="float")
* @Groups({"Purse:output"})
* @Groups({
* "Order:output",
* })
*/
private $amount;
/**
* @ORM\ManyToOne(targetEntity=Participation::class)
* @ORM\JoinColumn(nullable=true)
*/
private $participation;
/**
* @ORM\ManyToOne(targetEntity=Ean::class)
* @ORM\JoinColumn(nullable=true)
*/
private $ean;
/**
* @ORM\Column(type="string", length=30)
* @Groups({"Purse:output"})
*/
private $status;
/**
* @ORM\ManyToOne(targetEntity=Purse::class, inversedBy="items")
* @ORM\JoinColumn(nullable=false)
*/
private $purse;
/**
* @ORM\Column(type="integer")
* @Groups({"Purse:output"})
*/
private int $percentApplied = 100;
/**
* @ORM\Column(type="float")
* @Groups({"Purse:output"})
*/
private $originalAmount;
/**
* @var DateTime
* @ORM\Column(type="datetime", nullable=false, options={"default":"CURRENT_TIMESTAMP"})
* @Groups({"Purse:output"})
*/
private $dueDate;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Purse:output"})
*/
private ?bool $isClone;
/**
* Item constructor.
* @throws \Exception
*/
public function __construct()
{
$dateIntervalFormat = sprintf("P%sD", $_ENV['CONFIG_ITEM_DURATION_IN_DAYS'] ?? self::DEFAULT_CONFIG_ITEM_DURATION_IN_DAYS);
$this->dueDate = (new DateTime())->add(new DateInterval($dateIntervalFormat));
}
public function getId(): ?int
{
return $this->id;
}
public function getAmount(): ?float
{
return $this->amount;
}
public function setAmount(float $amount): self
{
$this->amount = $amount;
return $this;
}
public function getParticipation(): ?Participation
{
return $this->participation;
}
public function setParticipation(?Participation $participation): self
{
$this->participation = $participation;
return $this;
}
public function getEan(): ?Ean
{
return $this->ean;
}
public function setEan(?Ean $ean): self
{
$this->ean = $ean;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
public function getPurse(): ?Purse
{
return $this->purse;
}
public function setPurse(?Purse $purse): self
{
$this->purse = $purse;
return $this;
}
public function getPercentApplied(): ?int
{
return $this->percentApplied;
}
public function setPercentApplied(int $percentApplied): self
{
$this->percentApplied = $percentApplied;
return $this;
}
public function getOriginalAmount(): ?float
{
return $this->originalAmount;
}
public function setOriginalAmount(float $originalAmount): self
{
$this->originalAmount = $originalAmount;
return $this;
}
/**
* @return \DateTimeInterface
*/
public function getDueDate(): ?\DateTimeInterface
{
return $this->dueDate;
}
/**
* @param \DateTimeInterface $dueDate
* @return Item
*/
public function setDueDate(\DateTimeInterface $dueDate): Item
{
$this->dueDate = $dueDate;
return $this;
}
/**
* @return bool|null
*/
public function getIsClone(): ?bool
{
return $this->isClone;
}
/**
* @param bool|null $isClone
*
* @return Item
*/
public function setIsClone(?bool $isClone): Item
{
$this->isClone = $isClone;
return $this;
}
}