Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

226 lines
9.0KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Order;
  3. use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
  4. use Lc\CaracoleBundle\Factory\File\DocumentFactory;
  5. use Lc\CaracoleBundle\Model\File\DocumentModel;
  6. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  7. use Lc\CaracoleBundle\Resolver\Price\PriceResolver;
  8. use Lc\CaracoleBundle\Resolver\Reference\DocumentReferenceResolver;
  9. use Lc\SovBundle\Repository\AbstractStore;
  10. class OrderShopStore extends AbstractStore
  11. {
  12. protected OrderShopRepositoryQuery $query;
  13. protected PriceResolver $priceResolver;
  14. protected DocumentReferenceResolver $documentReferenceResolver;
  15. protected DocumentBuilder $documentBuilder;
  16. public function __construct(
  17. OrderShopRepositoryQuery $query,
  18. PriceResolver $priceResolver,
  19. DocumentReferenceResolver $documentReferenceResolver,
  20. DocumentBuilder $documentBuilder
  21. ) {
  22. $this->query = $query;
  23. $this->priceResolver = $priceResolver;
  24. $this->documentReferenceResolver = $documentReferenceResolver;
  25. $this->documentBuilder = $documentBuilder;
  26. }
  27. public function getDatas(OrderShopInterface $orderShop = null): array
  28. {
  29. $data = [];
  30. if (is_null($orderShop)) {
  31. $orderShop = $this->getCartCurrent();
  32. }
  33. $data['order'] = $orderShop;
  34. if ($orderShop) {
  35. $data['count'] = $orderShop->countQuantities();
  36. $data['total_with_tax'] = $this->priceResolver->getTotalWithTax($orderShop);
  37. $data['order_products_by_category'] = $orderShop->getOrderProductsByParentCategory();
  38. $data['total_remaining_to_be_paid'] = $this->getTotalRemainingToBePaid($orderShop);
  39. }
  40. return $data;
  41. }
  42. public function getAsJsonObject(OrderShopInterface $orderShop): array
  43. {
  44. $data['id'] = $orderShop->getId();
  45. $data['user'] = $orderShop->getUser()->getSummary();
  46. $data['orderStatus'] = $orderShop->getOrderStatus()->__toString();
  47. $data['deliveryAddress'] = $orderShop->getDeliveryAddress()->getSummary();
  48. $data['invoiceAddress'] = $orderShop->getInvoiceAddress()->getSummary();
  49. $data['total'] = $this->priceResolver->getTotal($orderShop);
  50. $data['totalWithTax'] = $this->priceResolver->getTotalWithTax($orderShop);
  51. $data['totalWithTaxAndReduction'] = $this->priceResolver->getTotalWithTax($orderShop);
  52. $i = 0;
  53. foreach ($orderShop->getOrderProductsByParentCategory() as $labelCategory => $orderProducts) {
  54. foreach ($orderProducts as $orderProduct) {
  55. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  56. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  57. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  58. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  59. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  60. $data['orderProducts'][$i]['price'] = $this->priceResolver->getPrice($orderProduct);
  61. $data['orderProducts'][$i]['priceWithTax'] = $this->priceResolver->getPriceWithTax($orderProduct);
  62. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceResolver->getPriceWithTaxAndReduction(
  63. $orderProduct
  64. );
  65. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  66. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceResolver->getTotalOrderProductsWithTaxAndReduction(
  67. array($orderProduct)
  68. );
  69. $i++;
  70. }
  71. }
  72. return $data;
  73. }
  74. public function groupOrderProductsByProductFamily(array $orderProducts): array
  75. {
  76. $orderProductsByProductFamily = [];
  77. foreach ($orderProducts as $orderProduct) {
  78. if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  79. $productFamily = $orderProduct->getProduct()->getProductFamily();
  80. if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
  81. $orderProductsByProductFamily[$productFamily->getId()] = [
  82. 'order_products' => [],
  83. 'total_quantity_weight' => 0,
  84. ];
  85. }
  86. $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
  87. $orderProductsByProductFamily[$productFamily->getId(
  88. )]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit(
  89. )->getCoefficient()) * $orderProduct->getQuantityOrder();
  90. }
  91. }
  92. return $orderProductsByProductFamily;
  93. }
  94. public function isPositiveAmount(OrderShopInterface $orderShop)
  95. {
  96. return $this->priceResolver->getTotalWithTax($orderShop) >= 0;
  97. }
  98. public function isPaid(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false)
  99. {
  100. $totalOrderPayments = $this->getTotalOrderPayments($orderShop, $mergeComplementaryOrderShop);
  101. $totalOrder = $this->priceResolver->getTotalWithTax($orderShop);
  102. if ((abs($totalOrderPayments - $totalOrder) < 0.00001
  103. || $totalOrderPayments >= $totalOrder)
  104. && $totalOrder > 0) {
  105. return true;
  106. } else {
  107. return false;
  108. }
  109. }
  110. public function getTotalOrderPayments(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): float
  111. {
  112. $totalAmount = floatval(0);
  113. foreach ($orderShop->getOrderPayments() as $orderPayment) {
  114. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  115. }
  116. if ($mergeComplementaryOrderShop) {
  117. foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
  118. foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
  119. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  120. }
  121. }
  122. }
  123. return $totalAmount;
  124. }
  125. public function getTotalRemainingToBePaid(OrderShopInterface $orderShop): float
  126. {
  127. return $this->priceResolver->getTotalWithTax($orderShop) - $this->getTotalOrderPayments($orderShop);
  128. }
  129. public function isOrderShopPositiveAmountRemainingToBePaid(OrderShopInterface $orderShop): bool
  130. {
  131. return $this->getTotalRemainingToBePaid($orderShop) > 0;
  132. }
  133. public function getCartByUserOrCreateIt($user)
  134. {
  135. $newOrderShop = $this->em->getRepository(OrderShopInterface::class)->findCartCurrent(['user' => $user]);
  136. if ($newOrderShop === null) {
  137. $newOrderShop = $this->createOrderShop(
  138. array(
  139. 'user' => $user,
  140. 'merchant' => $this->merchantUtils->getMerchantUser()
  141. )
  142. );
  143. }
  144. return $newOrderShop;
  145. }
  146. public function isCartAllowToBeOrder(OrderShopInterface $orderShop)
  147. {
  148. return true;
  149. }
  150. /*
  151. public function getCartCurrent(SectionInterface $section, UserInterface $user = null, VisitorInterface $visitor = null)
  152. {
  153. $paramsSearchOrderShop = [];
  154. $user = $this->security->getUser();
  155. $visitor = $this->userUtils->getVisitorCurrent();
  156. $orderShop = null;
  157. $orderShopUser = null;
  158. $orderShopVisitor = null;
  159. if ($user) {
  160. $orderShopUser = $this->orderShopRepo->findCartCurrent(
  161. [
  162. 'user' => $user
  163. ]
  164. );
  165. }
  166. if ($visitor) {
  167. $orderShopVisitor = $this->orderShopRepo->findCartCurrent(
  168. [
  169. 'visitor' => $visitor
  170. ]
  171. );
  172. }
  173. if ($orderShopUser || $orderShopVisitor) {
  174. // merge
  175. if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor
  176. && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())
  177. && $orderShopUser->getOrderStatus()->getAlias() == OrderStatus::ALIAS_CART) {
  178. $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
  179. $this->utils->addFlash(
  180. 'success',
  181. "Votre panier visiteur vient d'être fusionné avec votre panier client."
  182. );
  183. } else {
  184. $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
  185. }
  186. // set user
  187. if ($orderShop && $user && !$orderShop->getUser()) {
  188. $orderShop->setUser($user);
  189. $orderShop->setVisitor(null);
  190. $this->em->persist($orderShop);
  191. $this->em->flush();
  192. }
  193. }
  194. return $orderShop;
  195. }*/
  196. }