Skip to content

Commit

Permalink
enhancement: multi currency support - implemented LMS::getCurrencyVal…
Browse files Browse the repository at this point in the history
…ue() which uses NBP API (LMS+ #906)
  • Loading branch information
chilek committed Oct 26, 2019
1 parent 00aef6b commit 6ca9251
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 56 deletions.
1 change: 1 addition & 0 deletions bin/lms-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
$LMS = new LMS($DB, $AUTH, $SYSLOG);
$LMS->ui_lang = $_ui_language;
$LMS->lang = $_language;
$LMS->currency = $_currency;

$plugin_manager = new LMSPluginManager();
$LMS->setPluginManager($plugin_manager);
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
$LMS->lang = $_language;

$_default_currency = ConfigHelper::getConfig('phpui.default_currency', '', true);
if (empty($_default_currency) || !in_array($_default_currency, $CURRENCIES)) {
if (empty($_default_currency) || !isset($CURRENCIES[$_default_currency])) {
$_default_currency = $_currency;
}
$LMS->currency = $_currency;
Expand Down
6 changes: 6 additions & 0 deletions lib/LMS.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3143,6 +3143,12 @@ public function CheckNodeTariffRestrictions($aid, $nodes)
return $manager->CheckNodeTariffRestrictions($aid, $nodes);
}

public function getCurrencyValue($currency, $date = null)
{
$manager = $this->getFinanceManager();
return $manager->getCurrencyValue($currency, $date);
}

/**
* VoIP functions
*/
Expand Down
35 changes: 35 additions & 0 deletions lib/LMSManagers/LMSFinanceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
class LMSFinanceManager extends LMSManager implements LMSFinanceManagerInterface
{
private $currency_values = array();

public function GetPromotionNameBySchemaID($id)
{
return $this->db->GetOne('SELECT p.name FROM promotionschemas AS s
Expand Down Expand Up @@ -3973,4 +3975,37 @@ public function CheckNodeTariffRestrictions($aid, $nodes)
}
return $result;
}

public function getCurrencyValue($currency, $date = null)
{
if ($currency == $GLOBALS['currency']) {
return 1.0;
}
if (function_exists('get_currency_value')) {
if (!isset($GLOBALS['CURRENCIES'][$currency])) {
return null;
}
if (empty($date)) {
$date = mktime(12, 0, 0);
} elseif (strpos($date, '/') !== false) {
list ($year, $month, $day) = explode('/', $date);
$date = mktime(12, 0, 0, $month, $day, $year);
} else {
$date = mktime(
12,
0,
0,
date('n', $date),
date('j', $date),
date('Y', $date)
);
}
if (!isset($this->currency_values[$currency][$date])) {
$this->currency_values[$currency][$date] = get_currency_value($currency, $date);
}
return $this->currency_values[$currency][$date];
} else {
return null;
}
}
}
2 changes: 2 additions & 0 deletions lib/LMSManagers/LMSFinanceManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,6 @@ public function GetDocumentsForBalanceRecords($ids, $doctypes);
public function GetDocumentLastReference($docid);

public function CheckNodeTariffRestrictions($aid, $nodes);

public function getCurrencyValue($currency, $date = null);
}
76 changes: 38 additions & 38 deletions lib/definitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -952,44 +952,44 @@
define('EXISTINGASSIGNMENT_DELETE', 3);

