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.

PriceUtils.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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->getPrice($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. public function getPriceWithTaxAndReductionCatalog($entity, $price, $priceWithTax, $reductionCatalog = null): ?float
  100. {
  101. if($reductionCatalog) {
  102. $reductionCatalogValue = $reductionCatalog->getValue() ;
  103. $reductionCatalogUnit = $reductionCatalog->getUnit() ;
  104. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate() ;
  105. }
  106. else {
  107. if($entity instanceof ProductPropertyInterface) {
  108. $reductionCatalog = $entity->getReductionCatalogInherited() ;
  109. if($reductionCatalog) {
  110. $reductionCatalogValue = $reductionCatalog->getValue() ;
  111. $reductionCatalogUnit = $reductionCatalog->getUnit() ;
  112. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate() ;
  113. }
  114. }
  115. if($entity instanceof OrderProductInterface) {
  116. // OrderProductReductionCatalog
  117. $reductionCatalog = null ;
  118. }
  119. }
  120. if(isset($reductionCatalogValue) && isset($reductionCatalogUnit) && isset($reductionCatalogBehaviorTaxRate)) {
  121. if ($reductionCatalogUnit == 'percent') {
  122. return $this->applyReductionPercent(
  123. $priceWithTax,
  124. $reductionCatalogValue
  125. );
  126. }
  127. elseif ($reductionCatalogUnit == 'amount') {
  128. if($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
  129. return $this->applyTax(
  130. $this->applyReductionAmount(
  131. $price,
  132. $reductionCatalogValue
  133. ),
  134. $this->getTaxRateInherited()->getValue()
  135. );
  136. }
  137. elseif($reductionCatalogBehaviorTaxRate == 'tax-included') {
  138. return $this->applyReductionAmount(
  139. $priceWithTax,
  140. $reductionCatalogValue
  141. );
  142. }
  143. }
  144. }
  145. return $priceWithTax ;
  146. }
  147. public function applyTax($price, $taxRateValue)
  148. {
  149. return $this->round($this->applyPercent($price, $taxRateValue)) ;
  150. }
  151. public function applyReductionPercent($price, $percentage)
  152. {
  153. return $this->applyPercent($price, -$percentage) ;
  154. }
  155. public function applyReductionAmount($price, $amount)
  156. {
  157. return $price - $amount ;
  158. }
  159. public function applyPercent($price, $percentage)
  160. {
  161. return $price * ($percentage / 100 + 1) ;
  162. }
  163. public function round($price)
  164. {
  165. return round((($price * 100)) / 100, 2);
  166. }
  167. }