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.

366 lines
14KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Order;
  3. use App\Entity\Order\OrderProduct;
  4. use App\Entity\Section\Section;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
  7. use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
  8. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  9. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  10. use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
  11. use Lc\CaracoleBundle\Repository\Reduction\ReductionCreditStore;
  12. use Lc\CaracoleBundle\Repository\Section\SectionStore;
  13. use Lc\CaracoleBundle\Resolver\OpeningResolver;
  14. use Lc\CaracoleBundle\Resolver\Price\PriceResolver;
  15. use Lc\SovBundle\Model\User\UserInterface;
  16. use Lc\SovBundle\Repository\AbstractStore;
  17. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  18. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  19. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  20. class OrderShopStore extends AbstractStore
  21. {
  22. protected OrderShopRepositoryQuery $query;
  23. protected EntityManagerInterface $entityManager;
  24. protected PriceResolver $priceResolver;
  25. protected DocumentBuilder $documentBuilder;
  26. protected ReductionCreditStore $reductionCreditStore;
  27. protected SectionStore $sectionStore;
  28. protected OrderProductStore $orderProductStore;
  29. protected MerchantStore $merchantStore;
  30. protected FlashBagInterface $flashBag;
  31. protected OpeningResolver $openingResolver;
  32. protected ParameterBagInterface $parameterBag;
  33. protected UrlGeneratorInterface $router;
  34. public function __construct(
  35. OrderShopRepositoryQuery $query,
  36. EntityManagerInterface $entityManager,
  37. PriceResolver $priceResolver,
  38. DocumentBuilder $documentBuilder,
  39. ReductionCreditStore $reductionCreditStore,
  40. SectionStore $sectionStore,
  41. OrderProductStore $orderProductStore,
  42. MerchantStore $merchantStore,
  43. FlashBagInterface $flashBag,
  44. OpeningResolver $openingResolver,
  45. ParameterBagInterface $parameterBag,
  46. UrlGeneratorInterface $router
  47. ) {
  48. $this->query = $query;
  49. $this->entityManager = $entityManager;
  50. $this->priceResolver = $priceResolver;
  51. $this->documentBuilder = $documentBuilder;
  52. $this->reductionCreditStore = $reductionCreditStore;
  53. $this->sectionStore = $sectionStore;
  54. $this->orderProductStore = $orderProductStore;
  55. $this->merchantStore = $merchantStore;
  56. $this->flashBag = $flashBag;
  57. $this->openingResolver = $openingResolver;
  58. $this->parameterBag = $parameterBag;
  59. $this->router = $router;
  60. }
  61. // getOrderShopsOfWeek
  62. public function getByCycle(SectionInterface $section, $params = [])
  63. {
  64. $orderShops = $this->getBy(
  65. array_merge(
  66. [
  67. 'section' => $section,
  68. 'cycleNumber' => $this->getCycleNumberCurrentOrder($section),
  69. 'isValid' => true,
  70. ],
  71. $params
  72. )
  73. );
  74. return $orderShops;
  75. }
  76. // getOrderShopsOfWeekByUser
  77. public function getByCycleAndUser(SectionInterface $section, UserInterface $user, array $params = [])
  78. {
  79. return $this->getByCycle(
  80. $section,
  81. array_merge(
  82. [
  83. 'user' => $user,
  84. 'weekNumber' => $this->getCycleNumberCurrentOrder($section),
  85. 'excludeComplementaryOrderShops' => true
  86. ],
  87. $params
  88. )
  89. );
  90. }
  91. //public $countOrderShopsOfWeek = null;
  92. public function countByCycle(SectionInterface $section, bool $excludeComplementaryOrderShops = true)
  93. {
  94. return $this->getByCycle(
  95. $section,
  96. [
  97. 'count' => true,
  98. 'excludeComplementaryOrderShops' => $excludeComplementaryOrderShops
  99. ]
  100. );
  101. // @TODO : optimisation à remettre en place
  102. /*if (is_null($this->countOrderShopsOfWeek)) {
  103. $this->countOrderShopsOfWeek = $this->getByCycle(
  104. $section,
  105. [
  106. 'count' => true,
  107. 'excludeComplementaryOrderShops' => $excludeComplementaryOrderShops
  108. ]
  109. );
  110. }
  111. return $this->countOrderShopsOfWeek;*/
  112. }
  113. // getNextWeekId
  114. public function getNextCycleId(SectionInterface $section, int $cycleNumber): int
  115. {
  116. $lastOrder = $this->getOneLastOrderValidOfCycle($section, $cycleNumber);
  117. if ($lastOrder) {
  118. return intval($lastOrder->getCycleId() + 1);
  119. } else {
  120. return 1;
  121. }
  122. }
  123. public function getNextIdValidOrder(Section $section)
  124. {
  125. $lastOrder = $this->getOneLastOrderValid($section);
  126. if ($lastOrder) {
  127. return intval($lastOrder->getIdValidOrder() + 1);
  128. } else {
  129. return 1;
  130. }
  131. }
  132. // getOrderDatas
  133. public function getDatas(OrderShopInterface $orderShop, UserInterface $user = null): array
  134. {
  135. $data = [];
  136. $data['order'] = $orderShop;
  137. if ($orderShop) {
  138. $data['count'] = $orderShop->countQuantities();
  139. $data['total_with_tax'] = $this->priceResolver->getTotalWithTax($orderShop);
  140. $data['order_products_by_category'] = $orderShop->getOrderProductsByParentCategory();
  141. $data['total_remaining_to_be_paid'] = $this->getTotalRemainingToBePaid($orderShop);
  142. }
  143. return $data;
  144. }
  145. public function getAsJsonObject(OrderShopInterface $orderShop): array
  146. {
  147. $data['id'] = $orderShop->getId();
  148. $data['user'] = $orderShop->getUser()->getSummary();
  149. $data['orderStatus'] = $orderShop->getOrderStatus()->__toString();
  150. $data['deliveryAddress'] = $orderShop->getDeliveryAddress()->getSummary();
  151. $data['invoiceAddress'] = $orderShop->getInvoiceAddress()->getSummary();
  152. $data['total'] = $this->priceResolver->getTotal($orderShop);
  153. $data['totalWithTax'] = $this->priceResolver->getTotalWithTax($orderShop);
  154. $data['totalWithTaxAndReduction'] = $this->priceResolver->getTotalWithTax($orderShop);
  155. $i = 0;
  156. foreach ($orderShop->getOrderProductsByParentCategory() as $labelCategory => $orderProducts) {
  157. foreach ($orderProducts as $orderProduct) {
  158. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  159. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  160. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  161. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  162. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  163. $data['orderProducts'][$i]['price'] = $this->priceResolver->getPrice($orderProduct);
  164. $data['orderProducts'][$i]['priceWithTax'] = $this->priceResolver->getPriceWithTax($orderProduct);
  165. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceResolver->getPriceWithTaxAndReduction(
  166. $orderProduct
  167. );
  168. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  169. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceResolver->getTotalOrderProductsWithTaxAndReduction(
  170. array($orderProduct)
  171. );
  172. $i++;
  173. }
  174. }
  175. return $data;
  176. }
  177. public function groupOrderProductsByProductFamily(array $orderProducts): array
  178. {
  179. $orderProductsByProductFamily = [];
  180. foreach ($orderProducts as $orderProduct) {
  181. if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  182. $productFamily = $orderProduct->getProduct()->getProductFamily();
  183. if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
  184. $orderProductsByProductFamily[$productFamily->getId()] = [
  185. 'order_products' => [],
  186. 'total_quantity_weight' => 0,
  187. ];
  188. }
  189. $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
  190. $orderProductsByProductFamily[$productFamily->getId(
  191. )]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit(
  192. )->getCoefficient()) * $orderProduct->getQuantityOrder();
  193. }
  194. }
  195. return $orderProductsByProductFamily;
  196. }
  197. // isOrderShopPositiveAmount
  198. public function isPositiveAmount(OrderShopInterface $orderShop)
  199. {
  200. return $this->priceResolver->getTotalWithTax($orderShop) >= 0;
  201. }
  202. public function isPaid(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false)
  203. {
  204. $totalOrderPayments = $this->getTotalOrderPayments($orderShop, $mergeComplementaryOrderShop);
  205. $totalOrder = $this->priceResolver->getTotalWithTax($orderShop);
  206. if ((abs($totalOrderPayments - $totalOrder) < 0.00001
  207. || $totalOrderPayments >= $totalOrder)
  208. && $totalOrder > 0) {
  209. return true;
  210. } else {
  211. return false;
  212. }
  213. }
  214. public function getTotalOrderPayments(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): float
  215. {
  216. $totalAmount = floatval(0);
  217. foreach ($orderShop->getOrderPayments() as $orderPayment) {
  218. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  219. }
  220. if ($mergeComplementaryOrderShop) {
  221. foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
  222. foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
  223. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  224. }
  225. }
  226. }
  227. return $totalAmount;
  228. }
  229. public function getTotalRemainingToBePaid(OrderShopInterface $orderShop): float
  230. {
  231. return $this->priceResolver->getTotalWithTax($orderShop) - $this->getTotalOrderPayments($orderShop);
  232. }
  233. // isOrderShopPositiveAmountRemainingToBePaid
  234. public function isPositiveAmountRemainingToBePaid(OrderShopInterface $orderShop): bool
  235. {
  236. return $this->getTotalRemainingToBePaid($orderShop) > 0;
  237. }
  238. public function getCartByUserOrCreateIt($user)
  239. {
  240. $newOrderShop = $this->em->getRepository(OrderShopInterface::class)->findCartCurrent(['user' => $user]);
  241. if ($newOrderShop === null) {
  242. $newOrderShop = $this->createOrderShop(
  243. array(
  244. 'user' => $user,
  245. 'merchant' => $this->merchantUtils->getMerchantUser()
  246. )
  247. );
  248. }
  249. return $newOrderShop;
  250. }
  251. public function isCartAllowToBeOrder(OrderShopInterface $orderShop, bool $forceByAdmin = false)
  252. {
  253. return true;
  254. }
  255. // countValidOrderShopByUserAllMerchant
  256. public function countValidByUserAllMerchant($user)
  257. {
  258. $totalOrder = 0;
  259. foreach ($this->merchantStore->getRepositoryQuery()->findAll() as $merchant) {
  260. $totalOrder += $this->countValidByUser($user, $merchant);
  261. }
  262. return $totalOrder;
  263. }
  264. public function countValidByUser(UserInterface $user, MerchantInterface $merchant = null)
  265. {
  266. return $this->getBy(
  267. [
  268. 'user' => $user,
  269. 'isValid' => true,
  270. 'merchant' => $merchant,
  271. 'excludeComplementaryOrderShops' => true,
  272. 'count' => true
  273. ]
  274. );
  275. }
  276. /*
  277. public function getCartCurrent(SectionInterface $section, UserInterface $user = null, VisitorInterface $visitor = null)
  278. {
  279. $paramsSearchOrderShop = [];
  280. $user = $this->security->getUser();
  281. $visitor = $this->userUtils->getVisitorCurrent();
  282. $orderShop = null;
  283. $orderShopUser = null;
  284. $orderShopVisitor = null;
  285. if ($user) {
  286. $orderShopUser = $this->orderShopRepo->findCartCurrent(
  287. [
  288. 'user' => $user
  289. ]
  290. );
  291. }
  292. if ($visitor) {
  293. $orderShopVisitor = $this->orderShopRepo->findCartCurrent(
  294. [
  295. 'visitor' => $visitor
  296. ]
  297. );
  298. }
  299. if ($orderShopUser || $orderShopVisitor) {
  300. // merge
  301. if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor
  302. && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())
  303. && $orderShopUser->getOrderStatus()->getAlias() == OrderStatus::ALIAS_CART) {
  304. $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
  305. $this->utils->addFlash(
  306. 'success',
  307. "Votre panier visiteur vient d'être fusionné avec votre panier client."
  308. );
  309. } else {
  310. $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
  311. }
  312. // set user
  313. if ($orderShop && $user && !$orderShop->getUser()) {
  314. $orderShop->setUser($user);
  315. $orderShop->setVisitor(null);
  316. $this->em->persist($orderShop);
  317. $this->em->flush();
  318. }
  319. }
  320. return $orderShop;
  321. }*/
  322. }