Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

228 Zeilen
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. // isOrderShopPositiveAmount
  95. public function isPositiveAmount(OrderShopInterface $orderShop)
  96. {
  97. return $this->priceResolver->getTotalWithTax($orderShop) >= 0;
  98. }
  99. public function isPaid(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false)
  100. {
  101. $totalOrderPayments = $this->getTotalOrderPayments($orderShop, $mergeComplementaryOrderShop);
  102. $totalOrder = $this->priceResolver->getTotalWithTax($orderShop);
  103. if ((abs($totalOrderPayments - $totalOrder) < 0.00001
  104. || $totalOrderPayments >= $totalOrder)
  105. && $totalOrder > 0) {
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111. public function getTotalOrderPayments(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): float
  112. {
  113. $totalAmount = floatval(0);
  114. foreach ($orderShop->getOrderPayments() as $orderPayment) {
  115. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  116. }
  117. if ($mergeComplementaryOrderShop) {
  118. foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
  119. foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
  120. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  121. }
  122. }
  123. }
  124. return $totalAmount;
  125. }
  126. public function getTotalRemainingToBePaid(OrderShopInterface $orderShop): float
  127. {
  128. return $this->priceResolver->getTotalWithTax($orderShop) - $this->getTotalOrderPayments($orderShop);
  129. }
  130. // isOrderShopPositiveAmountRemainingToBePaid
  131. public function isPositiveAmountRemainingToBePaid(OrderShopInterface $orderShop): bool
  132. {
  133. return $this->getTotalRemainingToBePaid($orderShop) > 0;
  134. }
  135. public function getCartByUserOrCreateIt($user)
  136. {
  137. $newOrderShop = $this->em->getRepository(OrderShopInterface::class)->findCartCurrent(['user' => $user]);
  138. if ($newOrderShop === null) {
  139. $newOrderShop = $this->createOrderShop(
  140. array(
  141. 'user' => $user,
  142. 'merchant' => $this->merchantUtils->getMerchantUser()
  143. )
  144. );
  145. }
  146. return $newOrderShop;
  147. }
  148. public function isCartAllowToBeOrder(OrderShopInterface $orderShop)
  149. {
  150. return true;
  151. }
  152. /*
  153. public function getCartCurrent(SectionInterface $section, UserInterface $user = null, VisitorInterface $visitor = null)
  154. {
  155. $paramsSearchOrderShop = [];
  156. $user = $this->security->getUser();
  157. $visitor = $this->userUtils->getVisitorCurrent();
  158. $orderShop = null;
  159. $orderShopUser = null;
  160. $orderShopVisitor = null;
  161. if ($user) {
  162. $orderShopUser = $this->orderShopRepo->findCartCurrent(
  163. [
  164. 'user' => $user
  165. ]
  166. );
  167. }
  168. if ($visitor) {
  169. $orderShopVisitor = $this->orderShopRepo->findCartCurrent(
  170. [
  171. 'visitor' => $visitor
  172. ]
  173. );
  174. }
  175. if ($orderShopUser || $orderShopVisitor) {
  176. // merge
  177. if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor
  178. && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())
  179. && $orderShopUser->getOrderStatus()->getAlias() == OrderStatus::ALIAS_CART) {
  180. $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
  181. $this->utils->addFlash(
  182. 'success',
  183. "Votre panier visiteur vient d'être fusionné avec votre panier client."
  184. );
  185. } else {
  186. $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
  187. }
  188. // set user
  189. if ($orderShop && $user && !$orderShop->getUser()) {
  190. $orderShop->setUser($user);
  191. $orderShop->setVisitor(null);
  192. $this->em->persist($orderShop);
  193. $this->em->flush();
  194. }
  195. }
  196. return $orderShop;
  197. }*/
  198. }