$CURRENCIES = array(
'AUD',
'BGN',
'BRL',
'CAD',
'CHF',
'CLP',
'CNY',
'CZK',
'DKK',
'EUR',
'GBP',
'HKD',
'HRK',
'HUF',
'IDR',
'ILS',
'INR',
'ISK',
'JPY',
'KRW',
'LTL',
'LVL',
'MXN',
'MYR',
'NOK',
'NZD',
'PHP',
'PLN',
'RON',
'RUB',
'SEK',
'SGD',
'THB',
'TRY',
'UAH',
'USD',
'XDR',
'ZAR',
'AUD' => 'AUD',
'BGN' => 'BGN',
'BRL' => 'BRL',
'CAD' => 'CAD',
'CHF' => 'CHF',
'CLP' => 'CLP',
'CNY' => 'CNY',
'CZK' => 'CZK',
'DKK' => 'DKK',
'EUR' => 'EUR',
'GBP' => 'GBP',
'HKD' => 'HKO',
'HRK' => 'HRK',
'HUF' => 'HUF',
'IDR' => 'IDR',
'ILS' => 'ILS',
'INR' => 'INR',
'ISK' => 'ISK',
'JPY' => 'JPY',
'KRW' => 'KRW',
'LTL' => 'LTL',
'LVL' => 'LVL',
'MXN' => 'MXN',
'MYR' => 'MYR',
'NOK' => 'NOK',
'NZD' => 'NZD',
'PHP' => 'PHP',
'PLN' => 'PLN',
'RON' => 'RON',
'RUB' => 'RUB',
'SEK' => 'SEK',
'SGD' => 'SGD',
'THB' => 'THB',
'TRY' => 'TRY',
'UAH' => 'UAH',
'USD' => 'USD',
'XDR' => 'XOR',
'ZAR' => 'ZAR',
);

$EXISTINGASSIGNMENTS = array(
Expand Down
64 changes: 47 additions & 17 deletions lib/locale/pl/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ function check_ssn($ssn)
if (!preg_match('/^[0-9]{11}$/', $ssn)) {
return false;
}

$steps = array(1, 3, 7, 9, 1, 3, 7, 9, 1, 3);
$sum_nb = 0;

for ($x = 0; $x < 10; $x++) {
$sum_nb += $steps[$x] * $ssn[$x];
}

$sum_m = 10 - $sum_nb % 10;

if ($sum_m == 10) {
$sum_c = 0;
} else {
$sum_c = $sum_m;
}

if ($sum_c == $ssn[10]) {
return true;
}
Expand All @@ -94,54 +94,54 @@ function check_regon($regon)

if (strlen($regon) == 14) {
$steps = array(2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8);

for ($x = 0; $x < 13; $x++) {
$sum_nb += $steps[$x] * $regon[$x];
}

$mod = $sum_nb % 11;

if ($mod == 10) {
$mod = 0;
}

if ($mod == $regon[13]) {
return true;
}
} else if (strlen($regon) == 9) {
$steps = array(8, 9, 2, 3, 4, 5, 6, 7);

for ($x = 0; $x < 8; $x++) {
$sum_nb += $steps[$x] * $regon[$x];
}

$mod = $sum_nb % 11;

if ($mod == 10) {
$mod = 0;
}

if ($mod == $regon[8]) {
return true;
}
} elseif (strlen($regon) == 7) {
$steps = array(2, 3, 4, 5, 6, 7);

for ($x = 0; $x < 6; $x++) {
$sum_nb += $steps[$x] * $regon[$x];
}

$mod = $sum_nb % 11;

if ($mod == 10) {
$mod = 0;
}

if ($mod == $regon[6]) {
return true;
}
}

return false;
}

Expand Down Expand Up @@ -204,3 +204,33 @@ function generateRandomPostcode()
{
return sprintf("%02d", rand(0, 99)) . '-' . sprintf("%03d", rand(0, 999));
}

function get_currency_value($currency, $date = null)
{
if ($date) {
$day_of_week = date('N', $date);
if ($day_of_week > 5) {
$date -= ($day_of_week - 5) * 86400;
}
}

$i = 0;
do {
$result = file_get_contents('https://api.nbp.pl/api/exchangerates/rates/A/' . $currency . '/'
. (empty($date) ? '' : date('Y-m-d', $date) . '/') . '?format=json');
if ($result !== false) {
break;
}
$date -= 86400;
$i++;
} while ($result === false && $i < 5);
if ($i == 5) {
return null;
}

$result = json_decode($result, true);
if ($result === null) {
return null;
}
return $result['rates'][0]['mid'];
}

0 comments on commit 6ca9251

Please sign in to comment.