<?php
namespace App\Entity;
use App\Repository\OrderProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
#[ORM\Entity(repositoryClass: OrderProductRepository::class)]
class OrderProduct implements JsonSerializable
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\ManyToOne(targetEntity: Product::class)]
#[ORM\JoinColumn(nullable: false)]
private $product;
#[ORM\Column(type: 'decimal', precision: 10, scale: 3)]
private $price;
#[ORM\Column(type: 'integer')]
private $qty;
#[ORM\ManyToOne(targetEntity: Order::class, inversedBy: 'orderProducts')]
#[ORM\JoinColumn(nullable: false)]
private $orderParent;
#[ORM\Column(type: 'integer', nullable: true)]
private $qtePreCmd;
#[ORM\OneToMany(targetEntity: OrderProductSurface::class, mappedBy: 'orderProduct')]
private $surfaces;
#[ORM\OneToOne(targetEntity: ViewOrderProductSansArbres::class, mappedBy: 'orderProduct', cascade: ['persist', 'remove'])]
private $orderProductSansArbres;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $tva = null;
public function __construct()
{
$this->surfaces = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
return $this;
}
public function getPrice(): ?string
{
return $this->price;
}
public function setPrice(string $price): self
{
$this->price = $price;
return $this;
}
public function getQty(): ?int
{
return $this->qty;
}
public function setQty(int $qty): self
{
$this->qty = $qty;
return $this;
}
public function getOrderParent(): ?Order
{
return $this->orderParent;
}
public function setOrderParent(?Order $orderParent): self
{
$this->orderParent = $orderParent;
return $this;
}
public function getAmount() {
return $this->getPrice() * $this->getQty();
}
public function getAmountHT() {
return $this->getAmount() - $this->getTva();
}
public function getQtePreCmd(): ?int
{
return $this->qtePreCmd;
}
public function setQtePreCmd(?int $qtePreCmd): self
{
$this->qtePreCmd = $qtePreCmd;
return $this;
}
/**
* @return Collection<int, OrderProductSurface>
*/
public function getSurfaces(): Collection
{
return $this->surfaces;
}
public function addSurface(OrderProductSurface $surface): self
{
if (!$this->surfaces->contains($surface)) {
$this->surfaces[] = $surface;
$surface->setOrderProduct($this);
}
return $this;
}
public function removeSurface(OrderProductSurface $surface): self
{
if ($this->surfaces->removeElement($surface)) {
// set the owning side to null (unless already changed)
if ($surface->getOrderProduct() === $this) {
$surface->setOrderProduct(null);
}
}
return $this;
}
public function jsonSerialize()
{
$vars = get_object_vars($this);
unset($vars["orderParent"]);
unset($vars["product"]);
$vars["surfaces"] = $this->getSurfaces()->toArray();;
return $vars;
}
public function getZonesAvecNbrArbres(){
$result = [];
foreach($this->getSurfaces() as $s){
$key = $s->getSurface()->getId();
$result[$key] = ["id" => $key, "qte" => $s->getQte(), "surface" => $s->getSurface()];
}
return $result;
}
public function getOrderProductSansArbres(): ?ViewOrderProductSansArbres
{
return $this->orderProductSansArbres;
}
public function setOrderProductSansArbres(ViewOrderProductSansArbres $orderProductSansArbres): self
{
// set the owning side of the relation if necessary
if ($orderProductSansArbres->getOrderProduct() !== $this) {
$orderProductSansArbres->setOrderProduct($this);
}
$this->orderProductSansArbres = $orderProductSansArbres;
return $this;
}
public function getTva(): ?string
{
return $this->tva;
}
public function setTva(?string $tva): self
{
$this->tva = $tva;
return $this;
}
}