99 lines
2.5KB

  1. <?php
  2. namespace Lc\ShopBundle\Model ;
  3. use Lc\ShopBundle\Context\UnitInterface;
  4. trait PriceTrait
  5. {
  6. /**
  7. * @ORM\Column(type="float", nullable=true)
  8. */
  9. protected $price;
  10. /**
  11. * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\UnitInterface")
  12. * @ORM\JoinColumn(nullable=true)
  13. */
  14. protected $unit;
  15. /**
  16. * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\TaxRateInterface")
  17. * @ORM\JoinColumn(nullable=true)
  18. */
  19. protected $taxRate;
  20. public function getPrice(): ?float
  21. {
  22. return $this->price;
  23. }
  24. public function getPriceInherited(): ?float
  25. {
  26. return $this->getPrice() ;
  27. }
  28. public function getUnitInherited(): ?Unit
  29. {
  30. return $this->getUnit() ;
  31. }
  32. public function getTaxRateInherited(): ?TaxRate
  33. {
  34. return $this->getTaxRate() ;
  35. }
  36. public function getPriceWithTax(): ?float
  37. {
  38. return $this->calculatePriceWithTax($this->getPriceInherited(), $this->getTaxRateInherited()->getValue()) ;
  39. }
  40. public function getPriceByUnitRef(): ?float
  41. {
  42. return ($this->getPriceInherited() * $this->getUnitInherited()->getCoefficient()) / $this->getQuantityInherited() ;
  43. }
  44. public function getPriceByUnitRefWithTax(): ?float
  45. {
  46. return $this->calculatePriceWithTax($this->getPriceByUnitRef(), $this->getTaxRateInherited()->getValue()) ;
  47. }
  48. public function setPrice(float $price): self
  49. {
  50. $this->price = $price;
  51. return $this;
  52. }
  53. public function getUnit(): ?Unit
  54. {
  55. return $this->unit;
  56. }
  57. public function setUnit(Unit $unit): self
  58. {
  59. $this->unit = $unit;
  60. return $this;
  61. }
  62. public function getTaxRate(): ?TaxRate
  63. {
  64. return $this->taxRate;
  65. }
  66. public function setTaxRate(?TaxRate $taxRate): self
  67. {
  68. $this->taxRate = $taxRate;
  69. return $this;
  70. }
  71. private function calculatePriceWithTax($priceWithoutTax, $taxRateValue): ?float
  72. {
  73. $price = floatval($priceWithoutTax) * ($taxRateValue/100 + 1) ;
  74. $price = round(( ($price * 100)) / 100, 2) ;
  75. return $price ;
  76. }
  77. }