Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

146 lines
5.1KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Solver\Order;
  3. use Lc\CaracoleBundle\Model\File\DocumentInterface;
  4. use Lc\CaracoleBundle\Model\File\DocumentModel;
  5. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  6. use Lc\CaracoleBundle\Model\Order\OrderStatusInterface;
  7. use Lc\CaracoleBundle\Model\Order\OrderStatusModel;
  8. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  9. use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
  10. use Lc\CaracoleBundle\Model\Product\ProductInterface;
  11. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  12. class OrderShopSolver
  13. {
  14. protected PriceSolver $priceSolver;
  15. public function __construct(PriceSolver $priceSolver)
  16. {
  17. $this->priceSolver = $priceSolver;
  18. }
  19. public function countQuantities(OrderShopInterface $orderShop): int
  20. {
  21. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  22. }
  23. public function countQuantitiesByOrderProducts($orderProducts = []): int
  24. {
  25. $count = 0;
  26. foreach ($orderProducts as $orderProduct) {
  27. $count += $orderProduct->getQuantityOrder();
  28. }
  29. return $count;
  30. }
  31. public function getOrderProductsByParentCategory(OrderShopInterface $orderShop): array
  32. {
  33. $categoriesArray = [];
  34. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  35. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  36. $category = $productCategories[0]->getParentCategory();
  37. $labelCategory = $category->getTitle();
  38. if (!isset($categoriesArray[$labelCategory])) {
  39. $categoriesArray[$labelCategory] = [];
  40. }
  41. $categoriesArray[$labelCategory][] = $orderProduct;
  42. }
  43. return $categoriesArray;
  44. }
  45. // getOrderProductsByProductFamily
  46. public function getOrderProductsByProductFamily(
  47. OrderShopInterface $orderShop,
  48. ProductFamilyInterface $productFamily
  49. ): array {
  50. $arrayOrderProducts = [];
  51. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  52. if ($orderProduct->getProduct()->getProductFamily() == $productFamily) {
  53. $arrayOrderProducts[] = $orderProduct;
  54. }
  55. }
  56. return $arrayOrderProducts;
  57. }
  58. public function getQuantityOrderByProduct(
  59. OrderShopInterface $orderShop,
  60. ProductInterface $product,
  61. $byWeight = false
  62. ): int {
  63. $quantity = 0;
  64. $productFamily = $product->getProductFamily();
  65. $behaviorCountStock = $productFamily->getBehaviorCountStock();
  66. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  67. if ($orderProduct->getProduct()->getId() == $product->getId()
  68. || (($behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
  69. && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
  70. if ($byWeight) {
  71. $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct(
  72. ) / $orderProduct->getProduct()->getUnitInherited()->getCoefficient());
  73. } else {
  74. $quantity += $orderProduct->getQuantityOrder();
  75. }
  76. }
  77. }
  78. return $quantity;
  79. }
  80. public function getTotalOrderPayments(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): float
  81. {
  82. $totalAmount = floatval(0);
  83. foreach ($orderShop->getOrderPayments() as $orderPayment) {
  84. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  85. }
  86. if ($mergeComplementaryOrderShop) {
  87. foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
  88. foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
  89. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  90. }
  91. }
  92. }
  93. return $totalAmount;
  94. }
  95. public function getTotalRemainingToBePaid(OrderShopInterface $orderShop): float
  96. {
  97. return $this->priceSolver->getTotalWithTax($orderShop) - $this->getTotalOrderPayments($orderShop);
  98. }
  99. public function getOrderStatusHistory(OrderShopInterface $orderShop, OrderStatusInterface $status)
  100. {
  101. $orderStatusHistories = $orderShop->getOrderStatusHistories();
  102. if (count($orderStatusHistories) > 0) {
  103. foreach ($orderStatusHistories as $orderStatusHistory) {
  104. if ($orderStatusHistory->getOrderStatus() === $status) {
  105. return $orderStatusHistory;
  106. }
  107. }
  108. }
  109. return null;
  110. }
  111. public function getDocumentInvoice(OrderShopInterface $orderShop): ?DocumentInterface
  112. {
  113. foreach ($orderShop->getDocuments() as $document) {
  114. if ($document->getType() == DocumentModel::TYPE_INVOICE) {
  115. return $document;
  116. }
  117. }
  118. return null;
  119. }
  120. }