|
- <?php
-
- namespace Lc\ShopBundle\Model ;
-
- use Lc\ShopBundle\Context\ReductionCatalogInterface;
- use Lc\ShopBundle\Context\UnitInterface;
-
- trait PriceTrait
- {
- /**
- * @ORM\Column(type="float", nullable=true)
- */
- protected $price;
-
- /**
- * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\UnitInterface")
- * @ORM\JoinColumn(nullable=true)
- */
- protected $unit;
-
- /**
- * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\TaxRateInterface")
- * @ORM\JoinColumn(nullable=true)
- */
- protected $taxRate;
-
- public function getPrice(): ?float
- {
- return $this->price;
- }
-
- public function getPriceInherited(): ?float
- {
- return $this->getPrice() ;
- }
-
- public function getUnitInherited(): ?Unit
- {
- return $this->getUnit() ;
- }
-
- public function getTaxRateInherited(): ?TaxRate
- {
- return $this->getTaxRate() ;
- }
-
- public function getPriceWithTax(): ?float
- {
- return $this->calculatePriceWithTax($this->getPriceInherited(), $this->getTaxRateInherited()->getValue()) ;
- }
-
- public function getPriceByUnitRef(): ?float
- {
- return $this->calculatePriceByUnitRef($this->getPriceInherited()) ;
- }
-
- public function getPriceByUnitRefWithTax(): ?float
- {
- return $this->calculatePriceByUnitRef($this->getPriceWithTax()) ;
- }
-
- public function getTheReductionCatalog()
- {
- $reductionCatalog = false;
-
- if($this instanceof ProductFamily) {
- $reductionCatalog = $this->getReductionCatalog() ;
- }
- if($this instanceof Product) {
- $reductionCatalog = $this->getProductFamily()->getReductionCatalog() ;
- }
-
- return $reductionCatalog ;
- }
-
- public function getPriceByUnitRefWithTaxAndReduction(): ?float
- {
- $reductionCatalog = $this->getTheReductionCatalog() ;
-
- if (isset($reductionCatalog) && $reductionCatalog) {
- return $this->calculatePriceByUnitRef($this->getPriceWithTaxAndReductionCatalog($reductionCatalog)) ;
- }
- else {
- return $this->calculatePriceByUnitRef($this->getPriceWithTax());
- }
- }
-
- public function getPriceWithTaxAndReduction(): ?float
- {
- $reductionCatalog = $this->getTheReductionCatalog() ;
-
- if (isset($reductionCatalog) && $reductionCatalog) {
- return $this->getPriceWithTaxAndReductionCatalog($reductionCatalog);
- }
- else {
- return $this->getPriceWithTax();
- }
- }
-
- public function getPriceWithTaxAndReductionCatalog(ReductionCatalogInterface $reductionCatalog): ?float
- {
-
- if ($reductionCatalog->getUnit() == 'percent') {
- //Théoriquement que la réduction s'applique sur le prixHT ou le prixTTC le résultat est le même,j'ai laisser mon code au cas où ;)
- return $this->calculatePriceWithReductionPercent($this->getPriceWithTax(), $reductionCatalog->getValue());
- /*if ($reductionCatalog->getBehaviorTaxRate() == 'tax-excluded') {
- $priceReductionHT = $this->calculatePriceWithReductionPercent($this->getPriceInherited(), $reductionCatalog->getValue());
- return $this->calculatePriceWithTax($priceReductionHT, $this->getTaxRateInherited()->getValue());
- } else if ($reductionCatalog->getBehaviorTaxRate() == 'tax-included') {
-
- return $this->calculatePriceWithReductionPercent($this->getPriceWithTax(), $reductionCatalog->getValue());
- }*/
- }elseif ($reductionCatalog->getUnit() == 'amount') {
- if ($reductionCatalog->getBehaviorTaxRate() == 'tax-excluded') {
- $priceReductionHT = $this->calculatePriceWithReductionAmount($this->getPriceInherited(), $reductionCatalog->getValue());
- return $this->calculatePriceWithTax($priceReductionHT, $this->getTaxRateInherited()->getValue());
- }else if ($reductionCatalog->getBehaviorTaxRate() == 'tax-included') {
- return $this->calculatePriceWithReductionAmount($this->getPriceWithTax(), $reductionCatalog->getValue());
- }
- }
- }
-
- public function setPrice(float $price): self
- {
- $this->price = $price;
-
- return $this;
- }
-
- public function getUnit(): ?Unit
- {
- return $this->unit;
- }
-
- public function setUnit(Unit $unit): self
- {
- $this->unit = $unit;
-
- return $this;
- }
-
- public function getTaxRate(): ?TaxRate
- {
- return $this->taxRate;
- }
-
- public function setTaxRate(?TaxRate $taxRate): self
- {
- $this->taxRate = $taxRate;
-
- return $this;
- }
-
- private function calculatePriceWithTax($priceWithoutTax, $taxRateValue): ?float
- {
- $price = floatval($priceWithoutTax) * ($taxRateValue / 100 + 1);
- $price = round((($price * 100)) / 100, 2);
- return $price;
- }
-
- public function calculatePriceWithReductionPercent($price, $percentValue): ?float
- {
- $price = floatval($price) * (1 - $percentValue / 100);
- $price = round((($price * 100)) / 100, 2);
- return $price;
- }
- public function calculatePriceWithReductionAmount($price, $amountValue): ?float
- {
- $price = floatval($price) - $amountValue;
- $price = round((($price * 100)) / 100, 2);
- return $price;
- }
-
- private function calculatePriceByUnitRef($price)
- {
- return ($price * $this->getUnitInherited()->getCoefficient()) / $this->getQuantityInherited() ;
- }
- }
|