Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

266 rindas
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. //C'est bizzare ce truc là
  104. //if($entity instanceof OrderShopInterface) {
  105. // return $this->getTotalOrderProducts($entity->getOrderProducts(), true) ;
  106. //}
  107. return null ;
  108. }
  109. public function getTotalWithTaxAndReduction($entity)
  110. {
  111. if($entity instanceof OrderProductInterface) {
  112. return $this->getPriceWithTaxAndReductionCatalog(
  113. $entity,
  114. $this->getTotal($entity),
  115. $this->getTotalWithTax($entity)
  116. ) ;
  117. }
  118. if($entity instanceof OrderShopInterface) {
  119. return $this->getTotalOrderProductsWithTaxAndReduction($entity->getOrderProducts(), true, true) ;
  120. }
  121. }
  122. public function getTotalOrderProducts($entity)
  123. {
  124. return $this->getSumOrderProductsDispatch($entity) ;
  125. }
  126. public function getTotalOrderProductsWithTax($entity)
  127. {
  128. return $this->getSumOrderProductsDispatch($entity, true) ;
  129. }
  130. public function getTotalOrderProductsWithTaxAndReduction($entity)
  131. {
  132. return $this->getSumOrderProductsDispatch($entity, true, true) ;
  133. }
  134. public function getSumOrderProductsDispatch($entity, $withTax = false, $withReduction = false)
  135. {
  136. if($entity instanceof OrderShopInterface) {
  137. return $this->getSumOrderProducts($entity->getOrderProducts(), $withTax, $withReduction) ;
  138. }
  139. if($entity instanceof Collection || is_array($entity)) {
  140. return $this->getSumOrderProducts($entity, $withTax, $withReduction) ;
  141. }
  142. }
  143. public function getSumOrderProducts($orderProducts, $withTax = false, $withReduction = false)
  144. {
  145. $total = 0 ;
  146. foreach($orderProducts as $orderProduct) {
  147. if($withTax && $withReduction) {
  148. $total += $this->getTotalWithTaxAndReduction($orderProduct) ;
  149. }
  150. elseif($withTax) {
  151. $total += $this->getTotalWithTax($orderProduct) ;
  152. }
  153. else {
  154. $total += $this->getTotal($orderProduct) ;
  155. }
  156. }
  157. return $total ;
  158. }
  159. public function getPriceWithTaxAndReductionCatalog($entity, $price, $priceWithTax, $reductionCatalog = null): ?float
  160. {
  161. if($reductionCatalog) {
  162. $reductionCatalogValue = $reductionCatalog->getValue() ;
  163. $reductionCatalogUnit = $reductionCatalog->getUnit() ;
  164. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate() ;
  165. }
  166. else {
  167. if($entity instanceof ProductPropertyInterface) {
  168. $reductionCatalog = $entity->getReductionCatalogInherited() ;
  169. if($reductionCatalog) {
  170. $reductionCatalogValue = $reductionCatalog->getValue() ;
  171. $reductionCatalogUnit = $reductionCatalog->getUnit() ;
  172. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate() ;
  173. }
  174. }
  175. if($entity instanceof OrderProductInterface) {
  176. $orderProductReductionCatalog = $entity->getOrderProductReductionCatalog() ;
  177. if($orderProductReductionCatalog) {
  178. $reductionCatalogValue = $orderProductReductionCatalog->getValue() ;
  179. $reductionCatalogUnit = $orderProductReductionCatalog->getUnit() ;
  180. $reductionCatalogBehaviorTaxRate = $orderProductReductionCatalog->getBehaviorTaxRate() ;
  181. }
  182. }
  183. }
  184. if(isset($reductionCatalogValue) && isset($reductionCatalogUnit) && isset($reductionCatalogBehaviorTaxRate)) {
  185. if ($reductionCatalogUnit == 'percent') {
  186. $priceWithTax = $this->applyReductionPercent(
  187. $priceWithTax,
  188. $reductionCatalogValue
  189. );
  190. }
  191. elseif ($reductionCatalogUnit == 'amount') {
  192. if($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
  193. $priceWithTax = $this->applyTax(
  194. $this->applyReductionAmount(
  195. $price,
  196. $reductionCatalogValue
  197. ),
  198. $entity->getTaxRateInherited()->getValue()
  199. );
  200. }
  201. elseif($reductionCatalogBehaviorTaxRate == 'tax-included') {
  202. $priceWithTax = $this->applyReductionAmount(
  203. $priceWithTax,
  204. $reductionCatalogValue
  205. );
  206. }
  207. }
  208. }
  209. return $this->round($priceWithTax) ;
  210. }
  211. public function applyTax($price, $taxRateValue)
  212. {
  213. return $this->round($this->applyPercent($price, $taxRateValue)) ;
  214. }
  215. public function applyReductionPercent($price, $percentage)
  216. {
  217. return $this->applyPercent($price, -$percentage) ;
  218. }
  219. public function applyReductionAmount($price, $amount)
  220. {
  221. return $price - $amount ;
  222. }
  223. public function applyPercent($price, $percentage)
  224. {
  225. return $price * ($percentage / 100 + 1) ;
  226. }
  227. public function round($price)
  228. {
  229. return round((($price * 100)) / 100, 2);
  230. }
  231. }