|
- <?php
-
- namespace Lc\CaracoleBundle\Repository\Order;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
- use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
- use Lc\CaracoleBundle\Model\Order\OrderReductionCartInterface;
- use Lc\CaracoleBundle\Model\Order\OrderReductionCreditInterface;
- use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
- use Lc\CaracoleBundle\Model\Order\OrderStatusModel;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCartInterface;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
- use Lc\CaracoleBundle\Model\Section\SectionInterface;
- use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
- use Lc\CaracoleBundle\Repository\Reduction\ReductionCreditStore;
- use Lc\CaracoleBundle\Repository\Section\SectionStore;
- use Lc\CaracoleBundle\Resolver\OpeningResolver;
- use Lc\CaracoleBundle\Repository\SectionStoreTrait;
- use Lc\CaracoleBundle\Solver\Order\OrderShopSolver;
- use Lc\CaracoleBundle\Solver\Price\PriceSolver;
- use Lc\SovBundle\Model\User\UserInterface;
- use Lc\SovBundle\Repository\AbstractStore;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
- use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
-
- class OrderShopStore extends AbstractStore
- {
- use SectionStoreTrait;
-
- protected OrderShopRepositoryQuery $query;
- protected EntityManagerInterface $entityManager;
- protected PriceSolver $priceSolver;
- protected DocumentBuilder $documentBuilder;
- protected ReductionCreditStore $reductionCreditStore;
- protected SectionStore $sectionStore;
- protected OrderProductStore $orderProductStore;
- protected MerchantStore $merchantStore;
- protected FlashBagInterface $flashBag;
- protected OpeningResolver $openingResolver;
- protected ParameterBagInterface $parameterBag;
- protected UrlGeneratorInterface $router;
- protected OrderShopSolver $orderShopSolver;
-
- public function __construct(
- OrderShopRepositoryQuery $query,
- EntityManagerInterface $entityManager,
- PriceSolver $priceSolver,
- DocumentBuilder $documentBuilder,
- ReductionCreditStore $reductionCreditStore,
- SectionStore $sectionStore,
- OrderProductStore $orderProductStore,
- MerchantStore $merchantStore,
- FlashBagInterface $flashBag,
- OpeningResolver $openingResolver,
- ParameterBagInterface $parameterBag,
- UrlGeneratorInterface $router,
- OrderShopSolver $orderShopSolver
- ) {
- $this->query = $query;
- $this->entityManager = $entityManager;
- $this->priceSolver = $priceSolver;
- $this->documentBuilder = $documentBuilder;
- $this->reductionCreditStore = $reductionCreditStore;
- $this->sectionStore = $sectionStore;
- $this->orderProductStore = $orderProductStore;
- $this->merchantStore = $merchantStore;
- $this->flashBag = $flashBag;
- $this->openingResolver = $openingResolver;
- $this->parameterBag = $parameterBag;
- $this->router = $router;
- $this->orderShopSolver = $orderShopSolver;
- }
-
- // getOrderShopsOfWeek
- public function getByCurrentCycle($params = [])
- {
- $orderShops = $this->getBy(
- array_merge(
- [
- 'section' => $this->section,
- 'cycleNumber' => $this->getCycleNumberCurrentOrder($this->section),
- 'isValid' => true,
- ],
- $params
- )
- );
-
- return $orderShops;
- }
-
- // getOrderShopsOfWeekByUser
- public function getByCurrentCycleAndUser(UserInterface $user, array $params = [])
- {
- return $this->getByCurrentCycle(
- array_merge(
- [
- 'user' => $user,
- 'cycleNumber' => $this->getCycleNumberCurrentOrder($this->section),
- 'excludeComplementaryOrderShops' => true
- ],
- $params
- )
- );
- }
-
- //public $countOrderShopsOfWeek = null;
-
- public function countByCurrentCycle(bool $excludeComplementaryOrderShops = true)
- {
- return $this->getByCurrentCycle(
- [
- 'count' => true,
- 'excludeComplementaryOrderShops' => $excludeComplementaryOrderShops
-
- ]
- );
-
- // @TODO : optimisation à remettre en place
- /*if (is_null($this->countOrderShopsOfWeek)) {
- $this->countOrderShopsOfWeek = $this->getByCurrentCycle(
- $section,
- [
- 'count' => true,
- 'excludeComplementaryOrderShops' => $excludeComplementaryOrderShops
-
- ]
- );
- }
- return $this->countOrderShopsOfWeek;*/
- }
-
- // getNextWeekId
- public function getNextCycleId(int $cycleNumber): int
- {
- $lastOrder = $this->getOneLastValidOfCycle($cycleNumber);
- if ($lastOrder) {
- return intval($lastOrder->getCycleId() + 1);
- } else {
- return 1;
- }
- }
-
- public function getNextIdValidOrder(): int
- {
- $lastOrder = $this->getOneLastValid();
-
- if ($lastOrder) {
- return intval($lastOrder->getIdValidOrder() + 1);
- } else {
- return 1;
- }
- }
-
- // countValidOrderShopByUserAllMerchant
- public function countValidByUserAllMerchant($user): int
- {
- return $this->countBy(
- [
- 'user' => $user,
- 'isValid' => true,
- // @TODO : à tester
- 'isMerchantOnline' => true,
- 'excludeComplementaryOrderShops' => true
- ]
- );
- }
-
- public function countValidByUser(UserInterface $user): int
- {
- return $this->countBy(
- [
- 'user' => $user,
- 'isValid' => true,
- 'section' => $this->section,
- 'excludeComplementaryOrderShops' => true
- ]
- );
- }
-
- public function countValidByCurrentCycle(): int
- {
- return $this->countBy(
- [
- 'cycleNumber' => $this->getCycleNumberCurrentOrder($this->section),
- 'isValid' => true,
- 'section' => $this->section,
- 'excludeComplementaryOrderShops' => true
- ]
- );
- }
-
- // countValidOrderWithReductionCredit
- public function countValidWithReductionCredit(
- ReductionCreditInterface $reductionCredit,
- UserInterface $user = null
- ): int {
- $query = $this->query->create();
-
- if ($user) {
- $query->filterByUser($user);
- }
- $query
- ->selectCount()
- ->filterByReductionCredit($reductionCredit)
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid)
- ->filterBySection($this->section);
-
- return $query->count();
- }
-
- // countValidOrderWithReductionCart
- public function countValidWithReductionCart(
- ReductionCartInterface $reductionCart
- ): int {
- $query = $this->query->create();
- $query
- ->selectCount()
- ->filterByReductionCart($reductionCart)
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid)
- ->filterBySection($this->section);
-
- return $query->count();
- }
-
- // countValidOrderWithReductionCartPerUser
- public function countValidWithReductionCartByUser(
- ReductionCartInterface $reductionCart,
- UserInterface $user
- ): int {
- $query = $this->query->create();
-
- $query
- ->selectCount()
- ->filterByUser($user)
- ->filterByReductionCart($reductionCart)
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid)
- ->filterBySection($this->section);
-
- return $query->count();
- }
-
- // findCartCurrent
- public function getCartCurrent(array $params): ?OrderShopInterface
- {
- $query = $this->query->create();
-
- if (isset($params['user'])) {
- $query
- ->filterByUser($params['user']);
- }
- if (isset($params['visitor'])) {
- $query
- ->filterByVisitor($params['visitor']);
- }
-
- $query
- ->selectOrderReductionCarts()
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid)
- ->filterBySection($this->section);
-
- return $query->findOne();
-
- }
-
- // findLastOrderValidOfWeek
- public function getOneLastValidByCycle(int $cycleNumber): ?OrderShopInterface
- {
- $query = $this->query->create();
-
- $query
- ->filterByCycleNumber($cycleNumber)
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid)
- ->filterIsNotMainOrderShop()
- ->orderBy('.cycleId', 'DESC')
- ->filterBySection($this->section);
-
- return $query->findOne();
- }
-
- //findLastOrderValid
- public function getOneLastValid(): ?OrderShopInterface
- {
- $query = $this->query->create();
-
- $query
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid)
- ->filterIsNotMainOrderShop()
- ->orderBy('.idValidOrder', 'DESC')
- ->filterBySection($this->section);
-
- return $query->findOne();
- }
-
- public function countBy(array $params = [])
- {
- $query = $this->query->create();
-
- $query->selectCount();
-
- $query = $this->applyGetByFilters($query);
-
- return $query->count();
- }
-
-
- public function getBy(array $params = []): array
- {
- $query = $this->query->create();
-
- $query = $this->applyGetByFilters($query);
-
- $orderShops = $query->find();
-
- if (isset($params['mergeComplementaryOrderShops'])) {
- foreach ($orderShops as $orderShop) {
- $this->orderShopSolver->mergeComplentaryOrderShops($orderShop);
- }
- }
- return $orderShops;
- }
-
- protected function applyGetByFilters($query)
- {
- if (isset($params['section'])) {
- $query->filterBySection($params['section']);
- } else {
- $query->filterBySection($this->section);
- }
-
- if (isset($params['isMerchantOnline'])) {
- $query->filterIsMerchantOnline();
- }
-
- if (isset($params['select'])) {
- $query->selectParam($params['select']);
- }
-
- if (isset($params['dateStart']) || isset($params['dateEnd'])) {
- $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate';
- }
-
- if (isset($params['dateStart'])) {
- $query->filterByDateStart($params['dateField'], $params['dateStart']);
- }
-
- if (isset($params['dateEnd'])) {
- $query->filterByDateEnd($params['dateField'], $params['dateEnd']);
- }
-
- if (isset($params['cycleNumber'])) {
- $query->filterByCycleNumber($params['cycleNumber']);
- }
-
- if (isset($params['isCart'])) {
- $query->filterByStatus(OrderStatusModel::$statusAliasAsCart);
- }
-
- if (isset($params['isValid'])) {
- $query->filterByStatus(OrderStatusModel::$statusAliasAsValid);
- }
-
- if (isset($params['isWaitingDelivery'])) {
- $query->filterByStatus(OrderStatusModel::$statusAliasWaitingDelivery);
- }
-
- if (isset($params['orderStatus'])) {
- $query->filterByStatus($params['orderStatus']);
- }
-
- if (isset($params['user'])) {
- $query->filterByUser($params['user']);
- }
-
- if (isset($params['address'])) {
- $query->filterByAddress($params['address']);
- }
-
- if (isset($params['mergeComplementaryOrderShops'])) {
- $query
- ->joinComplementaryOrderShops();
- }
-
- if (isset($params['excludeComplementaryOrderShops']) || isset($params['mergeComplementaryOrderShops'])) {
- $query->filterIsNullMainOrderShop();
- }
-
- if (isset($params['orderBy'])) {
- $sort = isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC';
- $query->orderBy($params['orderBy'], $sort);
- } else {
- $query->orderBy('.id', 'DESC');
- }
-
- if (isset($params['groupBy'])) {
- $query->groupBy($params['groupBy']);
- }
- return $query;
- }
-
-
-
- public function isReductionGiftUsed(ReductionCreditInterface $reductionGift)
- {
- if ($this->countValidWithReductionCredit($reductionGift)) {
- return true;
- } else {
- return false;
- }
- }
-
- public function isReductionCreditUsed(ReductionCreditInterface $reductionCredit, UserInterface $user = null)
- {
- if ($this->countValidWithReductionCredit($reductionCredit, $user)) {
- return true;
- } else {
- return false;
- }
- }
-
- public function isReductionCreditAddedToOrder(
- OrderShopInterface $orderShop,
- ReductionCreditInterface $reductionCredit
- ) {
- foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
- if ($orderReductionCredit->getReductionCredit() == $reductionCredit) {
- return true;
- }
- }
-
- return false;
- }
-
- /*
- public function getCartCurrent(SectionInterface $section, UserInterface $user = null, VisitorInterface $visitor = null)
- {
- $paramsSearchOrderShop = [];
-
- $user = $this->security->getUser();
- $visitor = $this->userUtils->getVisitorCurrent();
-
- $orderShop = null;
- $orderShopUser = null;
- $orderShopVisitor = null;
-
- if ($user) {
- $orderShopUser = $this->orderShopRepo->findCartCurrent(
- [
- 'user' => $user
- ]
- );
- }
-
- if ($visitor) {
- $orderShopVisitor = $this->orderShopRepo->findCartCurrent(
- [
- 'visitor' => $visitor
- ]
- );
- }
-
- if ($orderShopUser || $orderShopVisitor) {
- // merge
- if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor
- && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())
- && $orderShopUser->getOrderStatus()->getAlias() == OrderStatus::ALIAS_CART) {
- $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
- $this->utils->addFlash(
- 'success',
- "Votre panier visiteur vient d'être fusionné avec votre panier client."
- );
- } else {
- $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
- }
- // set user
- if ($orderShop && $user && !$orderShop->getUser()) {
- $orderShop->setUser($user);
- $orderShop->setVisitor(null);
- $this->em->persist($orderShop);
- $this->em->flush();
- }
- }
-
- return $orderShop;
- }*/
- }
|