sarga/packages/Webkul/Core/src/Core.php

1084 lines
29 KiB
PHP
Raw Normal View History

2018-07-24 11:11:32 +00:00
<?php
namespace Webkul\Core;
2018-08-30 13:22:15 +00:00
use Carbon\Carbon;
use Webkul\Core\Models\Channel;
2018-10-04 06:42:06 +00:00
use Webkul\Core\Repositories\CurrencyRepository;
use Webkul\Core\Repositories\ExchangeRateRepository;
2018-10-12 12:54:10 +00:00
use Webkul\Core\Repositories\CountryRepository;
use Webkul\Core\Repositories\CountryStateRepository;
2018-10-17 07:21:47 +00:00
use Webkul\Core\Repositories\ChannelRepository;
use Webkul\Core\Repositories\LocaleRepository;
use Webkul\Core\Repositories\CoreConfigRepository;
2018-09-27 07:53:26 +00:00
use Illuminate\Support\Facades\Config;
2018-07-24 11:11:32 +00:00
class Core
{
2018-10-04 06:42:06 +00:00
/**
2018-10-17 07:21:47 +00:00
* ChannelRepository class
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Core\Repositories\ChannelRepository
2018-10-17 07:21:47 +00:00
*/
protected $channelRepository;
/**
* CurrencyRepository class
2018-10-04 06:42:06 +00:00
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Core\Repositories\CurrencyRepository
2018-10-04 06:42:06 +00:00
*/
protected $currencyRepository;
/**
2018-10-17 07:21:47 +00:00
* ExchangeRateRepository class
2018-10-04 06:42:06 +00:00
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Core\Repositories\ExchangeRateRepository
2018-10-04 06:42:06 +00:00
*/
protected $exchangeRateRepository;
2018-10-12 12:54:10 +00:00
/**
2018-10-17 07:21:47 +00:00
* CountryRepository class
2018-10-12 12:54:10 +00:00
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Core\Repositories\CountryRepository
2018-10-12 12:54:10 +00:00
*/
protected $countryRepository;
/**
2018-10-17 07:21:47 +00:00
* CountryStateRepository class
2018-10-12 12:54:10 +00:00
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Core\Repositories\CountryStateRepository
2018-10-12 12:54:10 +00:00
*/
protected $countryStateRepository;
2018-10-17 07:21:47 +00:00
/**
* LocaleRepository class
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Core\Repositories\LocaleRepository
2018-10-17 07:21:47 +00:00
*/
protected $localeRepository;
/**
* CoreConfigRepository class
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Core\Repositories\CoreConfigRepository
*/
protected $coreConfigRepository;
2018-10-04 06:42:06 +00:00
/**
* Create a new instance.
*
2020-03-05 14:19:14 +00:00
* @param \Webkul\Core\Repositories\ChannelRepository $channelRepository
* @param \Webkul\Core\Repositories\CurrencyRepository $currencyRepository
2020-03-05 05:34:57 +00:00
* @param \Webkul\Core\Repositories\ExchangeRateRepository $exchangeRateRepository
2020-03-05 14:19:14 +00:00
* @param \Webkul\Core\Repositories\CountryRepository $countryRepository
2020-03-05 05:34:57 +00:00
* @param \Webkul\Core\Repositories\CountryStateRepository $countryStateRepository
2020-03-05 14:19:14 +00:00
* @param \Webkul\Core\Repositories\LocaleRepository $localeRepository
* @param \Webkul\Core\Repositories\CoreConfigRepository $coreConfigRepository
*
2018-10-04 06:42:06 +00:00
* @return void
*/
2018-10-12 08:22:06 +00:00
public function __construct(
2018-10-17 07:21:47 +00:00
ChannelRepository $channelRepository,
2018-10-12 08:22:06 +00:00
CurrencyRepository $currencyRepository,
2018-10-12 12:54:10 +00:00
ExchangeRateRepository $exchangeRateRepository,
CountryRepository $countryRepository,
2018-10-17 07:21:47 +00:00
CountryStateRepository $countryStateRepository,
LocaleRepository $localeRepository,
CoreConfigRepository $coreConfigRepository
2018-10-12 08:22:06 +00:00
)
2018-10-04 06:42:06 +00:00
{
2018-10-17 07:21:47 +00:00
$this->channelRepository = $channelRepository;
2018-10-04 06:42:06 +00:00
$this->currencyRepository = $currencyRepository;
$this->exchangeRateRepository = $exchangeRateRepository;
2018-10-12 12:54:10 +00:00
$this->countryRepository = $countryRepository;
$this->countryStateRepository = $countryStateRepository;
2018-10-17 07:21:47 +00:00
$this->localeRepository = $localeRepository;
$this->coreConfigRepository = $coreConfigRepository;
2018-10-04 06:42:06 +00:00
}
2018-08-21 10:58:55 +00:00
/**
* Returns all channels
*
2020-03-05 05:34:57 +00:00
* @return \Illuminate\Support\Collection
*/
2018-10-17 13:57:51 +00:00
public function getAllChannels()
{
2018-08-30 13:22:15 +00:00
static $channels;
2020-02-19 12:26:12 +00:00
if ($channels) {
2018-08-30 13:22:15 +00:00
return $channels;
2020-02-19 12:26:12 +00:00
}
2018-08-30 13:22:15 +00:00
2018-10-17 07:21:47 +00:00
return $channels = $this->channelRepository->all();
2018-08-21 10:58:55 +00:00
}
/**
* Returns currenct channel models
*
2020-03-05 05:34:57 +00:00
* @return \Webkul\Core\Contracts\Channel
*/
2018-10-17 13:57:51 +00:00
public function getCurrentChannel()
{
2018-08-30 13:22:15 +00:00
static $channel;
2020-02-19 12:26:12 +00:00
if ($channel) {
2018-08-30 13:22:15 +00:00
return $channel;
2020-02-19 12:26:12 +00:00
}
2018-08-30 13:22:15 +00:00
2018-10-17 13:57:51 +00:00
$channel = $this->channelRepository->findWhereIn('hostname', [
request()->getHttpHost(),
'http://' . request()->getHttpHost(),
'https://' . request()->getHttpHost(),
])->first();
2018-10-25 16:15:28 +00:00
2020-02-19 12:26:12 +00:00
if (! $channel) {
2018-10-17 13:57:51 +00:00
$channel = $this->channelRepository->first();
2020-02-19 12:26:12 +00:00
}
2018-10-17 13:57:51 +00:00
return $channel;
2018-08-21 10:58:55 +00:00
}
/**
* Returns currenct channel code
*
2020-03-05 05:34:57 +00:00
* @return \Webkul\Core\Contracts\Channel
*/
public function getCurrentChannelCode(): string
2018-10-17 13:57:51 +00:00
{
2018-08-30 13:22:15 +00:00
static $channelCode;
2020-02-19 12:26:12 +00:00
if ($channelCode) {
2018-08-30 13:22:15 +00:00
return $channelCode;
2020-02-19 12:26:12 +00:00
}
2018-08-30 13:22:15 +00:00
return ($channel = $this->getCurrentChannel()) ? $channelCode = $channel->code : '';
2018-08-21 10:58:55 +00:00
}
2018-10-10 05:50:47 +00:00
/**
* Returns default channel models
*
2020-03-05 05:34:57 +00:00
* @return \Webkul\Core\Contracts\Channel
*/
public function getDefaultChannel(): ?Channel
2018-10-17 13:57:51 +00:00
{
2018-10-10 05:50:47 +00:00
static $channel;
if ($channel) {
return $channel;
}
$channel = $this->channelRepository->findOneByField('code', config('app.channel'));
if ($channel) {
2018-10-10 05:50:47 +00:00
return $channel;
}
2018-10-10 05:50:47 +00:00
2018-10-17 07:21:47 +00:00
return $channel = $this->channelRepository->first();
2018-10-10 05:50:47 +00:00
}
/**
* Returns the default channel code configured in config/app.php
*
* @return string
*/
public function getDefaultChannelCode(): string
2018-10-17 13:57:51 +00:00
{
2018-10-10 05:50:47 +00:00
static $channelCode;
if ($channelCode) {
2018-10-10 05:50:47 +00:00
return $channelCode;
}
2018-10-10 05:50:47 +00:00
return ($channel = $this->getDefaultChannel()) ? $channelCode = $channel->code : '';
}
2018-08-21 10:58:55 +00:00
/**
* Returns all locales
*
2020-03-05 05:34:57 +00:00
* @return \Illuminate\Support\Collection
*/
2018-10-17 13:57:51 +00:00
public function getAllLocales()
{
2018-08-30 13:22:15 +00:00
static $locales;
2020-02-19 12:26:12 +00:00
if ($locales) {
2018-08-30 13:22:15 +00:00
return $locales;
2020-02-19 12:26:12 +00:00
}
2018-08-30 13:22:15 +00:00
2018-10-17 07:21:47 +00:00
return $locales = $this->localeRepository->all();
2018-07-24 11:11:32 +00:00
}
/**
* Returns current locale
*
2020-03-05 05:34:57 +00:00
* @return \Webkul\Core\Contracts\Locale
*/
public function getCurrentLocale()
{
static $locale;
if ($locale) {
return $locale;
}
$locale = $this->localeRepository->findOneByField('code', app()->getLocale());
if (! $locale) {
$locale = $this->localeRepository->findOneByField('code', config('app.fallback_locale'));
}
return $locale;
}
2018-08-21 10:58:55 +00:00
/**
* Returns all currencies
*
2020-03-05 05:34:57 +00:00
* @return \Illuminate\Support\Collection
*/
2018-08-21 10:58:55 +00:00
public function getAllCurrencies()
{
2018-08-30 13:22:15 +00:00
static $currencies;
2020-02-19 12:26:12 +00:00
if ($currencies) {
2018-08-30 13:22:15 +00:00
return $currencies;
2020-02-19 12:26:12 +00:00
}
2018-08-30 13:22:15 +00:00
2018-10-17 07:21:47 +00:00
return $currencies = $this->currencyRepository->all();
2018-07-24 11:11:32 +00:00
}
2018-08-21 10:58:55 +00:00
2018-10-04 06:42:06 +00:00
/**
* Returns base channel's currency model
*
2020-03-05 05:34:57 +00:00
* @return \Webkul\Core\Contracts\Currency
*/
2018-10-04 06:42:06 +00:00
public function getBaseCurrency()
{
static $currency;
2020-02-19 12:26:12 +00:00
if ($currency) {
2018-10-04 06:42:06 +00:00
return $currency;
2020-02-19 12:26:12 +00:00
}
2018-10-04 06:42:06 +00:00
2018-10-17 07:21:47 +00:00
$baseCurrency = $this->currencyRepository->findOneByField('code', config('app.currency'));
2018-10-04 06:42:06 +00:00
2020-02-19 12:26:12 +00:00
if (! $baseCurrency) {
2018-10-17 07:21:47 +00:00
$baseCurrency = $this->currencyRepository->first();
2020-02-19 12:26:12 +00:00
}
2018-10-17 07:21:47 +00:00
return $currency = $baseCurrency;
2018-10-04 06:42:06 +00:00
}
/**
* Returns base channel's currency code
*
* @return string
*/
2018-10-04 06:42:06 +00:00
public function getBaseCurrencyCode()
{
static $currencyCode;
2020-02-19 12:26:12 +00:00
if ($currencyCode) {
2018-10-04 06:42:06 +00:00
return $currencyCode;
2020-02-19 12:26:12 +00:00
}
2018-10-04 06:42:06 +00:00
return ($currency = $this->getBaseCurrency()) ? $currencyCode = $currency->code : '';
}
2018-10-17 07:21:47 +00:00
/**
* Returns base channel's currency model
*
2020-03-05 05:34:57 +00:00
* @return \Webkul\Core\Contracts\Currency
*/
2018-10-17 07:21:47 +00:00
public function getChannelBaseCurrency()
{
static $currency;
2020-02-19 12:26:12 +00:00
if ($currency) {
2018-10-17 07:21:47 +00:00
return $currency;
2020-02-19 12:26:12 +00:00
}
2018-10-17 07:21:47 +00:00
$currenctChannel = $this->getCurrentChannel();
return $currency = $currenctChannel->base_currency;
}
/**
* Returns base channel's currency code
*
* @return string
*/
2018-10-17 07:21:47 +00:00
public function getChannelBaseCurrencyCode()
{
static $currencyCode;
2020-02-19 12:26:12 +00:00
if ($currencyCode) {
2018-10-17 07:21:47 +00:00
return $currencyCode;
2020-02-19 12:26:12 +00:00
}
2018-10-17 07:21:47 +00:00
return ($currency = $this->getChannelBaseCurrency()) ? $currencyCode = $currency->code : '';
}
2018-08-21 10:58:55 +00:00
/**
* Returns current channel's currency model
*
2020-03-05 05:34:57 +00:00
* @return \Webkul\Core\Contracts\Currency
*/
2018-08-21 10:58:55 +00:00
public function getCurrentCurrency()
{
2018-08-30 13:22:15 +00:00
static $currency;
2018-08-21 10:58:55 +00:00
2020-02-19 12:26:12 +00:00
if ($currency) {
2018-08-30 13:22:15 +00:00
return $currency;
2020-02-19 12:26:12 +00:00
}
2018-08-21 10:58:55 +00:00
2019-01-15 11:54:41 +00:00
if ($currencyCode = session()->get('currency')) {
2020-02-19 12:26:12 +00:00
if ($currency = $this->currencyRepository->findOneByField('code', $currencyCode)) {
return $currency;
2020-02-19 12:26:12 +00:00
}
}
return $currency = $this->getChannelBaseCurrency();
2018-08-21 10:58:55 +00:00
}
/**
* Returns current channel's currency code
*
* @return string
*/
2018-08-21 10:58:55 +00:00
public function getCurrentCurrencyCode()
{
2018-08-30 13:22:15 +00:00
static $currencyCode;
2020-02-19 12:26:12 +00:00
if ($currencyCode) {
2018-08-30 13:22:15 +00:00
return $currencyCode;
2020-02-19 12:26:12 +00:00
}
2018-08-30 13:22:15 +00:00
return ($currency = $this->getCurrentCurrency()) ? $currencyCode = $currency->code : '';
2018-08-21 10:58:55 +00:00
}
2018-09-26 10:19:18 +00:00
/**
* Converts price
*
2020-03-05 13:37:08 +00:00
* @param float $amount
2020-03-05 05:34:57 +00:00
* @param string $targetCurrencyCode
* @param string $orderCurrencyCode
* @return string
*/
2020-02-24 08:15:11 +00:00
public function convertPrice($amount, $targetCurrencyCode = null, $orderCurrencyCode = null)
2018-09-26 10:19:18 +00:00
{
2020-02-19 12:26:12 +00:00
if (! isset($this->lastCurrencyCode)) {
$this->lastCurrencyCode = $this->getBaseCurrency()->code;
2020-02-19 12:26:12 +00:00
}
2020-02-03 08:09:13 +00:00
2020-02-24 08:15:11 +00:00
if ($orderCurrencyCode) {
if (! isset($this->lastOrderCode)) {
$this->lastOrderCode = $orderCurrencyCode;
}
2020-02-25 06:14:34 +00:00
if (($targetCurrencyCode != $this->lastOrderCode)
&& ($targetCurrencyCode != $orderCurrencyCode)
&& ($orderCurrencyCode != $this->getBaseCurrencyCode())
2020-02-28 10:25:53 +00:00
&& ($orderCurrencyCode != $this->lastCurrencyCode)
) {
$amount = $this->convertToBasePrice($amount, $orderCurrencyCode);
2020-02-24 08:15:11 +00:00
}
}
2020-02-03 08:09:13 +00:00
2019-04-16 10:10:58 +00:00
$targetCurrency = ! $targetCurrencyCode
2020-02-27 08:03:03 +00:00
? $this->getCurrentCurrency()
: $this->currencyRepository->findOneByField('code', $targetCurrencyCode);
2018-10-04 06:42:06 +00:00
2020-02-19 12:26:12 +00:00
if (! $targetCurrency) {
2018-10-04 06:42:06 +00:00
return $amount;
2020-02-19 12:26:12 +00:00
}
2018-10-04 06:42:06 +00:00
$exchangeRate = $this->exchangeRateRepository->findOneWhere([
'target_currency' => $targetCurrency->id,
]);
2020-02-19 12:26:12 +00:00
if (null === $exchangeRate || ! $exchangeRate->rate) {
2018-10-04 06:42:06 +00:00
return $amount;
2020-02-19 12:26:12 +00:00
}
2018-10-04 06:42:06 +00:00
$result = (float)$amount * (float)($this->lastCurrencyCode == $targetCurrency->code ? 1.0 : $exchangeRate->rate);
2020-02-03 08:09:13 +00:00
2020-02-19 12:26:12 +00:00
if ($this->lastCurrencyCode != $targetCurrency->code) {
$this->lastCurrencyCode = $targetCurrency->code;
2020-02-19 12:26:12 +00:00
}
2020-02-03 08:09:13 +00:00
return $result;
2018-09-26 10:19:18 +00:00
}
/**
* Converts to base price
*
2020-03-05 13:37:08 +00:00
* @param float $amount
2020-03-05 05:34:57 +00:00
* @param string $targetCurrencyCode
* @return string
*/
public function convertToBasePrice($amount, $targetCurrencyCode = null)
{
$targetCurrency = ! $targetCurrencyCode
2020-02-27 08:03:03 +00:00
? $this->getCurrentCurrency()
: $this->currencyRepository->findOneByField('code', $targetCurrencyCode);
2020-02-19 12:26:12 +00:00
if (! $targetCurrency) {
return $amount;
2020-02-19 12:26:12 +00:00
}
$exchangeRate = $this->exchangeRateRepository->findOneWhere([
'target_currency' => $targetCurrency->id,
]);
2020-02-19 12:26:12 +00:00
if (null === $exchangeRate || ! $exchangeRate->rate) {
return $amount;
2020-02-19 12:26:12 +00:00
}
return (float)$amount / $exchangeRate->rate;
}
2018-08-21 10:58:55 +00:00
/**
* Format and convert price with currency symbol
*
2020-03-05 05:34:57 +00:00
* @param float $price
* @return string
*/
2018-10-04 06:42:06 +00:00
public function currency($amount = 0)
2018-08-21 10:58:55 +00:00
{
2020-02-19 12:26:12 +00:00
if (is_null($amount)) {
2018-10-12 08:22:06 +00:00
$amount = 0;
2020-02-19 12:26:12 +00:00
}
return $this->formatPrice($this->convertPrice($amount), $this->getCurrentCurrency()->code);
2018-08-21 10:58:55 +00:00
}
2018-12-21 06:28:02 +00:00
/**
* Return currency symbol from currency code
*
2020-03-05 05:34:57 +00:00
* @param float $price
* @return string
*/
2018-12-21 06:28:02 +00:00
public function currencySymbol($code)
{
$formatter = new \NumberFormatter(app()->getLocale() . '@currency=' . $code, \NumberFormatter::CURRENCY);
return $formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);
2018-12-21 06:28:02 +00:00
}
2020-02-24 08:15:11 +00:00
/**
* Format and convert price with currency symbol
*
2020-03-05 13:37:08 +00:00
* @param float $price
2020-03-05 05:34:57 +00:00
* @param string $currencyCode
* @return string
*/
2018-09-26 10:19:18 +00:00
public function formatPrice($price, $currencyCode)
{
2020-02-24 08:15:11 +00:00
$code = $this->getCurrentCurrency()->code;
2020-02-19 12:26:12 +00:00
if (is_null($price)) {
$price = 0;
2020-02-24 08:15:11 +00:00
} else {
if ($code != $currencyCode) {
$price = $this->convertPrice($price, $code, $currencyCode);
}
2020-02-19 12:26:12 +00:00
}
$formater = new \NumberFormatter(app()->getLocale(), \NumberFormatter::CURRENCY);
2020-02-24 08:15:11 +00:00
$symbol = $this->getCurrentCurrency()->symbol;
if ($symbol) {
if ($this->currencySymbol($currencyCode) == $symbol) {
return $formater->formatCurrency($price, $currencyCode);
} else {
$formater->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $symbol);
2020-02-24 08:15:11 +00:00
return $formater->format($price); // $this->convertPrice($price, $code)
}
} else {
return $formater->formatCurrency($price, $currencyCode);
}
2018-09-26 10:19:18 +00:00
}
2019-08-30 11:39:15 +00:00
/**
* Format and convert price with currency symbol
*
* @return array
*/
2019-08-30 11:39:15 +00:00
public function getAccountJsSymbols()
{
$formater = new \NumberFormatter(app()->getLocale(), \NumberFormatter::CURRENCY);
2019-08-30 11:39:15 +00:00
$pattern = $formater->getPattern();
$pattern = str_replace("¤", "%s", $pattern);
$pattern = str_replace("#,##0.00", "%v", $pattern);
return [
'symbol' => core()->currencySymbol(core()->getCurrentCurrencyCode()),
2019-08-30 11:39:15 +00:00
'decimal' => $formater->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL),
'format' => $pattern,
2019-08-30 11:39:15 +00:00
];
}
/**
* Format price with base currency symbol
*
2020-03-05 05:34:57 +00:00
* @param float $price
* @return string
*/
public function formatBasePrice($price)
{
2020-02-19 12:26:12 +00:00
if (is_null($price)) {
$price = 0;
2020-02-19 12:26:12 +00:00
}
$formater = new \NumberFormatter(app()->getLocale(), \NumberFormatter::CURRENCY);
2018-10-25 16:15:28 +00:00
if ($symbol = $this->getBaseCurrency()->symbol) {
if ($this->currencySymbol($this->getBaseCurrencyCode()) == $symbol) {
return $formater->formatCurrency($price, $this->getBaseCurrencyCode());
} else {
$formater->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $symbol);
return $formater->format($this->convertPrice($price));
}
} else {
return $formater->formatCurrency($price, $this->getBaseCurrencyCode());
}
}
2018-08-21 10:58:55 +00:00
/**
* Checks if current date of the given channel (in the channel timezone) is within the range
*
2020-03-05 05:34:57 +00:00
* @param int|string|\Webkul\Core\Contracts\Channel $channel
2020-03-05 13:37:08 +00:00
* @param string|null $dateFrom
* @param string|null $dateTo
2018-08-21 10:58:55 +00:00
* @return bool
*/
public function isChannelDateInInterval($dateFrom = null, $dateTo = null)
{
$channel = $this->getCurrentChannel();
$channelTimeStamp = $this->channelTimeStamp($channel);
$fromTimeStamp = strtotime($dateFrom);
2018-08-21 10:58:55 +00:00
$toTimeStamp = strtotime($dateTo);
2018-08-21 10:58:55 +00:00
if ($dateTo) {
$toTimeStamp += 86400;
}
2019-01-15 11:54:41 +00:00
if (! $this->is_empty_date($dateFrom) && $channelTimeStamp < $fromTimeStamp) {
2019-01-29 10:35:43 +00:00
$result = false;
2020-02-24 12:19:12 +00:00
} elseif (! $this->is_empty_date($dateTo) && $channelTimeStamp > $toTimeStamp) {
2019-01-29 10:35:43 +00:00
$result = false;
2018-08-21 10:58:55 +00:00
} else {
$result = true;
}
return $result;
}
/**
2020-03-05 05:34:57 +00:00
* Get channel timestamp, timstamp will be builded with channel timezone settings
*
2020-03-05 05:34:57 +00:00
* @param \Webkul\Core\Contracts\Channel $channel
2018-08-21 10:58:55 +00:00
* @return int
*/
public function channelTimeStamp($channel)
{
$timezone = $channel->timezone;
$currentTimezone = @date_default_timezone_get();
@date_default_timezone_set($timezone);
$date = date('Y-m-d H:i:s');
@date_default_timezone_set($currentTimezone);
return strtotime($date);
}
/**
* Check whether sql date is empty
*
2020-03-05 05:34:57 +00:00
* @param string $date
* @return bool
*/
2018-08-21 10:58:55 +00:00
function is_empty_date($date)
{
return preg_replace('#[ 0:-]#', '', $date) === '';
}
2018-08-30 13:22:15 +00:00
/**
* Format date using current channel.
*
2020-03-05 05:34:57 +00:00
* @param \Illuminate\Support\Carbon|null $date
2020-03-05 13:37:08 +00:00
* @param string $format
*
2018-08-30 13:22:15 +00:00
* @return string
*/
public function formatDate($date = null, $format = 'd-m-Y H:i:s')
{
$channel = $this->getCurrentChannel();
if (is_null($date)) {
$date = Carbon::now();
}
$date->setTimezone($channel->timezone);
return $date->format($format);
}
2018-09-27 07:53:26 +00:00
/**
* Retrieve information from payment configuration
*
2020-03-05 13:37:08 +00:00
* @param string $field
2020-03-05 05:34:57 +00:00
* @param int|string|null $channelId
2020-03-05 13:37:08 +00:00
* @param string|null $locale
2018-09-27 07:53:26 +00:00
*
* @return mixed
*/
public function getConfigData($field, $channel = null, $locale = null)
2018-09-27 07:53:26 +00:00
{
if (null === $channel) {
2018-12-07 07:08:29 +00:00
$channel = request()->get('channel') ?: ($this->getCurrentChannelCode() ?: $this->getDefaultChannelCode());
}
if (null === $locale) {
2018-12-07 07:08:29 +00:00
$locale = request()->get('locale') ?: app()->getLocale();
2018-09-27 07:53:26 +00:00
}
2018-12-14 06:26:52 +00:00
$fields = $this->getConfigField($field);
$channel_based = false;
$locale_based = false;
if (isset($fields['channel_based']) && $fields['channel_based']) {
$channel_based = true;
}
if (isset($fields['locale_based']) && $fields['locale_based']) {
$locale_based = true;
}
if (isset($fields['channel_based']) && $fields['channel_based']) {
if (isset($fields['locale_based']) && $fields['locale_based']) {
$coreConfigValue = $this->coreConfigRepository->findOneWhere([
'code' => $field,
2018-12-14 06:26:52 +00:00
'channel_code' => $channel,
'locale_code' => $locale,
2018-12-14 06:26:52 +00:00
]);
} else {
$coreConfigValue = $this->coreConfigRepository->findOneWhere([
'code' => $field,
2018-12-14 06:26:52 +00:00
'channel_code' => $channel,
]);
}
} else {
if (isset($fields['locale_based']) && $fields['locale_based']) {
$coreConfigValue = $this->coreConfigRepository->findOneWhere([
'code' => $field,
'locale_code' => $locale,
2018-12-14 06:26:52 +00:00
]);
} else {
2018-12-14 06:26:52 +00:00
$coreConfigValue = $this->coreConfigRepository->findOneWhere([
'code' => $field,
2018-12-14 06:26:52 +00:00
]);
}
}
2019-01-15 11:54:41 +00:00
if (! $coreConfigValue) {
2018-12-26 09:16:27 +00:00
$fields = explode(".", $field);
2020-03-05 05:34:57 +00:00
2018-12-26 09:16:27 +00:00
array_shift($fields);
2020-03-05 05:34:57 +00:00
2018-12-26 09:16:27 +00:00
$field = implode(".", $fields);
2018-12-07 07:08:29 +00:00
return Config::get($field);
2018-12-26 09:16:27 +00:00
}
2018-12-07 07:08:29 +00:00
return $coreConfigValue->value;
2018-09-27 07:53:26 +00:00
}
2018-10-12 08:22:06 +00:00
2019-06-25 06:57:13 +00:00
/**
* Retrieve a group of information from the core config table
*
2020-03-05 05:34:57 +00:00
* @param mixed $criteria
* @return mixed
2019-06-25 06:57:13 +00:00
*/
public function retrieveGroupConfig($criteria)
{
return $criteria;
}
2018-10-12 08:22:06 +00:00
/**
* Retrieve all countries
*
2020-03-05 05:34:57 +00:00
* @return \Illuminate\Support\Collection
2018-10-12 08:22:06 +00:00
*/
public function countries()
{
2018-10-12 12:54:10 +00:00
return $this->countryRepository->all();
2018-10-12 08:22:06 +00:00
}
/**
* Returns country name by code
*
2020-03-05 05:34:57 +00:00
* @param string $code
* @return string
*/
public function country_name($code)
{
$country = $this->countryRepository->findOneByField('code', $code);
return $country ? $country->name : '';
}
2018-10-12 08:22:06 +00:00
/**
* Retrieve all country states
*
2020-03-05 05:34:57 +00:00
* @param string $countryCode
* @return \Illuminate\Support\Collection
2018-10-12 08:22:06 +00:00
*/
public function states($countryCode)
{
2018-10-12 12:54:10 +00:00
return $this->countryStateRepository->findByField('country_code', $countryCode);
2018-10-12 08:22:06 +00:00
}
/**
* Retrieve all grouped states by country code
*
2020-03-05 05:34:57 +00:00
* @return \Illuminate\Support\Collection
2018-10-12 08:22:06 +00:00
*/
public function groupedStatesByCountries()
{
$collection = [];
2018-12-27 06:05:10 +00:00
foreach ($this->countryStateRepository->all() as $state) {
$collection[$state->country_code][] = $state->toArray();
2018-10-12 08:22:06 +00:00
}
return $collection;
}
2018-10-23 11:45:44 +00:00
/**
* Retrieve all grouped states by country code
*
2020-03-05 05:34:57 +00:00
* @return \Illuminate\Support\Collection
*/
public function findStateByCountryCode($countryCode = null, $stateCode = null)
{
$collection = array();
$collection = $this->countryStateRepository->findByField(['country_code' => $countryCode, 'code' => $stateCode]);
if (count($collection)) {
return $collection->first();
} else {
return false;
}
}
2018-10-23 11:45:44 +00:00
/**
* Returns time intervals
*
2020-03-05 05:34:57 +00:00
* @param \Illuminate\Support\Carbon $startDate
* @param \Illuminate\Support\Carbon $endDate
2018-10-23 11:45:44 +00:00
* @return array
*/
public function getTimeInterval($startDate, $endDate)
{
2018-10-23 11:45:44 +00:00
$timeIntervals = [];
2018-10-26 04:20:43 +00:00
$totalDays = $startDate->diffInDays($endDate) + 1;
$totalMonths = $startDate->diffInMonths($endDate) + 1;
2018-10-25 16:15:28 +00:00
2018-10-23 11:45:44 +00:00
$startWeekDay = Carbon::createFromTimeString($this->xWeekRange($startDate, 0) . ' 00:00:01');
$endWeekDay = Carbon::createFromTimeString($this->xWeekRange($endDate, 1) . ' 23:59:59');
$totalWeeks = $startWeekDay->diffInWeeks($endWeekDay);
2019-01-15 11:54:41 +00:00
if ($totalMonths > 5) {
2018-10-25 16:15:28 +00:00
for ($i = 0; $i < $totalMonths; $i++) {
2018-10-23 11:45:44 +00:00
$date = clone $startDate;
$date->addMonths($i);
$start = Carbon::createFromTimeString($date->format('Y-m-d') . ' 00:00:01');
$end = $totalMonths - 1 == $i
2020-02-27 08:03:03 +00:00
? $endDate
: Carbon::createFromTimeString($date->format('Y-m-d') . ' 23:59:59');
2018-10-23 11:45:44 +00:00
$timeIntervals[] = ['start' => $start, 'end' => $end, 'formatedDate' => $date->format('M')];
}
2020-02-24 12:19:12 +00:00
} elseif ($totalWeeks > 6) {
2018-10-25 16:15:28 +00:00
for ($i = 0; $i < $totalWeeks; $i++) {
2018-10-23 11:45:44 +00:00
$date = clone $startDate;
$date->addWeeks($i);
$start = $i == 0
2020-02-27 08:03:03 +00:00
? $startDate
: Carbon::createFromTimeString($this->xWeekRange($date, 0) . ' 00:00:01');
2018-10-23 11:45:44 +00:00
$end = $totalWeeks - 1 == $i
2020-02-27 08:03:03 +00:00
? $endDate
: Carbon::createFromTimeString($this->xWeekRange($date, 1) . ' 23:59:59');
2018-10-23 11:45:44 +00:00
$timeIntervals[] = ['start' => $start, 'end' => $end, 'formatedDate' => $date->format('d M')];
}
} else {
2018-10-25 16:15:28 +00:00
for ($i = 0; $i < $totalDays; $i++) {
2018-10-23 11:45:44 +00:00
$date = clone $startDate;
$date->addDays($i);
$start = Carbon::createFromTimeString($date->format('Y-m-d') . ' 00:00:01');
$end = Carbon::createFromTimeString($date->format('Y-m-d') . ' 23:59:59');
$timeIntervals[] = ['start' => $start, 'end' => $end, 'formatedDate' => $date->format('d M')];
}
}
return $timeIntervals;
}
/**
2020-03-05 05:34:57 +00:00
*
* @param string $date
2020-03-05 13:37:08 +00:00
* @param int $day
2018-10-23 11:45:44 +00:00
* @return string
*/
public function xWeekRange($date, $day)
{
2018-10-23 11:45:44 +00:00
$ts = strtotime($date);
2019-01-15 11:54:41 +00:00
if (! $day) {
2018-10-23 11:45:44 +00:00
$start = (date('D', $ts) == 'Sun') ? $ts : strtotime('last sunday', $ts);
return date('Y-m-d', $start);
} else {
$end = (date('D', $ts) == 'Sat') ? $ts : strtotime('next saturday', $ts);
return date('Y-m-d', $end);
}
}
2018-12-14 06:26:52 +00:00
/**
* Method to sort through the acl items and put them in order
*
2020-03-05 05:34:57 +00:00
* @param array $items
* @return array
*/
public function sortItems($items)
{
foreach ($items as &$item) {
if (count($item['children'])) {
$item['children'] = $this->sortItems($item['children']);
}
}
usort($items, function ($a, $b) {
if ($a['sort'] == $b['sort']) {
return 0;
}
return ($a['sort'] < $b['sort']) ? -1 : 1;
});
return $this->convertToAssociativeArray($items);
2018-12-14 06:26:52 +00:00
}
/**
2020-03-05 05:34:57 +00:00
* @param string $fieldName
2018-12-14 06:26:52 +00:00
* @return array
*/
public function getConfigField($fieldName)
{
2018-12-14 06:26:52 +00:00
foreach (config('core') as $coreData) {
if (isset($coreData['fields'])) {
foreach ($coreData['fields'] as $field) {
2018-12-26 09:16:27 +00:00
$name = $coreData['key'] . '.' . $field['name'];
2018-12-14 06:26:52 +00:00
if ($name == $fieldName) {
2018-12-14 06:26:52 +00:00
return $field;
}
}
}
}
}
2020-03-05 05:34:57 +00:00
/**
* @param array $items
* @return array
*/
public function convertToAssociativeArray($items)
{
foreach ($items as $key1 => $level1) {
unset($items[$key1]);
$items[$level1['key']] = $level1;
if (count($level1['children'])) {
foreach ($level1['children'] as $key2 => $level2) {
$temp2 = explode('.', $level2['key']);
$finalKey2 = end($temp2);
unset($items[$level1['key']]['children'][$key2]);
$items[$level1['key']]['children'][$finalKey2] = $level2;
2019-01-15 11:54:41 +00:00
if (count($level2['children'])) {
foreach ($level2['children'] as $key3 => $level3) {
$temp3 = explode('.', $level3['key']);
$finalKey3 = end($temp3);
unset($items[$level1['key']]['children'][$finalKey2]['children'][$key3]);
$items[$level1['key']]['children'][$finalKey2]['children'][$finalKey3] = $level3;
}
}
}
}
}
return $items;
}
2020-03-05 05:34:57 +00:00
/**
2020-03-05 13:37:08 +00:00
* @param array $items
* @param string $key
2020-03-05 05:34:57 +00:00
* @param string|int|float $value
* @return array
*/
public function array_set(&$array, $key, $value)
{
if (is_null($key)) {
return $array = $value;
}
$keys = explode('.', $key);
$count = count($keys);
while (count($keys) > 1) {
$key = array_shift($keys);
if (! isset($array[$key]) || ! is_array($array[$key])) {
$array[$key] = [];
}
$array = &$array[$key];
}
$finalKey = array_shift($keys);
2020-02-27 08:03:03 +00:00
if (isset($array[$finalKey])) {
$array[$finalKey] = $this->arrayMerge($array[$finalKey], $value);
} else {
$array[$finalKey] = $value;
}
return $array;
}
2020-03-05 05:34:57 +00:00
/**
* @param array $array1
* @param array $array2
* @return array
*/
protected function arrayMerge(array &$array1, array &$array2)
{
$merged = $array1;
2020-02-27 08:03:03 +00:00
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = $this->arrayMerge($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}
2020-03-05 05:34:57 +00:00
/**
* @param array $array1
* @return array
*/
2019-04-18 07:22:51 +00:00
public function convertEmptyStringsToNull($array)
{
foreach ($array as $key => $value) {
if ($value == "" || $value == "null") {
$array[$key] = null;
}
}
return $array;
}
2019-04-18 07:22:51 +00:00
/**
* Create singletom object through single facade
*
2020-03-05 05:34:57 +00:00
* @param string $className
2019-04-18 07:22:51 +00:00
* @return object
*/
public function getSingletonInstance($className)
{
static $instance = [];
2020-02-19 12:26:12 +00:00
if (array_key_exists($className, $instance)) {
2019-04-18 07:22:51 +00:00
return $instance[$className];
2020-02-19 12:26:12 +00:00
}
2019-04-18 07:22:51 +00:00
return $instance[$className] = app($className);
}
2020-02-04 10:18:40 +00:00
/**
* Returns a string as selector part for identifying elements in views
2020-03-05 05:34:57 +00:00
*
* @param float $taxRate
2020-02-04 10:18:40 +00:00
* @return string
*/
public static function taxRateAsIdentifier(float $taxRate): string
{
return str_replace('.', '_', (string)$taxRate);
}
/**
* Get Shop email sender details
*
* @return array
*/
public function getSenderEmailDetails()
{
$sender_name = core()->getConfigData('general.general.email_settings.sender_name') ? core()->getConfigData('general.general.email_settings.sender_name') : config('mail.from.name');
$sender_email = core()->getConfigData('general.general.email_settings.shop_email_from') ? core()->getConfigData('general.general.email_settings.shop_email_from') : config('mail.from.address');
return [
'name' => $sender_name,
'email' => $sender_email,
];
}
/**
* Get Admin email details
*
* @return array
*/
public function getAdminEmailDetails()
{
$admin_name = core()->getConfigData('general.general.email_settings.admin_name') ? core()->getConfigData('general.general.email_settings.admin_name') : config('mail.admin.name');
$admin_email = core()->getConfigData('general.general.email_settings.admin_email') ? core()->getConfigData('general.general.email_settings.admin_email') : config('mail.admin.address');
return [
'name' => $admin_name,
'email' => $admin_email,
];
}
2018-07-24 11:11:32 +00:00
}