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

359 lines
15KB

  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. } elseif ($entity->getBehaviorPriceInherited() == 'by-reference-unit') {
  18. if ($entity->getQuantityInherited() > 0) {
  19. return $entity->getPriceByRefUnitInherited() * ($entity->getQuantityInherited() / $entity->getUnitInherited()->getCoefficient());
  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. } 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. if ($entity instanceof OrderShopInterface) {
  78. $total = 0;
  79. foreach ($entity->getOrderProducts() as $orderProduct) {
  80. $total += $this->getTotal($orderProduct);
  81. }
  82. return $total;
  83. }
  84. return null;
  85. }
  86. public function getTotalWithTax($entity)
  87. {
  88. if ($entity instanceof OrderProductInterface) {
  89. return $this->applyTax(
  90. $this->getTotal($entity),
  91. $entity->getTaxRateInherited()->getValue()
  92. );
  93. }
  94. if ($entity instanceof OrderShopInterface) {
  95. $total = 0;
  96. foreach ($entity->getOrderProducts() as $orderProduct) {
  97. $total += $this->getTotalWithTax($orderProduct);
  98. }
  99. return $total;
  100. }
  101. //C'est bizzare ce truc là
  102. //if($entity instanceof OrderShopInterface) {
  103. // return $this->getTotalOrderProducts($entity->getOrderProducts(), true) ;
  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->getTotalOrderProductsWithTaxAndReduction($entity->getOrderProducts(), true, true);
  118. }
  119. }
  120. public function getTotalWithReduction($entity)
  121. {
  122. if ($entity instanceof OrderProductInterface) {
  123. return $this->getPriceWithReductionCatalog(
  124. $entity,
  125. $this->getTotal($entity),
  126. $this->getTotalWithTax($entity)
  127. );
  128. }
  129. if ($entity instanceof OrderShopInterface) {
  130. return $this->getTotalOrderProductsWithReduction($entity->getOrderProducts(), true, true);
  131. }
  132. }
  133. public function getTotalOrderProductsWithReductionCart(OrderShopInterface $order)
  134. {
  135. $this->_getTotalOrderProductsWithReductionCartGeneric($order, false);
  136. }
  137. public function getTotalOrderProductsWithTaxAndReductionCart(OrderShopInterface $order)
  138. {
  139. $this->_getTotalOrderProductsWithReductionCartGeneric($order, true);
  140. }
  141. private function _getTotalOrderProductsWithReductionCartGeneric(OrderShopInterface $order, $withTax = true)
  142. {
  143. if ($withTax) {
  144. $priceToReturn = $this->getTotalOrderProductsWithTaxAndReductionCatalog();
  145. }
  146. else {
  147. $priceToReturn = $this->getTotalOrderProductsWithReductionCatalog();
  148. }
  149. foreach ($order->getOrderReductionCarts() as $orderReductionCart) {
  150. if ($orderReductionCart->getUnit() == 'percent') {
  151. $priceToReturn = $this->applyReductionPercent(
  152. $priceToReturn,
  153. $orderReductionCart->getValue
  154. );
  155. } else if ($orderReductionCart->getUnit() == 'amount') {
  156. if ($orderReductionCart->getBehaviorTaxRate() == 'tax-inlcluded') {
  157. $priceToReturn =
  158. $this->applyReductionAmount(
  159. $priceToReturn,
  160. $orderReductionCart->getValue()
  161. );
  162. }
  163. }
  164. }
  165. }
  166. public function getTotalOrderProducts($entity)
  167. {
  168. return $this->getSumOrderProductsDispatch($entity);
  169. }
  170. public function getTotalOrderProductsWithTax($entity)
  171. {
  172. return $this->getSumOrderProductsDispatch($entity, true);
  173. }
  174. public function getTotalOrderProductsWithReductionCatalog($entity)
  175. {
  176. return $this->getSumOrderProductsDispatch($entity, false, true);
  177. }
  178. public function getTotalOrderProductsWithTaxAndReductionCatalog($entity)
  179. {
  180. return $this->getSumOrderProductsDispatch($entity, true, true);
  181. }
  182. public function getSumOrderProductsDispatch($entity, $withTax = false, $withReductionCatalog = false)
  183. {
  184. if ($entity instanceof OrderShopInterface) {
  185. return $this->getSumOrderProducts($entity->getOrderProducts(), $withTax, $withReductionCatalog);
  186. }
  187. if ($entity instanceof Collection || is_array($entity)) {
  188. return $this->getSumOrderProducts($entity, $withTax, $withReductionCatalog);
  189. }
  190. }
  191. public function getSumOrderProducts($orderProducts, $withTax = false, $withReductionCatalog = false)
  192. {
  193. $total = 0;
  194. foreach ($orderProducts as $orderProduct) {
  195. if ($withTax && $withReductionCatalog) {
  196. $total += $this->getTotalWithTaxAndReduction($orderProduct);
  197. } elseif ($withTax) {
  198. $total += $this->getTotalWithTax($orderProduct);
  199. } elseif ($withReductionCatalog) {
  200. $total += $this->getTotalWithReduction($orderProduct);
  201. } else {
  202. $total += $this->getTotal($orderProduct);
  203. }
  204. }
  205. return $total;
  206. }
  207. public function getPriceWithTaxAndReductionCatalog($entity, $price, $priceWithTax, $reductionCatalog = null)
  208. {
  209. return $this->getPriceWithReductionCatalogGeneric($entity, $price, $priceWithTax, $reductionCatalog, true);
  210. }
  211. public function getPriceWithReductionCatalog($entity, $price, $priceWithTax, $reductionCatalog = null)
  212. {
  213. return $this->getPriceWithReductionCatalogGeneric($entity, $price, $priceWithTax, $reductionCatalog, false);
  214. }
  215. public function getPriceWithReductionCatalogGeneric($entity, $price, $priceWithTax, $reductionCatalog = null, $withTax = true): ?float
  216. {
  217. if ($reductionCatalog) {
  218. $reductionCatalogValue = $reductionCatalog->getValue();
  219. $reductionCatalogUnit = $reductionCatalog->getUnit();
  220. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
  221. } else {
  222. if ($entity instanceof ProductPropertyInterface) {
  223. $reductionCatalog = $entity->getReductionCatalogInherited();
  224. if ($reductionCatalog) {
  225. $reductionCatalogValue = $reductionCatalog->getValue();
  226. $reductionCatalogUnit = $reductionCatalog->getUnit();
  227. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
  228. }
  229. }
  230. if ($entity instanceof OrderProductInterface) {
  231. $orderProductReductionCatalog = $entity->getOrderProductReductionCatalog();
  232. if ($orderProductReductionCatalog) {
  233. $reductionCatalogValue = $orderProductReductionCatalog->getValue();
  234. $reductionCatalogUnit = $orderProductReductionCatalog->getUnit();
  235. $reductionCatalogBehaviorTaxRate = $orderProductReductionCatalog->getBehaviorTaxRate();
  236. }
  237. }
  238. }
  239. if (isset($reductionCatalogValue) && isset($reductionCatalogUnit) && isset($reductionCatalogBehaviorTaxRate)) {
  240. if ($reductionCatalogUnit == 'percent') {
  241. $priceWithTax = $this->applyReductionPercent(
  242. $priceWithTax,
  243. $reductionCatalogValue
  244. );
  245. } elseif ($reductionCatalogUnit == 'amount') {
  246. if ($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
  247. $priceWithTax = $this->applyTax(
  248. $this->applyReductionAmount(
  249. $price,
  250. $reductionCatalogValue
  251. ),
  252. $entity->getTaxRateInherited()->getValue()
  253. );
  254. } elseif ($reductionCatalogBehaviorTaxRate == 'tax-included') {
  255. $priceWithTax = $this->applyReductionAmount(
  256. $priceWithTax,
  257. $reductionCatalogValue
  258. );
  259. }
  260. }
  261. }
  262. if ($withTax) {
  263. $priceReturn = $priceWithTax;
  264. } else {
  265. $priceReturn = $this->applyPercentNegative($priceWithTax, $entity->getTaxRateInherited()->getValue());
  266. }
  267. return $this->round($priceReturn);
  268. }
  269. public function getTotalTaxes($entity)
  270. {
  271. if ($entity instanceof OrderProductInterface) {
  272. return $this->getTotalWithReduction($entity) * ($entity->getTaxRateInherited()->getValue() / 100);
  273. }
  274. if ($entity instanceof OrderShopInterface) {
  275. $totalTaxes = 0;
  276. foreach ($entity->getOrderProducts() as $orderProduct) {
  277. $totalTaxes += $this->getTotalTaxes($orderProduct);
  278. }
  279. return $totalTaxes;
  280. }
  281. return 0;
  282. }
  283. public function applyTax($price, $taxRateValue)
  284. {
  285. return $this->round($this->applyPercent($price, $taxRateValue));
  286. }
  287. public function applyReductionPercent($price, $percentage)
  288. {
  289. return $this->applyPercent($price, -$percentage);
  290. }
  291. public function applyReductionAmount($price, $amount)
  292. {
  293. return $price - $amount;
  294. }
  295. public function applyPercent($price, $percentage)
  296. {
  297. return $price * ($percentage / 100 + 1);
  298. }
  299. public function applyPercentNegative($price, $percentage)
  300. {
  301. return $price / ($percentage / 100 + 1);
  302. }
  303. public function round($price)
  304. {
  305. return round((($price * 100)) / 100, 2);
  306. }
  307. }