__ __ __ __ _____ _ _ _____ _ _ _ | \/ | \ \ / / | __ \ (_) | | / ____| | | | | | \ / |_ __\ V / | |__) | __ ___ ____ _| |_ ___ | (___ | |__ ___| | | | |\/| | '__|> < | ___/ '__| \ \ / / _` | __/ _ \ \___ \| '_ \ / _ \ | | | | | | |_ / . \ | | | | | |\ V / (_| | || __/ ____) | | | | __/ | | |_| |_|_(_)_/ \_\ |_| |_| |_| \_/ \__,_|\__\___| |_____/|_| |_|\___V 2.1 if you need WebShell for Seo everyday contact me on Telegram Telegram Address : @jackleetFor_More_Tools:
<?php
/**
* @package DPCalendar
* @copyright Copyright (C) 2024 Digital Peak GmbH. <https://www.digital-peak.com>
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL
*/
namespace DigitalPeak\Component\DPCalendar\Administrator\Model;
\defined('_JEXEC') or die();
use DigitalPeak\Component\DPCalendar\Administrator\Helper\DPCalendarHelper;
use DigitalPeak\ThinHTTP\CurlClient;
use Joomla\CMS\Application\CMSWebApplicationInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
class CurrencyModel extends BaseDatabaseModel
{
/**
* Returns the actual currency.
*/
public function getActualCurrency(): \stdClass
{
$currencies = $this->getCurrencies();
$code = null;
$app = Factory::getApplication();
if ($app instanceof CMSWebApplicationInterface) {
$code = $app->getSession()->get('com_dpcalendar.user.currency') ;
}
foreach ($currencies as $currency) {
if ($currency->currency === $code) {
return $currency;
}
}
return reset($currencies);
}
/**
* Returns the list of currencies.
*/
public function getCurrencies(): array
{
$currencies = (array)DPCalendarHelper::getComponentParameter('bookingsys_currencies');
if ($currencies === []) {
return ['bookingsys_currencies0' => (object)[
'currency' => 'EUR',
'symbol' => '€',
'separator' => '.',
'thousands_separator' => "'"
]];
}
return $currencies;
}
/**
* The exchange rates list for today or default if API can't be called.
*/
public function getExchangeRates(): array
{
// Setup the file path
$file = JPATH_CACHE . '/com_dpcalendar-exchange-rates/' . DPCalendarHelper::getDate()->format('Y-m-d') . '.json';
// When the file exists, use it
if (file_exists($file)) {
return (array)json_decode(file_get_contents($file) ?: '{}');
}
// Ensure the directories do exist
if (!is_dir(\dirname($file))) {
mkdir(\dirname($file));
}
// The API key
$key = DPCalendarHelper::getComponentParameter('bookingsys_exchangeratesapi_api_key');
if ($key) {
// Fetch the content
$response = (new CurlClient())->get('https://api.exchangeratesapi.io/v1/latest?access_key=' . $key);
// When rates field exists write the file
if (!empty($response->rates)) {
file_put_contents($file, json_encode($response->rates));
}
}
// Use the default one when no file is found
if (!file_exists($file)) {
copy(JPATH_ADMINISTRATOR . '/components/com_dpcalendar/config/default-exchange-rates.json', $file);
}
// Return the rates
return (array)json_decode(file_get_contents($file) ?: '{}');
}
/**
* Returns the prices for the currency from the session. If none do exist an automatic conversion is done.
*/
public function setupCurrencyPrices(\stdClass $event): void
{
// Get the defined currencies
$currency = $this->getActualCurrency();
$event->prices = $this->convert($event->prices, $currency->currency);
$event->booking_options = $this->convert($event->booking_options, $currency->currency);
}
private function convert(?\stdClass $prices, string $actualCurrency): ?\stdClass
{
// Ignore empty prices
if (!$prices instanceof \stdClass || get_object_vars($prices) === []) {
return null;
}
// The filtered prices
$filteredPrices = new \stdClass();
foreach ((array)$prices as $index => $price) {
// If it matches the current currency, use it
if ($price->currency === $actualCurrency) {
$filteredPrices->$index = $price;
}
}
// When not empty, then we assume event is properly set up
if (get_object_vars($filteredPrices) !== []) {
return $filteredPrices;
}
// The current exchange rates
$currencies = $this->getExchangeRates();
$fee = (float)DPCalendarHelper::getComponentParameter('bookingsys_exchange_fee', 0);
// Loop over the prices
foreach ((array)$prices as $index => $price) {
$newPrice = clone $price;
$newPrice->currency = $actualCurrency;
$newPrice->value = ($price->value / $currencies[$price->currency]) * $currencies[$actualCurrency];
$newPrice->value = $fee !== 0.0 ? $newPrice->value + (($newPrice->value / 100) * $fee) : $newPrice->value;
$newPrice->value = $newPrice->value > 10 ? ceil($newPrice->value) : $newPrice->value;
// Add a converted price
$filteredPrices->$index = $newPrice;
}
// Return the prices
return $filteredPrices;
}
}
| Name | Type | Size | Permission | Actions |
|---|---|---|---|---|
| BookingsModel.php | File | 10.01 KB | 0664 |
|
| CaldavModel.php | File | 1.97 KB | 0664 |
|
| CalendarModel.php | File | 2.01 KB | 0664 |
|
| CountriesModel.php | File | 3.99 KB | 0664 |
|
| CountryModel.php | File | 2.86 KB | 0664 |
|
| CouponModel.php | File | 3.76 KB | 0664 |
|
| CouponsModel.php | File | 4.04 KB | 0664 |
|
| CpanelModel.php | File | 4.28 KB | 0664 |
|
| CurrencyModel.php | File | 4.32 KB | 0664 |
|
| EventModel.php | File | 30.82 KB | 0664 |
|
| EventsModel.php | File | 14.41 KB | 0664 |
|
| ExtcalendarModel.php | File | 5.59 KB | 0664 |
|
| ExtcalendarsModel.php | File | 4.92 KB | 0664 |
|
| FieldsOrderModel.php | File | 5.9 KB | 0664 |
|
| GeoModel.php | File | 19.71 KB | 0664 |
|
| IcalModel.php | File | 14.81 KB | 0664 |
|
| ImportModel.php | File | 7.37 KB | 0664 |
|
| LayoutModel.php | File | 704 B | 0664 |
|
| LocationModel.php | File | 12.06 KB | 0664 |
|
| LocationsModel.php | File | 8.94 KB | 0664 |
|
| PluginModel.php | File | 784 B | 0664 |
|
| TaxrateModel.php | File | 4.62 KB | 0664 |
|
| TaxratesModel.php | File | 4.33 KB | 0664 |
|
| TicketsModel.php | File | 10.21 KB | 0664 |
|
| ToolsModel.php | File | 1.61 KB | 0664 |
|
| VersionModel.php | File | 2.5 KB | 0664 |
|