diff --git a/Console/Commands.php b/Console/Commands.php index af1dcba..12c4b7b 100644 --- a/Console/Commands.php +++ b/Console/Commands.php @@ -16,6 +16,7 @@ use Qwqer\Express\Model\Api\GetOrder; use Qwqer\Express\Model\Api\TradingPoint; use Qwqer\Express\Service\PublishOrder; +use Qwqer\Express\Provider\ConfigurationProvider; class Commands extends Command { @@ -49,6 +50,21 @@ class Commands extends Command */ private GetOrder $getOrder; + /** + * @var TradingPoint + */ + private TradingPoint $tradingPoint; + + /** + * @var PublishOrder + */ + private PublishOrder $publishOrder; + + /** + * @var ConfigurationProvider + */ + private ConfigurationProvider $configurationProvider; + /** * @param State $state * @param ParcelMachines $parcelMachines @@ -57,6 +73,8 @@ class Commands extends Command * @param QuoteFactory $quoteFactory * @param GetOrdersList $getOrdersList * @param GetOrder $getOrder + * @param TradingPoint $tradingPoint + * @param ConfigurationProvider $configurationProvider * @param string|null $name */ public function __construct( @@ -68,6 +86,7 @@ public function __construct( GetOrdersList $getOrdersList, GetOrder $getOrder, TradingPoint $tradingPoint, + ConfigurationProvider $configurationProvider, ?string $name = null ) { $this->state = $state; @@ -78,6 +97,7 @@ public function __construct( $this->getOrdersList = $getOrdersList; $this->getOrder = $getOrder; $this->tradingPoint = $tradingPoint; + $this->configurationProvider = $configurationProvider; parent::__construct($name); } @@ -112,6 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->initState(); $output->writeLn("Started"); + $this->configurationProvider->checkWorkingHours(); //$result = $this->tradingPoint->executeRequest(); /**$orderId = 7; $order = $this->orderRepository->get($orderId); diff --git a/Cron/WorkingHours.php b/Cron/WorkingHours.php new file mode 100644 index 0000000..a971fc9 --- /dev/null +++ b/Cron/WorkingHours.php @@ -0,0 +1,62 @@ +tradingPoint = $tradingPoint; + $this->_resourceConfig = $_resourceConfig; + } + + /** + * Execute CRON job + * @throws Exception + */ + public function execute() + { + if ( + $this->tradingPoint->configurationProvider->getIsQwqerParcelEnabled() + || $this->tradingPoint->configurationProvider->getIsQwqerDoorEnabled() + || $this->tradingPoint->configurationProvider->getIsQwqerEnabled() + ) { + try { + $workingHours = $this->tradingPoint->executeRequest(); + $this->_resourceConfig->saveConfig( + ConfigurationProvider::API_WORKING_HOURS, + json_encode($workingHours) + ); + } catch (Exception $e) { + throw new Exception($e->getMessage()); + } + } + } +} diff --git a/Model/Api/AbstractRequest.php b/Model/Api/AbstractRequest.php index ab3b5ce..d00f6e2 100644 --- a/Model/Api/AbstractRequest.php +++ b/Model/Api/AbstractRequest.php @@ -27,7 +27,7 @@ abstract class AbstractRequest /** * @var ConfigurationProvider */ - protected ConfigurationProvider $configurationProvider; + public ConfigurationProvider $configurationProvider; /** * @var RequestInterface diff --git a/Model/Carrier/Express.php b/Model/Carrier/Express.php index 9ccd5d4..6b323bf 100644 --- a/Model/Carrier/Express.php +++ b/Model/Carrier/Express.php @@ -2,6 +2,7 @@ namespace Qwqer\Express\Model\Carrier; +use Magento\Checkout\Model\Session; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Quote\Model\Quote\Address\RateRequest; use Magento\Quote\Model\Quote\Address\RateResult\Method; @@ -53,15 +54,20 @@ class Express extends AbstractCarrier implements CarrierInterface protected ShippingCost $shippingCost; /** - * @var \Magento\Checkout\Model\Session + * @var Session */ - private $_checkoutSession; + private Session $_checkoutSession; /** * @var LoggerInterface */ protected $_logger; + /** + * @var ConfigurationProvider + */ + private ConfigurationProvider $configurationProvider; + /** * Constructor Express * @@ -72,7 +78,8 @@ class Express extends AbstractCarrier implements CarrierInterface * @param MethodFactory $rateMethodFactory * @param GeoCode $geoCode * @param ShippingCost $shippingCost - * @param \Magento\Checkout\Model\Session $_checkoutSession + * @param Session $_checkoutSession + * @param ConfigurationProvider $configurationProvider * @param array $data */ public function __construct( @@ -83,7 +90,8 @@ public function __construct( MethodFactory $rateMethodFactory, GeoCode $geoCode, ShippingCost $shippingCost, - \Magento\Checkout\Model\Session $_checkoutSession, + Session $_checkoutSession, + ConfigurationProvider $configurationProvider, array $data = [] ) { parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data); @@ -92,6 +100,7 @@ public function __construct( $this->geoCode = $geoCode; $this->shippingCost = $shippingCost; $this->_checkoutSession = $_checkoutSession; + $this->configurationProvider = $configurationProvider; $this->_logger = $logger; } @@ -108,8 +117,14 @@ public function collectRates(RateRequest $request) if (!$this->getConfigFlag('active')) { return false; } + $available = $this->checkAvailableProduct(); - if(!$available) { + if (!$available) { + return false; + } + + $available = $this->configurationProvider->checkWorkingHours(); + if (!$available) { return false; } diff --git a/Model/Carrier/ScheduledToDoor.php b/Model/Carrier/ScheduledToDoor.php index 18944cc..31c7f83 100644 --- a/Model/Carrier/ScheduledToDoor.php +++ b/Model/Carrier/ScheduledToDoor.php @@ -2,6 +2,7 @@ namespace Qwqer\Express\Model\Carrier; +use Magento\Checkout\Model\Session; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Quote\Model\Quote\Address\RateRequest; use Magento\Quote\Model\Quote\Address\RateResult\Method; @@ -53,15 +54,20 @@ class ScheduledToDoor extends AbstractCarrier implements CarrierInterface protected ShippingCost $shippingCost; /** - * @var \Magento\Checkout\Model\Session + * @var Session */ - private $_checkoutSession; + private Session $_checkoutSession; /** * @var LoggerInterface */ protected $_logger; + /** + * @var ConfigurationProvider + */ + private ConfigurationProvider $configurationProvider; + /** * Constructor Express * @@ -72,7 +78,8 @@ class ScheduledToDoor extends AbstractCarrier implements CarrierInterface * @param MethodFactory $rateMethodFactory * @param GeoCode $geoCode * @param ShippingCost $shippingCost - * @param \Magento\Checkout\Model\Session $_checkoutSession + * @param Session $_checkoutSession + * @param ConfigurationProvider $configurationProvider * @param array $data */ public function __construct( @@ -83,7 +90,8 @@ public function __construct( MethodFactory $rateMethodFactory, GeoCode $geoCode, ShippingCost $shippingCost, - \Magento\Checkout\Model\Session $_checkoutSession, + Session $_checkoutSession, + ConfigurationProvider $configurationProvider, array $data = [] ) { parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data); @@ -92,6 +100,7 @@ public function __construct( $this->geoCode = $geoCode; $this->shippingCost = $shippingCost; $this->_checkoutSession = $_checkoutSession; + $this->configurationProvider = $configurationProvider; $this->_logger = $logger; } @@ -110,7 +119,12 @@ public function collectRates(RateRequest $request) } $available = $this->checkAvailableProduct(); - if(!$available) { + if (!$available) { + return false; + } + + $available = $this->configurationProvider->checkWorkingHours(); + if (!$available) { return false; } diff --git a/Model/Carrier/ScheduledToParcel.php b/Model/Carrier/ScheduledToParcel.php index 0012309..d207879 100644 --- a/Model/Carrier/ScheduledToParcel.php +++ b/Model/Carrier/ScheduledToParcel.php @@ -2,6 +2,7 @@ namespace Qwqer\Express\Model\Carrier; +use Magento\Checkout\Model\Session; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Quote\Model\Quote\Address\RateRequest; use Magento\Quote\Model\Quote\Address\RateResult\Method; @@ -53,15 +54,20 @@ class ScheduledToParcel extends AbstractCarrier implements CarrierInterface protected ShippingCost $shippingCost; /** - * @var \Magento\Checkout\Model\Session + * @var Session */ - private $_checkoutSession; + private Session $_checkoutSession; /** * @var LoggerInterface */ protected $_logger; + /** + * @var ConfigurationProvider + */ + private ConfigurationProvider $configurationProvider; + /** * Constructor Express * @@ -72,7 +78,8 @@ class ScheduledToParcel extends AbstractCarrier implements CarrierInterface * @param MethodFactory $rateMethodFactory * @param GeoCode $geoCode * @param ShippingCost $shippingCost - * @param \Magento\Checkout\Model\Session $_checkoutSession + * @param Session $_checkoutSession + * @param ConfigurationProvider $configurationProvider * @param array $data */ public function __construct( @@ -83,7 +90,8 @@ public function __construct( MethodFactory $rateMethodFactory, GeoCode $geoCode, ShippingCost $shippingCost, - \Magento\Checkout\Model\Session $_checkoutSession, + Session $_checkoutSession, + ConfigurationProvider $configurationProvider, array $data = [] ) { parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data); @@ -92,6 +100,7 @@ public function __construct( $this->geoCode = $geoCode; $this->shippingCost = $shippingCost; $this->_checkoutSession = $_checkoutSession; + $this->configurationProvider = $configurationProvider; $this->_logger = $logger; } @@ -110,7 +119,12 @@ public function collectRates(RateRequest $request) } $available = $this->checkAvailableProduct(); - if(!$available) { + if (!$available) { + return false; + } + + $available = $this->configurationProvider->checkWorkingHours(); + if (!$available) { return false; } diff --git a/Observer/Config/Save.php b/Observer/Config/Save.php index 9966557..04dd6e7 100644 --- a/Observer/Config/Save.php +++ b/Observer/Config/Save.php @@ -91,13 +91,11 @@ public function execute(Observer $observer) $tradingPointId = $fields['trading_point_id']; if ($tradingPointId) { $workingHours = $this->_apiRequestHelper->executeRequest(); - if (is_array($workingHours)) { - $this->_resourceConfig->saveConfig( - ConfigurationProvider::API_WORKING_HOURS, - json_encode($workingHours), - $scope - ); - } + $this->_resourceConfig->saveConfig( + ConfigurationProvider::API_WORKING_HOURS, + json_encode($workingHours), + $scope + ); } } return $this; diff --git a/Provider/ConfigurationProvider.php b/Provider/ConfigurationProvider.php index f33693a..14ffdd4 100644 --- a/Provider/ConfigurationProvider.php +++ b/Provider/ConfigurationProvider.php @@ -3,7 +3,9 @@ namespace Qwqer\Express\Provider; use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\Serialize\Serializer\Json; use Magento\Store\Model\ScopeInterface; +use Magento\Framework\Stdlib\DateTime\DateTime as FrameworkDateTime; class ConfigurationProvider { @@ -47,13 +49,29 @@ class ConfigurationProvider */ protected ScopeConfigInterface $scopeConfig; + /** + * @var FrameworkDateTime + */ + private FrameworkDateTime $dateTime; + + /** + * @var Json + */ + protected $_json; + /** * @param ScopeConfigInterface $scopeConfig + * @param FrameworkDateTime $dateTime + * @param Json $json */ public function __construct( - ScopeConfigInterface $scopeConfig + ScopeConfigInterface $scopeConfig, + FrameworkDateTime $dateTime, + Json $json ) { $this->scopeConfig = $scopeConfig; + $this->dateTime = $dateTime; + $this->_json = $json; } /** @@ -286,6 +304,39 @@ public function getOrdersList(): string return str_replace('{trading-point-id}', $this->getTradingPointId(), self::API_ORDER_LIST_URL); } + /** + * @return bool + */ + public function checkWorkingHours(): bool + { + $workingHoursConfig = $this->getStoreConfig(self::API_WORKING_HOURS); + if(!$workingHoursConfig) { + return true; + } + $workingHoursArray = $this->_json->unserialize($workingHoursConfig); + if(!is_array($workingHoursArray) || empty($workingHoursArray)) { + return true; + } + $days = ['Sunday','Monday', 'Tuesday', 'Wednesday','Thursday','Friday','Saturday']; + $today = $this->dateTime->date('N') * 1; + $dayOfWeek = $days[$today]; + $isOpen = false; + foreach ($workingHoursArray as $workingHour) { + if($workingHour['day_of_week'] == $dayOfWeek) { + $dateTime = new \DateTime(); + $startTimeObj = $dateTime::createFromFormat('H:i', $workingHour['time_from']); + $endTimeObj = $dateTime->createFromFormat('H:i', $workingHour['time_to']); + $currentTime = $this->dateTime->gmtDate('H:i'); + $currentTimeObj = $dateTime->createFromFormat('H:i', $currentTime); + if ($currentTimeObj >= $startTimeObj && $currentTimeObj <= $endTimeObj) { + $isOpen = true; + break; + } + } + } + return $isOpen; + } + /** * UseSslVerify * diff --git a/composer.json b/composer.json index c1d61a7..0269bc4 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "magento/module-store": "101.0.*" }, "type": "magento2-module", - "version": "1.0.4", + "version": "1.0.5", "autoload": { "files": [ "registration.php" diff --git a/etc/crontab.xml b/etc/crontab.xml new file mode 100644 index 0000000..2876cf5 --- /dev/null +++ b/etc/crontab.xml @@ -0,0 +1,8 @@ + + + + + 0 2 * * * + + +