150 line
5.2KB

  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 getPriceWithTaxAndReduction(): ?float
  49. {
  50. if ($this->reductionCatalog) {
  51. return $this->getPriceWithTaxAndReductionCatalog($this->reductionCatalog);
  52. } else {
  53. return null;
  54. }
  55. }
  56. public function getPriceWithTaxAndReductionCatalog(ReductionInterface $reductionCatalog): ?float
  57. {
  58. if ($reductionCatalog->getUnit() == 'percent') {
  59. //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ù ;)
  60. return $this->calculatePriceWithReductionPercent($this->getPriceWithTax(), $reductionCatalog->getValue());
  61. /*if ($reductionCatalog->getBehaviorTaxRate() == 'tax-excluded') {
  62. $priceReductionHT = $this->calculatePriceWithReductionPercent($this->getPriceInherited(), $reductionCatalog->getValue());
  63. return $this->calculatePriceWithTax($priceReductionHT, $this->getTaxRateInherited()->getValue());
  64. } else if ($reductionCatalog->getBehaviorTaxRate() == 'tax-included') {
  65. return $this->calculatePriceWithReductionPercent($this->getPriceWithTax(), $reductionCatalog->getValue());
  66. }*/
  67. }elseif ($reductionCatalog->getUnit() == 'amount') {
  68. if ($reductionCatalog->getBehaviorTaxRate() == 'tax-excluded') {
  69. $priceReductionHT = $this->calculatePriceWithReductionAmount($this->getPriceInherited(), $reductionCatalog->getValue());
  70. return $this->calculatePriceWithTax($priceReductionHT, $this->getTaxRateInherited()->getValue());
  71. }else if ($reductionCatalog->getBehaviorTaxRate() == 'tax-included') {
  72. return $this->calculatePriceWithReductionAmount($this->getPriceWithTax(), $reductionCatalog->getValue());
  73. }
  74. }
  75. }
  76. public function setPrice(float $price): self
  77. {
  78. $this->price = $price;
  79. return $this;
  80. }
  81. public function getUnit(): ?Unit
  82. {
  83. return $this->unit;
  84. }
  85. public function setUnit(Unit $unit): self
  86. {
  87. $this->unit = $unit;
  88. return $this;
  89. }
  90. public function getTaxRate(): ?TaxRate
  91. {
  92. return $this->taxRate;
  93. }
  94. public function setTaxRate(?TaxRate $taxRate): self
  95. {
  96. $this->taxRate = $taxRate;
  97. return $this;
  98. }
  99. private function calculatePriceWithTax($priceWithoutTax, $taxRateValue): ?float
  100. {
  101. $price = floatval($priceWithoutTax) * ($taxRateValue / 100 + 1);
  102. $price = round((($price * 100)) / 100, 2);
  103. return $price;
  104. }
  105. public function calculatePriceWithReductionPercent($price, $percentValue): ?float
  106. {
  107. $price = floatval($price) * (1 - $percentValue / 100);
  108. $price = round((($price * 100)) / 100, 2);
  109. return $price;
  110. }
  111. public function calculatePriceWithReductionAmount($price, $amountValue): ?float
  112. {
  113. $price = floatval($price) - $amountValue;
  114. $price = round((($price * 100)) / 100, 2);
  115. return $price;
  116. }
  117. private function calculatePriceByUnitRef($price)
  118. {
  119. return ($price * $this->getUnitInherited()->getCoefficient()) / $this->getQuantityInherited() ;
  120. }
  121. }