You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
10KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Solver\Order;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Model\File\DocumentInterface;
  5. use Lc\CaracoleBundle\Model\File\DocumentModel;
  6. use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
  7. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  8. use Lc\CaracoleBundle\Model\Order\OrderShopModel;
  9. use Lc\CaracoleBundle\Model\Order\OrderStatusInterface;
  10. use Lc\CaracoleBundle\Model\Order\OrderStatusModel;
  11. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  12. use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
  13. use Lc\CaracoleBundle\Model\Product\ProductInterface;
  14. use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
  15. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  16. class OrderShopSolver
  17. {
  18. protected PriceSolver $priceSolver;
  19. protected EntityManagerInterface $entityManager;
  20. public function __construct(PriceSolver $priceSolver, EntityManagerInterface $entityManager)
  21. {
  22. $this->priceSolver = $priceSolver;
  23. $this->entityManager = $entityManager;
  24. }
  25. public function countQuantities(OrderShopInterface $orderShop): int
  26. {
  27. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  28. }
  29. public function countQuantitiesByOrderProducts($orderProducts = []): int
  30. {
  31. $count = 0;
  32. foreach ($orderProducts as $orderProduct) {
  33. $count += $orderProduct->getQuantityOrder();
  34. }
  35. return $count;
  36. }
  37. public function getOrderProductsByParentCategory(OrderShopInterface $orderShop): array
  38. {
  39. $categoriesArray = [];
  40. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  41. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  42. $category = $productCategories[0]->getParentCategory();
  43. $labelCategory = $category->getTitle();
  44. if (!isset($categoriesArray[$labelCategory])) {
  45. $categoriesArray[$labelCategory] = [];
  46. }
  47. $categoriesArray[$labelCategory][] = $orderProduct;
  48. }
  49. return $categoriesArray;
  50. }
  51. // getOrderProductsByProductFamily
  52. public function getOrderProductsByProductFamily(
  53. OrderShopInterface $orderShop,
  54. ProductFamilyInterface $productFamily
  55. ): array {
  56. $arrayOrderProducts = [];
  57. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  58. if ($orderProduct->getProduct()->getProductFamily() == $productFamily) {
  59. $arrayOrderProducts[] = $orderProduct;
  60. }
  61. }
  62. return $arrayOrderProducts;
  63. }
  64. public function getQuantityOrderByProduct(
  65. OrderShopInterface $orderShop,
  66. ProductInterface $product,
  67. $byWeight = false
  68. ): int {
  69. $quantity = 0;
  70. $productFamily = $product->getProductFamily();
  71. $behaviorCountStock = $productFamily->getBehaviorCountStock();
  72. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  73. if ($orderProduct->getProduct()->getId() == $product->getId()
  74. || (($behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
  75. && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
  76. if ($byWeight) {
  77. $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct(
  78. ) / $orderProduct->getProduct()->getUnitInherited()->getCoefficient());
  79. } else {
  80. $quantity += $orderProduct->getQuantityOrder();
  81. }
  82. }
  83. }
  84. return $quantity;
  85. }
  86. public function getTotalOrderPayments(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): float
  87. {
  88. $totalAmount = floatval(0);
  89. foreach ($orderShop->getOrderPayments() as $orderPayment) {
  90. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  91. }
  92. if ($mergeComplementaryOrderShop) {
  93. foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
  94. foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
  95. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  96. }
  97. }
  98. }
  99. return $totalAmount;
  100. }
  101. public function getTotalRemainingToBePaid(OrderShopInterface $orderShop): float
  102. {
  103. return $this->priceSolver->getTotalWithTax($orderShop) - $this->getTotalOrderPayments($orderShop);
  104. }
  105. public function getOrderStatusHistory(OrderShopInterface $orderShop, OrderStatusInterface $status)
  106. {
  107. $orderStatusHistories = $orderShop->getOrderStatusHistories();
  108. if (count($orderStatusHistories) > 0) {
  109. foreach ($orderStatusHistories as $orderStatusHistory) {
  110. if ($orderStatusHistory->getOrderStatus() === $status) {
  111. return $orderStatusHistory;
  112. }
  113. }
  114. }
  115. return null;
  116. }
  117. public function getDocumentInvoice(OrderShopInterface $orderShop): ?DocumentInterface
  118. {
  119. foreach ($orderShop->getDocuments() as $document) {
  120. if ($document->getType() == DocumentModel::TYPE_INVOICE) {
  121. return $document;
  122. }
  123. }
  124. return null;
  125. }
  126. public function isDeliveryHome(OrderShopInterface $orderShop): bool
  127. {
  128. return $orderShop->getDeliveryType() == OrderShopModel::DELIVERY_TYPE_HOME;
  129. }
  130. public function isDeliveryPointSale(OrderShopInterface $orderShop): bool
  131. {
  132. return $orderShop->getDeliveryType() == OrderShopModel::DELIVERY_TYPE_POINTSALE;
  133. }
  134. public function isComplementaryOrderShop(OrderShopInterface $orderShop): bool
  135. {
  136. return (bool) $orderShop->getMainOrderShop();
  137. }
  138. public function mergeComplentaryOrderShops(
  139. OrderShopInterface $orderShop,
  140. bool $combineProducts = true
  141. ): OrderShopInterface {
  142. $this->entityManager->refresh($orderShop);
  143. if ($orderShop->getComplementaryOrderShops()) {
  144. foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
  145. foreach ($complementaryOrderShop->getOrderProducts() as $orderProductAdd) {
  146. $updated = false;
  147. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  148. if ($combineProducts && $orderProduct->getProduct()->getId() == $orderProductAdd->getProduct(
  149. )->getId()
  150. && (string)$orderProduct->getPrice() == (string)$orderProductAdd->getPrice()
  151. ) {
  152. $orderProduct->setUpdatedOnMergeComplementaryOrderShop(true);
  153. $orderProduct->setQuantityOrder(
  154. $orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder()
  155. );
  156. $updated = true;
  157. }
  158. }
  159. if (!$updated) {
  160. $orderProductAdd->setOnMergeComplementaryOrderShop($complementaryOrderShop);
  161. $orderProductAdd->setCreatedOnMergeComplementaryOrderShop(true);
  162. $orderShop->addOrderProduct($orderProductAdd);
  163. }
  164. }
  165. }
  166. }
  167. return $orderShop;
  168. }
  169. public function isReductionCreditAddedToOrder(
  170. OrderShopInterface $orderShop,
  171. ReductionCreditInterface $reductionCredit
  172. ) {
  173. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  174. if ($orderReductionCredit->getReductionCredit() == $reductionCredit) {
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. public function hasOrderProductAlreadyInCart(
  181. OrderShopInterface $orderShop,
  182. OrderProductInterface $orderProductTest
  183. ): ?OrderProductInterface {
  184. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  185. if ($orderProduct->getProduct() == $orderProductTest->getProduct()) {
  186. return $orderProduct;
  187. }
  188. }
  189. return null;
  190. }
  191. public function isValid(OrderShopInterface $orderShop): bool
  192. {
  193. if ($orderShop->getOrderStatus() && in_array(
  194. $orderShop->getOrderStatus()->getAlias(),
  195. OrderStatusModel::$statusAliasAsValid
  196. ) > 0) {
  197. return true;
  198. }
  199. return false;
  200. }
  201. public function isCart(OrderShopInterface $orderShop): bool
  202. {
  203. if ($orderShop->getOrderStatus() && in_array(
  204. $orderShop->getOrderStatus()->getAlias(),
  205. OrderStatusModel::$statusAliasAsCart
  206. ) > 0) {
  207. return true;
  208. }
  209. return false;
  210. }
  211. // isOrderShopPositiveAmount
  212. public function isPositiveAmount(OrderShopInterface $orderShop): bool
  213. {
  214. return $this->priceSolver->getTotalWithTax($orderShop) >= 0;
  215. }
  216. public function isPaid(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): bool
  217. {
  218. $totalOrderPayments = $this->getTotalOrderPayments($orderShop, $mergeComplementaryOrderShop);
  219. $totalOrder = $this->priceSolver->getTotalWithTax($orderShop);
  220. if ((abs($totalOrderPayments - $totalOrder) < 0.00001
  221. || $totalOrderPayments >= $totalOrder)
  222. && $totalOrder > 0) {
  223. return true;
  224. } else {
  225. return false;
  226. }
  227. }
  228. // isOrderShopPositiveAmountRemainingToBePaid
  229. public function isPositiveAmountRemainingToBePaid(OrderShopInterface $orderShop): bool
  230. {
  231. return $this->getTotalRemainingToBePaid($orderShop) > 0;
  232. }
  233. public function isCartAllowToBeOrder(OrderShopInterface $orderShop): bool
  234. {
  235. return true;
  236. }
  237. }