|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574 |
- <?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)
- {
- $orderShops = $this->getBy(
- array_merge(
- [
- 'cycleNumber' => $this->getCycleNumberCurrentOrder($this->section),
- 'isValid' => true,
- ],
- $params
- ),
- $query
- );
-
- return $orderShops;
- }
-
- // getOrderShopsOfWeekByUser
- public function getByCurrentCycleAndUser(UserInterface $user, array $params = [], $query = null)
- {
- return $this->getByCurrentCycle(
- array_merge(
- [
- 'user' => $user,
- 'cycleNumber' => $this->getCycleNumberCurrentOrder($this->section),
- 'excludeComplementaryOrderShops' => true
- ],
- $params
- ),
- $query
- );
- }
-
- //public $countOrderShopsOfWeek = null;
- public function countByCurrentCycle(bool $excludeComplementaryOrderShops = true, $query = null)
- {
- return $this->getByCurrentCycle(
- [
- 'count' => true,
- 'excludeComplementaryOrderShops' => $excludeComplementaryOrderShops
-
- ],
- $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->getOneLastValidOfCycle($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->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()
- ->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;
- }
- }
|