Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

179 lines
6.3KB

  1. <?php
  2. namespace Lc\ShopBundle\Model ;
  3. use Lc\ShopBundle\Context\ReductionCatalogInterface;
  4. use Lc\ShopBundle\Context\UnitInterface;
  5. trait PriceTrait
  6. {
  7. /**
  8. * @ORM\Column(type="float", nullable=true)
  9. */
  10. protected $price;
  11. /**
  12. * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\UnitInterface")
  13. * @ORM\JoinColumn(nullable=true)
  14. */
  15. protected $unit;
  16. /**
  17. * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\TaxRateInterface")
  18. * @ORM\JoinColumn(nullable=true)
  19. */
  20. protected $taxRate;
  21. public function getPrice(): ?float
  22. {
  23. return $this->price;
  24. }
  25. public function getPriceInherited(): ?float
  26. {
  27. return $this->getPrice() ;
  28. }
  29. public function getUnitInherited(): ?Unit
  30. {
  31. return $this->getUnit() ;
  32. }
  33. public function getTaxRateInherited(): ?TaxRate
  34. {
  35. return $this->getTaxRate() ;
  36. }
  37. public function getPriceWithTax(): ?float
  38. {
  39. return $this->calculatePriceWithTax($this->getPriceInherited(), $this->getTaxRateInherited()->getValue()) ;
  40. }
  41. public function getPriceByUnitRef(): ?float
  42. {
  43. return $this->calculatePriceByUnitRef($this->getPriceInherited()) ;
  44. }
  45. public function getPriceByUnitRefWithTax(): ?float
  46. {
  47. return $this->calculatePriceByUnitRef($this->getPriceWithTax()) ;
  48. }
  49. public function getTheReductionCatalog()
  50. {
  51. $reductionCatalog = false;
  52. if($this instanceof ProductFamily) {
  53. $reductionCatalog = $this->getReductionCatalog() ;
  54. }
  55. if($this instanceof Product) {
  56. $reductionCatalog = $this->getProductFamily()->getReductionCatalog() ;
  57. }
  58. return $reductionCatalog ;
  59. }
  60. public function getPriceByUnitRefWithTaxAndReduction(): ?float
  61. {
  62. $reductionCatalog = $this->getTheReductionCatalog() ;
  63. if (isset($reductionCatalog) && $reductionCatalog) {
  64. return $this->calculatePriceByUnitRef($this->getPriceWithTaxAndReductionCatalog($reductionCatalog)) ;
  65. }
  66. else {
  67. return $this->calculatePriceByUnitRef($this->getPriceWithTax());
  68. }
  69. }
  70. public function getPriceWithTaxAndReduction(): ?float
  71. {
  72. $reductionCatalog = $this->getTheReductionCatalog() ;
  73. if (isset($reductionCatalog) && $reductionCatalog) {
  74. return $this->getPriceWithTaxAndReductionCatalog($reductionCatalog);
  75. }
  76. else {
  77. return $this->getPriceWithTax();
  78. }
  79. }
  80. public function getPriceWithTaxAndReductionCatalog(ReductionCatalogInterface $reductionCatalog): ?float
  81. {
  82. if ($reductionCatalog->getUnit() == 'percent') {
  83. //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ù ;)
  84. return $this->calculatePriceWithReductionPercent($this->getPriceWithTax(), $reductionCatalog->getValue());
  85. /*if ($reductionCatalog->getBehaviorTaxRate() == 'tax-excluded') {
  86. $priceReductionHT = $this->calculatePriceWithReductionPercent($this->getPriceInherited(), $reductionCatalog->getValue());
  87. return $this->calculatePriceWithTax($priceReductionHT, $this->getTaxRateInherited()->getValue());
  88. } else if ($reductionCatalog->getBehaviorTaxRate() == 'tax-included') {
  89. return $this->calculatePriceWithReductionPercent($this->getPriceWithTax(), $reductionCatalog->getValue());
  90. }*/
  91. }elseif ($reductionCatalog->getUnit() == 'amount') {
  92. if ($reductionCatalog->getBehaviorTaxRate() == 'tax-excluded') {
  93. $priceReductionHT = $this->calculatePriceWithReductionAmount($this->getPriceInherited(), $reductionCatalog->getValue());
  94. return $this->calculatePriceWithTax($priceReductionHT, $this->getTaxRateInherited()->getValue());
  95. }else if ($reductionCatalog->getBehaviorTaxRate() == 'tax-included') {
  96. return $this->calculatePriceWithReductionAmount($this->getPriceWithTax(), $reductionCatalog->getValue());
  97. }
  98. }
  99. }
  100. public function setPrice(float $price): self
  101. {
  102. $this->price = $price;
  103. return $this;
  104. }
  105. public function getUnit(): ?Unit
  106. {
  107. return $this->unit;
  108. }
  109. public function setUnit(Unit $unit): self
  110. {
  111. $this->unit = $unit;
  112. return $this;
  113. }
  114. public function getTaxRate(): ?TaxRate
  115. {
  116. return $this->taxRate;
  117. }
  118. public function setTaxRate(?TaxRate $taxRate): self
  119. {
  120. $this->taxRate = $taxRate;
  121. return $this;
  122. }
  123. private function calculatePriceWithTax($priceWithoutTax, $taxRateValue): ?float
  124. {
  125. $price = floatval($priceWithoutTax) * ($taxRateValue / 100 + 1);
  126. $price = round((($price * 100)) / 100, 2);
  127. return $price;
  128. }
  129. public function calculatePriceWithReductionPercent($price, $percentValue): ?float
  130. {
  131. $price = floatval($price) * (1 - $percentValue / 100);
  132. $price = round((($price * 100)) / 100, 2);
  133. return $price;
  134. }
  135. public function calculatePriceWithReductionAmount($price, $amountValue): ?float
  136. {
  137. $price = floatval($price) - $amountValue;
  138. $price = round((($price * 100)) / 100, 2);
  139. return $price;
  140. }
  141. private function calculatePriceByUnitRef($price)
  142. {
  143. return ($price * $this->getUnitInherited()->getCoefficient()) / $this->getQuantityInherited() ;
  144. }
  145. }