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.

612 line
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['mergeComplementaryOrderShops'])) {
  352. $query
  353. ->joinComplementaryOrderShops();
  354. }
  355. if (isset($params['excludeComplementaryOrderShops']) || isset($params['mergeComplementaryOrderShops'])) {
  356. $query->filterIsNullMainOrderShop();
  357. }
  358. if (isset($params['joinProductFamily'])) {
  359. $query->joinProductFamily(true);
  360. }
  361. if (isset($params['orderBy'])) {
  362. $sort = isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC';
  363. $query->orderBy($params['orderBy'], $sort);
  364. } else {
  365. $query->orderBy('.id', 'DESC');
  366. }
  367. if (isset($params['groupBy'])) {
  368. $query->groupBy($params['groupBy']);
  369. }
  370. return $query;
  371. }
  372. public function isReductionGiftUsed(ReductionCreditInterface $reductionGift, $query = null)
  373. {
  374. if ($this->countValidWithReductionCredit($reductionGift, null, $query)) {
  375. return true;
  376. } else {
  377. return false;
  378. }
  379. }
  380. public function isReductionCreditUsed(
  381. ReductionCreditInterface $reductionCredit,
  382. UserInterface $user = null,
  383. $query = null
  384. ) {
  385. if ($this->countValidWithReductionCredit($reductionCredit, $user, $query)) {
  386. return true;
  387. } else {
  388. return false;
  389. }
  390. }
  391. public function getReductionCreditsAvailableByUser(UserInterface $user): array
  392. {
  393. $reductionCredits = $this->reductionCreditStore->getByTypeAndUser(ReductionCreditModel::TYPE_CREDIT, $user);
  394. $reductionCreditsArray = [];
  395. foreach ($reductionCredits as $reductionCredit) {
  396. if (!$this->countValidWithReductionCredit($reductionCredit, $user)
  397. && ($reductionCredit->getSection()->getMerchant() == $this->merchant)) {
  398. $reductionCreditsArray[] = $reductionCredit;
  399. }
  400. }
  401. return $reductionCreditsArray;
  402. }
  403. public function getReductionGiftsAvailableByUser($user): array
  404. {
  405. $reductionGifts = $this->reductionCreditStore->getByTypeAndUser(ReductionCreditModel::TYPE_GIFT, $user);
  406. $reductionGiftsArray = [];
  407. foreach ($reductionGifts as $reductionGift) {
  408. if (!$this->countValidWithReductionCredit($reductionGift)) {
  409. $reductionGiftsArray[] = $reductionGift;
  410. }
  411. }
  412. return $reductionGiftsArray;
  413. }
  414. // getReductionCartRemainingQuantity
  415. public function getReductionCartRemainingQuantity(ReductionCartInterface $reductionCart): float
  416. {
  417. return $reductionCart->getAvailableQuantity() - $this->countValidWithReductionCart(
  418. $reductionCart
  419. );
  420. }
  421. // getReductionCartUsedQuantityPerUser
  422. public function getReductionCartUsedQuantityByUser(
  423. ReductionCartInterface $reductionCart,
  424. UserInterface $user
  425. ): float {
  426. return $this->countValidWithReductionCartByUser($reductionCart, $user);
  427. }
  428. // getReductionCartUsedQuantity
  429. public function getReductionCartUsedQuantity(ReductionCartInterface $reductionCart): float
  430. {
  431. return $this->countValidWithReductionCart($reductionCart);
  432. }
  433. // getReductionCartRemainingQuantityPerUser
  434. public function getReductionCartRemainingQuantityByUser(
  435. ReductionCartInterface $reductionCart,
  436. UserInterface $user
  437. ): float {
  438. if ($reductionCart->getAvailableQuantityPerUser()) {
  439. return $reductionCart->getAvailableQuantityPerUser() - $this->countValidWithReductionCartByUser(
  440. $reductionCart,
  441. $user
  442. );
  443. }
  444. return false;
  445. }
  446. // findAllAvailableForUser / getReductionCartsAvailableByUser
  447. public function getReductionCartAvailableByUser(UserInterface $user, $query = null)
  448. {
  449. $reductionCarts = $this->reductionCartStore->getOnline();
  450. $reductionCartsArray = [];
  451. foreach ($reductionCarts as $reductionCart) {
  452. if ($this->reductionCartSolver->matchWithUser($reductionCart, $user)
  453. && $this->reductionCartSolver->matchWithGroupUser($reductionCart, $user)
  454. && $this->getReductionCartRemainingQuantityByUser($reductionCart, $user)
  455. && ($reductionCart->getUsers()->count() > 0 || $reductionCart->getGroupUsers()->count() > 0)
  456. && (!$this->merchant || $reductionCart->getSection()->getMerchant() == $this->merchant)) {
  457. $reductionCartsArray[] = $reductionCart;
  458. }
  459. }
  460. return $reductionCartsArray;
  461. }
  462. //countValidOrderProductsOfCyclesByProducts
  463. public function countValidOrderProductsOfDistributionsByProducts(
  464. array $distributions,
  465. array $products,
  466. $query = null
  467. ): array {
  468. $query = $this->createDefaultQuery($query);
  469. $query
  470. ->filterByAlias(OrderStatusModel::$statusAliasAsValid)
  471. ->filterByDistributions($distributions)
  472. ->filterByProducts($products)
  473. ->selectSum()
  474. ->groupBy('distribution.cycleNumber, product.id');
  475. return $query->find();
  476. }
  477. //countValidOrderProductsOfCycleByProduct
  478. public function countValidOrderProductsOfDistributionByProduct(DistributionInterface $distribution, int $productId, $query = null): ?string
  479. {
  480. $query = $this->createDefaultQuery($query);
  481. $query
  482. ->filterByAlias(OrderStatusModel::$statusAliasAsValid)
  483. ->filterByDistribution($distribution)
  484. ->filterByProduct($productId)
  485. ->selectSumQuantityOrder()
  486. ->groupBy('distribution.cycleNumber, product.id');
  487. $result = $query->findOne();
  488. if ($result) {
  489. return $result['quantity'];
  490. }
  491. return null;
  492. }
  493. public function isReductionCreditAllowAddToOrder(
  494. OrderShopInterface $orderShop,
  495. ReductionCreditInterface $reductionCredit
  496. ) {
  497. $user = $orderShop->getUser();
  498. // appartient à l'utilisateur
  499. if (!$reductionCredit->getUsers()->contains($user)) {
  500. // @TODO : déplacer la gestion du flash message
  501. //$this->flashBag->add('error', 'error.reductionCredit.userNotAllow');
  502. return false;
  503. }
  504. // n'a pas été utilisé
  505. if ($reductionCredit->getType() == ReductionCreditModel::TYPE_CREDIT) {
  506. if ($this->countValidWithReductionCredit($reductionCredit, $user) > 0) {
  507. // @TODO : déplacer la gestion du flash message
  508. //$this->flashBah->add('error', 'error.reductionCredit.alreadyUse');
  509. return false;
  510. }
  511. } else {
  512. if ($this->countValidWithReductionCredit($reductionCredit) > 0) {
  513. // @TODO : déplacer la gestion du flash message
  514. //$this->flashBah->add('error', 'error.reductionCredit.alreadyUse');
  515. return false;
  516. }
  517. }
  518. return true;
  519. }
  520. }