Skip to content

Commit

Permalink
Added working hours
Browse files Browse the repository at this point in the history
  • Loading branch information
mage2godev committed Apr 6, 2024
1 parent 6285006 commit f645926
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 25 deletions.
21 changes: 21 additions & 0 deletions Console/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -68,6 +86,7 @@ public function __construct(
GetOrdersList $getOrdersList,
GetOrder $getOrder,
TradingPoint $tradingPoint,
ConfigurationProvider $configurationProvider,
?string $name = null
) {
$this->state = $state;
Expand All @@ -78,6 +97,7 @@ public function __construct(
$this->getOrdersList = $getOrdersList;
$this->getOrder = $getOrder;
$this->tradingPoint = $tradingPoint;
$this->configurationProvider = $configurationProvider;
parent::__construct($name);
}

Expand Down Expand Up @@ -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);
Expand Down
62 changes: 62 additions & 0 deletions Cron/WorkingHours.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Qwqer\Express\Cron;

use Magento\Config\Model\ResourceModel\Config;
use Qwqer\Express\Model\Api\TradingPoint;
use Exception;
use Qwqer\Express\Provider\ConfigurationProvider;

class WorkingHours
{
/**
* *
*
* @var Config
*/
private Config $_resourceConfig;

/**
* *
*
* @var TradingPoint
*/
private TradingPoint $tradingPoint;

/**
* *
*
* @param TradingPoint $tradingPoint
* @param Config $_resourceConfig
*/
public function __construct(
TradingPoint $tradingPoint,
Config $_resourceConfig
) {
$this->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());
}
}
}
}
2 changes: 1 addition & 1 deletion Model/Api/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class AbstractRequest
/**
* @var ConfigurationProvider
*/
protected ConfigurationProvider $configurationProvider;
public ConfigurationProvider $configurationProvider;

/**
* @var RequestInterface
Expand Down
25 changes: 20 additions & 5 deletions Model/Carrier/Express.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand All @@ -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(
Expand All @@ -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);
Expand All @@ -92,6 +100,7 @@ public function __construct(
$this->geoCode = $geoCode;
$this->shippingCost = $shippingCost;
$this->_checkoutSession = $_checkoutSession;
$this->configurationProvider = $configurationProvider;
$this->_logger = $logger;
}

Expand All @@ -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;
}

Expand Down
24 changes: 19 additions & 5 deletions Model/Carrier/ScheduledToDoor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand All @@ -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(
Expand All @@ -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);
Expand All @@ -92,6 +100,7 @@ public function __construct(
$this->geoCode = $geoCode;
$this->shippingCost = $shippingCost;
$this->_checkoutSession = $_checkoutSession;
$this->configurationProvider = $configurationProvider;
$this->_logger = $logger;
}

Expand All @@ -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;
}

Expand Down
24 changes: 19 additions & 5 deletions Model/Carrier/ScheduledToParcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand All @@ -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(
Expand All @@ -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);
Expand All @@ -92,6 +100,7 @@ public function __construct(
$this->geoCode = $geoCode;
$this->shippingCost = $shippingCost;
$this->_checkoutSession = $_checkoutSession;
$this->configurationProvider = $configurationProvider;
$this->_logger = $logger;
}

Expand All @@ -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;
}

Expand Down
12 changes: 5 additions & 7 deletions Observer/Config/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit f645926

Please sign in to comment.