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.

480 lines
16KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Order;
  3. use App\Entity\Delivery\DeliveryAvailabilityPointSale;
  4. use App\Entity\Delivery\DeliveryAvailabilityZone;
  5. use App\Entity\Order\OrderStatus;
  6. use App\Entity\Section\Section;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
  9. use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
  10. use Lc\CaracoleBundle\Model\Order\OrderReductionCartInterface;
  11. use Lc\CaracoleBundle\Model\Order\OrderReductionCreditInterface;
  12. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  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\Resolver\Price\PriceResolver;
  20. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  21. use Lc\SovBundle\Model\User\UserInterface;
  22. use Lc\SovBundle\Repository\AbstractStore;
  23. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  24. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  25. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  26. class OrderShopStore extends AbstractStore
  27. {
  28. use SectionStoreTrait;
  29. protected OrderShopRepositoryQuery $query;
  30. protected EntityManagerInterface $entityManager;
  31. protected PriceSolver $priceSolver;
  32. protected DocumentBuilder $documentBuilder;
  33. protected ReductionCreditStore $reductionCreditStore;
  34. protected SectionStore $sectionStore;
  35. protected OrderProductStore $orderProductStore;
  36. protected MerchantStore $merchantStore;
  37. protected FlashBagInterface $flashBag;
  38. protected OpeningResolver $openingResolver;
  39. protected ParameterBagInterface $parameterBag;
  40. protected UrlGeneratorInterface $router;
  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. ) {
  55. $this->query = $query;
  56. $this->entityManager = $entityManager;
  57. $this->priceSolver = $priceSolver;
  58. $this->documentBuilder = $documentBuilder;
  59. $this->reductionCreditStore = $reductionCreditStore;
  60. $this->sectionStore = $sectionStore;
  61. $this->orderProductStore = $orderProductStore;
  62. $this->merchantStore = $merchantStore;
  63. $this->flashBag = $flashBag;
  64. $this->openingResolver = $openingResolver;
  65. $this->parameterBag = $parameterBag;
  66. $this->router = $router;
  67. }
  68. // getOrderShopsOfWeek
  69. public function getByCycle(SectionInterface $section, $params = [])
  70. {
  71. $orderShops = $this->getBy(
  72. array_merge(
  73. [
  74. 'section' => $section,
  75. 'cycleNumber' => $this->getCycleNumberCurrentOrder($section),
  76. 'isValid' => true,
  77. ],
  78. $params
  79. )
  80. );
  81. return $orderShops;
  82. }
  83. // getOrderShopsOfWeekByUser
  84. public function getByCycleAndUser(SectionInterface $section, UserInterface $user, array $params = [])
  85. {
  86. return $this->getByCycle(
  87. $section,
  88. array_merge(
  89. [
  90. 'user' => $user,
  91. 'weekNumber' => $this->getCycleNumberCurrentOrder($section),
  92. 'excludeComplementaryOrderShops' => true
  93. ],
  94. $params
  95. )
  96. );
  97. }
  98. //public $countOrderShopsOfWeek = null;
  99. public function countByCycle(SectionInterface $section, bool $excludeComplementaryOrderShops = true)
  100. {
  101. return $this->getByCycle(
  102. $section,
  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->getByCycle(
  111. $section,
  112. [
  113. 'count' => true,
  114. 'excludeComplementaryOrderShops' => $excludeComplementaryOrderShops
  115. ]
  116. );
  117. }
  118. return $this->countOrderShopsOfWeek;*/
  119. }
  120. // getNextWeekId
  121. public function getNextCycleId(SectionInterface $section, int $cycleNumber): int
  122. {
  123. $lastOrder = $this->getOneLastOrderValidOfCycle($section, $cycleNumber);
  124. if ($lastOrder) {
  125. return intval($lastOrder->getCycleId() + 1);
  126. } else {
  127. return 1;
  128. }
  129. }
  130. public function getNextIdValidOrder(Section $section)
  131. {
  132. $lastOrder = $this->getOneLastValid($section);
  133. if ($lastOrder) {
  134. return intval($lastOrder->getIdValidOrder() + 1);
  135. } else {
  136. return 1;
  137. }
  138. }
  139. // countValidOrderShopByUserAllMerchant
  140. public function countValidByUserAllMerchant($user)
  141. {
  142. $totalOrder = 0;
  143. foreach ($this->merchantStore->getRepositoryQuery()->findAll() as $merchant) {
  144. $totalOrder += $this->countValidByUser($user, $merchant);
  145. }
  146. return $totalOrder;
  147. }
  148. public function countValidByUser(UserInterface $user, MerchantInterface $merchant = null)
  149. {
  150. return $this->getBy(
  151. [
  152. 'user' => $user,
  153. 'isValid' => true,
  154. 'merchant' => $merchant,
  155. 'excludeComplementaryOrderShops' => true,
  156. 'count' => true
  157. ]
  158. );
  159. }
  160. // countValidOrderWithReductionCredit
  161. public function countValidWithReductionCredit(
  162. OrderReductionCreditInterface $reductionCredit,
  163. UserInterface $user = null
  164. ): string {
  165. $query = $this->query->create();
  166. if ($user) {
  167. $query->filterByUser($user);
  168. }
  169. $query
  170. ->selectCount()
  171. ->filterByReductionCredit($reductionCredit)
  172. ->filterByStatus(OrderStatus::$statusAliasAsValid)
  173. ->filterBySection($this->section);
  174. return $query->count();
  175. }
  176. // countValidOrderWithReductionCart
  177. public function countValidWithReductionCart(
  178. OrderReductionCartInterface $reductionCart
  179. ): int {
  180. $query = $this->query->create();
  181. $query
  182. ->selectCount()
  183. ->filterByReductionCart($reductionCart)
  184. ->filterByStatus(OrderStatus::$statusAliasAsValid)
  185. ->filterBySection($this->section);
  186. return $query->count();
  187. }
  188. // countValidOrderWithReductionCartPerUser
  189. public function countValidWithReductionCartPerUser(
  190. OrderReductionCartInterface $reductionCart,
  191. UserInterface $user
  192. ): string {
  193. $query = $this->query->create();
  194. $query
  195. ->selectCount()
  196. ->filterByUser($user)
  197. ->filterByReductionCart($reductionCart)
  198. ->filterByStatus(OrderStatus::$statusAliasAsValid)
  199. ->filterBySection($this->section);
  200. return $query->count();
  201. }
  202. // findCartCurrent
  203. public function getCartCurrent(array $params): ?OrderShopInterface
  204. {
  205. $query = $this->query->create();
  206. if (isset($params['user'])) {
  207. $query
  208. ->filterByUser($params['user']);
  209. }
  210. if (isset($params['visitor'])) {
  211. $query
  212. ->filterByVisitor($params['visitor']);
  213. }
  214. $query
  215. ->selectOrderReductionCarts()
  216. ->filterByStatus(OrderStatus::$statusAliasAsValid)
  217. ->filterBySection($this->section);
  218. $results = $query->find();
  219. if ($results) {
  220. return $results[0];
  221. }
  222. return null;
  223. }
  224. // findLastOrderValidOfWeek
  225. public function getOneLastValidOfCycle(int $cycleNumber): ?OrderShopInterface
  226. {
  227. $query = $this->query->create();
  228. $query
  229. ->filterByCycleNumber($cycleNumber)
  230. ->filterByStatus(OrderStatus::$statusAliasAsValid)
  231. ->filterIsNotMainOrderShop()
  232. ->orderBy('.weekId', 'DESC')
  233. ->filterBySection($this->section);
  234. return $query->findOne();
  235. }
  236. //findLastOrderValid
  237. public function getOneLastValid(): ?OrderShopInterface
  238. {
  239. $query = $this->query->create();
  240. $query
  241. ->filterByStatus(OrderStatus::$statusAliasAsValid)
  242. ->filterIsNotMainOrderShop()
  243. ->orderBy('.idValidOrder', 'DESC')
  244. ->filterBySection($this->section);
  245. return $query->findOne();
  246. }
  247. // @TODO : Fonction à tester
  248. // findAllBy
  249. public function getBy(array $params = [])
  250. {
  251. $query = $this->query->create();
  252. if (isset($params['section'])) {
  253. $query->filterBySection($params['section']);
  254. } else {
  255. $query->filterBySection($this->section);
  256. }
  257. if (isset($params['count']) && $params['count']) {
  258. $query->selectCount();
  259. } else {
  260. if (isset($params['select'])) {
  261. $query->selectParam($params['select']);
  262. }
  263. }
  264. if (isset($params['dateStart']) || isset($params['dateEnd'])) {
  265. $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate';
  266. }
  267. if (isset($params['dateStart'])) {
  268. $query->filterByDateStart($params['dateField'], $params['dateStart']);
  269. }
  270. if (isset($params['dateEnd'])) {
  271. $query->filterByDateEnd($params['dateField'], $params['dateEnd']);
  272. }
  273. if (isset($params['cycleNumber'])) {
  274. $query->filterByCycleNumber($params['cycleNumber']);
  275. }
  276. if (isset($params['isCart'])) {
  277. $query->filterByStatus(OrderStatus::$statusAliasAsCart);
  278. }
  279. if (isset($params['isValid'])) {
  280. $query->filterByStatus(OrderStatus::$statusAliasAsValid);
  281. }
  282. if (isset($params['isWaitingDelivery'])) {
  283. $query->filterByStatus(OrderStatus::$statusAliasWaitingDelivery);
  284. }
  285. if (isset($params['orderStatus'])) {
  286. $query->filterByStatus($params['orderStatus']);
  287. }
  288. if (isset($params['user'])) {
  289. $query->filterByUser($params['user']);
  290. }
  291. if (isset($params['address'])) {
  292. $query->filterByAddress($params['address']);
  293. }
  294. if (isset($params['weekDeliveryTrucks'])) {
  295. $query->filterByWeekDeliveryTruck($params['weekDeliveryTrucks']);
  296. }
  297. if (isset($params['estimatedDeliveryDateTime'])) {
  298. $date = clone $params['estimatedDeliveryDateTime'];
  299. $query
  300. ->filterByEstimatedDeliveryDateStart($date->format('Y-m-d 00:00:00'))
  301. ->filterByEstimatedDeliveryDateEnd($date->modify('+1 day')->format('Y-m-d 00:00:00'));
  302. }
  303. if (isset($params['deliveryDate'])) {
  304. $date = clone $params['deliveryDate'];
  305. $query
  306. ->filterByDeliveryDateStart($date->format('Y-m-d 00:00:00'))
  307. ->filterByDeliveryDateEnd($date->modify('+1 day')->format('Y-m-d 00:00:00'));
  308. }
  309. if (isset($params['mergeComplementaryOrderShops'])) {
  310. $query
  311. ->joinComplementaryOrderShops();
  312. }
  313. if (isset($params['excludeComplementaryOrderShops']) || isset($params['mergeComplementaryOrderShops'])) {
  314. $query->filterIsNullMainOrderShop();
  315. }
  316. if (isset($params['isCircuit'])) {
  317. $query->filterIsNullDeliveryPointSale();
  318. }
  319. if (isset($params['isDepository'])) {
  320. $query->filterIsNotNullDeliveryPointSale();
  321. }
  322. if (isset($params['isOffCircuit'])) {
  323. $query->filterIsPointSale('devAliasHorsTournee');
  324. }
  325. if (isset($params['isGiftVoucher'])) {
  326. $query->filterIsPointSale('devAliasGiftVoucher');
  327. }
  328. if (isset($params['deliveryAvailability'])) {
  329. $deliveryAvailability = $params['deliveryAvailability'];
  330. $deliveryAvailabilityZone = ($deliveryAvailability instanceof DeliveryAvailabilityZone) ? $deliveryAvailability : false;
  331. $deliveryAvailabilityPointSale = ($deliveryAvailability instanceof DeliveryAvailabilityPointSale) ? $deliveryAvailability : false;
  332. if ($deliveryAvailabilityZone) {
  333. $query->filterByAvailabilityPointZone($deliveryAvailabilityZone);
  334. }
  335. if ($deliveryAvailabilityPointSale) {
  336. $query->filterByAvailabilityPointZone($deliveryAvailabilityPointSale);
  337. }
  338. } else {
  339. $query->joinDeliverySlotZone();
  340. $query->joinDeliverySlotPointSale();
  341. }
  342. if (isset($params['orderBy'])) {
  343. $sort = isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC';
  344. $query->orderBy($params['orderBy'], $sort);
  345. } else {
  346. $query->orderBy('.id', 'DESC');
  347. }
  348. if (isset($params['groupBy'])) {
  349. $query->groupBy($params['groupBy']);
  350. }
  351. if (isset($params['count']) && $params['count']) {
  352. return $query->count();
  353. }
  354. return $query->find();
  355. }
  356. /*
  357. public function getCartCurrent(SectionInterface $section, UserInterface $user = null, VisitorInterface $visitor = null)
  358. {
  359. $paramsSearchOrderShop = [];
  360. $user = $this->security->getUser();
  361. $visitor = $this->userUtils->getVisitorCurrent();
  362. $orderShop = null;
  363. $orderShopUser = null;
  364. $orderShopVisitor = null;
  365. if ($user) {
  366. $orderShopUser = $this->orderShopRepo->findCartCurrent(
  367. [
  368. 'user' => $user
  369. ]
  370. );
  371. }
  372. if ($visitor) {
  373. $orderShopVisitor = $this->orderShopRepo->findCartCurrent(
  374. [
  375. 'visitor' => $visitor
  376. ]
  377. );
  378. }
  379. if ($orderShopUser || $orderShopVisitor) {
  380. // merge
  381. if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor
  382. && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())
  383. && $orderShopUser->getOrderStatus()->getAlias() == OrderStatus::ALIAS_CART) {
  384. $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
  385. $this->utils->addFlash(
  386. 'success',
  387. "Votre panier visiteur vient d'être fusionné avec votre panier client."
  388. );
  389. } else {
  390. $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
  391. }
  392. // set user
  393. if ($orderShop && $user && !$orderShop->getUser()) {
  394. $orderShop->setUser($user);
  395. $orderShop->setVisitor(null);
  396. $this->em->persist($orderShop);
  397. $this->em->flush();
  398. }
  399. }
  400. return $orderShop;
  401. }*/
  402. }