You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 satır
2.6KB

  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->calculatePriceByUnitRef($this->getPriceInherited()) ;
  43. }
  44. public function getPriceByUnitRefWithTax(): ?float
  45. {
  46. return $this->calculatePriceByUnitRef($this->getPriceWithTax()) ;
  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. private function calculatePriceByUnitRef($price)
  78. {
  79. return ($price * $this->getUnitInherited()->getCoefficient()) / $this->getQuantityInherited() ;
  80. }
  81. }