<?php
namespace App\Entity;
use App\Entity\SuperClass\BaseEntity;
use App\Repository\OrderRepository;
use App\Validator\Cart\UniqueItem;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=OrderRepository::class)
* @ORM\Table(name="`order`")
*/
class Order extends BaseEntity
{
/**
* An order that is in progress, not placed yet.
*
* @var string
*/
public const STATUS_CART = 'cart';
/**
* @ORM\Column(type="string", length=255)
*/
private $status = self::STATUS_CART;
/**
* @ORM\OneToMany(targetEntity=OrderItem::class, mappedBy="orderRef", cascade={"persist","remove"})
* @UniqueItem()
*/
private $orderItems;
/**
* @ORM\ManyToOne(targetEntity=Customer::class, inversedBy="orders")
*/
private $customer;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $accepted_terms_at;
/**
* @ORM\Column(type="boolean")
*/
private $public_accessable = true;
/**
* @ORM\ManyToOne(targetEntity=Source::class, cascade={"persist", "remove"})
*/
private $source;
/**
* @ORM\ManyToOne(targetEntity=Address::class, cascade={"persist", "remove"})
*/
private $billing;
/**
* @ORM\ManyToOne(targetEntity=Address::class, cascade={"persist", "remove"})
*/
private $shipping;
public function __construct()
{
$this->orderItems = new ArrayCollection();
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
/**
* @return Collection<int, OrderItem>
*/
public function getOrderItems(): Collection
{
return $this->orderItems;
}
public function addOrderItem(OrderItem $orderItem): self
{
if (!$this->orderItems->contains($orderItem)) {
$this->orderItems[] = $orderItem;
$orderItem->setOrderRef($this);
}
return $this;
}
public function removeOrderItem(OrderItem $orderItem): self
{
if ($this->orderItems->removeElement($orderItem)) {
// set the owning side to null (unless already changed)
if ($orderItem->getOrderRef() === $this) {
$orderItem->setOrderRef(null);
}
}
return $this;
}
/**
* Calculates the order total.
*/
public function getTotal(): float
{
$total = 0;
foreach ($this->getOrderItems() as $item) {
$total += $item->getPaymentOption()->getAmount();
}
return $total;
}
public function getCustomer(): ?Customer
{
return $this->customer;
}
public function setCustomer(?Customer $customer): self
{
$this->customer = $customer;
return $this;
}
public function getAcceptedTermsAt(): ?\DateTimeInterface
{
return $this->accepted_terms_at;
}
public function setAcceptedTermsAt(?\DateTimeInterface $accepted_terms_at): self
{
$this->accepted_terms_at = $accepted_terms_at;
return $this;
}
public function isPublicAccessable(): ?bool
{
return $this->public_accessable;
}
public function setPublicAccessable(bool $public_accessable): self
{
$this->public_accessable = $public_accessable;
return $this;
}
public function getSource(): ?Source
{
return $this->source;
}
public function setSource(?Source $source): self
{
$this->source = $source;
return $this;
}
public function getBilling(): ?Address
{
return $this->billing;
}
public function setBilling(?Address $billing): self
{
$this->billing = $billing;
return $this;
}
public function getShipping(): ?Address
{
return $this->shipping;
}
public function setShipping(?Address $shipping): self
{
$this->shipping = $shipping;
return $this;
}
}