[Tiki-devel] Fwd: [TikiWiki-commits] [Git][tikiwiki/tiki][23.x] [ENH] add system currency exchange rates tracker preferences
Victor Emanouilov via TikiWiki-devel <[email protected]>
| Newsgroups | gmane.comp.cms.tiki.devel |
|---|---|
| Message-ID | <[email protected]> |
Hello Tiki-devs, Attn to those using Currency tracker field (Marc, Bernard, Jonny, not sure about others): I have made a long-waited update to the field to move some of the guesswork to Tiki preferences. Added a new section 'System Trackers' in Control Panels -> Trackers -> bottom of the page. You can enable Exchange rates tracker, select which one it is, select the corresponding fields (Currency, Rate, Date) and also the conversion logic (if the rates are entered straight or reverse pairs). An important update here is removal of the Currency Tracker selection in the Currency field options. We no longer need or want to use that from the field properties as we have a system tracker now. The important part for those of you managing Currency fields is to go to Trackers settings page and configure the system tracker fields. This update only affects master/23.x. Regards, Victor -------- Forwarded Message -------- Subject: [TikiWiki-commits] [Git][tikiwiki/tiki][23.x] [ENH] add system currency exchange rates tracker preferences Date: Thu, 02 Dec 2021 13:13:28 +0000 From: Victor Emanouilov (@kroky) via TikiWiki-cvs <[email protected]> Reply-To: [email protected], Tiki code commits information <[email protected]> To: [email protected] CC: Victor Emanouilov (@kroky) <gitlab-o2+R/[email protected]> GitLab Victor Emanouilov pushed to branch 23.x at Tiki Wiki CMS Groupware / Tiki <https://gitlab.com/tikiwiki/tiki> Commits: * *e134dab7 <https://gitlab.com/tikiwiki/tiki/-/commit/e134dab7d304824966bef0b9dc6ffd866251f4aa>* by Victor Emanouilov /at 2021-12-02T15:13:14+02:00/ [ENH] add system currency exchange rates tracker preferences 8 changed files: * lib/core/Math/Formula/Currency.php <#d25413b12f4a846a8d7be6f19e2b637ff4814111> * lib/core/Math/Formula/Function/Currency.php <#04fc7a7aac594db572f6cea37ca0b28805f207fe> * lib/core/Search/Formatter/ValueFormatter/Currency.php <#1c5420cf258eb3a7a188e0d454998b136aa692ff> * lib/core/Tracker/Field/Currency.php <#233e1ccb54c425c80761c2b73bf084cc6b12c9e4> * lib/prefs/tracker.php <#fbb7a71eb6260322a76d501883aafa19f660bce6> * lib/smarty_tiki/function.currency.php <#7d141ce98eb741b2c23d91873dede8db58d06b76> * lib/trackers/trackerlib.php <#fefbae2c1067fed5d299174ab4f97c9659e700df> * templates/admin/include_trackers.tpl <#aa0395e2aa4b912714349a5ed6fbfb043e65b02d> Changes: # *lib/core/Math/Formula/Currency.php* <https://gitlab.com/tikiwiki/tiki/-/commit/e134dab7d304824966bef0b9dc6ffd866251f4aa#d25413b12f4a846a8d7be6f19e2b637ff4814111> ------------------------------------------------------------------------ ... ... @@ -28,7 +28,7 @@ class Math_Formula_Currency implements Math_Formula_Applicator 28 28 public static function fromCurrencyField($handler) 29 29 { 30 30 $data = $handler->getFieldData(); 31 -$rates = TikiLib::lib('trk')->exchange_rates($handler->getOption('currencyTracker'),$data['date']); 31 +$rates = TikiLib::lib('trk')->exchange_rates($data['date']); 32 32 return new self($data['amount'], $data['currency'], $rates); 33 33 } 34 34 ... ... @@ -41,7 +41,7 @@ class Math_Formula_Currency implements Math_Formula_Applicator 41 41 public static function tryFromString($currency) 42 42 { 43 43 if (preg_match("/^(-?\d+(\.\d+)?)([A-Z]{3})$/i", $currency, $m)) { 44 -$rates = TikiLib::lib('trk')->exchange_rates(null,null); 44 +$rates = TikiLib::lib('trk')->exchange_rates(null); 45 45 return new self($m[1], $m[3], $rates); 46 46 } else { 47 47 return $currency; # *lib/core/Math/Formula/Function/Currency.php* <https://gitlab.com/tikiwiki/tiki/-/commit/e134dab7d304824966bef0b9dc6ffd866251f4aa#04fc7a7aac594db572f6cea37ca0b28805f207fe> ------------------------------------------------------------------------ ... ... @@ -43,7 +43,6 @@ class Math_Formula_Function_Currency extends Math_Formula_Function 43 43 [ 44 44 'amount' => $amount, 45 45 'sourceCurrency' => $currency, 46 -'exchangeRatesTrackerId' => $options->getParam('currencyTracker'), 47 46 'date' => $date, 48 47 'prepend' => $options->getParam('prepend'), 49 48 'append' => $options->getParam('append'), # *lib/core/Search/Formatter/ValueFormatter/Currency.php* <https://gitlab.com/tikiwiki/tiki/-/commit/e134dab7d304824966bef0b9dc6ffd866251f4aa#1c5420cf258eb3a7a188e0d454998b136aa692ff> ------------------------------------------------------------------------ ... ... @@ -85,11 +85,10 @@ class Search_Formatter_ValueFormatter_Currency extends Search_Formatter_ValueFor 85 85 return $value; 86 86 } 87 87 88 -$currencyTracker = $handler->getOption('currencyTracker'); 89 88 $data = $handler->getFieldData(); 90 89 91 90 if ($this->target_currency) { 92 -$rates = TikiLib::lib('trk')->exchange_rates($currencyTracker,$this->date); 91 +$rates = TikiLib::lib('trk')->exchange_rates($this->date); 93 92 $currencyObj = new Math_Formula_Currency($data['amount'], $data['currency'], $rates); 94 93 $currencyObj = $currencyObj->convertTo($this->target_currency); 95 94 $amount = $currencyObj->getAmount(); ... ... @@ -109,7 +108,6 @@ class Search_Formatter_ValueFormatter_Currency extends Search_Formatter_ValueFor 109 108 [ 110 109 'amount' => $amount, 111 110 'sourceCurrency' => $currency, 112 -'exchangeRatesTrackerId' => $currencyTracker, 113 111 'date' => $this->date, 114 112 'prepend' => $handler->getOption('prepend'), 115 113 'append' => $handler->getOption('append'), # *lib/core/Tracker/Field/Currency.php* <https://gitlab.com/tikiwiki/tiki/-/commit/e134dab7d304824966bef0b9dc6ffd866251f4aa#233e1ccb54c425c80761c2b73bf084cc6b12c9e4> ------------------------------------------------------------------------ ... ... @@ -19,7 +19,7 @@ class Tracker_Field_Currency extends Tracker_Field_Abstract implements Tracker_F 19 19 return [ 20 20 'b' => [ 21 21 'name' => tr('Currency'), 22 -'description' => tr('Provide a one-line field for numeric input only. Prepended or appended values may be alphanumeric.'), 22 +'description' => tr('Provide a single field to enter numeric amount and choose a currency. Prepended or appended values may be alphanumeric.'), 23 23 'help' => 'Currency Amount Tracker Field', 24 24 'prefs' => ['trackerfield_currency'], 25 25 'tags' => ['basic'], ... ... @@ -95,18 +95,11 @@ class Tracker_Field_Currency extends Tracker_Field_Abstract implements Tracker_F 95 95 ], 96 96 'legacy_index' => 7, 97 97 ], 98 -'currencyTracker' => [ 99 -'name' => tr('Currency Tracker'), 100 -'description' => tr('Tracker containing available currencies and exchange rates.'), 101 -'filter' => 'int', 102 -'legacy_index' => 8, 103 -'profile_reference' => 'tracker', 104 -], 105 98 'dateFieldId' => [ 106 99 'name' => tr('Date Field ID'), 107 100 'description' => tr('Currency conversions will be performed based on a date in another field in this tracker rather than the current date. This is usually the date of the transaction.'), 108 101 'filter' => 'int', 109 -'legacy_index' => 9, 102 +'legacy_index' => 8, 110 103 'profile_reference' => 'tracker_field', 111 104 'parent' => 'input[name=trackerId]', 112 105 'parentkey' => 'tracker_id', ... ... @@ -181,7 +174,6 @@ class Tracker_Field_Currency extends Tracker_Field_Abstract implements Tracker_F 181 174 [ 182 175 'amount' => $data['amount'], 183 176 'sourceCurrency' => $data['currency'], 184 -'exchangeRatesTrackerId' => $this->getOption('currencyTracker'), 185 177 'date' => $data['date'], 186 178 'prepend' => $this->getOption('prepend'), 187 179 'append' => $this->getOption('append'), ... ... @@ -300,30 +292,19 @@ class Tracker_Field_Currency extends Tracker_Field_Abstract implements Tracker_F 300 292 301 293 private function getAvailableCurrencies() 302 294 { 295 +global $prefs; 303 296 $data = []; 304 297 305 298 $trk = TikiLib::lib('trk'); 306 -$currencyTracker = $this->getOption('currencyTracker'); 307 - 308 -if ($currencyTracker) { 309 -$fieldId = null; 310 -$definition = Tracker_Definition::get($currencyTracker); 311 -if (! empty($definition)) { 312 -$fields = $definition->getFields(); 313 -foreach ($fields as $field) { 314 -switch ($field['type']) { 315 -case 't': 316 -$fieldId = $field['fieldId']; 317 -break; 318 -} 319 -} 320 -} 321 -if ($fieldId) { 322 -$data['currencies'] = $trk->list_tracker_field_values($currencyTracker, $fieldId); 323 -sort($data['currencies']); 324 -} else { 325 -$data['error'] = 'Missing Currency field in tracker ' . $currencyTracker; 326 -} 299 +if ($prefs['tracker_system_currency'] != 'y') { 300 +$data['error'] = tr('Currency system tracker not enabled.'); 301 +} elseif (! $prefs['tracker_system_currency_tracker']) { 302 +$data['error'] = tr('Currency system tracker not configured: missing tracker selection.'); 303 +} elseif (! $prefs['tracker_system_currency_currency']) { 304 +$data['error'] = tr('Currency system tracker not configured: missing currency field.'); 305 +} else { 306 +$data['currencies'] = $trk->list_tracker_field_values($prefs['tracker_system_currency_tracker'], $prefs['tracker_system_currency_currency']); 307 +sort($data['currencies']); 327 308 } 328 309 329 310 return $data; ... ... @@ -332,20 +313,14 @@ class Tracker_Field_Currency extends Tracker_Field_Abstract implements Tracker_F 332 313 private function convertToDefaultCurrency($data) 333 314 { 334 315 $trk = TikiLib::lib('trk'); 335 -$currencyTracker = $this->getOption('currencyTracker'); 336 - 337 -if (! empty($currencyTracker)) { 338 -$rates = $trk->exchange_rates($currencyTracker, $data['date']); 339 - 340 -$defaultCurrency = array_search(1, $rates); 341 -if (empty($defaultCurrency)) { 342 -$defaultCurrency = 'USD'; 343 -} 316 +$rates = $trk->exchange_rates($data['date']); 344 317 345 -$currency = new Math_Formula_Currency($data['amount'], $data['currency'], $rates); 346 -return $currency->convertTo($defaultCurrency)->getAmount(); 347 -} else { 348 -return 0; 318 +$defaultCurrency = array_search(1, $rates); 319 +if (empty($defaultCurrency)) { 320 +$defaultCurrency = 'USD'; 349 321 } 322 + 323 +$currency = new Math_Formula_Currency($data['amount'], $data['currency'], $rates); 324 +return $currency->convertTo($defaultCurrency)->getAmount(); 350 325 } 351 326 } # *lib/prefs/tracker.php* <https://gitlab.com/tikiwiki/tiki/-/commit/e134dab7d304824966bef0b9dc6ffd866251f4aa#fbb7a71eb6260322a76d501883aafa19f660bce6> ------------------------------------------------------------------------ ... ... @@ -226,6 +226,71 @@ function prefs_tracker_list() 226 226 'default' => 'n', 227 227 'tags' => ['experimental'], 228 228 'warning' => tra('Use with care, potential for data loss.'), 229 -] 229 +], 230 +'tracker_system_currency' => [ 231 +'name' => tr('Exchange rates tracker'), 232 +'description' => tr('Allow defining a specific tracker to hold the exchange rates for currency calculations. You should manually create a tracker with at least 3 fields to hold Currency, Rate and Date.'), 233 +'type' => 'flag', 234 +'dependencies' => [ 235 +'trackerfield_currency', 236 +], 237 +'default' => 'n', 238 +], 239 +'tracker_system_currency_tracker' => [ 240 +'name' => tr('Choose tracker'), 241 +'description' => tr('Choose which tracker will store the currency conversion rates used for currency calculations.'), 242 +'type' => 'text', 243 +'dependencies' => [ 244 +'tracker_system_currency', 245 +], 246 +'default' => '', 247 +'profile_reference' => 'tracker', 248 +], 249 +'tracker_system_currency_rate' => [ 250 +'name' => tr('Rate field'), 251 +'description' => tr('Choose the field that stores the exchange rate in the system currency tracker.'), 252 +'type' => 'text', 253 +'dependencies' => [ 254 +'tracker_system_currency', 255 +], 256 +'default' => '', 257 +'parent' => 'input[name=tracker_system_currency_tracker]', 258 +'parentkey' => 'tracker_id', 259 +'profile_reference' => 'tracker_field', 260 +], 261 +'tracker_system_currency_currency' => [ 262 +'name' => tr('Currency field'), 263 +'description' => tr('Choose the field that stores the currency in the system currency tracker.'), 264 +'type' => 'text', 265 +'dependencies' => [ 266 +'tracker_system_currency', 267 +], 268 +'default' => '', 269 +'parent' => 'input[name=tracker_system_currency_tracker]', 270 +'parentkey' => 'tracker_id', 271 +'profile_reference' => 'tracker_field', 272 +], 273 +'tracker_system_currency_date' => [ 274 +'name' => tr('Date field'), 275 +'description' => tr('Choose the field that stores the exchange rate date in the system currency tracker.'), 276 +'type' => 'text', 277 +'dependencies' => [ 278 +'tracker_system_currency', 279 +], 280 +'default' => '', 281 +'parent' => 'input[name=tracker_system_currency_tracker]', 282 +'parentkey' => 'tracker_id', 283 +'profile_reference' => 'tracker_field', 284 +], 285 +'tracker_system_currency_direction' => [ 286 +'name' => tr('Rate direction'), 287 +'description' => tr('Stright means Base currency is equal to Target multiplied by the exchange rate. Reverse means Base is equal to Target divided by the exchange rate. In both cases, the Base currency should have an exchange rate of 1.00'), 288 +'type' => 'list', 289 +'options' => [ 290 +'stright' => tr('Stright (Target/Base)'), 291 +'reverse' => tr('Reverse (Base/Target)'), 292 +], 293 +'default' => 'stright', 294 +], 230 295 ]; 231 296 } # *lib/smarty_tiki/function.currency.php* <https://gitlab.com/tikiwiki/tiki/-/commit/e134dab7d304824966bef0b9dc6ffd866251f4aa#7d141ce98eb741b2c23d91873dede8db58d06b76> ------------------------------------------------------------------------ ... ... @@ -29,28 +29,26 @@ function smarty_function_currency($params, $smarty) 29 29 } 30 30 31 31 $conversions = []; 32 -if (! empty($exchangeRatesTrackerId)) { 33 -$rates = $trk->exchange_rates($exchangeRatesTrackerId, $date); 32 +$rates = $trk->exchange_rates($date); 34 33 35 -$defaultCurrency = array_search(1, $rates); 36 -if (empty($defaultCurrency)) { 37 -$defaultCurrency = 'USD'; 38 -} 34 +$defaultCurrency = array_search(1, $rates); 35 +if (empty($defaultCurrency)) { 36 +$defaultCurrency = 'USD'; 37 +} 39 38 40 -if (empty($sourceCurrency)) { 41 -$sourceCurrency = $defaultCurrency; 42 -} 39 +if (empty($sourceCurrency)) { 40 +$sourceCurrency = $defaultCurrency; 41 +} 43 42 44 -// convert amount to default currency before converting to other currencies 45 -$defaultAmount = $amount; 46 -if ($sourceCurrency != $defaultCurrency && ! empty($rates[$sourceCurrency])) { 47 -$defaultAmount = (float)$defaultAmount / (float)$rates[$sourceCurrency]; 48 -$conversions[$defaultCurrency] = $defaultAmount; 49 -} 50 -foreach ($rates as $currency => $rate) { 51 -if ($currency != $sourceCurrency) { 52 -$conversions[$currency] = (float)$rate * (float)$defaultAmount; 53 -} 43 +// convert amount to default currency before converting to other currencies 44 +$defaultAmount = $amount; 45 +if ($sourceCurrency != $defaultCurrency && ! empty($rates[$sourceCurrency])) { 46 +$defaultAmount = (float)$defaultAmount / (float)$rates[$sourceCurrency]; 47 +$conversions[$defaultCurrency] = $defaultAmount; 48 +} 49 +foreach ($rates as $currency => $rate) { 50 +if ($currency != $sourceCurrency) { 51 +$conversions[$currency] = (float)$rate * (float)$defaultAmount; 54 52 } 55 53 } 56 54 # *lib/trackers/trackerlib.php* <https://gitlab.com/tikiwiki/tiki/-/commit/e134dab7d304824966bef0b9dc6ffd866251f4aa#fefbae2c1067fed5d299174ab4f97c9659e700df> ------------------------------------------------------------------------ ... ... @@ -6500,14 +6500,19 @@ class TrackerLib extends TikiLib 6500 6500 } 6501 6501 6502 6502 /** 6503 -* Given a currency exchange ratetracker and a date, 6503 +* Given a configured system tracker with currency exchange rates and a date, 6504 6504 * return all available currency rates valid for that time. 6505 -* @param $trackerId the currency tracker 6506 6505 * @param $date 6507 6506 * @return array of exchange rates 6508 6507 */ 6509 -public function exchange_rates($trackerId,$date) 6508 +public function exchange_rates($date) 6510 6509 { 6510 +global $prefs; 6511 + 6512 +if ($prefs['tracker_system_currency'] != 'y') { 6513 +return []; 6514 +} 6515 + 6511 6516 if (is_numeric($date)) { 6512 6517 $date = date('Y-m-d', $date); 6513 6518 } elseif (! empty($date)) { ... ... @@ -6515,50 +6520,20 @@ class TrackerLib extends TikiLib 6515 6520 } else { 6516 6521 $date = date('Y-m-d'); 6517 6522 } 6523 + 6518 6524 static $rates = []; 6519 6525 if (isset($rates[$date])) { 6520 6526 return $rates[$date]; 6521 6527 } 6528 + 6522 6529 $rates[$date] = []; 6523 -$currencyField = $dateField = $rateField = null; 6524 -if (empty($trackerId)) { 6525 -// TODO: consider using system trackers here 6526 -$rates_trackers = $this->list_trackers(0, -1, 'trackerId_asc', 'exchange rate'); 6527 -if (! empty($rates_trackers['data'])) { 6528 -$trackerId = $rates_trackers['data'][0]['trackerId']; 6529 -} 6530 -} 6531 -if (empty($trackerId)) { 6532 -return $rates[$date]; 6533 -} 6534 -$definition = Tracker_Definition::get($trackerId); 6535 -if (empty($definition)) { 6536 -return $rates[$date]; 6537 -} 6538 -$fields = $definition->getFields(); 6539 -foreach ($fields as $field) { 6540 -switch ($field['type']) { 6541 -case 't': 6542 -if (! $currencyField) { 6543 -$currencyField = $field; 6544 -} 6545 -break; 6546 -case 'f': 6547 -case 'j': 6548 -case 'math': 6549 -if (! $dateField) { 6550 -$dateField = $field; 6551 -} 6552 -break; 6553 -case 'n': 6554 -if (! $rateField) { 6555 -$rateField = $field; 6556 -} 6557 -break; 6558 -} 6559 -} 6560 -if ($currencyField && $dateField && $rateField) { 6561 -$currencies = $this->list_tracker_field_values($trackerId, $currencyField['fieldId']); 6530 +$trackerId = $prefs['tracker_system_currency_tracker']; 6531 +$currencyField = $prefs['tracker_system_currency_currency']; 6532 +$dateField = $prefs['tracker_system_currency_date']; 6533 +$rateField = $prefs['tracker_system_currency_rate']; 6534 + 6535 +if ($trackerId && $currencyField && $dateField && $rateField) { 6536 +$currencies = $this->list_tracker_field_values($trackerId, $currencyField); 6562 6537 foreach ($currencies as $currency) { 6563 6538 $rates[$date][$currency] = $this->getOne( 6564 6539 'SELECT ttif3.value as rate FROM tiki_tracker_items tti ... ... @@ -6567,8 +6542,11 @@ class TrackerLib extends TikiLib 6567 6542 LEFT JOIN tiki_tracker_item_fields ttif3 ON tti.itemId = ttif3.itemId AND ttif3.fieldId = ? 6568 6543 WHERE tti.trackerId = ? AND ttif1.value = ? AND DATE_FORMAT(FROM_UNIXTIME(ttif2.value), \'%Y-%m-%d\') <= ? 6569 6544 ORDER BY ttif2.value DESC', 6570 -[$currencyField['fieldId'], $dateField['fieldId'], $rateField['fieldId'], $trackerId, $currency, $date] 6545 +[$currencyField, $dateField, $rateField, $trackerId, $currency, $date] 6571 6546 ); 6547 +if ($prefs['tracker_system_currency_direction'] == 'reverse' && $rates[$date][$currency]) { 6548 +$rates[$date][$currency] = 1 / $rates[$date][$currency]; 6549 +} 6572 6550 } 6573 6551 } 6574 6552 return $rates[$date]; # *templates/admin/include_trackers.tpl* <https://gitlab.com/tikiwiki/tiki/-/commit/e134dab7d304824966bef0b9dc6ffd866251f4aa#aa0395e2aa4b912714349a5ed6fbfb043e65b02d> ------------------------------------------------------------------------ ... ... @@ -84,6 +84,17 @@ 84 84 {preference name=tracker_force_tracker_fields} 85 85 {preference name=user_force_avatar_upload} 86 86 </fieldset> 87 +<fieldset class="admin"> 88 +<legend>{tr}System trackers{/tr}</legend> 89 +{preference name=tracker_system_currency} 90 +<div class="adminoptionboxchild" id="tracker_system_currency_childcontainer"> 91 +{preference name=tracker_system_currency_tracker} 92 +{preference name=tracker_system_currency_rate} 93 +{preference name=tracker_system_currency_currency} 94 +{preference name=tracker_system_currency_date} 95 +{preference name=tracker_system_currency_direction} 96 +</div> 97 +</fieldset> 87 98 {/tab} 88 99 89 100 {tab name="{tr}Plugins{/tr}"} — View it on GitLab <https://gitlab.com/tikiwiki/tiki/-/commit/e134dab7d304824966bef0b9dc6ffd866251f4aa>. You're receiving this email because of your account on gitlab.com. If you'd like to receive fewer emails, you can adjust your notification settings. _______________________________________________ TikiWiki-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/tikiwiki-devel
Attached Message Part
(text/plain, 1 B)
Attached Message Part
(text/plain, 168 B)
_______________________________________________ TikiWiki-cvs mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs