|
- <?php
-
- namespace Lc\CaracoleBundle\Repository\Order;
-
- use App\Entity\Delivery\DeliveryAvailabilityPointSale;
- use App\Entity\Delivery\DeliveryAvailabilityZone;
- use App\Entity\Order\OrderStatus;
- use App\Entity\Section\Section;
- 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\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\Resolver\Price\PriceResolver;
- 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 PriceResolver $priceResolver;
- 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;
-
- public function __construct(
- OrderShopRepositoryQuery $query,
- EntityManagerInterface $entityManager,
- PriceResolver $priceResolver,
- DocumentBuilder $documentBuilder,
- ReductionCreditStore $reductionCreditStore,
- SectionStore $sectionStore,
- OrderProductStore $orderProductStore,
- MerchantStore $merchantStore,
- FlashBagInterface $flashBag,
- OpeningResolver $openingResolver,
- ParameterBagInterface $parameterBag,
- UrlGeneratorInterface $router
- ) {
- $this->query = $query;
- $this->entityManager = $entityManager;
- $this->priceResolver = $priceResolver;
- $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;
- }
-
- // getOrderShopsOfWeek
- public function getByCycle(SectionInterface $section, $params = [])
- {
- $orderShops = $this->getBy(
- array_merge(
- [
- 'section' => $section,
- 'cycleNumber' => $this->getCycleNumberCurrentOrder($section),
- 'isValid' => true,
- ],
- $params
- )
- );
-
- return $orderShops;
- }
-
- // getOrderShopsOfWeekByUser
- public function getByCycleAndUser(SectionInterface $section, UserInterface $user, array $params = [])
- {
- return $this->getByCycle(
- $section,
- array_merge(
- [
- 'user' => $user,
- 'weekNumber' => $this->getCycleNumberCurrentOrder($section),
- 'excludeComplementaryOrderShops' => true
- ],
- $params
- )
- );
- }
-
- //public $countOrderShopsOfWeek = null;
-
- public function countByCycle(SectionInterface $section, bool $excludeComplementaryOrderShops = true)
- {
- return $this->getByCycle(
- $section,
- [
- 'count' => true,
- 'excludeComplementaryOrderShops' => $excludeComplementaryOrderShops
-
- ]
- );
-
- // @TODO : optimisation à remettre en place
- /*if (is_null($this->countOrderShopsOfWeek)) {
- $this->countOrderShopsOfWeek = $this->getByCycle(
- $section,
- [
- 'count' => true,
- 'excludeComplementaryOrderShops' => $excludeComplementaryOrderShops
-
- ]
- );
- }
- return $this->countOrderShopsOfWeek;*/
- }
-
- // getNextWeekId
- public function getNextCycleId(SectionInterface $section, int $cycleNumber): int
- {
- $lastOrder = $this->getOneLastOrderValidOfCycle($section, $cycleNumber);
- if ($lastOrder) {
- return intval($lastOrder->getCycleId() + 1);
- } else {
- return 1;
- }
- }
-
- public function getNextIdValidOrder(Section $section)
- {
- $lastOrder = $this->getOneLastOrderValid($section);
-
- if ($lastOrder) {
- return intval($lastOrder->getIdValidOrder() + 1);
- } else {
- return 1;
- }
- }
-
- // getOrderDatas
- public function getDatas(OrderShopInterface $orderShop, UserInterface $user = null): array
- {
- $data = [];
-
- $data['order'] = $orderShop;
-
- if ($orderShop) {
- $data['count'] = $orderShop->countQuantities();
- $data['total_with_tax'] = $this->priceResolver->getTotalWithTax($orderShop);
- $data['order_products_by_category'] = $orderShop->getOrderProductsByParentCategory();
- $data['total_remaining_to_be_paid'] = $this->getTotalRemainingToBePaid($orderShop);
- }
- return $data;
- }
-
- public function getAsJsonObject(OrderShopInterface $orderShop): array
- {
- $data['id'] = $orderShop->getId();
- $data['user'] = $orderShop->getUser()->getSummary();
- $data['orderStatus'] = $orderShop->getOrderStatus()->__toString();
- $data['deliveryAddress'] = $orderShop->getDeliveryAddress()->getSummary();
- $data['invoiceAddress'] = $orderShop->getInvoiceAddress()->getSummary();
- $data['total'] = $this->priceResolver->getTotal($orderShop);
- $data['totalWithTax'] = $this->priceResolver->getTotalWithTax($orderShop);
- $data['totalWithTaxAndReduction'] = $this->priceResolver->getTotalWithTax($orderShop);
- $i = 0;
-
- foreach ($orderShop->getOrderProductsByParentCategory() as $labelCategory => $orderProducts) {
- foreach ($orderProducts as $orderProduct) {
- $data['orderProducts'][$i]['id'] = $orderProduct->getId();
- $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
- $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
- $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
- $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
- $data['orderProducts'][$i]['price'] = $this->priceResolver->getPrice($orderProduct);
- $data['orderProducts'][$i]['priceWithTax'] = $this->priceResolver->getPriceWithTax($orderProduct);
- $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceResolver->getPriceWithTaxAndReduction(
- $orderProduct
- );
- $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
- $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceResolver->getTotalOrderProductsWithTaxAndReduction(
- array($orderProduct)
- );
- $i++;
- }
- }
-
- return $data;
- }
-
- public function groupOrderProductsByProductFamily(array $orderProducts): array
- {
- $orderProductsByProductFamily = [];
- foreach ($orderProducts as $orderProduct) {
- if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
- $productFamily = $orderProduct->getProduct()->getProductFamily();
- if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
- $orderProductsByProductFamily[$productFamily->getId()] = [
- 'order_products' => [],
- 'total_quantity_weight' => 0,
- ];
- }
- $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
- $orderProductsByProductFamily[$productFamily->getId(
- )]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit(
- )->getCoefficient()) * $orderProduct->getQuantityOrder();
- }
- }
-
- return $orderProductsByProductFamily;
- }
-
- // isOrderShopPositiveAmount
- public function isPositiveAmount(OrderShopInterface $orderShop)
- {
- return $this->priceResolver->getTotalWithTax($orderShop) >= 0;
- }
-
- public function isPaid(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false)
- {
- $totalOrderPayments = $this->getTotalOrderPayments($orderShop, $mergeComplementaryOrderShop);
- $totalOrder = $this->priceResolver->getTotalWithTax($orderShop);
-
- if ((abs($totalOrderPayments - $totalOrder) < 0.00001
- || $totalOrderPayments >= $totalOrder)
- && $totalOrder > 0) {
- return true;
- } else {
- return false;
- }
- }
-
- public function getTotalOrderPayments(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): float
- {
- $totalAmount = floatval(0);
- foreach ($orderShop->getOrderPayments() as $orderPayment) {
- $totalAmount = $orderPayment->getAmount() + $totalAmount;
- }
- if ($mergeComplementaryOrderShop) {
- foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
- foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
- $totalAmount = $orderPayment->getAmount() + $totalAmount;
- }
- }
- }
- return $totalAmount;
- }
-
- public
- function getTotalRemainingToBePaid(
- OrderShopInterface $orderShop
- ): float {
- return $this->priceResolver->getTotalWithTax($orderShop) - $this->getTotalOrderPayments($orderShop);
- }
-
- // isOrderShopPositiveAmountRemainingToBePaid
- public function isPositiveAmountRemainingToBePaid(OrderShopInterface $orderShop): bool
- {
- return $this->getTotalRemainingToBePaid($orderShop) > 0;
- }
-
- public function getCartByUserOrCreateIt($user)
- {
- $newOrderShop = $this->em->getRepository(OrderShopInterface::class)->findCartCurrent(['user' => $user]);
- if ($newOrderShop === null) {
- $newOrderShop = $this->createOrderShop(
- array(
- 'user' => $user,
- 'merchant' => $this->merchantUtils->getMerchantUser()
- )
- );
- }
-
- return $newOrderShop;
- }
-
- public function isCartAllowToBeOrder(OrderShopInterface $orderShop)
- {
- return true;
- }
-
- // countValidOrderShopByUserAllMerchant
- public function countValidByUserAllMerchant($user)
- {
- $totalOrder = 0;
-
- foreach ($this->merchantStore->getRepositoryQuery()->findAll() as $merchant) {
- $totalOrder += $this->countValidByUser($user, $merchant);
- }
-
- return $totalOrder;
- }
-
- public function countValidByUser(UserInterface $user, MerchantInterface $merchant = null)
- {
- return $this->getBy(
- [
- 'user' => $user,
- 'isValid' => true,
- 'merchant' => $merchant,
- 'excludeComplementaryOrderShops' => true,
- 'count' => true
- ]
- );
- }
-
- /*
- 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;
- }*/
-
-
- public function countValidOrderWithReductionCredit(
- OrderReductionCreditInterface $reductionCredit,
- UserInterface $user = null
- ): string {
- $query = $this->query->create();
-
- if ($user) {
- $query->filterByUser($user);
- }
- $query
- ->selectCount()
- ->filterByReductionCredit($reductionCredit)
- ->filterByStatus(OrderStatus::$statusAliasAsValid)
- ->filterBySection($this->section);
-
- return $query->count();
- }
-
- public function countValidOrderWithReductionCart(
- OrderReductionCartInterface $reductionCart
- ): string {
- $query = $this->query->create();
-
- $query
- ->selectCount()
- ->filterByReductionCart($reductionCart)
- ->filterByStatus(OrderStatus::$statusAliasAsValid)
- ->filterBySection($this->section);
-
- return $query->count();
- }
-
- public function countValidOrderWithReductionCartPerUser(
- OrderReductionCartInterface $reductionCart,
- UserInterface $user
- ): string {
- $query = $this->query->create();
-
- $query
- ->selectCount()
- ->filterByUser($user)
- ->filterByReductionCart($reductionCart)
- ->filterByStatus(OrderStatus::$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(OrderStatus::$statusAliasAsValid)
- ->filterBySection($this->section);
-
- $results = $query->find();
-
- if ($results) {
- return $results[0];
- }
-
- return null;
- }
-
- //findLastOrderValidOfWeek
- public function getOneLastOrderValidOfWeek(int $weekNumber): ?OrderShopInterface
- {
- $query = $this->query->create();
-
- $query
- ->filterByWeekNumber($weekNumber)
- ->filterByStatus(OrderStatus::$statusAliasAsValid)
- ->filterIsNotMainOrderShop()
- ->orderBy('.weekId', 'DESC')
- ->filterBySection($this->section);
-
- return $query->findOne();
- }
-
- //findLastOrderValid
- public function getOneLastOrderValid(): ?OrderShopInterface
- {
- $query = $this->query->create();
-
- $query
- ->filterByStatus(OrderStatus::$statusAliasAsValid)
- ->filterIsNotMainOrderShop()
- ->orderBy('.idValidOrder', 'DESC')
- ->filterBySection($this->section);
-
- return $query->findOne();
- }
-
- //TODO Fonction à tester
-
- // findAllBy
- public function getAllBy(array $params = [])
- {
- $query = $this->query->create();
-
- if (isset($params['section'])) {
- $query->filterBySection($params['section']);
- } else {
- $query->filterBySection($this->section);
- }
-
- if (isset($params['count']) && $params['count']) {
- $query->selectCount();
- } else {
- 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['weekNumber'])) {
- $query->filterByWeekNumber($params['weekNumber']);
- }
-
- if (isset($params['isCart'])) {
- $query->filterByStatus(OrderStatus::$statusAliasAsCart);
- }
-
- if (isset($params['isValid'])) {
- $query->filterByStatus(OrderStatus::$statusAliasAsValid);
- }
-
- if (isset($params['isWaitingDelivery'])) {
- $query->filterByStatus(OrderStatus::$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['weekDeliveryTrucks'])) {
- $query->filterByWeekDeliveryTruck($params['weekDeliveryTrucks']);
- }
-
- if (isset($params['estimatedDeliveryDateTime'])) {
- $date = clone $params['estimatedDeliveryDateTime'];
- $query
- ->filterByEstimatedDeliveryDateStart($date->format('Y-m-d 00:00:00'))
- ->filterByEstimatedDeliveryDateEnd($date->modify('+1 day')->format('Y-m-d 00:00:00'));
- }
-
-
- if (isset($params['deliveryDate'])) {
- $date = clone $params['deliveryDate'];
- $query
- ->filterByDeliveryDateStart($date->format('Y-m-d 00:00:00'))
- ->filterByDeliveryDateEnd($date->modify('+1 day')->format('Y-m-d 00:00:00'));
- }
-
- if (isset($params['mergeComplementaryOrderShops'])) {
- //TODO jointure peut être pas utile
- $query
- ->joinComplementaryOrderShops();
- }
-
- if (isset($params['excludeComplementaryOrderShops']) || isset($params['mergeComplementaryOrderShops'])) {
- $query->filterIsNullMainOrderShop();
- }
-
- if (isset($params['isCircuit'])) {
- $query->filterIsNullDeliveryPointSale();
- }
-
- if (isset($params['isDepository'])) {
- $query->filterIsNotNullDeliveryPointSale();
- }
-
- if (isset($params['isOffCircuit'])) {
- $query->filterIsPointSale('devAliasHorsTournee');
- }
-
- if (isset($params['isGiftVoucher'])) {
- $query->filterIsPointSale('devAliasGiftVoucher');
- }
-
- if (isset($params['deliveryAvailability'])) {
- $deliveryAvailability = $params['deliveryAvailability'];
- $deliveryAvailabilityZone = ($deliveryAvailability instanceof DeliveryAvailabilityZone) ? $deliveryAvailability : false;
- $deliveryAvailabilityPointSale = ($deliveryAvailability instanceof DeliveryAvailabilityPointSale) ? $deliveryAvailability : false;
-
- if ($deliveryAvailabilityZone) {
- $query->filterByAvailabilityPointZone($deliveryAvailabilityZone);
- }
-
- if ($deliveryAvailabilityPointSale) {
- $query->filterByAvailabilityPointZone($deliveryAvailabilityPointSale);
- }
- } else {
- $query->joinDeliverySlotZone();
- $query->joinDeliverySlotPointSale();
- }
-
- 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']);
- }
-
- if (isset($params['count']) && $params['count']) {
- return $query->count();
- }
-
- return $query->find();
- }
- }
|