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.

143 lines
6.0KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Order;
  3. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  4. use Lc\CaracoleBundle\Resolver\Price\PriceResolver;
  5. use Lc\SovBundle\Repository\AbstractStore;
  6. class OrderShopStore extends AbstractStore
  7. {
  8. protected OrderShopRepositoryQuery $query;
  9. protected PriceResolver $priceResolver;
  10. public function __construct(
  11. OrderShopRepositoryQuery $query,
  12. PriceResolver $priceResolver
  13. ) {
  14. $this->query = $query;
  15. $this->priceResolver = $priceResolver;
  16. }
  17. public function getDatas(OrderShopInterface $orderShop = null): array
  18. {
  19. $data = [];
  20. if (is_null($orderShop)) {
  21. $orderShop = $this->getCartCurrent();
  22. }
  23. $data['order'] = $orderShop;
  24. if ($orderShop) {
  25. $data['count'] = $orderShop->countQuantities();
  26. $data['total_with_tax'] = $this->priceResolver->getTotalWithTax($orderShop);
  27. $data['order_products_by_category'] = $orderShop->getOrderProductsByParentCategory();
  28. $data['total_remaining_to_be_paid'] = $this->getTotalRemainingToBePaid($orderShop);
  29. }
  30. return $data;
  31. }
  32. public function getAsJsonObject(OrderShopInterface $orderShop): array
  33. {
  34. $data['id'] = $orderShop->getId();
  35. $data['user'] = $orderShop->getUser()->getSummary();
  36. $data['orderStatus'] = $orderShop->getOrderStatus()->__toString();
  37. $data['deliveryAddress'] = $orderShop->getDeliveryAddress()->getSummary();
  38. $data['invoiceAddress'] = $orderShop->getInvoiceAddress()->getSummary();
  39. $data['total'] = $this->priceResolver->getTotal($orderShop);
  40. $data['totalWithTax'] = $this->priceResolver->getTotalWithTax($orderShop);
  41. $data['totalWithTaxAndReduction'] = $this->priceResolver->getTotalWithTax($orderShop);
  42. $i = 0;
  43. foreach ($orderShop->getOrderProductsByParentCategory() as $labelCategory => $orderProducts) {
  44. foreach ($orderProducts as $orderProduct) {
  45. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  46. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  47. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  48. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  49. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  50. $data['orderProducts'][$i]['price'] = $this->priceResolver->getPrice($orderProduct);
  51. $data['orderProducts'][$i]['priceWithTax'] = $this->priceResolver->getPriceWithTax($orderProduct);
  52. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceResolver->getPriceWithTaxAndReduction(
  53. $orderProduct
  54. );
  55. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  56. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceResolver->getTotalOrderProductsWithTaxAndReduction(
  57. array($orderProduct)
  58. );
  59. $i++;
  60. }
  61. }
  62. return $data;
  63. }
  64. public function groupOrderProductsByProductFamily(array $orderProducts): array
  65. {
  66. $orderProductsByProductFamily = [];
  67. foreach ($orderProducts as $orderProduct) {
  68. if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  69. $productFamily = $orderProduct->getProduct()->getProductFamily();
  70. if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
  71. $orderProductsByProductFamily[$productFamily->getId()] = [
  72. 'order_products' => [],
  73. 'total_quantity_weight' => 0,
  74. ];
  75. }
  76. $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
  77. $orderProductsByProductFamily[$productFamily->getId(
  78. )]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit(
  79. )->getCoefficient()) * $orderProduct->getQuantityOrder();
  80. }
  81. }
  82. return $orderProductsByProductFamily;
  83. }
  84. public function isPositiveAmount(OrderShopInterface $orderShop)
  85. {
  86. return $this->priceResolver->getTotalWithTax($orderShop) >= 0;
  87. }
  88. public function isPaid(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false)
  89. {
  90. $totalOrderPayments = $this->getTotalOrderPayments($orderShop, $mergeComplementaryOrderShop);
  91. $totalOrder = $this->priceResolver->getTotalWithTax($orderShop);
  92. if ((abs($totalOrderPayments - $totalOrder) < 0.00001
  93. || $totalOrderPayments >= $totalOrder)
  94. && $totalOrder > 0) {
  95. return true;
  96. } else {
  97. return false;
  98. }
  99. }
  100. public function getTotalOrderPayments(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): float
  101. {
  102. $totalAmount = floatval(0);
  103. foreach ($orderShop->getOrderPayments() as $orderPayment) {
  104. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  105. }
  106. if ($mergeComplementaryOrderShop) {
  107. foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
  108. foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
  109. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  110. }
  111. }
  112. }
  113. return $totalAmount;
  114. }
  115. public function getTotalRemainingToBePaid(OrderShopInterface $orderShop): float
  116. {
  117. return $this->priceResolver->getTotalWithTax($orderShop) - $this->getTotalOrderPayments($orderShop);
  118. }
  119. public function isOrderShopPositiveAmountRemainingToBePaid(OrderShopInterface $orderShop): bool
  120. {
  121. return $this->getTotalRemainingToBePaid($orderShop) > 0;
  122. }
  123. }