|
- <?php
-
- namespace Lc\CaracoleBundle\Repository\Order;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
- 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\Reduction\ReductionCreditModel;
- 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\CaracoleBundle\Solver\Reduction\ReductionCartSolver;
- use Lc\SovBundle\Model\User\UserInterface;
- use Lc\SovBundle\Repository\AbstractStore;
- use Lc\SovBundle\Repository\RepositoryQueryInterface;
- 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 ReductionCartSolver $reductionCartSolver;
- 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,
- ReductionCartSolver $reductionCartSolver,
- SectionStore $sectionStore,
- OrderProductStore $orderProductStore,
- MerchantStore $merchantStore,
- FlashBagInterface $flashBag,
- ParameterBagInterface $parameterBag,
- UrlGeneratorInterface $router,
- OrderShopSolver $orderShopSolver
- ) {
- $this->query = $query;
- $this->entityManager = $entityManager;
- $this->priceSolver = $priceSolver;
- $this->documentBuilder = $documentBuilder;
- $this->reductionCreditStore = $reductionCreditStore;
- $this->reductionCartSolver = $reductionCartSolver;
- $this->sectionStore = $sectionStore;
- $this->orderProductStore = $orderProductStore;
- $this->merchantStore = $merchantStore;
- $this->flashBag = $flashBag;
- $this->parameterBag = $parameterBag;
- $this->router = $router;
- $this->orderShopSolver = $orderShopSolver;
- }
-
- public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
- {
- $query->orderBy('id');
- return $query;
- }
-
- public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
- {
- $query->filterBySection($this->section);
- return $query;
- }
-
- public function relationsDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
- {
- return $query;
- }
-
- // getOrderShopsOfWeek
- public function getByCurrentCycle($params = [], $query = null)
- {
- return $this->getBy(
- array_merge(
- [
- 'cycleNumber' => $this->orderShopSolver->getCycleNumberCurrentOrder($this->section),
- 'isValid' => true,
- ],
- $params
- ),
- $query
- );
- }
-
- // getOrderShopsOfWeekByUser
- public function getByCurrentCycleAndUser(UserInterface $user, array $params = [], $query = null)
- {
- return $this->getByCurrentCycle(
- array_merge(
- [
- 'user' => $user,
- 'cycleNumber' => $this->orderShopSolver->getCycleNumberCurrentOrder($this->section),
- 'excludeComplementaryOrderShops' => true
- ],
- $params
- ),
- $query
- );
- }
-
- //public $countOrderShopsOfWeek = null;
- // public function countByCurrentCycle(bool $excludeComplementaryOrderShops = true, $query = null)
- public function countByCurrentCycle(array $params, $query = null)
- {
- return $this->countBy(
- [
- 'cycleNumber' => $this->orderShopSolver->getCycleNumberCurrentOrder($this->section),
- 'excludeComplementaryOrderShops' => isset($params['excludeComplementaryOrderShops']) ?? true,
- ],
- $query
- );
-
- // @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, $query = null): int
- {
- $lastOrder = $this->getOneLastValidByCycle($cycleNumber, $query);
- if ($lastOrder) {
- return intval($lastOrder->getCycleId() + 1);
- } else {
- return 1;
- }
- }
-
- public function getNextIdValidOrder($query = null): int
- {
- $lastOrder = $this->getOneLastValid($query);
-
- if ($lastOrder) {
- return intval($lastOrder->getIdValidOrder() + 1);
- } else {
- return 1;
- }
- }
-
- // countValidOrderShopByUserAllMerchant
- public function countValidByUserAllMerchant($user, $query = null): int
- {
- return $this->countBy(
- [
- 'user' => $user,
- 'isValid' => true,
- // @TODO : à tester
- 'isMerchantOnline' => true,
- 'excludeComplementaryOrderShops' => true
- ],
- $query
- );
- }
-
- public function countValidByUser(UserInterface $user, $query = null): int
- {
- return $this->countBy(
- [
- 'user' => $user,
- 'isValid' => true,
- 'excludeComplementaryOrderShops' => true
- ],
- $query
- );
- }
-
- public function countValidByCurrentCycle($query = null): int
- {
- return $this->countBy(
- [
- 'cycleNumber' => $this->orderShopSolver->getCycleNumberCurrentOrder($this->section),
- 'isValid' => true,
- 'excludeComplementaryOrderShops' => true
- ],
- $query
- );
- }
-
- // countValidOrderWithReductionCredit
- public function countValidWithReductionCredit(
- ReductionCreditInterface $reductionCredit,
- UserInterface $user = null,
- $query = null
- ): int {
- $query = $this->createDefaultQuery($query);
-
- if ($user) {
- $query->filterByUser($user);
- }
-
- $query
- ->selectCount()
- ->filterByReductionCredit($reductionCredit)
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid);
-
- return $query->count();
- }
-
- // countValidOrderWithReductionCart
- public function countValidWithReductionCart(
- ReductionCartInterface $reductionCart,
- $query = null
- ): int {
- $query = $this->createDefaultQuery($query);
-
- $query
- ->selectCount()
- ->filterByReductionCart($reductionCart)
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid);
-
- return $query->count();
- }
-
- // countValidOrderWithReductionCartPerUser
- public function countValidWithReductionCartByUser(
- ReductionCartInterface $reductionCart,
- UserInterface $user,
- $query = null
- ): int {
- $query = $this->createDefaultQuery($query);
-
- $query
- ->selectCount()
- ->filterByUser($user)
- ->filterByReductionCart($reductionCart)
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid);
-
- return $query->count();
- }
-
- // findCartCurrent
- public function getOneCartCurrent(array $params = [], $query = null): ?OrderShopInterface
- {
- $query = $this->createDefaultQuery($query);
-
- if (isset($params['user'])) {
- $query->filterByUser($params['user']);
- }
-
- if (isset($params['visitor'])) {
- $query->filterByVisitor($params['visitor']);
- }
-
- $query
- ->selectOrderReductionCarts()
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid);
-
- return $query->findOne();
- }
-
- // findLastOrderValidOfWeek
- public function getOneLastValidByCycle(int $cycleNumber, $query = null): ?OrderShopInterface
- {
- $query = $this->createDefaultQuery($query);
-
- $query
- ->filterByCycleNumber($cycleNumber)
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid)
- ->filterIsNotComplementaryOrderShop()
- ->orderBy('.cycleId', 'DESC');
-
- return $query->findOne();
- }
-
- //findLastOrderValid
- public function getOneLastValid($query = null): ?OrderShopInterface
- {
- $query = $this->createDefaultQuery($query);
-
- $query
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid)
- ->filterIsNotComplementaryOrderShop()
- ->orderBy('.idValidOrder', 'DESC');
-
- return $query->findOne();
- }
-
- public function countBy(array $params = [], $query = null)
- {
- $query = $this->createDefaultQuery($query);
- $query->selectCount();
- $this->applyGetByFilters($query);
- return $query->count();
- }
-
-
- public function getBy(array $params = [], $query = null): array
- {
- $query = $this->createDefaultQuery($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['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, $query = null)
- {
- if ($this->countValidWithReductionCredit($reductionGift, null, $query)) {
- return true;
- } else {
- return false;
- }
- }
-
- public function isReductionCreditUsed(
- ReductionCreditInterface $reductionCredit,
- UserInterface $user = null,
- $query = null
- ) {
- if ($this->countValidWithReductionCredit($reductionCredit, $user, $query)) {
- return true;
- } else {
- return false;
- }
- }
-
- public function getReductionCreditsAvailableByUser(UserInterface $user): array
- {
- $reductionCredits = $this->reductionCreditStore->getByTypeAndUser(ReductionCreditModel::TYPE_CREDIT, $user);
-
- $reductionCreditsArray = [];
- foreach ($reductionCredits as $reductionCredit) {
- if (!$this->countValidWithReductionCredit($reductionCredit, $user)) {
- $reductionCreditsArray[] = $reductionCredit;
- }
- }
-
- return $reductionCreditsArray;
- }
-
- public function getReductionGiftsAvailableByUser($user): array
- {
- $reductionGifts = $this->reductionCreditStore->getByTypeAndUser(ReductionCreditModel::TYPE_GIFT, $user);
-
- $reductionGiftsArray = [];
- foreach ($reductionGifts as $reductionGift) {
- if (!$this->countValidWithReductionCredit($reductionGift)) {
- $reductionGiftsArray[] = $reductionGift;
- }
- }
-
- return $reductionGiftsArray;
- }
-
- // getReductionCartRemainingQuantity
- public function getReductionCartRemainingQuantity(ReductionCartInterface $reductionCart): float
- {
- return $reductionCart->getAvailableQuantity() - $this->countValidWithReductionCart(
- $reductionCart
- );
- }
-
- // getReductionCartUsedQuantityPerUser
- public function getReductionCartUsedQuantityByUser(
- ReductionCartInterface $reductionCart,
- UserInterface $user
- ): float {
- return $this->countValidWithReductionCartByUser($reductionCart, $user);
- }
-
- // getReductionCartUsedQuantity
- public function getReductionCartUsedQuantity(ReductionCartInterface $reductionCart): float
- {
- return $this->countValidWithReductionCart($reductionCart);
- }
-
- // getReductionCartRemainingQuantityPerUser
- public function getReductionCartRemainingQuantityByUser(
- ReductionCartInterface $reductionCart,
- UserInterface $user
- ): float {
- if ($reductionCart->getAvailableQuantityPerUser()) {
- return $reductionCart->getAvailableQuantityPerUser() - $this->countValidWithReductionCartByUser(
- $reductionCart,
- $user
- );
- }
-
- return false;
- }
-
- // findAllAvailableForUser / getReductionCartsAvailableByUser
- public function getReductionCartAvailableByUser(UserInterface $user, $query = null)
- {
- $query = $this->createQuery($query);
- $reductionCarts = $query->find();
-
- $reductionCartsArray = [];
- foreach ($reductionCarts as $reductionCart) {
- if ($this->reductionCartSolver->matchWithUser($user)
- && $this->reductionCartSolver->matchWithGroupUser($user)
- && $this->getRemainingQuantityByUser($reductionCart, $user)
- && ($reductionCart->getUsers()->count() > 0 || $reductionCart->getGroupUsers()->count() > 0)) {
- $reductionCartsArray[] = $reductionCart;
- }
- }
-
- return $reductionCartsArray;
- }
-
- public function countValidOrderProductsOfCyclesByProducts(
- array $cycleNumbers,
- array $products,
- $query = null
- ): array {
- $query = $this->createDefaultQuery($query);
-
- $query
- ->filterByAlias(OrderStatusModel::$statusAliasAsValid)
- ->filterByCycleNumbers($cycleNumbers)
- ->filterByProducts($products)
- ->selectSum()
- ->groupBy('.cycleNumber, product.id');
-
-
- return $query->find();
- }
-
- public function countValidOrderProductsOfCycleByProduct(int $cycleNumber, int $productId, $query = null): ?string
- {
- $query = $this->createDefaultQuery($query);
-
- $query
- ->filterByAlias(OrderStatusModel::$statusAliasAsValid)
- ->filterByCycleNumber($cycleNumber)
- ->filterByProduct($productId)
- ->selectSumQuantityOrder()
- ->groupBy('.cycleNumber, product.id');
-
- $result = $query->findOne();
-
- if ($result) {
- return $result['quantity'];
- }
- return null;
- }
-
- public function isReductionCreditAllowAddToOrder(
- OrderShopInterface $orderShop,
- ReductionCreditInterface $reductionCredit
- ) {
- $user = $orderShop->getUser();
-
- // appartient à l'utilisateur
- if (!$reductionCredit->getUsers()->contains($user)) {
- // @TODO : déplacer la gestion du flash message
- //$this->flashBag->add('error', 'error.reductionCredit.userNotAllow');
- return false;
- }
-
- // n'a pas été utilisé
- if ($reductionCredit->getType() == ReductionCreditModel::TYPE_CREDIT) {
- if ($this->countValidWithReductionCredit($reductionCredit, $user) > 0) {
- // @TODO : déplacer la gestion du flash message
- //$this->flashBah->add('error', 'error.reductionCredit.alreadyUse');
- return false;
- }
- } else {
- if ($this->countValidWithReductionCredit($reductionCredit) > 0) {
- // @TODO : déplacer la gestion du flash message
- //$this->flashBah->add('error', 'error.reductionCredit.alreadyUse');
- return false;
- }
- }
-
- return true;
- }
- }
|