<?php
namespace App\Entity\Order;
use App\Repository\OrderLineRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Events;
use Doctrine\ORM\Event\PrePersistEventArgs;
use App\Entity\Produit\Produit;
use App\Entity\Order\OrderLineColor;
use App\EventListener\OrderLineListener;
#[ORM\Entity(repositoryClass: OrderLineRepository::class)]
#[ORM\HasLifecycleCallbacks]
class OrderLine
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'orderLines', cascade: ['persist'])]
#[ORM\JoinColumn(nullable: false)]
private ?Order $orderentity = null;
#[ORM\Column]
private ?int $quantity = null;
#[ORM\Column(length: 255, nullable:true)]
private ?string $description = null;
#[ORM\ManyToOne]
private ?Produit $produit = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $priceHt = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $priceTtc = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $totalLineTtc = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $totalLineHt = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $vat = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $decompte = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $totalCarrierWithoutTaxe = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $totalCarrierWithTaxes = null;
#[ORM\OneToOne(cascade: ['persist', 'remove'], orphanRemoval: true )]
private ?OrderLineColor $orderLineColor = null;
#[ORM\OneToMany(mappedBy: 'orderLine', targetEntity: OrderLineOption::class, orphanRemoval: true, cascade: ["persist", "remove"], indexBy: 'opt_id')]
private Collection $orderLineOptions;
#[ORM\OneToMany(mappedBy: 'orderLine', targetEntity: OrderLineGarantie::class, orphanRemoval: true, cascade: ["persist", "remove"], indexBy: 'garantie_id')]
private Collection $orderLineGaranties;
#[ORM\OneToOne(cascade: ['persist', 'remove'], orphanRemoval: true)]
private ?OrderLineFinition $orderLineFinition = null;
public function __construct()
{
$this->orderLineColors = new ArrayCollection();
$this->orderLineOptions = new ArrayCollection();
$this->orderLineGaranties = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getOrderentity(): ?Order
{
return $this->orderentity;
}
public function setOrderentity(?Order $orderentity): self
{
$this->orderentity = $orderentity;
return $this;
}
public function getQuantity()
{
if(empty($this->quantity)) return 0;
return $this->quantity;
}
public function setQuantity( $quantity): self
{
$this->quantity = $quantity;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getProduit(): ?Produit
{
return $this->produit;
}
public function setProduit(?Produit $produit): self
{
$this->produit = $produit;
return $this;
}
public function getPriceHt(): ?string
{
return $this->priceHt;
}
public function setPriceHt(string $priceHt): self
{
$this->priceHt = $priceHt;
return $this;
}
public function getPriceTtc()
{
return $this->priceTtc;
}
public function setPriceTtc($priceTtc): self
{
$this->priceTtc = $priceTtc;
return $this;
}
public function getVat(): ?string
{
return $this->vat;
}
public function setVat(string $vat): self
{
$this->vat = $vat;
return $this;
}
public function getDecompte(): ?string
{
return $this->decompte;
}
public function setDecompte(string $decompte): self
{
$this->decompte = $decompte;
return $this;
}
public function getTotalLineHt()
{
return $this->totalLineHt;
}
public function setTotalLineHt($totalLineHt): self
{
$this->totalLineHt = $totalLineHt;
return $this;
}
public function getTotalLineTtc()
{
return $this->totalLineTtc;
}
public function setTotalLineTtc($totalLineTtc): self
{
$this->totalLineTtc = $totalLineTtc;
return $this;
}
public function getTotalCarrierWithoutTaxe(): ?string
{
return $this->totalCarrierWithoutTaxe;
}
public function setTotalCarrierWithoutTaxe(?string $totalCarrierWithoutTaxe): static
{
$this->totalCarrierWithoutTaxe = $totalCarrierWithoutTaxe;
return $this;
}
public function getTotalCarrierWithTaxes(): ?string
{
return $this->totalCarrierWithTaxes;
}
public function setTotalCarrierWithTaxes(?string $totalCarrierWithTaxes): static
{
$this->totalCarrierWithTaxes = $totalCarrierWithTaxes;
return $this;
}
public function updateTotalLine()
{
$priceTtcColor = 0;
$priceHtColor = 0;
$priceTtcOptions = 0;
$priceHtOptions = 0;
$priceTtcGaranties = 0;
$priceHtGaranties = 0;
$priceTtcFinition = 0;
$priceHtFinition = 0;
if(!empty($this->getOrderLineFinition())){
$priceTtcFinition = $this->getOrderLineFinition()->getPriceTtc();
$priceHtFinition = $this->getOrderLineFinition()->getPriceHt();
}
if(!empty($this->getOrderLineColor())){
$priceTtcColor = $this->getOrderLineColor()->getPriceTtc();
$priceHtColor = $this->getOrderLineColor()->getPriceHt();
}
$orderLine = $this;
if(!empty($this->getOrderLineOptions())){
foreach($this->getOrderLineOptions() as $opt){
$priceTtcOptions += $opt->getPriceTtc();
$priceHtOptions += $opt->getPriceHt();
}
}
if(!empty($this->getOrderLineGaranties())){
foreach($this->getOrderLineGaranties() as $g){
$priceTtcGaranties += $g->getPriceTtc();
$priceHtGaranties += $g->getPriceHt();
}
}
$produit_ttc = $this->getProduit()->getPriceTtc();
$produit_ht = $this->getProduit()->getPriceHt();
$totalTtc = $produit_ttc + $priceTtcColor + $priceTtcOptions + $priceTtcGaranties + $priceTtcFinition;
$totalHt = $produit_ht + $priceHtColor + $priceHtOptions + $priceHtGaranties + $priceHtFinition;
$totalLineHt = $totalHt * $orderLine->getQuantity();
$totalLineTtc = $totalTtc * $orderLine->getQuantity();
$orderLine->setPriceHt($totalHt);
$orderLine->setPriceTtc($totalTtc);
$orderLine->setTotalLineTtc($totalLineTtc);
$orderLine->setTotalLineHt($totalLineHt);
}
public function getOrderLineColor(): ?OrderLineColor
{
return $this->orderLineColor;
}
public function setOrderLineColor(?OrderLineColor $orderLineColor): static
{
$this->orderLineColor = $orderLineColor;
return $this;
}
/**
* @return Collection<int, OrderLineOption>
*/
public function getOrderLineOptions(): Collection
{
return $this->orderLineOptions;
}
/**
* @return Collection<int, OrderLineOption>
*/
public function setOrderLineOptions($orderLineOptions)
{
$this->orderLineOptions = $orderLineOptions;
return $this;
}
public function addOrderLineOption(OrderLineOption $orderLineOption): static
{
if (!$this->orderLineOptions->contains($orderLineOption)) {
$this->orderLineOptions->add($orderLineOption);
$orderLineOption->setOrderLine($this);
}
return $this;
}
public function removeOrderLineOption(OrderLineOption $orderLineOption): static
{
if ($this->orderLineOptions->removeElement($orderLineOption)) {
// set the owning side to null (unless already changed)
if ($orderLineOption->getOrderLine() === $this) {
$orderLineOption->setOrderLine(null);
}
}
return $this;
}
/**
* @return Collection<int, OrderLineGarantie>
*/
public function getOrderLineGaranties(): Collection
{
return $this->orderLineGaranties;
}
public function addOrderLineGaranty(OrderLineGarantie $orderLineGaranty): static
{
if (!$this->orderLineGaranties->contains($orderLineGaranty)) {
$this->orderLineGaranties->add($orderLineGaranty);
$orderLineGaranty->setOrderLine($this);
}
return $this;
}
public function removeOrderLineGaranty(OrderLineGarantie $orderLineGaranty): static
{
if ($this->orderLineGaranties->removeElement($orderLineGaranty)) {
// set the owning side to null (unless already changed)
if ($orderLineGaranty->getOrderLine() === $this) {
$orderLineGaranty->setOrderLine(null);
}
}
return $this;
}
public function getOrderLineFinition(): ?OrderLineFinition
{
return $this->orderLineFinition;
}
public function setOrderLineFinition(?OrderLineFinition $orderLineFinition): static
{
$this->orderLineFinition = $orderLineFinition;
return $this;
}
public function getTotalTtcProduct(){
$priceTtcColor = 0;
$priceHtColor = 0;
$priceTtcOptions = 0;
$priceHtOptions = 0;
$priceTtcGaranties = 0;
$priceHtGaranties = 0;
if(!empty($this->getOrderLineColor())){
$priceTtcColor = $this->getOrderLineColor()->getPriceTtc();
$priceHtColor = $this->getOrderLineColor()->getPriceHt();
}
$orderLine = $this;
$ht_unit = $orderLine->getPriceHt();
if(!empty($this->getOrderLineOptions())){
foreach($this->getOrderLineOptions() as $opt){
$priceTtcOptions += $opt->getPriceTtc();
$priceHtOptions += $opt->getPriceHt();
}
}
if(!empty($this->getOrderLineGaranties())){
foreach($this->getOrderLineGaranties() as $g){
$priceTtcGaranties += $g->getPriceTtc();
$priceHtGaranties += $g->getPriceHt();
}
}
return $orderLine->getPriceTtc() + $priceTtcColor + $priceTtcOptions + $priceTtcGaranties;
}
}