<?php
namespace App\Entity;
use App\Repository\IncomingOrderRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass=IncomingOrderRepository::class)
*/
class IncomingOrder
{
use TimestampableEntity;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"list_incoming_order"})
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="incomingOrders")
* @Groups({"list_incoming_order"})
*/
private ?User $user;
/**
* @ORM\Column(type="date")
* @Groups({"list_incoming_order"})
*/
private ?\DateTimeInterface $orderDate;
/**
* @ORM\ManyToOne(targetEntity=Supplier::class, inversedBy="incomingOrders")
* @Groups({"list_incoming_order"})
*/
private $supplier;
/**
* @ORM\Column(type="text", nullable=true)
* @Groups({"list_incoming_order"})
*/
private ?string $notes;
/**
* @ORM\OneToMany(targetEntity=ProductIn::class, mappedBy="incomingOrder", cascade={"persist"})
*/
private $productIns;
/**
* @ORM\Column(type="string", length=255)
*/
private ?string $status;
const STATUS_PENDING = "pending";
const STATUS_SENT = "sent";
const STATUS_COMPLETED = "completed";
/** @var array user friendly named type */
protected static array $statusName = [
self::STATUS_PENDING => 'Pending',
self::STATUS_SENT => 'Sent',
self::STATUS_COMPLETED => 'Completed'
];
public function __construct()
{
$this->productIns = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getOrderDate(): ?\DateTimeInterface
{
return $this->orderDate;
}
public function setOrderDate(\DateTimeInterface $orderDate): self
{
$this->orderDate = $orderDate;
return $this;
}
public function getSupplier(): ?Supplier
{
return $this->supplier;
}
public function setSupplier(?Supplier $supplier): self
{
$this->supplier = $supplier;
return $this;
}
public function getNotes(): ?string
{
return $this->notes;
}
public function setNotes(?string $notes): self
{
$this->notes = $notes;
return $this;
}
/**
* @return Collection<int, ProductIn>
*/
public function getProductIns(): Collection
{
return $this->productIns;
}
public function addProductIn(ProductIn $productIn): self
{
if (!$this->productIns->contains($productIn)) {
$this->productIns[] = $productIn;
$productIn->setIncomingOrder($this);
}
return $this;
}
public function removeProductIn(ProductIn $productIn): self
{
if ($this->productIns->removeElement($productIn)) {
// set the owning side to null (unless already changed)
if ($productIn->getIncomingOrder() === $this) {
$productIn->setIncomingOrder(null);
}
}
return $this;
}
public function getTotalProductInsQty()
{
$productIns = $this->getProductIns();
$total = 0;
foreach ($productIns as $productIn){
$total = $total + $productIn->getQty();
}
return $total;
}
public function getStatus(): ?string
{
return $this->status;
}
public static function getStatusName($statusShortName): ?string
{
if (!isset(static::$statusName[$statusShortName])) {
return "Unknown status ($statusShortName)";
}
return static::$statusName[$statusShortName];
//return $this->status;
}
public function setStatus(string $status): self
{
if (!in_array($status, IncomingOrder::getAvailableStatuses())) {
throw new \InvalidArgumentException("Invalid type");
}
$this->status = $status;
return $this;
}
/**
* @return array<string>
*/
public static function getAvailableStatuses(): array
{
return [
self::STATUS_PENDING,
self::STATUS_SENT,
self::STATUS_COMPLETED
];
}
}