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.

189 lines
7.2KB

  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. }