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.

616 lines
21KB

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