Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PriceUtils.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use Lc\ShopBundle\Context\OrderProductInterface;
  4. use Lc\ShopBundle\Context\ProductFamilyInterface;
  5. use Lc\ShopBundle\Context\ProductInterface;
  6. use Lc\ShopBundle\Context\ProductPropertyInterface;
  7. use Lc\ShopBundle\Context\ReductionCatalogInterface;
  8. class PriceUtils
  9. {
  10. public function getPrice($entity)
  11. {
  12. if($entity instanceof ProductPropertyInterface) {
  13. if($entity->getBehaviorPriceInherited() == 'by-piece') {
  14. return $entity->getPriceInherited() ;
  15. }
  16. elseif($entity->getBehaviorPriceInherited() == 'by-reference-unit') {
  17. if($entity->getQuantityInherited() > 0) {
  18. return $entity->getPriceByRefUnitInherited() * $entity->getQuantityInherited() ;
  19. }
  20. }
  21. }
  22. if($entity instanceof OrderProductInterface) {
  23. return $entity->getPrice() ;
  24. }
  25. return null ;
  26. }
  27. public function getPriceWithTax($entity)
  28. {
  29. return $this->applyTax(
  30. $this->getPrice($entity),
  31. $entity->getTaxRateInherited()->getValue()
  32. ) ;
  33. }
  34. public function getPriceWithTaxAndReduction($entity)
  35. {
  36. return $this->getPriceWithTaxAndReductionCatalog(
  37. $entity,
  38. $this->getPrice($entity),
  39. $this->getPriceWithTax($entity)
  40. );
  41. }
  42. public function getPriceByRefUnit($entity)
  43. {
  44. if($entity instanceof ProductPropertyInterface) {
  45. if($entity->getBehaviorPriceInherited() == 'by-piece') {
  46. return ($this->getPriceInherited($entity) * $entity->getUnitInherited()->getCoefficient()) / $entity->getQuantityInherited() ;
  47. }
  48. elseif($entity->getBehaviorPriceInherited() == 'by-reference-unit') {
  49. return $entity->getPriceByRefUnitInherited() ;
  50. }
  51. }
  52. if($entity instanceof OrderProductInterface) {
  53. return ($this->getPrice($entity) * $entity->getUnitInherited()->getCoefficient()) / $entity->getQuantityProduct() ;
  54. }
  55. return null ;
  56. }
  57. public function getPriceByRefUnitWithTax($entity)
  58. {
  59. return $this->applyTax(
  60. $this->getPriceByRefUnit($entity),
  61. $entity->getTaxRateInherited()->getValue()
  62. ) ;
  63. }
  64. public function getPriceByRefUnitWithTaxAndReduction($entity)
  65. {
  66. return $this->getPriceWithTaxAndReductionCatalog(
  67. $entity,
  68. $this->getPriceByRefUnit($entity),
  69. $this->getPriceByRefUnitWithTax($entity)
  70. );
  71. }
  72. public function getTotal($entity)
  73. {
  74. if($entity instanceof OrderProductInterface) {
  75. return $entity->getQuantityOrder() * $this->getPrice($entity) ;
  76. }
  77. return null ;
  78. }
  79. public function getTotalWithTax($entity)
  80. {
  81. if($entity instanceof OrderProductInterface) {
  82. return $this->applyTax(
  83. $this->getTotal($entity),
  84. $entity->getTaxRateInherited()->getValue()
  85. ) ;
  86. }
  87. return null ;
  88. }
  89. public function getTotalWithTaxAndReduction($entity)
  90. {
  91. if($entity instanceof OrderProductInterface) {
  92. return $this->getPriceWithTaxAndReductionCatalog(
  93. $entity,
  94. $this->getTotal($entity),
  95. $this->getTotalWithTax($entity)
  96. ) ;
  97. }
  98. }
  99. private function getPriceWithTaxAndReductionCatalog($entity, $price, $priceWithTax): ?float
  100. {
  101. if($entity instanceof ProductPropertyInterface) {
  102. $reductionCatalog = $entity->getReductionCatalogInherited() ;
  103. if($reductionCatalog) {
  104. $reductionCatalogValue = $reductionCatalog->getValue() ;
  105. $reductionCatalogUnit = $reductionCatalog->getUnit() ;
  106. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate() ;
  107. }
  108. }
  109. if($entity instanceof OrderProductInterface) {
  110. // OrderProductReductionCatalog
  111. $reductionCatalog = null ;
  112. }
  113. if(isset($reductionCatalogValue) && isset($reductionCatalogUnit) && isset($reductionCatalogBehaviorTaxRate)) {
  114. if ($reductionCatalogUnit == 'percent') {
  115. return $this->applyReductionPercent(
  116. $priceWithTax,
  117. $reductionCatalogValue
  118. );
  119. }
  120. elseif ($reductionCatalogUnit == 'amount') {
  121. if($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
  122. return $this->applyTax(
  123. $this->applyReductionAmount(
  124. $price,
  125. $reductionCatalogValue
  126. ),
  127. $this->getTaxRateInherited()->getValue()
  128. );
  129. }
  130. elseif($reductionCatalogBehaviorTaxRate == 'tax-included') {
  131. return $this->applyReductionAmount(
  132. $priceWithTax,
  133. $reductionCatalogValue
  134. );
  135. }
  136. }
  137. }
  138. return $priceWithTax ;
  139. }
  140. public function applyTax($price, $taxRateValue)
  141. {
  142. return $this->round($this->applyPercent($price, $taxRateValue)) ;
  143. }
  144. public function applyReductionPercent($price, $percentage)
  145. {
  146. return $this->applyPercent($price, -$percentage) ;
  147. }
  148. public function applyReductionAmount($price, $amount)
  149. {
  150. return $price - $amount ;
  151. }
  152. public function applyPercent($price, $percentage)
  153. {
  154. return $price * ($percentage / 100 + 1) ;
  155. }
  156. public function round($price)
  157. {
  158. return round((($price * 100)) / 100, 2);
  159. }
  160. }