Files

341 lines
9.2 KiB
PHP
Executable File

<?php
namespace JR\NetSuite\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Customer\Model\CustomerFactory;
use JR\NetSuite\Model\NetSuiteFactory;
use JR\NetSuite\Model\MessageFactory;
use Magento\GiftMessage\Model\MessageFactory as GiftMessageFactory;
use Magento\Directory\Model\CountryFactory;
class Data extends AbstractHelper {
const XML_PATH_NETSUITE_ENABLE = 'jr_netsuite/general/enable';
const XML_PATH_MULTI_LOCATION_ENABLE = 'jr_netsuite/general/multi_location_enable';
const XML_PATH_OAUTH_ORDER_PUSH_ENABLE = 'jr_netsuite/cronScheduled/enable';
const XML_PATH_NETSUITE_COUNT_FROM = 'jr_netsuite/cronScheduled/netsuite_count_from';
const XML_PATH_NETSUITE_MAX_ATTEMPT = 'jr_netsuite/cronScheduled/netsuite_max_attempt';
const XML_PATH_NETSUITE_API_URL = 'jr_netsuite/apidetail/api_url';
const XML_PATH_NETSUITE_REALM = 'jr_netsuite/apidetail/oauth_realm';
const XML_PATH_NETSUITE_SCRIPT = 'jr_netsuite/apidetail/script';
const XML_PATH_NETSUITE_DEPLOY = 'jr_netsuite/apidetail/deploy';
const XML_PATH_NETSUITE_CONSUMER_KEY = 'jr_netsuite/apidetail/oauth_consumer_key';
const XML_PATH_NETSUITE_CONSUMER_SECRET = 'jr_netsuite/apidetail/oauth_consumer_secret';
const XML_PATH_DEFAULT_CUSTOMER_GROUP = 'jr_netsuite/default/customer_group';
const XML_PATH_DEFAULT_STORE_ID = 'jr_netsuite/default/store_id';
const XML_PATH_DEFAULT_TAX_CLASS = 'jr_netsuite/default/tax_class';
const XML_PATH_DEFAULT_CATEGORY = 'jr_netsuite/default/category';
const XML_PATH_DEFAULT_VISIBILITY_ID = 'jr_netsuite/default/visibility_id';
const XML_PATH_CRON_BATCH_SIZE = 'jr_netsuite/advanced/batch_size';
/**
* @var storeManager
*/
protected $storeManager;
/**
* @var scopeConfig
*/
protected $scopeConfig;
/**
* @var orderCollectionFactory
*/
protected $orderCollectionFactory;
/**
* @var \DateTime
*/
protected $_date;
/**
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
*/
protected $timezone;
/**
* @var customerFactory
*/
protected $customerFactory;
/**
* @var \JR\NetSuite\Logger\Logger
*/
protected $_logger;
/**
* @var netSuiteFactory
*/
protected $netSuiteFactory;
/**
* @var messageFactory
*/
protected $messageFactory;
/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
* @param \JR\NetSuite\Logger\Logger $logger
* @param \JR\NetSuite\Model\NetSuiteFactory $netSuiteFactory
* @param \Magento\GiftMessage\Model\MessageFactory $messageFactory
* @param \Magento\Directory\Model\CountryFactory $countryFactory
**/
public function __construct(
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig,
CollectionFactory $orderCollectionFactory,
\Magento\Framework\Stdlib\DateTime\DateTime $date,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
CustomerFactory $customerFactory,
\JR\NetSuite\Logger\Logger $logger,
) {
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
$this->orderCollectionFactory = $orderCollectionFactory;
$this->_date = $date;
$this->timezone = $timezone;
$this->customerFactory = $customerFactory;
$this->_logger = $logger;
}
/**
* Get Logger
*
* @return \JR\NetSuite\Logger\Logger
*/
public function getLogger() {
return $this->_logger;
}
/**
* Get NetSuite API URL
*
* @return string
*/
public function getApiUrl() {
return $this->getConfigValue(self::XML_PATH_NETSUITE_API_URL);
}
/**
* Get NetSuite Enable
*
* @return string
*/
public function getNetSuiteIsEnabled() {
return $this->getConfigValue(self::XML_PATH_NETSUITE_ENABLE);
}
/**
* Get Multi Location Enable
*
* @return string
*/
public function getMultiLocationEnabled() {
return $this->getConfigValue(self::XML_PATH_MULTI_LOCATION_ENABLE);
}
/**
* Get Root Category
*
* @return string
*/
public function getDefaultCategory() {
return $this->getConfigValue(self::XML_PATH_DEFAULT_CATEGORY);
}
/**
* Get NetSuite visibility id
*
* @return string
*/
public function getDefaultVisibilityId() {
return $this->getConfigValue(self::XML_PATH_DEFAULT_VISIBILITY_ID);
}
/**
* Get NetSuite assign tax class
*
* @return string
*/
public function getDefaultTaxClass() {
return $this->getConfigValue(self::XML_PATH_DEFAULT_TAX_CLASS);
}
/**
* Get XML_PATH_NETSUITE_MAX_ATTEMPT
*
* @return string
*/
public function getNetSuiteMaxAttempt() {
return $this->getConfigValue(self::XML_PATH_NETSUITE_MAX_ATTEMPT);
}
/**
* Get XML_PATH_CRON_BATCH_SIZE
*
* @return string
*/
public function getCronBatchSize() {
return $this->getConfigValue(self::XML_PATH_CRON_BATCH_SIZE);
}
/**
* Get default_customer_group
*
* @return string
*/
public function getDefaultCustomerGroup() {
return $this->getConfigValue(self::XML_PATH_DEFAULT_CUSTOMER_GROUP);
}
/**
* Get default_store_id
*
* @return string
*/
public function getDefaultStoreId() {
return $this->getConfigValue(self::XML_PATH_DEFAULT_STORE_ID);
}
/**
* Get calculate date for order fatch
*
* @return \DateTime time
*/
public function getNetSuiteCountFromDate() {
$dayes = $this->getConfigValue(self::XML_PATH_NETSUITE_COUNT_FROM);
$currenctDate = $this->_date->date('Y-m-d H:i:s');
return $this->_date->date('Y-m-d H:i:s', (strtotime('-' . $dayes . ' day', strtotime($currenctDate))));
}
/**
* Get calculate date for order fatch
*
* @return \DateTime time
*/
public function getCurrentDate() {
return $this->_date->date('Y-m-d H:i:s');
}
public function convertUtcToLocalTime($utcDate, $format = 'Y-m-d H:i:s', $modify = null) {
$date = new \DateTime($utcDate, new \DateTimeZone('UTC'));
if (!empty($modify)) $date = $date->modify($modify);
$date->setTimezone(new \DateTimeZone($this->timezone->getConfigTimezone()));
return $date->format($format);
}
public function convertLocalTimeToUtc($localTimeString, $format = "Y-m-d H:i:s") {
try {
// Assum ing $localTimeString is in a specific format, e.g., "Y-m-d H:i:s"
$date = new \DateTime($localTimeString, new \DateTimeZone($this->timezone->getConfigTimezone()));
if (!$date) {
throw new \Exception("Invalid date format");
}
// Convert to UTC
$date->setTimezone(new \DateTimeZone('UTC'));
return $date->format($format); // Format as needed for the database
} catch (\Exception $e) {
// Handle the exception as needed
return null;
}
}
/**
* Get NetSuite Config value
*
* @return string
*/
public function getConfigValue($path) {
$store = $this->getStoreId();
$config_data = $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $store);
return $config_data;
}
/**
* Get store id
* @return int
*/
public function getStoreId() {
return $this->storeManager->getStore()->getStoreId();
}
public function convertStringToCode($string) {
return trim(preg_replace('/[^a-z0-9]+/', '-', strtolower($string)), '-');
}
public function formatBytes($bytes) {
if ($bytes >= 1073741824) {
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
$bytes = number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
$bytes = number_format($bytes / 1024, 2) . ' KB';
} elseif ($bytes > 1) {
$bytes = $bytes . ' bytes';
} elseif ($bytes == 1) {
$bytes = $bytes . ' byte';
} else {
$bytes = '0 bytes';
}
return $bytes;
}
public function nDigitRandom($digits) {
return rand(pow(10, $digits - 1) - 1, pow(10, $digits) - 1);
}
public function secondsToTime($s) {
$h = floor($s / 3600);
$s -= $h * 3600;
$m = floor($s / 60);
$s -= $m * 60;
return $h . ':' . sprintf('%02d', $m) . ':' . sprintf('%02d', $s);
}
//This function transforms the php.ini notation for numbers (like '2M') to an integer (2*1024*1024 in this case)
public function convertPHPSizeToBytes($sSize) {
if (is_numeric($sSize)) {
return $sSize;
}
$sSuffix = substr($sSize, -1);
$iValue = substr($sSize, 0, -1);
switch (strtoupper($sSuffix)) {
case 'P':
$iValue *= 1024;
break;
case 'T':
$iValue *= 1024;
break;
case 'G':
$iValue *= 1024;
break;
case 'M':
$iValue *= 1024;
break;
case 'K':
$iValue *= 1024;
break;
}
return $iValue;
}
}