![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/Checkout/Model/ |
<?php /** * Copyright (c) 2021 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Cnc * Krzysztof Majkowski <[email protected]> */ namespace Cnc\Checkout\Model; use Cnc\Checkout\Api\Data\DeliveryEstimationInterface; use Cnc\Checkout\Api\EstimateDeliveryInterface; use Cnc\Checkout\Model\ResourceModel\DeliveryEstimationModel\DeliveryEstimationCollection; use Cnc\Checkout\Model\ResourceModel\DeliveryEstimationModel\DeliveryEstimationCollectionFactory; use Magento\Checkout\Model\Session; use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\Stdlib\DateTime\TimezoneInterface; class EstimateDelivery implements EstimateDeliveryInterface { /** * @var DeliveryEstimationCollection */ private $collection; /** * @var SerializerInterface */ private $serializer; /** * @var TimezoneInterface */ private $timezone; /** * @var Session */ private $checkoutSession; /** * EstimateDelivery constructor. * @param DeliveryEstimationCollectionFactory $collectionFactory * @param SerializerInterface $serializer * @param TimezoneInterface $timezone * @param Session $checkoutSession */ public function __construct( DeliveryEstimationCollectionFactory $collectionFactory, SerializerInterface $serializer, TimezoneInterface $timezone, Session $checkoutSession ) { $this->collection = $collectionFactory->create(); $this->serializer = $serializer; $this->timezone = $timezone; $this->checkoutSession = $checkoutSession; } /** * @return string * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function executeByQuote() { $quote = $this->checkoutSession->getQuote(); if ($quote) { $countryCode = $quote->getShippingAddress()->getCountry(); $method = $quote->getShippingAddress()->getShippingMethod(); if (is_array($method) && count($method)) { foreach ($method as $key => $value) { $method = $value; break; } } if ($countryCode && $method) { return $this->execute($countryCode, $method); } } return ''; } /** * @param string $countryCode * @param string $shippingMethod * @return string */ public function execute(string $countryCode, string $shippingMethod) { if (strpos($shippingMethod, 'matrixrate_matrixrate') !== false) { $shippingMethod = 'matrixrate_matrixrate'; } /** @var DeliveryEstimationInterface $item */ foreach ($this->collection as $item) { if ($item->getCountryCode() !== $countryCode) { continue; } $estimations = $this->serializer->unserialize($item->getEstimation()); if (!is_array($estimations)) { break; } if (isset($estimations[$shippingMethod]) && ((int) $estimations[$shippingMethod]) > 0) { return $this->calculateDate($estimations[$shippingMethod]); } } return ''; } protected function calculateDate(int $shippingTime) { $currentDate = $this->timezone->date(new \DateTime()); if (strtotime($currentDate->format('H:i:m')) > strtotime('16:30:00')) { $shippingTime++; } if ($this->isFriday($currentDate)) { $shippingTime += 3; } if ($this->isWeekend($currentDate)) { $shippingTime += 2; } return $currentDate ->modify(sprintf('+%d days', $shippingTime)) ->format('d/m/y'); } protected function isWeekend($date) { return $date->format('N') >= 6; } protected function isFriday($date) { return $date->format('N') == 5; } }