|
- <?php
-
- namespace Lc\ShopBundle\Model ;
-
- 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()
- {
- return $this->getPrice() ;
- }
-
- public function getUnitInherited()
- {
- return $this->getUnit() ;
- }
-
- public function getTaxRateInherited(): ?TaxRate
- {
- return $this->getTaxRate() ;
- }
-
- public function getPriceWithTax()
- {
- return $this->calculatePriceWithTax($this->getPriceInherited(), $this->getTaxRateInherited()->getValue()) ;
- }
-
- public function getPriceByUnitRef()
- {
- return $this->getPriceInherited() * $this->getUnitInherited()->getCoefficient() ;
- }
-
- public function getPriceByUnitRefWithTax()
- {
- return $this->calculatePriceWithTax($this->getPriceByUnitRef(), $this->getTaxRateInherited()->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)
- {
- $price = floatval($priceWithoutTax) * ($taxRateValue + 1) ;
- $price = number_format(( ($price * 100)) / 100, 2) ;
- return $price ;
- }
- }
|