Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

264 lines
11KB

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