|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739 |
- <?php
-
- namespace Lc\CaracoleBundle\Repository\Order;
-
- use App\Builder\Distribution\DistributionBuilder;
- use App\Entity\Distribution\Distribution;
- use App\Entity\User\User;
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
- use Lc\CaracoleBundle\Model\Distribution\DistributionInterface;
- use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
- use Lc\CaracoleBundle\Model\Order\OrderStatusModel;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
- use Lc\CaracoleBundle\Model\Product\ProductInterface;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCartInterface;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCreditModel;
- use Lc\CaracoleBundle\Model\User\VisitorInterface;
- use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
- use Lc\CaracoleBundle\Repository\MerchantStoreTrait;
- use Lc\CaracoleBundle\Repository\Reduction\ReductionCartStore;
- 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\CaracoleBundle\Repository\AbstractStore;
- use Lc\SovBundle\Repository\RepositoryQueryInterface;
- use Lc\SovBundle\Solver\Setting\SettingSolver;
- use Lc\SovBundle\Translation\FlashBagTranslator;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
-
- class OrderShopStore extends AbstractStore
- {
- use SectionStoreTrait;
- use MerchantStoreTrait;
-
- 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 FlashBagTranslator $flashBagTranslator;
- protected OpeningResolver $openingResolver;
- protected ParameterBagInterface $parameterBag;
- protected UrlGeneratorInterface $router;
- protected OrderShopSolver $orderShopSolver;
- protected ReductionCartStore $reductionCartStore;
- protected DistributionBuilder $distributionBuilder;
- protected SettingSolver $settingSolver;
-
- public function __construct(
- OrderShopRepositoryQuery $query,
- EntityManagerInterface $entityManager,
- PriceSolver $priceSolver,
- DocumentBuilder $documentBuilder,
- ReductionCreditStore $reductionCreditStore,
- ReductionCartSolver $reductionCartSolver,
- SectionStore $sectionStore,
- OrderProductStore $orderProductStore,
- MerchantStore $merchantStore,
- FlashBagTranslator $flashBagTranslator,
- ParameterBagInterface $parameterBag,
- UrlGeneratorInterface $router,
- OrderShopSolver $orderShopSolver,
- ReductionCartStore $reductionCartStore,
- DistributionBuilder $distributionBuilder,
- SettingSolver $settingSolver
- )
- {
- $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->flashBagTranslator = $flashBagTranslator;
- $this->parameterBag = $parameterBag;
- $this->router = $router;
- $this->orderShopSolver = $orderShopSolver;
- $this->reductionCartStore = $reductionCartStore;
- $this->distributionBuilder = $distributionBuilder;
- $this->settingSolver = $settingSolver;
- }
-
- public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
- {
- //$query->orderBy('id', 'DESC');
- return $query;
- }
-
- public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
- {
- $this
- ->addFilterBySectionOptionnal($query)
- ->addFilterByMerchantViaSectionOptionnal($query);
-
- return $query;
- }
-
- public function relationsDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
- {
- $query->joinOrderProducts(true);
- return $query;
- }
-
- // getOrderShopsOfWeek
- //getByCurrentCycle
- public function getByCurrentDistribution($params = [], $query = null)
- {
- return $this->getBy(
- array_merge(
- [
- 'distribution' => $this->distributionBuilder->guessCurrentDistributionOrder(
- $this->section
- ),
- 'isValid' => true,
- ],
- $params
- ),
- $query
- );
- }
-
-
- //public $countOrderShopsOfWeek = null;
- // public function countByCurrentCycle(bool $excludeComplementaryOrderShops = true, $query = null)
- //countByCurrentCycle
- public function countByCurrentDistribution(array $params, $query = null)
- {
- return $this->countBy(
- array_merge(
- [
- 'distribution' => $this->distributionBuilder->guessCurrentDistributionOrder(
- $this->section
- ),
- 'excludeComplementaryOrderShops' => isset($params['excludeComplementaryOrderShops']) ?? true,
- ],
- $params
- ),
- $query
- );
-
- // @TODO : optimisation à remettre en place
- /*if (is_null($this->countOrderShopsOfWeek)) {
- $this->countOrderShopsOfWeek = $this->getByCurrentCycle(
- $section,
- [
- 'count' => true,
- 'excludeComplementaryOrderShops' => $excludeComplementaryOrderShops
-
- ]
- );
- }
- return $this->countOrderShopsOfWeek;*/
- }
-
- public function getByCurrentDistributionAndUser(UserInterface $user = null, array $params = [], $query = null)
- {
- return $this->getByCurrentDistribution(
- array_merge(
- [
- 'user' => $user,
- 'excludeComplementaryOrderShops' => true
- ],
- $params
- ),
- $query
- );
- }
-
- public function countValidByCurrentDistributionAndUser(UserInterface $user = null, array $params = [], $query = null)
- {
- return $this->countByCurrentDistribution(
- array_merge(
- [
- 'isValid' => true,
- 'user' => $user,
- 'excludeComplementaryOrderShops' => true
- ],
- $params
- ),
- $query
- );
- }
-
- // getNextWeekId
- public function getNextCycleId(Distribution $distribution, $query = null): int
- {
- $lastOrder = $this->getOneLastValidByDistribution($distribution, $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;
- }
- }
-
- public function countValidByUserAllMerchant($user, $query = null): int
- {
- $this->resetContext();
- return $this->countBy(
- [
- 'user' => $user,
- 'isValid' => true,
- 'excludeComplementaryOrderShops' => true
- ],
- $query
- );
- }
-
- public function countValidByUser(UserInterface $user = null, $query = null): int
- {
- return $this->countBy(
- [
- 'user' => $user,
- 'isValid' => true,
- 'excludeComplementaryOrderShops' => true
- ],
- $query
- );
- }
-
- //countValidByCurrentCycle
- public function countValidByCurrentDistribution($query = null): int
- {
- return $this->countBy(
- [
- 'distribution' => $this->distributionBuilder->guessCurrentDistributionOrder($this->section),
- 'isValid' => true,
- 'excludeComplementaryOrderShops' => true
- ],
- $query
- );
- }
-
- // countValidOrderWithReductionCredit
- public function countValidWithReductionCredit(
- ReductionCreditInterface $reductionCredit,
- UserInterface $user = null,
- $query = null
- ): int
- {
- //TODO vérifier que ne pas utiliser createDefaultQuery est pertinent
- $query = $this->createQuery($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->createQuery($query);
-
- $query
- ->selectCount()
- ->filterByReductionCart($reductionCart)
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid);
-
- return $query->count();
- }
-
- // countValidOrderWithReductionCartPerUser
- public function countValidWithReductionCartByUser(
- ReductionCartInterface $reductionCart,
- UserInterface $user,
- $query = null
- ): int
- {
- $query = $this->createQuery($query);
-
- $query
- ->selectCount()
- ->filterByUser($user)
- ->filterByReductionCart($reductionCart)
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid);
-
- return $query->count();
- }
-
- public function isFirstOrder(UserInterface $user): bool
- {
- return $this->countBy([
- 'user' => $user,
- 'isValid' => true,
- 'excludeComplementaryOrderShops' => false
- ]) == 1;
- }
-
- // findCartCurrent
- public function getOneCartCurrent(
- UserInterface $user = null,
- VisitorInterface $visitor = null,
- $query = null
- ): ?OrderShopInterface
- {
- if (is_null($user) && is_null($visitor)) {
- return null;
- }
-
- $query = $this->createDefaultQuery($query);
-
- if (!is_null($user)) {
- $query->filterByUser($user);
- } else {
- if (!is_null($visitor)) {
- $query->filterByVisitor($visitor);
- }
- }
-
- $query
- ->selectOrderReductionCarts()
- ->filterByStatus(OrderStatusModel::$statusAliasAsCart);
-
- return $query->findOne();
- }
-
- // findLastOrderValidOfWeek
- //getOneLastValidByCycle
- public function getOneLastValidByDistribution(
- DistributionInterface $distribution,
- $query = null
- ): ?OrderShopInterface
- {
- $query = $this->createDefaultQuery($query);
-
- $query
- ->filterByDistribution($distribution)
- ->filterByStatus(OrderStatusModel::$statusAliasAsValid)
- ->filterIsNotComplementaryOrderShop()
- ->orderBy('.cycleId', 'DESC');
-
- return $query->findOne();
- }
-
- public function getOneValidByDistributionAndUser(
- DistributionInterface $distribution,
- UserInterface $user,
- $query = null
- ): ?OrderShopInterface
- {
- $query = $this->createDefaultQuery($query);
-
- $query
- ->filterByDistribution($distribution)
- ->filterByUser($user)
- ->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($params, $query);
-
- return $query->count();
- }
-
-
- public function getBy(array $params = [], $query = null): array
- {
- $query = $this->createDefaultQuery($query);
-
- $this->applyGetByFilters($params, $query);
-
- $orderShops = $query->find();
-
- if (isset($params['mergeComplementaryOrderShops']) && $params['mergeComplementaryOrderShops'] == true) {
- $mergeComplementaryOrderShopsSameSection = isset($params['mergeComplementaryOrderShopsSameSection'])
- ? $params['mergeComplementaryOrderShopsSameSection'] : false;
- foreach ($orderShops as $orderShop) {
- $this->orderShopSolver->mergeComplentaryOrderShops($orderShop, true, $mergeComplementaryOrderShopsSameSection);
- }
- }
-
- return $orderShops;
- }
-
- protected function applyGetByFilters(array $params, $query)
- {
- if (isset($params['isDeliveryHome']) && $params['isDeliveryHome']) {
- $query->filterIsDeliveryHome();
- }
-
- if (isset($params['isDeliveryPointSale']) && $params['isDeliveryPointSale']) {
- $query->filterIsDeliveryPointSale();
- }
-
- 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['distribution'])) {
- $query->filterByDistribution($params['distribution']);
- }
-
- if (isset($params['distributions'])) {
- $query->filterByDistributions($params['distributions']);
- }
-
- 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['minimumTomorrowDelivery'])) {
- $query->filterMinimumTomorrowDelivery();
- }
-
- if (isset($params['mergeComplementaryOrderShops']) && $params['mergeComplementaryOrderShops']) {
- $query
- ->joinComplementaryOrderShops();
- }
-
- if (isset($params['isComplementaryOrderShop']) && $params['isComplementaryOrderShop']) {
- $query->filterIsComplementaryOrderShop();
-
- if (isset($params['sectionMainOrderShop']) && $params['sectionMainOrderShop']) {
- $query->filterSectionMainOrderShop($params['sectionMainOrderShop']);
- }
- }
-
- if ((isset($params['excludeComplementaryOrderShops']) && $params['excludeComplementaryOrderShops'])
- || (isset($params['mergeComplementaryOrderShops']) && $params['mergeComplementaryOrderShops'])) {
- $query->filterIsNullMainOrderShop();
- }
-
- if (isset($params['joinProductFamily'])) {
- $query->joinProductFamily(true);
- }
-
- 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
- ->setMerchant($this->merchant)
- ->getByTypeAndUser(ReductionCreditModel::TYPE_CREDIT, $user);
-
- $reductionCreditsArray = [];
- foreach ($reductionCredits as $reductionCredit) {
- if (!$this->countValidWithReductionCredit($reductionCredit, $user)) {
- $reductionCreditsArray[] = $reductionCredit;
- }
- }
-
- return $reductionCreditsArray;
- }
-
- public function getReductionGiftsAvailableByUser(UserInterface $user): array
- {
- $reductionGifts = $this->reductionCreditStore
- ->setMerchant($this->merchant)
- ->getByTypeAndUser(ReductionCreditModel::TYPE_GIFT, $user);
-
- $reductionGiftsArray = [];
- foreach ($reductionGifts as $reductionGift) {
- if (!$this->countValidWithReductionCredit($reductionGift, $user)) {
- $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, string $type = null, $query = null)
- {
- $reductionCarts = $this->reductionCartStore
- ->setMerchant($this->merchant)
- ->getOnline();
-
- $reductionCartsArray = [];
- foreach ($reductionCarts as $reductionCart) {
- if ($this->reductionCartSolver->matchWithUser($reductionCart, $user)
- && $this->reductionCartSolver->matchWithGroupUser($reductionCart, $user)
- && $this->getReductionCartRemainingQuantityByUser($reductionCart, $user)
- && ($reductionCart->getUsers()->count() > 0 || $reductionCart->getGroupUsers()->count() > 0)
- && (!$this->merchant || $reductionCart->getMerchant() == $this->merchant)) {
-
- if(!$type || $reductionCart->getType() == $type) {
- $reductionCartsArray[] = $reductionCart;
- }
- }
- }
-
- return $reductionCartsArray;
- }
-
- //countValidOrderProductsOfCyclesByProducts
- public function countValidOrderProductsOfDistributionsByProducts(
- array $distributions,
- array $products,
- ProductFamilyInterface $productFamily,
- $query = null
- ): array
- {
- $query = $this->createDefaultQuery($query);
- $query
- ->filterByAlias(OrderStatusModel::$statusAliasAsValid)
- ->filterByDistributions($distributions)
- ->filterByProducts($products)
- ->selectSum()
- ->groupBy('distribution.cycleNumber, product.id');
-
- //TODO vérifier ou est utilisé cette fonction ???
-
- return $query->find();
- }
-
- //countValidOrderProductsOfCycleByProduct
- public function countValidOrderProductsOfDistributionByProduct(
- DistributionInterface $distribution,
- ProductInterface $product,
- $query = null
- ): float
- {
- //TODO attention à vérifier
- $query = $this->createQuery($query);
-
- $query
- ->filterByAlias(OrderStatusModel::$statusAliasAsValid)
- ->filterByDistribution($distribution)
- ->filterByProduct($product)
- ->selectSumQuantityOrder()
- ->joinDistribution()
- ->groupBy('distribution.cycleNumber, product.id');
-
- $result = $query->findOne();
-
- if ($result) {
- return $result['quantity'];
- }
- return 0;
- }
-
- 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->flashBagTranslator->add('error', 'userNotAllow', 'ReductionCredit');
- 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->flashBagTranslator->add('error', 'alreadyUse', 'ReductionCredit');
- return false;
- }
- } else {
- if ($this->countValidWithReductionCredit($reductionCredit) > 0) {
- // @TODO : déplacer la gestion du flash message
- $this->flashBagTranslator->add('error', 'alreadyUse', 'ReductionCredit');
- return false;
- }
- }
-
- return true;
- }
-
- public function getCartAlone($query = null)
- {
-
- $query = $this->createQuery($query);
-
- $query->filterByAlias(OrderStatusModel::$statusAliasAsCart);
- $query->filterByUserIsNull();
- $query->filterByVisitorIsNull();
-
- return $query->limit(10000)->find();
- }
- }
|