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.

575 lines
19KB

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