<?php
namespace Evo\Infrastructure\MappingORM;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use App\Enum\VATRateEnum;
use App\Utils\OrderUtils;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(attributes={
* "normalization_context"={"groups"={"read_order"}},
* "denormalization_context"={"groups"={"write_order"}}
* }, itemOperations={"get", "put"})
* @ORM\Entity(repositoryClass="App\Repository\OrderRepository")
* @ORM\Table(name="orders")
* @ApiFilter(SearchFilter::class, properties={"organization": "exact", "category": "exact"})
*/
class Order
{
/**
* @Groups({"read_order", "read_organization", "write_organization"})
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @Groups({"read_order", "write_order", "read_organization", "write_organization", "read_item", "write_item"})
* @ORM\OneToMany(targetEntity="Evo\Infrastructure\MappingORM\Item", mappedBy="itemOrder", cascade={"persist"})
*/
private ?Collection $items;
/**
* @Groups({"read_order", "write_order", "read_organization", "write_organization"})
* @ORM\Column(type="string", length=255)
*/
private ?string $status = null;
/**
* @Groups({"read_order", "write_order", "read_organization", "write_organization"})
* @ORM\Column(type="datetime", nullable=true)
*/
private $createdAt;
/**
* @Groups({"read_order", "write_order", "read_organization", "write_organization"})
* @ORM\Column(type="float", nullable=true)
*/
private $totalPriceWithoutTax = 0;
/**
* @Groups({"read_order", "write_order", "read_organization", "write_organization"})
* @ORM\Column(type="string", length=255)
*/
private ?string $VATRate = VATRateEnum::VAT_20;
/**
* @Groups({"read_order", "write_order", "read_organization", "write_organization"})
* @ORM\Column(type="float", nullable=true)
*/
private $totalPrice = 0;
/**
* @Groups({"read_order", "write_order", "read_organization", "write_organization"})
* @ORM\OneToOne(targetEntity="Evo\Infrastructure\MappingORM\Invoice", cascade={"persist"})
*/
private ?Invoice $invoice = null;
/**
* @Groups({"read_order", "write_order", "read_organization", "write_organization"})
* @ORM\ManyToOne(targetEntity="Evo\Infrastructure\MappingORM\Organization", inversedBy="orders")
* @ORM\JoinColumn(nullable=false)
*/
private ?Organization $organization = null;
/**
* @Groups({"read_order", "write_order", "read_organization", "write_organization"})
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $category;
public function __construct()
{
$this->items = new ArrayCollection();
$this->createdAt = new \DateTime();
}
/**
* @return Collection|Item[]
*/
public function getItems(): Collection
{
return $this->items;
}
public function addItem(Item $item): self
{
if (!$this->items->contains($item)) {
$this->items[] = $item;
$item->setItemOrder($this);
}
return $this;
}
public function removeItem(Item $item): self
{
if ($this->items->contains($item)) {
$this->items->removeElement($item);
// set the owning side to null (unless already changed)
if ($item->getItemOrder() === $this) {
$item->setItemOrder(null);
}
}
return $this;
}
public function getPack()
{
$subscriptionProducts = [];
foreach ($this->getItems() as $item) {
if ($item->getProduct()->getUniqueKey()) {
$subscriptionProducts[] = $item->getProduct()->getUniqueKey();
}
}
return OrderUtils::getPackByProducts($subscriptionProducts);
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getTotalPriceWithoutTax(): ?float
{
return $this->totalPriceWithoutTax;
}
public function setTotalPriceWithoutTax(float $totalPriceWithoutTax): self
{
$this->totalPriceWithoutTax = $totalPriceWithoutTax;
return $this;
}
public function getVATRate(): ?string
{
return $this->VATRate;
}
public function setVATRate(string $VATRate): self
{
$this->VATRate = $VATRate;
return $this;
}
public function getTotalPrice(): ?float
{
return $this->totalPrice;
}
public function setTotalPrice(float $totalPrice): self
{
$this->totalPrice = $totalPrice;
return $this;
}
public function getInvoice(): ?Invoice
{
return $this->invoice;
}
public function setInvoice(?Invoice $invoice): self
{
$this->invoice = $invoice;
return $this;
}
public function getOrganization(): ?Organization
{
return $this->organization;
}
public function setOrganization(?Organization $organization): self
{
$this->organization = $organization;
return $this;
}
public function __toString()
{
return '#'.$this->getId();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return mixed
*/
public function getCategory()
{
return $this->category;
}
/**
* @param mixed $category
*/
public function setCategory($category): self
{
$this->category = $category;
return $this;
}
}