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

540 lines
12 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;
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;
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
*
* @var mixed
*/
protected $channelRepository;
/**
* CurrencyRepository class
2018-10-04 06:42:06 +00:00
*
* @var mixed
*/
protected $currencyRepository;
/**
2018-10-17 07:21:47 +00:00
* ExchangeRateRepository class
2018-10-04 06:42:06 +00:00
*
* @var mixed
*/
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
*
* @var mixed
*/
protected $countryRepository;
/**
2018-10-17 07:21:47 +00:00
* CountryStateRepository class
2018-10-12 12:54:10 +00:00
*
* @var mixed
*/
protected $countryStateRepository;
2018-10-17 07:21:47 +00:00
/**
* LocaleRepository class
*
* @var mixed
*/
protected $localeRepository;
2018-10-04 06:42:06 +00:00
/**
* Create a new instance.
*
2018-10-17 07:21:47 +00:00
* @param Webkul\Core\Repositories\ChannelRepository $channelRepository
2018-10-04 06:42:06 +00:00
* @param Webkul\Core\Repositories\CurrencyRepository $currencyRepository
* @param Webkul\Core\Repositories\ExchangeRateRepository $exchangeRateRepository
2018-10-12 12:54:10 +00:00
* @param Webkul\Core\Repositories\CountryRepository $countryRepository
* @param Webkul\Core\Repositories\CountryStateRepository $countryStateRepository
2018-10-17 07:21:47 +00:00
* @param Webkul\Core\Repositories\LocaleRepository $localeRepository
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
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;
2018-10-04 06:42:06 +00:00
}
2018-08-21 10:58:55 +00:00
/**
* Returns all channels
*
* @return Collection
*/
public function getAllChannels() {
2018-08-30 13:22:15 +00:00
static $channels;
if($channels)
return $channels;
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
*
* @return mixed
*/
public function getCurrentChannel() {
2018-08-30 13:22:15 +00:00
static $channel;
if($channel)
return $channel;
2018-10-17 07:21:47 +00:00
return $channel = $this->channelRepository->first();
2018-08-21 10:58:55 +00:00
}
/**
* Returns currenct channel code
*
* @return string
*/
public function getCurrentChannelCode() {
2018-08-30 13:22:15 +00:00
static $channelCode;
if($channelCode)
return $channelCode;
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
*
* @return mixed
*/
public function getDefaultChannel() {
static $channel;
if($channel)
return $channel;
2018-10-17 07:21:47 +00:00
return $channel = $this->channelRepository->first();
2018-10-10 05:50:47 +00:00
}
/**
* Returns default channel code
*
* @return string
*/
public function getDefaultChannelCode() {
static $channelCode;
if($channelCode)
return $channelCode;
return ($channel = $this->getDefaultChannel()) ? $channelCode = $channel->code : '';
}
2018-08-21 10:58:55 +00:00
/**
* Returns all locales
*
* @return Collection
*/
2018-08-09 04:35:13 +00:00
public function getAllLocales() {
2018-08-30 13:22:15 +00:00
static $locales;
if($locales)
return $locales;
2018-10-17 07:21:47 +00:00
return $locales = $this->localeRepository->all();
2018-07-24 11:11:32 +00:00
}
2018-08-21 10:58:55 +00:00
/**
* Returns all currencies
*
* @return Collection
*/
public function getAllCurrencies()
{
2018-08-30 13:22:15 +00:00
static $currencies;
if($currencies)
return $currencies;
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
*
* @return mixed
*/
public function getBaseCurrency()
{
static $currency;
if($currency)
return $currency;
2018-10-17 07:21:47 +00:00
$baseCurrency = $this->currencyRepository->findOneByField('code', config('app.currency'));
2018-10-04 06:42:06 +00:00
2018-10-17 07:21:47 +00:00
if(!$baseCurrency)
$baseCurrency = $this->currencyRepository->first();
return $currency = $baseCurrency;
2018-10-04 06:42:06 +00:00
}
/**
* Returns base channel's currency code
*
* @return string
*/
public function getBaseCurrencyCode()
{
static $currencyCode;
if($currencyCode)
return $currencyCode;
return ($currency = $this->getBaseCurrency()) ? $currencyCode = $currency->code : '';
}
/**
* Returns base channel's currency symbol
*
* @return string
*/
public function getBaseCurrencySymbol()
{
static $currencySymbol;
if($currencySymbol)
return $currencySymbol;
2018-10-17 07:21:47 +00:00
return $currencySymbol = $this->getBaseCurrency()->symbol ?? $this->getBaseCurrencyCode();
}
/**
* Returns base channel's currency model
*
* @return mixed
*/
public function getChannelBaseCurrency()
{
static $currency;
if($currency)
return $currency;
$currenctChannel = $this->getCurrentChannel();
return $currency = $currenctChannel->base_currency;
}
/**
* Returns base channel's currency code
*
* @return string
*/
public function getChannelBaseCurrencyCode()
{
static $currencyCode;
if($currencyCode)
return $currencyCode;
return ($currency = $this->getChannelBaseCurrency()) ? $currencyCode = $currency->code : '';
}
/**
* Returns base channel's currency symbol
*
* @return string
*/
public function getChannelBaseCurrencySymbol()
{
static $currencySymbol;
if($currencySymbol)
return $currencySymbol;
return $currencySymbol = $this->getChannelBaseCurrency()->symbol;
2018-10-04 06:42:06 +00:00
}
2018-08-21 10:58:55 +00:00
/**
* Returns current channel's currency model
*
* @return mixed
*/
public function getCurrentCurrency()
{
2018-08-30 13:22:15 +00:00
static $currency;
2018-08-21 10:58:55 +00:00
2018-08-30 13:22:15 +00:00
if($currency)
return $currency;
2018-08-21 10:58:55 +00:00
2018-10-17 07:21:47 +00:00
return $currency = $this->currencyRepository->first();
2018-08-21 10:58:55 +00:00
}
/**
* Returns current channel's currency code
*
* @return string
*/
public function getCurrentCurrencyCode()
{
2018-08-30 13:22:15 +00:00
static $currencyCode;
if($currencyCode)
return $currencyCode;
return ($currency = $this->getCurrentCurrency()) ? $currencyCode = $currency->code : '';
2018-08-21 10:58:55 +00:00
}
/**
* Returns current channel's currency symbol
*
* @return string
*/
public function getCurrentCurrencySymbol()
{
2018-08-30 13:22:15 +00:00
static $currencySymbol;
if($currencySymbol)
return $currencySymbol;
return $currencySymbol = $this->getCurrentCurrency()->symbol;
2018-08-21 10:58:55 +00:00
}
2018-09-26 10:19:18 +00:00
/**
2018-10-17 07:21:47 +00:00
* Converts price
2018-09-26 10:19:18 +00:00
*
2018-10-17 07:21:47 +00:00
* @param float $price
* @param string $targetCurrencyCode
* @return string
2018-09-26 10:19:18 +00:00
*/
2018-10-17 07:21:47 +00:00
public function convertPrice($amount, $targetCurrencyCode = null)
2018-09-26 10:19:18 +00:00
{
2018-10-04 06:42:06 +00:00
$targetCurrency = !$targetCurrencyCode
? $this->getCurrentCurrency()
: $this->currencyRepository->findByField('code', $targetCurrencyCode);
2018-10-17 07:21:47 +00:00
if(!$targetCurrency)
2018-10-04 06:42:06 +00:00
return $amount;
$exchangeRate = $this->exchangeRateRepository->findOneWhere([
'target_currency' => $targetCurrency->id,
]);
if (null === $exchangeRate)
return $amount;
2018-10-17 07:21:47 +00:00
return (float) round($amount * $exchangeRate->rate);
2018-09-26 10:19:18 +00:00
}
2018-08-21 10:58:55 +00:00
/**
* Format and convert price with currency symbol
*
* @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
{
if(is_null($amount))
2018-10-12 08:22:06 +00:00
$amount = 0;
2018-10-04 06:42:06 +00:00
$currencyCode = $this->getCurrentCurrency()->code;
2018-10-04 06:42:06 +00:00
return currency($this->convertPrice($amount), $currencyCode);
2018-08-21 10:58:55 +00:00
}
2018-09-26 10:19:18 +00:00
/**
* Format and convert price with currency symbol
*
* @param float $price
* @return string
*/
public function formatPrice($price, $currencyCode)
{
if(is_null($price))
$price = 0;
2018-09-26 10:19:18 +00:00
return currency($price, $currencyCode);
}
/**
* Format price with base currency symbol
*
* @param float $price
* @return string
*/
public function formatBasePrice($price)
{
if(is_null($price))
$price = 0;
return currency($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
*
* @param int|string|Channel $channel
* @param string|null $dateFrom
* @param string|null $dateTo
* @return bool
*/
public function isChannelDateInInterval($dateFrom = null, $dateTo = null)
{
$channel = $this->getCurrentChannel();
$channelTimeStamp = $this->channelTimeStamp($channel);
$fromTimeStamp = strtotime($dateFrom);
$toTimeStamp = strtotime($dateTo);
if ($dateTo) {
$toTimeStamp += 86400;
}
$result = false;
if (!$this->is_empty_date($dateFrom) && $channelTimeStamp < $fromTimeStamp) {
} elseif (!$this->is_empty_date($dateTo) && $channelTimeStamp > $toTimeStamp) {
} else {
$result = true;
}
return $result;
}
/**
* Get channel timestamp
* Timstamp will be builded with channel timezone settings
*
* @param mixed $channel
* @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
*
* @param string $date
* @return boolean
*/
function is_empty_date($date)
{
return preg_replace('#[ 0:-]#', '', $date) === '';
}
2018-08-30 13:22:15 +00:00
/**
* Format date using current channel.
*
* @param date|null $date
* @param string $format
* @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
*
* @param string $field
* @param int|string|null $channelId
*
* @return mixed
*/
public function getConfigData($field, $channelId = null)
{
if (null === $channelId) {
$channelId = $this->getCurrentChannel()->id;
}
return Config::get($field);
}
2018-10-12 08:22:06 +00:00
/**
* Retrieve all countries
*
* @return Collection
*/
public function countries()
{
2018-10-12 12:54:10 +00:00
return $this->countryRepository->all();
2018-10-12 08:22:06 +00:00
}
/**
* Retrieve all country states
*
* @return Collection
*/
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
*
* @return Collection
*/
public function groupedStatesByCountries()
{
$collection = [];
2018-10-12 12:54:10 +00:00
foreach ($this->countries() as $country) {
$collection[$country->code] = $this->states($country->code)->toArray();
2018-10-12 08:22:06 +00:00
}
return $collection;
}
2018-07-24 11:11:32 +00:00
}