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.

488 lines
15KB

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