Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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