PriceUtils.php 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use Lc\ShopBundle\Context\OrderProductInterface;
  4. use Lc\ShopBundle\Context\OrderShopInterface;
  5. use Lc\ShopBundle\Context\ProductFamilyInterface;
  6. use Lc\ShopBundle\Context\ProductInterface;
  7. use Lc\ShopBundle\Context\ProductPropertyInterface;
  8. use Lc\ShopBundle\Context\ReductionCatalogInterface;
  9. class PriceUtils
  10. {
  11. public function getPrice($entity)
  12. {
  13. if($entity instanceof ProductPropertyInterface) {
  14. if($entity->getBehaviorPriceInherited() == 'by-piece') {
  15. return $entity->getPriceInherited() ;
  16. }
  17. elseif($entity->getBehaviorPriceInherited() == 'by-reference-unit') {
  18. if($entity->getQuantityInherited() > 0) {
  19. return $entity->getPriceByRefUnitInherited() * $entity->getQuantityInherited() ;
  20. }
  21. }
  22. }
  23. if($entity instanceof OrderProductInterface) {
  24. return $entity->getPrice() ;
  25. }
  26. return null ;
  27. }
  28. public function getPriceWithTax($entity)
  29. {
  30. return $this->applyTax(
  31. $this->getPrice($entity),
  32. $entity->getTaxRateInherited()->getValue()
  33. ) ;
  34. }
  35. public function getPriceWithTaxAndReduction($entity)
  36. {
  37. return $this->getPriceWithTaxAndReductionCatalog(
  38. $entity,
  39. $this->getPrice($entity),
  40. $this->getPriceWithTax($entity)
  41. );
  42. }
  43. public function getPriceByRefUnit($entity)
  44. {
  45. if($entity instanceof ProductPropertyInterface) {
  46. if($entity->getBehaviorPriceInherited() == 'by-piece') {
  47. return ($this->getPrice($entity) * $entity->getUnitInherited()->getCoefficient()) / $entity->getQuantityInherited() ;
  48. }
  49. elseif($entity->getBehaviorPriceInherited() == 'by-reference-unit') {
  50. return $entity->getPriceByRefUnitInherited() ;
  51. }
  52. }
  53. if($entity instanceof OrderProductInterface) {
  54. return ($this->getPrice($entity) * $entity->getUnitInherited()->getCoefficient()) / $entity->getQuantityProduct() ;
  55. }
  56. return null ;
  57. }
  58. public function getPriceByRefUnitWithTax($entity)
  59. {
  60. return $this->applyTax(
  61. $this->getPriceByRefUnit($entity),
  62. $entity->getTaxRateInherited()->getValue()
  63. ) ;
  64. }
  65. public function getPriceByRefUnitWithTaxAndReduction($entity)
  66. {
  67. return $this->getPriceWithTaxAndReductionCatalog(
  68. $entity,
  69. $this->getPriceByRefUnit($entity),
  70. $this->getPriceByRefUnitWithTax($entity)
  71. );
  72. }
  73. public function getTotal($entity)
  74. {
  75. if($entity instanceof OrderProductInterface) {
  76. return $entity->getQuantityOrder() * $this->getPrice($entity) ;
  77. }
  78. if($entity instanceof OrderShopInterface) {
  79. $total = 0 ;
  80. foreach($entity->getOrderProducts() as $orderProduct) {
  81. $total += $this->getTotal($orderProduct) ;
  82. }
  83. return $total ;
  84. }
  85. return null ;
  86. }
  87. public function getTotalWithTax($entity)
  88. {
  89. if($entity instanceof OrderProductInterface) {
  90. return $this->applyTax(
  91. $this->getTotal($entity),
  92. $entity->getTaxRateInherited()->getValue()
  93. ) ;
  94. }
  95. if($entity instanceof OrderShopInterface) {
  96. $total = 0 ;
  97. foreach($entity->getOrderProducts() as $orderProduct) {
  98. $total += $this->getTotalWithTax($orderProduct) ;
  99. }
  100. return $total ;
  101. }
  102. if($entity instanceof OrderShopInterface) {
  103. return $this->getTotalWithTaxByOrderProducts($entity->getOrderProducts()) ;
  104. }
  105. return null ;
  106. }
  107. public function getTotalWithTaxAndReduction($entity)
  108. {
  109. if($entity instanceof OrderProductInterface) {
  110. return $this->getPriceWithTaxAndReductionCatalog(
  111. $entity,
  112. $this->getTotal($entity),
  113. $this->getTotalWithTax($entity)
  114. ) ;
  115. }
  116. if($entity instanceof OrderShopInterface) {
  117. return $this->getTotalWithTaxAndReductionByOrderProducts($entity->getOrderProducts()) ;
  118. }
  119. }
  120. public function getTotalWithTaxByOrderProducts($orderProducts)
  121. {
  122. $totalWithTax = 0;
  123. foreach ($orderProducts as $orderProduct) {
  124. $totalWithTax += $this->getTotalWithTax($orderProduct);
  125. }
  126. return $totalWithTax;
  127. }
  128. public function getTotalWithTaxAndReductionByOrderProducts($orderProducts)
  129. {
  130. $totalWithTax = 0;
  131. foreach ($orderProducts as $orderProduct) {
  132. $totalWithTax += $this->getTotalWithTaxAndReduction($orderProduct);
  133. }
  134. return $totalWithTax;
  135. }
  136. public function getPriceWithTaxAndReductionCatalog($entity, $price, $priceWithTax, $reductionCatalog = null): ?float
  137. {
  138. if($reductionCatalog) {
  139. $reductionCatalogValue = $reductionCatalog->getValue() ;
  140. $reductionCatalogUnit = $reductionCatalog->getUnit() ;
  141. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate() ;
  142. }
  143. else {
  144. if($entity instanceof ProductPropertyInterface) {
  145. $reductionCatalog = $entity->getReductionCatalogInherited() ;
  146. if($reductionCatalog) {
  147. $reductionCatalogValue = $reductionCatalog->getValue() ;
  148. $reductionCatalogUnit = $reductionCatalog->getUnit() ;
  149. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate() ;
  150. }
  151. }
  152. if($entity instanceof OrderProductInterface) {
  153. $orderProductReductionCatalog = $entity->getOrderProductReductionCatalog() ;
  154. if($orderProductReductionCatalog) {
  155. $reductionCatalogValue = $orderProductReductionCatalog->getValue() ;
  156. $reductionCatalogUnit = $orderProductReductionCatalog->getUnit() ;
  157. $reductionCatalogBehaviorTaxRate = $orderProductReductionCatalog->getBehaviorTaxRate() ;
  158. }
  159. }
  160. }
  161. if(isset($reductionCatalogValue) && isset($reductionCatalogUnit) && isset($reductionCatalogBehaviorTaxRate)) {
  162. if ($reductionCatalogUnit == 'percent') {
  163. return $this->applyReductionPercent(
  164. $priceWithTax,
  165. $reductionCatalogValue
  166. );
  167. }
  168. elseif ($reductionCatalogUnit == 'amount') {
  169. if($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
  170. return $this->applyTax(
  171. $this->applyReductionAmount(
  172. $price,
  173. $reductionCatalogValue
  174. ),
  175. $this->getTaxRateInherited()->getValue()
  176. );
  177. }
  178. elseif($reductionCatalogBehaviorTaxRate == 'tax-included') {
  179. return $this->applyReductionAmount(
  180. $priceWithTax,
  181. $reductionCatalogValue
  182. );
  183. }
  184. }
  185. }
  186. return $priceWithTax ;
  187. }
  188. public function applyTax($price, $taxRateValue)
  189. {
  190. return $this->round($this->applyPercent($price, $taxRateValue)) ;
  191. }
  192. public function applyReductionPercent($price, $percentage)
  193. {
  194. return $this->applyPercent($price, -$percentage) ;
  195. }
  196. public function applyReductionAmount($price, $amount)
  197. {
  198. return $price - $amount ;
  199. }
  200. public function applyPercent($price, $percentage)
  201. {
  202. return $price * ($percentage / 100 + 1) ;
  203. }
  204. public function round($price)
  205. {
  206. return round((($price * 100)) / 100, 2);
  207. }
  208. }