369 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 PriceUtilsOld
  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($order);
  145. }
  146. else {
  147. $priceToReturn = $this->getTotalOrderProductsWithReductionCatalog($order);
  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 getTotalOrderProductsWithReduction($entity)
  175. {
  176. return $this->getTotalOrderProductsWithReductionCatalog($entity);
  177. }
  178. public function getTotalOrderProductsWithTaxAndReduction($entity)
  179. {
  180. return $this->getTotalOrderProductsWithTaxAndReductionCatalog($entity);
  181. }
  182. public function getTotalOrderProductsWithReductionCatalog($entity)
  183. {
  184. return $this->getSumOrderProductsDispatch($entity, false, true);
  185. }
  186. public function getTotalOrderProductsWithTaxAndReductionCatalog($entity)
  187. {
  188. return $this->getSumOrderProductsDispatch($entity, true, true);
  189. }
  190. public function getSumOrderProductsDispatch($entity, $withTax = false, $withReductionCatalog = false)
  191. {
  192. if ($entity instanceof OrderShopInterface) {
  193. return $this->getSumOrderProducts($entity->getOrderProducts(), $withTax, $withReductionCatalog);
  194. }
  195. if ($entity instanceof Collection || is_array($entity)) {
  196. return $this->getSumOrderProducts($entity, $withTax, $withReductionCatalog);
  197. }
  198. }
  199. public function getSumOrderProducts($orderProducts, $withTax = false, $withReductionCatalog = false)
  200. {
  201. $total = 0;
  202. foreach ($orderProducts as $orderProduct) {
  203. if ($withTax && $withReductionCatalog) {
  204. $total += $this->getTotalWithTaxAndReduction($orderProduct);
  205. } elseif ($withTax) {
  206. $total += $this->getTotalWithTax($orderProduct);
  207. } elseif ($withReductionCatalog) {
  208. $total += $this->getTotalWithReduction($orderProduct);
  209. } else {
  210. $total += $this->getTotal($orderProduct);
  211. }
  212. }
  213. return $total;
  214. }
  215. public function getPriceWithTaxAndReductionCatalog($entity, $price, $priceWithTax, $reductionCatalog = null)
  216. {
  217. return $this->getPriceWithReductionCatalogGeneric($entity, $price, $priceWithTax, $reductionCatalog, true);
  218. }
  219. public function getPriceWithReductionCatalog($entity, $price, $priceWithTax, $reductionCatalog = null)
  220. {
  221. return $this->getPriceWithReductionCatalogGeneric($entity, $price, $priceWithTax, $reductionCatalog, false);
  222. }
  223. public function getPriceWithReductionCatalogGeneric($entity, $price, $priceWithTax, $reductionCatalog = null, $withTax = true): ?float
  224. {
  225. if ($reductionCatalog) {
  226. $reductionCatalogValue = $reductionCatalog->getValue();
  227. $reductionCatalogUnit = $reductionCatalog->getUnit();
  228. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
  229. } else {
  230. if ($entity instanceof ProductPropertyInterface) {
  231. $reductionCatalog = $entity->getReductionCatalogInherited();
  232. if ($reductionCatalog) {
  233. $reductionCatalogValue = $reductionCatalog->getValue();
  234. $reductionCatalogUnit = $reductionCatalog->getUnit();
  235. $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
  236. }
  237. }
  238. if ($entity instanceof OrderProductInterface) {
  239. $orderProductReductionCatalog = $entity->getOrderProductReductionCatalog();
  240. if ($orderProductReductionCatalog) {
  241. $reductionCatalogValue = $orderProductReductionCatalog->getValue();
  242. $reductionCatalogUnit = $orderProductReductionCatalog->getUnit();
  243. $reductionCatalogBehaviorTaxRate = $orderProductReductionCatalog->getBehaviorTaxRate();
  244. }
  245. }
  246. }
  247. if (isset($reductionCatalogValue) && isset($reductionCatalogUnit) && isset($reductionCatalogBehaviorTaxRate)) {
  248. if ($reductionCatalogUnit == 'percent') {
  249. $priceWithTax = $this->applyReductionPercent(
  250. $priceWithTax,
  251. $reductionCatalogValue
  252. );
  253. } elseif ($reductionCatalogUnit == 'amount') {
  254. if ($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
  255. $priceWithTax = $this->applyTax(
  256. $this->applyReductionAmount(
  257. $price,
  258. $reductionCatalogValue
  259. ),
  260. $entity->getTaxRateInherited()->getValue()
  261. );
  262. } elseif ($reductionCatalogBehaviorTaxRate == 'tax-included') {
  263. $priceWithTax = $this->applyReductionAmount(
  264. $priceWithTax,
  265. $reductionCatalogValue
  266. );
  267. }
  268. }
  269. }
  270. if ($withTax) {
  271. $priceReturn = $priceWithTax;
  272. } else {
  273. $priceReturn = $this->applyPercentNegative($priceWithTax, $entity->getTaxRateInherited()->getValue());
  274. }
  275. return $this->round($priceReturn);
  276. }
  277. public function getTotalTaxes($entity)
  278. {
  279. if ($entity instanceof OrderProductInterface) {
  280. return $this->getTotalWithReduction($entity) * ($entity->getTaxRateInherited()->getValue() / 100);
  281. }
  282. if ($entity instanceof OrderShopInterface) {
  283. $totalTaxes = 0;
  284. foreach ($entity->getOrderProducts() as $orderProduct) {
  285. $totalTaxes += $this->getTotalTaxes($orderProduct);
  286. }
  287. return $totalTaxes;
  288. }
  289. return 0;
  290. }
  291. public function applyTax($price, $taxRateValue)
  292. {
  293. return $this->round($this->applyPercent($price, $taxRateValue));
  294. }
  295. public function applyReductionPercent($price, $percentage)
  296. {
  297. return $this->applyPercent($price, -$percentage);
  298. }
  299. public function applyReductionAmount($price, $amount)
  300. {
  301. return $price - $amount;
  302. }
  303. public function applyPercent($price, $percentage)
  304. {
  305. return $price * ($percentage / 100 + 1);
  306. }
  307. public function applyPercentNegative($price, $percentage)
  308. {
  309. return $price / ($percentage / 100 + 1);
  310. }
  311. public function round($price)
  312. {
  313. return round((($price * 100)) / 100, 2);
  314. }
  315. }