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.

688 lines
23KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Order;
  3. use App\Builder\Distribution\DistributionBuilder;
  4. use App\Entity\Distribution\Distribution;
  5. use App\Entity\User\User;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
  8. use Lc\CaracoleBundle\Model\Distribution\DistributionInterface;
  9. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  10. use Lc\CaracoleBundle\Model\Order\OrderStatusModel;
  11. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  12. use Lc\CaracoleBundle\Model\Product\ProductInterface;
  13. use Lc\CaracoleBundle\Model\Reduction\ReductionCartInterface;
  14. use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
  15. use Lc\CaracoleBundle\Model\Reduction\ReductionCreditModel;
  16. use Lc\CaracoleBundle\Model\User\VisitorInterface;
  17. use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
  18. use Lc\CaracoleBundle\Repository\MerchantStoreTrait;
  19. use Lc\CaracoleBundle\Repository\Reduction\ReductionCartStore;
  20. use Lc\CaracoleBundle\Repository\Reduction\ReductionCreditStore;
  21. use Lc\CaracoleBundle\Repository\Section\SectionStore;
  22. use Lc\CaracoleBundle\Resolver\OpeningResolver;
  23. use Lc\CaracoleBundle\Repository\SectionStoreTrait;
  24. use Lc\CaracoleBundle\Solver\Order\OrderShopSolver;
  25. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  26. use Lc\CaracoleBundle\Solver\Reduction\ReductionCartSolver;
  27. use Lc\SovBundle\Model\User\UserInterface;
  28. use Lc\CaracoleBundle\Repository\AbstractStore;
  29. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  30. use Lc\SovBundle\Solver\Setting\SettingSolver;
  31. use Lc\SovBundle\Translation\FlashBagTranslator;
  32. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  33. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  34. class OrderShopStore extends AbstractStore
  35. {
  36. use SectionStoreTrait;
  37. use MerchantStoreTrait;
  38. protected OrderShopRepositoryQuery $query;
  39. protected EntityManagerInterface $entityManager;
  40. protected PriceSolver $priceSolver;
  41. protected DocumentBuilder $documentBuilder;
  42. protected ReductionCreditStore $reductionCreditStore;
  43. protected ReductionCartSolver $reductionCartSolver;
  44. protected SectionStore $sectionStore;
  45. protected OrderProductStore $orderProductStore;
  46. protected MerchantStore $merchantStore;
  47. protected FlashBagTranslator $flashBagTranslator;
  48. protected OpeningResolver $openingResolver;
  49. protected ParameterBagInterface $parameterBag;
  50. protected UrlGeneratorInterface $router;
  51. protected OrderShopSolver $orderShopSolver;
  52. protected ReductionCartStore $reductionCartStore;
  53. protected DistributionBuilder $distributionBuilder;
  54. protected SettingSolver $settingSolver;
  55. public function __construct(
  56. OrderShopRepositoryQuery $query,
  57. EntityManagerInterface $entityManager,
  58. PriceSolver $priceSolver,
  59. DocumentBuilder $documentBuilder,
  60. ReductionCreditStore $reductionCreditStore,
  61. ReductionCartSolver $reductionCartSolver,
  62. SectionStore $sectionStore,
  63. OrderProductStore $orderProductStore,
  64. MerchantStore $merchantStore,
  65. FlashBagTranslator $flashBagTranslator,
  66. ParameterBagInterface $parameterBag,
  67. UrlGeneratorInterface $router,
  68. OrderShopSolver $orderShopSolver,
  69. ReductionCartStore $reductionCartStore,
  70. DistributionBuilder $distributionBuilder,
  71. SettingSolver $settingSolver
  72. )
  73. {
  74. $this->query = $query;
  75. $this->entityManager = $entityManager;
  76. $this->priceSolver = $priceSolver;
  77. $this->documentBuilder = $documentBuilder;
  78. $this->reductionCreditStore = $reductionCreditStore;
  79. $this->reductionCartSolver = $reductionCartSolver;
  80. $this->sectionStore = $sectionStore;
  81. $this->orderProductStore = $orderProductStore;
  82. $this->merchantStore = $merchantStore;
  83. $this->flashBagTranslator = $flashBagTranslator;
  84. $this->parameterBag = $parameterBag;
  85. $this->router = $router;
  86. $this->orderShopSolver = $orderShopSolver;
  87. $this->reductionCartStore = $reductionCartStore;
  88. $this->distributionBuilder = $distributionBuilder;
  89. $this->settingSolver = $settingSolver;
  90. }
  91. public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
  92. {
  93. //$query->orderBy('id', 'DESC');
  94. return $query;
  95. }
  96. public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
  97. {
  98. $this
  99. ->addFilterBySectionOptionnal($query)
  100. ->addFilterByMerchantViaSectionOptionnal($query);
  101. return $query;
  102. }
  103. public function relationsDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
  104. {
  105. $query->joinOrderProducts(true);
  106. return $query;
  107. }
  108. // getOrderShopsOfWeek
  109. //getByCurrentCycle
  110. public function getByCurrentDistribution($params = [], $query = null)
  111. {
  112. return $this->getBy(
  113. array_merge(
  114. [
  115. 'distribution' => $this->distributionBuilder->guessCurrentDistributionOrder(
  116. $this->section
  117. ),
  118. 'isValid' => true,
  119. ],
  120. $params
  121. ),
  122. $query
  123. );
  124. }
  125. // getOrderShopsOfWeekByUser
  126. //getByCurrentCycleAndUser
  127. public function getByCurrentDistributionAndUser(UserInterface $user = null, array $params = [], $query = null)
  128. {
  129. return $this->getByCurrentDistribution(
  130. array_merge(
  131. [
  132. 'user' => $user,
  133. 'excludeComplementaryOrderShops' => true
  134. ],
  135. $params
  136. ),
  137. $query
  138. );
  139. }
  140. //public $countOrderShopsOfWeek = null;
  141. // public function countByCurrentCycle(bool $excludeComplementaryOrderShops = true, $query = null)
  142. //countByCurrentCycle
  143. public function countByCurrentDistribution(array $params, $query = null)
  144. {
  145. return $this->countBy(
  146. array_merge(
  147. [
  148. 'distribution' => $this->distributionBuilder->guessCurrentDistributionOrder(
  149. $this->section
  150. ),
  151. 'excludeComplementaryOrderShops' => isset($params['excludeComplementaryOrderShops']) ?? true,
  152. ],
  153. $params
  154. ),
  155. $query
  156. );
  157. // @TODO : optimisation à remettre en place
  158. /*if (is_null($this->countOrderShopsOfWeek)) {
  159. $this->countOrderShopsOfWeek = $this->getByCurrentCycle(
  160. $section,
  161. [
  162. 'count' => true,
  163. 'excludeComplementaryOrderShops' => $excludeComplementaryOrderShops
  164. ]
  165. );
  166. }
  167. return $this->countOrderShopsOfWeek;*/
  168. }
  169. // getNextWeekId
  170. public function getNextCycleId(Distribution $distribution, $query = null): int
  171. {
  172. $lastOrder = $this->getOneLastValidByDistribution($distribution, $query);
  173. if ($lastOrder) {
  174. return intval($lastOrder->getCycleId() + 1);
  175. } else {
  176. return 1;
  177. }
  178. }
  179. public function getNextIdValidOrder($query = null): int
  180. {
  181. $lastOrder = $this->getOneLastValid($query);
  182. if ($lastOrder) {
  183. return intval($lastOrder->getIdValidOrder() + 1);
  184. } else {
  185. return 1;
  186. }
  187. }
  188. public function countValidByUserAllMerchant($user, $query = null): int
  189. {
  190. $this->resetContext();
  191. return $this->countBy(
  192. [
  193. 'user' => $user,
  194. 'isValid' => true,
  195. 'excludeComplementaryOrderShops' => true
  196. ],
  197. $query
  198. );
  199. }
  200. public function countValidByUser(UserInterface $user = null, $query = null): int
  201. {
  202. return $this->countBy(
  203. [
  204. 'user' => $user,
  205. 'isValid' => true,
  206. 'excludeComplementaryOrderShops' => true
  207. ],
  208. $query
  209. );
  210. }
  211. //countValidByCurrentCycle
  212. public function countValidByCurrentDistribution($query = null): int
  213. {
  214. return $this->countBy(
  215. [
  216. 'distribution' => $this->distributionBuilder->guessCurrentDistributionOrder($this->section),
  217. 'isValid' => true,
  218. 'excludeComplementaryOrderShops' => true
  219. ],
  220. $query
  221. );
  222. }
  223. // countValidOrderWithReductionCredit
  224. public function countValidWithReductionCredit(
  225. ReductionCreditInterface $reductionCredit,
  226. UserInterface $user = null,
  227. $query = null
  228. ): int
  229. {
  230. //TODO vérifier que ne pas utiliser createDefaultQuery est pertinent
  231. $query = $this->createQuery($query);
  232. if ($user) {
  233. $query->filterByUser($user);
  234. }
  235. $query
  236. ->selectCount()
  237. ->filterByReductionCredit($reductionCredit)
  238. ->filterByStatus(OrderStatusModel::$statusAliasAsValid);
  239. return $query->count();
  240. }
  241. // countValidOrderWithReductionCart
  242. public function countValidWithReductionCart(
  243. ReductionCartInterface $reductionCart,
  244. $query = null
  245. ): int
  246. {
  247. $query = $this->createQuery($query);
  248. $query
  249. ->selectCount()
  250. ->filterByReductionCart($reductionCart)
  251. ->filterByStatus(OrderStatusModel::$statusAliasAsValid);
  252. return $query->count();
  253. }
  254. // countValidOrderWithReductionCartPerUser
  255. public function countValidWithReductionCartByUser(
  256. ReductionCartInterface $reductionCart,
  257. UserInterface $user,
  258. $query = null
  259. ): int
  260. {
  261. $query = $this->createDefaultQuery($query);
  262. $query
  263. ->selectCount()
  264. ->filterByUser($user)
  265. ->filterByReductionCart($reductionCart)
  266. ->filterByStatus(OrderStatusModel::$statusAliasAsValid);
  267. return $query->count();
  268. }
  269. // findCartCurrent
  270. public function getOneCartCurrent(
  271. UserInterface $user = null,
  272. VisitorInterface $visitor = null,
  273. $query = null
  274. ): ?OrderShopInterface
  275. {
  276. if (is_null($user) && is_null($visitor)) {
  277. return null;
  278. }
  279. $query = $this->createDefaultQuery($query);
  280. if (!is_null($user)) {
  281. $query->filterByUser($user);
  282. } else {
  283. if (!is_null($visitor)) {
  284. $query->filterByVisitor($visitor);
  285. }
  286. }
  287. $query
  288. ->selectOrderReductionCarts()
  289. ->filterByStatus(OrderStatusModel::$statusAliasAsCart);
  290. return $query->findOne();
  291. }
  292. // findLastOrderValidOfWeek
  293. //getOneLastValidByCycle
  294. public function getOneLastValidByDistribution(
  295. DistributionInterface $distribution,
  296. $query = null
  297. ): ?OrderShopInterface
  298. {
  299. $query = $this->createDefaultQuery($query);
  300. $query
  301. ->filterByDistribution($distribution)
  302. ->filterByStatus(OrderStatusModel::$statusAliasAsValid)
  303. ->filterIsNotComplementaryOrderShop()
  304. ->orderBy('.cycleId', 'DESC');
  305. return $query->findOne();
  306. }
  307. //findLastOrderValid
  308. public function getOneLastValid($query = null): ?OrderShopInterface
  309. {
  310. $query = $this->createDefaultQuery($query);
  311. $query
  312. ->filterByStatus(OrderStatusModel::$statusAliasAsValid)
  313. ->filterIsNotComplementaryOrderShop()
  314. ->orderBy('.idValidOrder', 'DESC');
  315. return $query->findOne();
  316. }
  317. public function countBy(array $params = [], $query = null)
  318. {
  319. $query = $this->createDefaultQuery($query);
  320. $query->selectCount();
  321. $this->applyGetByFilters($params, $query);
  322. return $query->count();
  323. }
  324. public function getBy(array $params = [], $query = null): array
  325. {
  326. $query = $this->createDefaultQuery($query);
  327. $this->applyGetByFilters($params, $query);
  328. $orderShops = $query->find();
  329. if (isset($params['mergeComplementaryOrderShops']) && $params['mergeComplementaryOrderShops'] == true) {
  330. $mergeComplementaryOrderShopsSameSection = isset($params['mergeComplementaryOrderShopsSameSection'])
  331. ? $params['mergeComplementaryOrderShopsSameSection'] : false;
  332. foreach ($orderShops as $orderShop) {
  333. $this->orderShopSolver->mergeComplentaryOrderShops($orderShop, true, $mergeComplementaryOrderShopsSameSection);
  334. }
  335. }
  336. return $orderShops;
  337. }
  338. protected function applyGetByFilters(array $params, $query)
  339. {
  340. if (isset($params['isMerchantOnline'])) {
  341. $query->filterIsMerchantOnline();
  342. }
  343. if (isset($params['select'])) {
  344. $query->selectParam($params['select']);
  345. }
  346. if (isset($params['dateStart']) || isset($params['dateEnd'])) {
  347. $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate';
  348. }
  349. if (isset($params['dateStart'])) {
  350. $query->filterByDateStart($params['dateField'], $params['dateStart']);
  351. }
  352. if (isset($params['dateEnd'])) {
  353. $query->filterByDateEnd($params['dateField'], $params['dateEnd']);
  354. }
  355. if (isset($params['distribution'])) {
  356. $query->filterByDistribution($params['distribution']);
  357. }
  358. if (isset($params['distributions'])) {
  359. $query->filterByDistributions($params['distributions']);
  360. }
  361. if (isset($params['isCart'])) {
  362. $query->filterByStatus(OrderStatusModel::$statusAliasAsCart);
  363. }
  364. if (isset($params['isValid'])) {
  365. $query->filterByStatus(OrderStatusModel::$statusAliasAsValid);
  366. }
  367. if (isset($params['isWaitingDelivery'])) {
  368. $query->filterByStatus(OrderStatusModel::$statusAliasWaitingDelivery);
  369. }
  370. if (isset($params['orderStatus'])) {
  371. $query->filterByStatus([$params['orderStatus']]);
  372. }
  373. if (isset($params['user'])) {
  374. $query->filterByUser($params['user']);
  375. }
  376. if (isset($params['address'])) {
  377. $query->filterByAddress($params['address']);
  378. }
  379. if (isset($params['minimumTomorrowDelivery'])) {
  380. $query->filterMinimumTomorrowDelivery();
  381. }
  382. if (isset($params['mergeComplementaryOrderShops']) && $params['mergeComplementaryOrderShops']) {
  383. $query
  384. ->joinComplementaryOrderShops();
  385. }
  386. if (isset($params['isComplementaryOrderShop']) && $params['isComplementaryOrderShop']) {
  387. $query->filterIsComplementaryOrderShop();
  388. if (isset($params['sectionMainOrderShop']) && $params['sectionMainOrderShop']) {
  389. $query->filterSectionMainOrderShop($params['sectionMainOrderShop']);
  390. }
  391. }
  392. if ((isset($params['excludeComplementaryOrderShops']) && $params['excludeComplementaryOrderShops'])
  393. || (isset($params['mergeComplementaryOrderShops']) && $params['mergeComplementaryOrderShops'])) {
  394. $query->filterIsNullMainOrderShop();
  395. }
  396. if (isset($params['joinProductFamily'])) {
  397. $query->joinProductFamily(true);
  398. }
  399. if (isset($params['orderBy'])) {
  400. $sort = isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC';
  401. $query->orderBy($params['orderBy'], $sort);
  402. } else {
  403. $query->orderBy('.id', 'DESC');
  404. }
  405. if (isset($params['groupBy'])) {
  406. $query->groupBy($params['groupBy']);
  407. }
  408. return $query;
  409. }
  410. public function isReductionGiftUsed(ReductionCreditInterface $reductionGift, $query = null)
  411. {
  412. if ($this->countValidWithReductionCredit($reductionGift, null, $query)) {
  413. return true;
  414. } else {
  415. return false;
  416. }
  417. }
  418. public function isReductionCreditUsed(
  419. ReductionCreditInterface $reductionCredit,
  420. UserInterface $user = null,
  421. $query = null
  422. )
  423. {
  424. if ($this->countValidWithReductionCredit($reductionCredit, $user, $query)) {
  425. return true;
  426. } else {
  427. return false;
  428. }
  429. }
  430. public function getReductionCreditsAvailableByUser(UserInterface $user): array
  431. {
  432. $reductionCredits = $this->reductionCreditStore
  433. ->setMerchant($this->merchant)
  434. ->getByTypeAndUser(ReductionCreditModel::TYPE_CREDIT, $user);
  435. $reductionCreditsArray = [];
  436. foreach ($reductionCredits as $reductionCredit) {
  437. if (!$this->countValidWithReductionCredit($reductionCredit, $user)) {
  438. $reductionCreditsArray[] = $reductionCredit;
  439. }
  440. }
  441. return $reductionCreditsArray;
  442. }
  443. public function getReductionGiftsAvailableByUser(UserInterface $user): array
  444. {
  445. $reductionGifts = $this->reductionCreditStore
  446. ->setMerchant($this->merchant)
  447. ->getByTypeAndUser(ReductionCreditModel::TYPE_GIFT, $user);
  448. $reductionGiftsArray = [];
  449. foreach ($reductionGifts as $reductionGift) {
  450. if (!$this->countValidWithReductionCredit($reductionGift, $user)) {
  451. $reductionGiftsArray[] = $reductionGift;
  452. }
  453. }
  454. return $reductionGiftsArray;
  455. }
  456. // getReductionCartRemainingQuantity
  457. public function getReductionCartRemainingQuantity(ReductionCartInterface $reductionCart): float
  458. {
  459. return $reductionCart->getAvailableQuantity() - $this->countValidWithReductionCart(
  460. $reductionCart
  461. );
  462. }
  463. // getReductionCartUsedQuantityPerUser
  464. public function getReductionCartUsedQuantityByUser(
  465. ReductionCartInterface $reductionCart,
  466. UserInterface $user
  467. ): float
  468. {
  469. return $this->countValidWithReductionCartByUser($reductionCart, $user);
  470. }
  471. // getReductionCartUsedQuantity
  472. public function getReductionCartUsedQuantity(ReductionCartInterface $reductionCart): float
  473. {
  474. return $this->countValidWithReductionCart($reductionCart);
  475. }
  476. // getReductionCartRemainingQuantityPerUser
  477. public function getReductionCartRemainingQuantityByUser(
  478. ReductionCartInterface $reductionCart,
  479. UserInterface $user
  480. ): float
  481. {
  482. if ($reductionCart->getAvailableQuantityPerUser()) {
  483. return $reductionCart->getAvailableQuantityPerUser() - $this->countValidWithReductionCartByUser(
  484. $reductionCart,
  485. $user
  486. );
  487. }
  488. return false;
  489. }
  490. // findAllAvailableForUser / getReductionCartsAvailableByUser
  491. public function getReductionCartAvailableByUser(UserInterface $user, $query = null)
  492. {
  493. $reductionCarts = $this->reductionCartStore
  494. ->setMerchant($this->merchant)
  495. ->getOnline();
  496. $reductionCartsArray = [];
  497. foreach ($reductionCarts as $reductionCart) {
  498. if ($this->reductionCartSolver->matchWithUser($reductionCart, $user)
  499. && $this->reductionCartSolver->matchWithGroupUser($reductionCart, $user)
  500. && $this->getReductionCartRemainingQuantityByUser($reductionCart, $user)
  501. && ($reductionCart->getUsers()->count() > 0 || $reductionCart->getGroupUsers()->count() > 0)
  502. && (!$this->merchant || $reductionCart->getMerchant() == $this->merchant)) {
  503. $reductionCartsArray[] = $reductionCart;
  504. }
  505. }
  506. return $reductionCartsArray;
  507. }
  508. //countValidOrderProductsOfCyclesByProducts
  509. public function countValidOrderProductsOfDistributionsByProducts(
  510. array $distributions,
  511. array $products,
  512. ProductFamilyInterface $productFamily,
  513. $query = null
  514. ): array
  515. {
  516. $query = $this->createDefaultQuery($query);
  517. $query
  518. ->filterByAlias(OrderStatusModel::$statusAliasAsValid)
  519. ->filterByDistributions($distributions)
  520. ->filterByProducts($products)
  521. ->selectSum()
  522. ->groupBy('distribution.cycleNumber, product.id');
  523. //TODO vérifier ou est utilisé cette fonction ???
  524. return $query->find();
  525. }
  526. //countValidOrderProductsOfCycleByProduct
  527. public function countValidOrderProductsOfDistributionByProduct(
  528. DistributionInterface $distribution,
  529. ProductInterface $product,
  530. $query = null
  531. ): float
  532. {
  533. //TODO attention à vérifier
  534. $query = $this->createQuery($query);
  535. $query
  536. ->filterByAlias(OrderStatusModel::$statusAliasAsValid)
  537. ->filterByDistribution($distribution)
  538. ->filterByProduct($product)
  539. ->selectSumQuantityOrder()
  540. ->joinDistribution()
  541. ->groupBy('distribution.cycleNumber, product.id');
  542. $result = $query->findOne();
  543. if ($result) {
  544. return $result['quantity'];
  545. }
  546. return 0;
  547. }
  548. public function isReductionCreditAllowAddToOrder(
  549. OrderShopInterface $orderShop,
  550. ReductionCreditInterface $reductionCredit
  551. )
  552. {
  553. $user = $orderShop->getUser();
  554. // appartient à l'utilisateur
  555. if (!$reductionCredit->getUsers()->contains($user)) {
  556. // @TODO : déplacer la gestion du flash message
  557. $this->flashBagTranslator->add('error', 'userNotAllow', 'ReductionCredit');
  558. return false;
  559. }
  560. // n'a pas été utilisé
  561. if ($reductionCredit->getType() == ReductionCreditModel::TYPE_CREDIT) {
  562. if ($this->countValidWithReductionCredit($reductionCredit, $user) > 0) {
  563. // @TODO : déplacer la gestion du flash message
  564. $this->flashBagTranslator->add('error', 'alreadyUse', 'ReductionCredit');
  565. return false;
  566. }
  567. } else {
  568. if ($this->countValidWithReductionCredit($reductionCredit) > 0) {
  569. // @TODO : déplacer la gestion du flash message
  570. $this->flashBagTranslator->add('error', 'alreadyUse', 'ReductionCredit');
  571. return false;
  572. }
  573. }
  574. return true;
  575. }
  576. public function getCartAlone($query = null)
  577. {
  578. $query = $this->createQuery($query);
  579. $query->filterByAlias(OrderStatusModel::$statusAliasAsCart);
  580. $query->filterByUserIsNull();
  581. $query->filterByVisitorIsNull();
  582. return $query->limit(10000)->find();
  583. }
  584. }