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.

214 lines
7.8KB

  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\OrderShopInterface;
  7. use Lc\CaracoleBundle\Model\Order\OrderShopModel;
  8. use Lc\CaracoleBundle\Model\Order\OrderStatusInterface;
  9. use Lc\CaracoleBundle\Model\Order\OrderStatusModel;
  10. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  11. use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
  12. use Lc\CaracoleBundle\Model\Product\ProductInterface;
  13. use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
  14. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  15. class OrderShopSolver
  16. {
  17. protected PriceSolver $priceSolver;
  18. protected EntityManagerInterface $entityManager;
  19. public function __construct(PriceSolver $priceSolver, EntityManagerInterface $entityManager)
  20. {
  21. $this->priceSolver = $priceSolver;
  22. $this->entityManager = $entityManager;
  23. }
  24. public function countQuantities(OrderShopInterface $orderShop): int
  25. {
  26. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  27. }
  28. public function countQuantitiesByOrderProducts($orderProducts = []): int
  29. {
  30. $count = 0;
  31. foreach ($orderProducts as $orderProduct) {
  32. $count += $orderProduct->getQuantityOrder();
  33. }
  34. return $count;
  35. }
  36. public function getOrderProductsByParentCategory(OrderShopInterface $orderShop): array
  37. {
  38. $categoriesArray = [];
  39. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  40. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  41. $category = $productCategories[0]->getParentCategory();
  42. $labelCategory = $category->getTitle();
  43. if (!isset($categoriesArray[$labelCategory])) {
  44. $categoriesArray[$labelCategory] = [];
  45. }
  46. $categoriesArray[$labelCategory][] = $orderProduct;
  47. }
  48. return $categoriesArray;
  49. }
  50. // getOrderProductsByProductFamily
  51. public function getOrderProductsByProductFamily(
  52. OrderShopInterface $orderShop,
  53. ProductFamilyInterface $productFamily
  54. ): array {
  55. $arrayOrderProducts = [];
  56. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  57. if ($orderProduct->getProduct()->getProductFamily() == $productFamily) {
  58. $arrayOrderProducts[] = $orderProduct;
  59. }
  60. }
  61. return $arrayOrderProducts;
  62. }
  63. public function getQuantityOrderByProduct(
  64. OrderShopInterface $orderShop,
  65. ProductInterface $product,
  66. $byWeight = false
  67. ): int {
  68. $quantity = 0;
  69. $productFamily = $product->getProductFamily();
  70. $behaviorCountStock = $productFamily->getBehaviorCountStock();
  71. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  72. if ($orderProduct->getProduct()->getId() == $product->getId()
  73. || (($behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
  74. && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
  75. if ($byWeight) {
  76. $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct(
  77. ) / $orderProduct->getProduct()->getUnitInherited()->getCoefficient());
  78. } else {
  79. $quantity += $orderProduct->getQuantityOrder();
  80. }
  81. }
  82. }
  83. return $quantity;
  84. }
  85. public function getTotalOrderPayments(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): float
  86. {
  87. $totalAmount = floatval(0);
  88. foreach ($orderShop->getOrderPayments() as $orderPayment) {
  89. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  90. }
  91. if ($mergeComplementaryOrderShop) {
  92. foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
  93. foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
  94. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  95. }
  96. }
  97. }
  98. return $totalAmount;
  99. }
  100. public function getTotalRemainingToBePaid(OrderShopInterface $orderShop): float
  101. {
  102. return $this->priceSolver->getTotalWithTax($orderShop) - $this->getTotalOrderPayments($orderShop);
  103. }
  104. public function getOrderStatusHistory(OrderShopInterface $orderShop, OrderStatusInterface $status)
  105. {
  106. $orderStatusHistories = $orderShop->getOrderStatusHistories();
  107. if (count($orderStatusHistories) > 0) {
  108. foreach ($orderStatusHistories as $orderStatusHistory) {
  109. if ($orderStatusHistory->getOrderStatus() === $status) {
  110. return $orderStatusHistory;
  111. }
  112. }
  113. }
  114. return null;
  115. }
  116. public function getDocumentInvoice(OrderShopInterface $orderShop): ?DocumentInterface
  117. {
  118. foreach ($orderShop->getDocuments() as $document) {
  119. if ($document->getType() == DocumentModel::TYPE_INVOICE) {
  120. return $document;
  121. }
  122. }
  123. return null;
  124. }
  125. public function isDeliveryHome(OrderShopInterface $orderShop): bool
  126. {
  127. return $orderShop->getDeliveryType() == OrderShopModel::DELIVERY_TYPE_HOME;
  128. }
  129. public function isDeliveryPointSale(OrderShopInterface $orderShop): bool
  130. {
  131. return $orderShop->getDeliveryType() == OrderShopModel::DELIVERY_TYPE_POINTSALE;
  132. }
  133. public function isComplementaryOrderShop(OrderShopInterface $orderShop): bool
  134. {
  135. return (bool) $orderShop->getMainOrderShop() ;
  136. }
  137. public function mergeComplentaryOrderShops(OrderShopInterface $orderShop, bool $combineProducts = true) :OrderShopInterface
  138. {
  139. $this->entityManager->refresh($orderShop);
  140. if ($orderShop->getComplementaryOrderShops()) {
  141. foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
  142. foreach ($complementaryOrderShop->getOrderProducts() as $orderProductAdd) {
  143. $updated = false;
  144. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  145. if ($combineProducts && $orderProduct->getProduct()->getId() == $orderProductAdd->getProduct(
  146. )->getId()
  147. && (string)$orderProduct->getPrice() == (string)$orderProductAdd->getPrice()
  148. ) {
  149. $orderProduct->setUpdatedOnMergeComplementaryOrderShop(true);
  150. $orderProduct->setQuantityOrder(
  151. $orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder()
  152. );
  153. $updated = true;
  154. }
  155. }
  156. if (!$updated) {
  157. $orderProductAdd->setOnMergeComplementaryOrderShop($complementaryOrderShop);
  158. $orderProductAdd->setCreatedOnMergeComplementaryOrderShop(true);
  159. $orderShop->addOrderProduct($orderProductAdd);
  160. }
  161. }
  162. }
  163. }
  164. return $orderShop;
  165. }
  166. public function isReductionCreditAddedToOrder(
  167. OrderShopInterface $orderShop,
  168. ReductionCreditInterface $reductionCredit
  169. ) {
  170. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  171. if ($orderReductionCredit->getReductionCredit() == $reductionCredit) {
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. }