<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Blameable\Traits\BlameableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Validator\Constraints as Assert;
use App\Repository\OrderDetailRepository;
#[ORM\Entity(repositoryClass: OrderDetailRepository::class)]
class OrderDetail
{
use BlameableEntity; //Hook blameable behaviour. Updates createdBy, updatedBy fields
use TimestampableEntity; //Hook timestampable behaviour. Updates createdAt, updatedAt fields
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "IDENTITY")]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'orderDetails')]
#[ORM\JoinColumn(nullable: false)]
private ?Order $header = null;
#[ORM\ManyToOne(inversedBy: 'orderDetails')]
private ?Product $product = null;
#[ORM\ManyToOne(inversedBy: 'orderDetails')]
private ?ProductPriceScale $productPriceScale = null;
#[ORM\Column]
#[Assert\NotNull()]
#[Assert\PositiveOrZero()]
private ?int $quantity = null;
#[ORM\Column]
#[Assert\NotNull()]
#[Assert\PositiveOrZero()]
private ?float $originalPrice = null;
#[ORM\Column(nullable: true)]
#[Assert\PositiveOrZero()]
private ?float $finalPrice = null;
#[ORM\Column]
#[Assert\NotNull()]
#[Assert\PositiveOrZero()]
private ?float $ivaPercentage = null;
#[ORM\OneToOne(mappedBy: 'originOrderDetail', cascade: ['persist', 'remove'])]
private ?UserShared $originUserShared = null;
#[ORM\ManyToOne(inversedBy: 'destitationOrderDetails')]
private ?UserShared $destinationUserShared = null;
#[ORM\Column(nullable: true)]
private ?bool $buy_not_enought = null;
#[ORM\Column(nullable: true)]
private ?bool $better_price = null;
#[ORM\Column(nullable: true)]
private ?bool $isIndividualPurchase = null;
public function __toString()
{
return ($this->header ? $this->header->getId() : "") . "_" . $this->id;
}
public function getId(): ?int
{
return $this->id;
}
public function getHeader(): ?Order
{
return $this->header;
}
public function setHeader(?Order $header): self
{
$this->header = $header;
return $this;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
return $this;
}
public function getProductPriceScale(): ?ProductPriceScale
{
return $this->productPriceScale;
}
public function setProductPriceScale(?ProductPriceScale $productPriceScale): self
{
$this->productPriceScale = $productPriceScale;
return $this;
}
public function getQuantity(): ?int
{
return $this->quantity;
}
public function setQuantity(int $quantity): self
{
$this->quantity = $quantity;
return $this;
}
public function getOriginalPrice(): ?float
{
return $this->originalPrice;
}
public function setOriginalPrice(float $originalPrice): self
{
$this->originalPrice = $originalPrice;
return $this;
}
public function getFinalPrice(): ?float
{
return $this->finalPrice;
}
public function setFinalPrice(?float $finalPrice): self
{
$this->finalPrice = $finalPrice;
return $this;
}
public function getIvaPercentage(): ?float
{
return $this->ivaPercentage;
}
public function setIvaPercentage(float $ivaPercentage): self
{
$this->ivaPercentage = $ivaPercentage;
return $this;
}
public function getOriginUserShared(): ?UserShared
{
return $this->originUserShared;
}
public function setOriginUserShared(UserShared $originUserShared): self
{
// set the owning side of the relation if necessary
if ($originUserShared->getOriginOrderDetail() !== $this) {
$originUserShared->setOriginOrderDetail($this);
}
$this->originUserShared = $originUserShared;
return $this;
}
public function getDestinationUserShared(): ?UserShared
{
return $this->destinationUserShared;
}
public function setDestinationUserShared(?UserShared $destinationUserShared): self
{
$this->destinationUserShared = $destinationUserShared;
return $this;
}
public function isBuyNotEnought(): ?bool
{
return $this->buy_not_enought;
}
public function setBuyNotEnought(?bool $buy_not_enought): self
{
$this->buy_not_enought = $buy_not_enought;
return $this;
}
public function isBetterPrice(): ?bool
{
return $this->better_price;
}
public function setBetterPrice(?bool $better_price): self
{
$this->better_price = $better_price;
return $this;
}
public function isIndividualPurchase(): ?bool
{
return $this->isIndividualPurchase;
}
public function setIsIndividualPurchase(bool $isIndividualPurchase): self
{
$this->isIndividualPurchase = $isIndividualPurchase;
return $this;
}
public function getDeliveryDate()
{
$product = $this->getProduct();
$firstOrderForProduct = (!$product->hasBeenPurchasedRecently($this->header->getId()) && $product->getSharedShippingDuration());
$deliveryDate = ($firstOrderForProduct
? date_modify(new \DateTime('now'), "+{$product->getSharedShippingDuration()}weekdays")
: $product->getShippingDate()
)->format('d-m-Y');
return $deliveryDate;
}
public function getSaving(): float
{
$_product = $this->getProduct();
$finalPrice = $this->getFinalPrice() > 0 ? $this->getFinalPrice() : $this->getOriginalPrice();
$group = $this->getHeader()->getBuyingGroup();
$saving = 0;
if ($group) {
$originalUnitPrice = $_product->getOriginalPrice() / $_product->getUnitsPerBoxGrouped();
$unitPrice = $finalPrice / $_product->getUnitsPerBoxGrouped();
$saving = ($_product->getUnitsPerBoxGrouped() > 0)
? (100 - (100 * $unitPrice / $originalUnitPrice))
: (100 - (100 * $finalPrice / $_product->getOriginalPrice()));
} else {
$originalUnitPrice = $_product->getOriginalPrice() / $_product->getUnitsPerBox();
$unitPrice = $finalPrice / $_product->getUnitsPerBox();
$saving = ($_product->getUnitsPerBox() > 0)
? (100 - (100 * $unitPrice / $originalUnitPrice))
: (100 - (100 * $finalPrice / $_product->getOriginalPrice()));
}
return $saving;
}
}