<?php
namespace App\Entity\OnlineShop;
use App\Entity\AbstractBase;
use App\Repository\OnlineShop\ProductOrderRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Money\Currency;
use Money\Money;
/**
* @ORM\Table(name="vulco_order_product")
*
* @ORM\Entity(repositoryClass=ProductOrderRepository::class)
*
* @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
*/
class ProductOrder extends AbstractBase
{
/**
* @ORM\Column(type="integer")
*/
private int $quantity;
/**
* @ORM\Column(type="money", nullable=true)
*/
private ?Money $sigaus;
/**
* @ORM\Column(type="money", nullable=true)
*/
private ?Money $greenPoint;
/**
* @ORM\Column(type="money", nullable=true)
*/
private ?Money $ecoTax;
/**
* @ORM\Column(type="money", nullable=false)
*/
private Money $ratePrice;
/**
* @ORM\Column(type="money", nullable=false)
*/
private Money $costPrice;
/**
* @ORM\Column(type="money", nullable=true)
* At the moment we only use this field for orders export of garages with GRIPS software
*/
private ?Money $pvpPrice = null;
/**
* @ORM\Column(type="money", nullable=false)
*/
private Money $totalPrice;
/**
* @ORM\Column(type="money", nullable=true)
*/
private ?Money $totalPriceWithoutTax = null;
/**
* @ORM\Column(type="text", length=10000, nullable=true)
*/
private ?string $promotion = null;
/**
* @ORM\Column(type="text", length=10000, nullable=true)
*/
private ?string $comment = null;
/**
* @ORM\Column(type="float", nullable=true)
*/
private ?float $rateDiscount = 0.0;
/**
* @ORM\Column(type="float", nullable=true)
*/
private ?float $costDiscount = 0.0;
/**
* @ORM\Column(type="money", nullable=true, options={"default": "EUR 0"})
*/
private ?Money $noLinealDiscountRate;
/**
* @ORM\Column(type="money", nullable=true, options={"default": "EUR 0"})
*/
private ?Money $noLinealDiscountCost;
/**
* @ORM\Column(type="integer", nullable=true, options={"default": 0})
*/
private ?int $linealDiscountRate = 0;
/**
* @ORM\Column(type="integer", nullable=true, options={"default": 0})
*/
private ?int $linealDiscountCost = 0;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\OnlineShop\Order", inversedBy="products")
*
* @ORM\JoinColumn(name="order_id", referencedColumnName="id")
*/
private Order $order;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\OnlineShop\Product", fetch="EAGER")
*
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private ?Product $product = null;
/**
* @ORM\Column(type="string", nullable=true)
*/
private ?string $productFamilyName = null;
/**
* @ORM\Column(type="string", nullable=true)
*/
private ?string $productName = null;
public function __construct()
{
$this->sigaus = new Money(0, new Currency(self::DEFAULT_EURO_CURRENCY));
$this->greenPoint = new Money(0, new Currency(self::DEFAULT_EURO_CURRENCY));
$this->ecoTax = new Money(0, new Currency(self::DEFAULT_EURO_CURRENCY));
$this->ratePrice = new Money(0, new Currency(self::DEFAULT_EURO_CURRENCY));
$this->costPrice = new Money(0, new Currency(self::DEFAULT_EURO_CURRENCY));
$this->totalPrice = new Money(0, new Currency(self::DEFAULT_EURO_CURRENCY));
$this->noLinealDiscountCost = new Money(0, new Currency(self::DEFAULT_EURO_CURRENCY));
$this->noLinealDiscountRate = new Money(0, new Currency(self::DEFAULT_EURO_CURRENCY));
}
public function getQuantity(): int
{
return $this->quantity;
}
public function setQuantity(int $quantity): self
{
$this->quantity = $quantity;
return $this;
}
public function getSigaus(): ?Money
{
return $this->sigaus;
}
public function setSigaus(?Money $sigaus): self
{
$this->sigaus = $sigaus;
return $this;
}
public function getGreenPoint(): ?Money
{
return $this->greenPoint;
}
public function setGreenPoint(?Money $greenPoint): self
{
$this->greenPoint = $greenPoint;
return $this;
}
public function getEcoTax(): ?Money
{
return $this->ecoTax;
}
public function setEcoTax(?Money $ecoTax): self
{
$this->ecoTax = $ecoTax;
return $this;
}
public function getRatePrice(): Money
{
return $this->ratePrice;
}
public function getRatePriceWithAdditionalDiscount(): Money
{
return $this->ratePrice->multiply(1 - $this->getRateDiscount() / 100);
}
private function getRatePriceWithDiscountAndTax(): Money
{
return $this->getRatePriceWithAdditionalDiscount()
->add($this->sigaus)
->add($this->greenPoint)
->add($this->ecoTax);
}
public function getRatePriceWithAllDiscountsForCSVExport(): Money
{
$price = $this->getRatePriceWithAdditionalDiscount();
if ($this->linealDiscountRate > 0) {
return $price->multiply(1 - $this->linealDiscountRate / 100);
}
if ($this->noLinealDiscountRate->isPositive()) {
return $price->subtract($this->noLinealDiscountRate);
}
return $price;
}
public function setRatePrice(Money $ratePrice): self
{
$this->ratePrice = $ratePrice;
return $this;
}
public function getCostPrice(): Money
{
return $this->costPrice;
}
public function getCostPriceWithAdditionalDiscount(): Money
{
return $this->costPrice->multiply(1 - $this->getCostDiscount() / 100);
}
public function getCostPriceWithAllDiscountsForCSVExport(): Money
{
$price = $this->getCostPriceWithAdditionalDiscount();
if ($this->linealDiscountCost > 0) {
return $price->multiply(1 - $this->linealDiscountCost / 100);
}
if ($this->noLinealDiscountCost->isPositive()) {
return $price->subtract($this->noLinealDiscountCost);
}
return $price;
}
public function setCostPrice(Money $costPrice): self
{
$this->costPrice = $costPrice;
return $this;
}
public function getPvpPrice(): ?Money
{
return $this->pvpPrice;
}
public function setPvpPrice(?Money $pvpPrice): self
{
$this->pvpPrice = $pvpPrice;
return $this;
}
public function getTotalPrice(): Money
{
return $this->totalPrice;
}
public function setTotalPrice(Money $totalPrice): self
{
$this->totalPrice = $totalPrice;
return $this;
}
public function getTotalPriceWithoutTax(): ?Money
{
return $this->totalPriceWithoutTax;
}
public function setTotalPriceWithoutTax(?Money $totalPriceWithoutTax): self
{
$this->totalPriceWithoutTax = $totalPriceWithoutTax;
return $this;
}
public function getPromotion(): ?string
{
return $this->promotion;
}
public function setPromotion(?string $promotion): self
{
$this->promotion = $promotion;
return $this;
}
public function getComment(): ?string
{
return $this->comment;
}
public function setComment(?string $comment): self
{
$this->comment = $comment;
return $this;
}
public function getRateDiscount(): ?float
{
return $this->rateDiscount;
}
public function setRateDiscount(?float $rateDiscount): self
{
$this->rateDiscount = $rateDiscount;
return $this;
}
public function getCostDiscount(): ?float
{
return $this->costDiscount;
}
public function setCostDiscount(?float $costDiscount): self
{
$this->costDiscount = $costDiscount;
return $this;
}
public function getNoLinealDiscountRate(): ?Money
{
return $this->noLinealDiscountRate;
}
public function setNoLinealDiscountRate(?Money $noLinealDiscountRate): self
{
$this->noLinealDiscountRate = $noLinealDiscountRate;
return $this;
}
public function getNoLinealDiscountCost(): ?Money
{
return $this->noLinealDiscountCost;
}
public function setNoLinealDiscountCost(?Money $noLinealDiscountCost): self
{
$this->noLinealDiscountCost = $noLinealDiscountCost;
return $this;
}
public function getLinealDiscountRate(): ?int
{
return $this->linealDiscountRate;
}
public function setLinealDiscountRate(?int $linealDiscountRate): self
{
$this->linealDiscountRate = $linealDiscountRate;
return $this;
}
public function getLinealDiscountCost(): ?int
{
return $this->linealDiscountCost;
}
public function setLinealDiscountCost(?int $linealDiscountCost): self
{
$this->linealDiscountCost = $linealDiscountCost;
return $this;
}
public function getOrder(): Order
{
return $this->order;
}
public function setOrder(Order $order): self
{
$this->order = $order;
return $this;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
return $this;
}
public function getProductFamilyName(): ?string
{
return $this->productFamilyName;
}
public function setProductFamilyName(?string $productFamilyName): self
{
$this->productFamilyName = $productFamilyName;
return $this;
}
public function getProductName(): ?string
{
return $this->productName;
}
public function setProductName(?string $productName): self
{
$this->productName = $productName;
return $this;
}
public function update(Product $product, $quantity): self
{
$this->product = $product;
$this->sigaus = $product->getSigaus();
$this->ecoTax = $product->getEcoTax();
$this->greenPoint = $product->getGreenPoint();
if ($product->hasSpecialPriceActive()) {
$this->ratePrice = $product->getSpecialPriceActive()->getPrice();
$this->costPrice = $product->getSpecialPriceActive()->getCostPrice();
} else {
$this->ratePrice = $product->getRatePriceWithDiscount($quantity);
$this->costPrice = $product->getCostPriceWithDiscount($quantity);
}
$this->pvpPrice = $product->getPvpPrice();
$this->quantity = $quantity;
$this->totalPrice = $this->getRatePriceWithDiscountAndTax()->multiply($quantity);
$this->totalPriceWithoutTax = $this->getRatePriceWithAdditionalDiscount()->multiply($quantity);
$this->promotion = $product->getPromotion();
return $this;
}
public function updateFromSpecialOfferProduct(SpecialOfferProduct $specialOfferProduct, $quantity): self
{
$realQuantity = $quantity * $specialOfferProduct->units()->quantity();
$product = $specialOfferProduct->getProduct();
$this->product = $product;
$this->sigaus = $product->getSigaus();
$this->ecoTax = $product->getEcoTax();
$this->greenPoint = $product->getGreenPoint();
$this->ratePrice = new Money((int) ($specialOfferProduct->finalPrice() * 100), new Currency(self::DEFAULT_EURO_CURRENCY));
$this->costPrice = $product->getCostPriceWithDiscount($realQuantity);
$this->pvpPrice = $product->getPvpPrice();
$this->quantity = $realQuantity;
$this->totalPrice = $this->getRatePriceWithDiscountAndTax()->multiply($realQuantity);
$this->totalPriceWithoutTax = $this->getRatePriceWithAdditionalDiscount()->multiply($realQuantity);
$this->promotion = $specialOfferProduct->specialOffer()->code();
return $this;
}
}