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.

517 lines
17KB

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