Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

620 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. if(is_null($user) && is_null($visitor)) {
  259. return null;
  260. }
  261. $query = $this->createDefaultQuery($query);
  262. if (!is_null($user)) {
  263. $query->filterByUser($user);
  264. }
  265. else {
  266. if (!is_null($visitor)) {
  267. $query->filterByVisitor($visitor);
  268. }
  269. }
  270. $query
  271. ->selectOrderReductionCarts()
  272. ->filterByStatus(OrderStatusModel::$statusAliasAsCart);
  273. return $query->findOne();
  274. }
  275. // findLastOrderValidOfWeek
  276. //getOneLastValidByCycle
  277. public function getOneLastValidByDistribution(DistributionInterface $distribution, $query = null): ?OrderShopInterface
  278. {
  279. $query = $this->createDefaultQuery($query);
  280. $query
  281. ->filterByDistribution($distribution)
  282. ->filterByStatus(OrderStatusModel::$statusAliasAsValid)
  283. ->filterIsNotComplementaryOrderShop()
  284. ->orderBy('.cycleId', 'DESC');
  285. return $query->findOne();
  286. }
  287. //findLastOrderValid
  288. public function getOneLastValid($query = null): ?OrderShopInterface
  289. {
  290. $query = $this->createDefaultQuery($query);
  291. $query
  292. ->filterByStatus(OrderStatusModel::$statusAliasAsValid)
  293. ->filterIsNotComplementaryOrderShop()
  294. ->orderBy('.idValidOrder', 'DESC');
  295. return $query->findOne();
  296. }
  297. public function countBy(array $params = [], $query = null)
  298. {
  299. $query = $this->createDefaultQuery($query);
  300. $query->selectCount();
  301. $this->applyGetByFilters($params, $query);
  302. return $query->count();
  303. }
  304. public function getBy(array $params = [], $query = null): array
  305. {
  306. $query = $this->createDefaultQuery($query);
  307. $this->applyGetByFilters($params, $query);
  308. $orderShops = $query->find();
  309. if (isset($params['mergeComplementaryOrderShops'])) {
  310. foreach ($orderShops as $orderShop) {
  311. $this->orderShopSolver->mergeComplentaryOrderShops($orderShop);
  312. }
  313. }
  314. return $orderShops;
  315. }
  316. protected function applyGetByFilters(array $params, $query)
  317. {
  318. if (isset($params['isMerchantOnline'])) {
  319. $query->filterIsMerchantOnline();
  320. }
  321. if (isset($params['select'])) {
  322. $query->selectParam($params['select']);
  323. }
  324. if (isset($params['dateStart']) || isset($params['dateEnd'])) {
  325. $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate';
  326. }
  327. if (isset($params['dateStart'])) {
  328. $query->filterByDateStart($params['dateField'], $params['dateStart']);
  329. }
  330. if (isset($params['dateEnd'])) {
  331. $query->filterByDateEnd($params['dateField'], $params['dateEnd']);
  332. }
  333. if (isset($params['distribution'])) {
  334. $query->filterByDistribution($params['distribution']);
  335. }
  336. if (isset($params['isCart'])) {
  337. $query->filterByStatus(OrderStatusModel::$statusAliasAsCart);
  338. }
  339. if (isset($params['isValid'])) {
  340. $query->filterByStatus(OrderStatusModel::$statusAliasAsValid);
  341. }
  342. if (isset($params['isWaitingDelivery'])) {
  343. $query->filterByStatus(OrderStatusModel::$statusAliasWaitingDelivery);
  344. }
  345. if (isset($params['orderStatus'])) {
  346. $query->filterByStatus([$params['orderStatus']]);
  347. }
  348. if (isset($params['user'])) {
  349. $query->filterByUser($params['user']);
  350. }
  351. if (isset($params['address'])) {
  352. $query->filterByAddress($params['address']);
  353. }
  354. if(isset($params['minimumTomorrowDelivery'])) {
  355. $query->filterMinimumTomorrowDelivery();
  356. }
  357. if (isset($params['mergeComplementaryOrderShops'])) {
  358. $query
  359. ->joinComplementaryOrderShops();
  360. }
  361. if (isset($params['excludeComplementaryOrderShops']) || isset($params['mergeComplementaryOrderShops'])) {
  362. $query->filterIsNullMainOrderShop();
  363. }
  364. if (isset($params['joinProductFamily'])) {
  365. $query->joinProductFamily(true);
  366. }
  367. if (isset($params['orderBy'])) {
  368. $sort = isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC';
  369. $query->orderBy($params['orderBy'], $sort);
  370. } else {
  371. $query->orderBy('.id', 'DESC');
  372. }
  373. if (isset($params['groupBy'])) {
  374. $query->groupBy($params['groupBy']);
  375. }
  376. return $query;
  377. }
  378. public function isReductionGiftUsed(ReductionCreditInterface $reductionGift, $query = null)
  379. {
  380. if ($this->countValidWithReductionCredit($reductionGift, null, $query)) {
  381. return true;
  382. } else {
  383. return false;
  384. }
  385. }
  386. public function isReductionCreditUsed(
  387. ReductionCreditInterface $reductionCredit,
  388. UserInterface $user = null,
  389. $query = null
  390. ) {
  391. if ($this->countValidWithReductionCredit($reductionCredit, $user, $query)) {
  392. return true;
  393. } else {
  394. return false;
  395. }
  396. }
  397. public function getReductionCreditsAvailableByUser(UserInterface $user): array
  398. {
  399. $reductionCredits = $this->reductionCreditStore->getByTypeAndUser(ReductionCreditModel::TYPE_CREDIT, $user);
  400. $reductionCreditsArray = [];
  401. foreach ($reductionCredits as $reductionCredit) {
  402. if (!$this->countValidWithReductionCredit($reductionCredit, $user)
  403. && ($reductionCredit->getSection()->getMerchant() == $this->merchant)) {
  404. $reductionCreditsArray[] = $reductionCredit;
  405. }
  406. }
  407. return $reductionCreditsArray;
  408. }
  409. public function getReductionGiftsAvailableByUser($user): array
  410. {
  411. $reductionGifts = $this->reductionCreditStore->getByTypeAndUser(ReductionCreditModel::TYPE_GIFT, $user);
  412. $reductionGiftsArray = [];
  413. foreach ($reductionGifts as $reductionGift) {
  414. if (!$this->countValidWithReductionCredit($reductionGift)) {
  415. $reductionGiftsArray[] = $reductionGift;
  416. }
  417. }
  418. return $reductionGiftsArray;
  419. }
  420. // getReductionCartRemainingQuantity
  421. public function getReductionCartRemainingQuantity(ReductionCartInterface $reductionCart): float
  422. {
  423. return $reductionCart->getAvailableQuantity() - $this->countValidWithReductionCart(
  424. $reductionCart
  425. );
  426. }
  427. // getReductionCartUsedQuantityPerUser
  428. public function getReductionCartUsedQuantityByUser(
  429. ReductionCartInterface $reductionCart,
  430. UserInterface $user
  431. ): float {
  432. return $this->countValidWithReductionCartByUser($reductionCart, $user);
  433. }
  434. // getReductionCartUsedQuantity
  435. public function getReductionCartUsedQuantity(ReductionCartInterface $reductionCart): float
  436. {
  437. return $this->countValidWithReductionCart($reductionCart);
  438. }
  439. // getReductionCartRemainingQuantityPerUser
  440. public function getReductionCartRemainingQuantityByUser(
  441. ReductionCartInterface $reductionCart,
  442. UserInterface $user
  443. ): float {
  444. if ($reductionCart->getAvailableQuantityPerUser()) {
  445. return $reductionCart->getAvailableQuantityPerUser() - $this->countValidWithReductionCartByUser(
  446. $reductionCart,
  447. $user
  448. );
  449. }
  450. return false;
  451. }
  452. // findAllAvailableForUser / getReductionCartsAvailableByUser
  453. public function getReductionCartAvailableByUser(UserInterface $user, $query = null)
  454. {
  455. $reductionCarts = $this->reductionCartStore->getOnline();
  456. $reductionCartsArray = [];
  457. foreach ($reductionCarts as $reductionCart) {
  458. if ($this->reductionCartSolver->matchWithUser($reductionCart, $user)
  459. && $this->reductionCartSolver->matchWithGroupUser($reductionCart, $user)
  460. && $this->getReductionCartRemainingQuantityByUser($reductionCart, $user)
  461. && ($reductionCart->getUsers()->count() > 0 || $reductionCart->getGroupUsers()->count() > 0)
  462. && (!$this->merchant || $reductionCart->getSection()->getMerchant() == $this->merchant)) {
  463. $reductionCartsArray[] = $reductionCart;
  464. }
  465. }
  466. return $reductionCartsArray;
  467. }
  468. //countValidOrderProductsOfCyclesByProducts
  469. public function countValidOrderProductsOfDistributionsByProducts(
  470. array $distributions,
  471. array $products,
  472. $query = null
  473. ): array {
  474. $query = $this->createDefaultQuery($query);
  475. $query
  476. ->filterByAlias(OrderStatusModel::$statusAliasAsValid)
  477. ->filterByDistributions($distributions)
  478. ->filterByProducts($products)
  479. ->selectSum()
  480. ->groupBy('distribution.cycleNumber, product.id');
  481. return $query->find();
  482. }
  483. //countValidOrderProductsOfCycleByProduct
  484. public function countValidOrderProductsOfDistributionByProduct(DistributionInterface $distribution, int $productId, $query = null): ?string
  485. {
  486. $query = $this->createDefaultQuery($query);
  487. $query
  488. ->filterByAlias(OrderStatusModel::$statusAliasAsValid)
  489. ->filterByDistribution($distribution)
  490. ->filterByProduct($productId)
  491. ->selectSumQuantityOrder()
  492. ->groupBy('distribution.cycleNumber, product.id');
  493. $result = $query->findOne();
  494. if ($result) {
  495. return $result['quantity'];
  496. }
  497. return null;
  498. }
  499. public function isReductionCreditAllowAddToOrder(
  500. OrderShopInterface $orderShop,
  501. ReductionCreditInterface $reductionCredit
  502. ) {
  503. $user = $orderShop->getUser();
  504. // appartient à l'utilisateur
  505. if (!$reductionCredit->getUsers()->contains($user)) {
  506. // @TODO : déplacer la gestion du flash message
  507. //$this->flashBag->add('error', 'error.reductionCredit.userNotAllow');
  508. return false;
  509. }
  510. // n'a pas été utilisé
  511. if ($reductionCredit->getType() == ReductionCreditModel::TYPE_CREDIT) {
  512. if ($this->countValidWithReductionCredit($reductionCredit, $user) > 0) {
  513. // @TODO : déplacer la gestion du flash message
  514. //$this->flashBah->add('error', 'error.reductionCredit.alreadyUse');
  515. return false;
  516. }
  517. } else {
  518. if ($this->countValidWithReductionCredit($reductionCredit) > 0) {
  519. // @TODO : déplacer la gestion du flash message
  520. //$this->flashBah->add('error', 'error.reductionCredit.alreadyUse');
  521. return false;
  522. }
  523. }
  524. return true;
  525. }
  526. }