1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* The MIT License (MIT) |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2016 Daniel Popiniuc |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
namespace danielgp\salariu; |
30
|
|
|
|
31
|
|
|
trait ForeignCurrency |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
private $currencyDetails; |
35
|
|
|
|
36
|
|
|
private function establishIfNewCurrencyExchangeRatesFileIsNeeded($appSettings) |
37
|
|
|
{ |
38
|
|
|
$needNewFX = false; |
39
|
|
|
if (file_exists($appSettings['Exchange Rate Local'])) { |
40
|
|
|
$daysAllowed = $appSettings['Exchange Rate File Aging Allowed'] * 24 * 60 * 60; |
41
|
|
|
if ((filemtime($appSettings['Exchange Rate Local']) + $daysAllowed) < time()) { |
42
|
|
|
$needNewFX = true; |
43
|
|
|
} |
44
|
|
|
} else { |
45
|
|
|
$needNewFX = true; |
46
|
|
|
} |
47
|
|
|
return $needNewFX; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function getCurrencyExchangeRates(\XMLReader $xml) |
51
|
|
|
{ |
52
|
|
|
switch ($xml->localName) { |
53
|
|
|
case 'Cube': |
54
|
|
|
$this->currencyDetails['CXD'] = strtotime($xml->getAttribute('date')); |
55
|
|
|
break; |
56
|
|
|
case 'Rate': |
57
|
|
|
$multiplier = 1; |
58
|
|
|
if (!is_null($xml->getAttribute('multiplier'))) { |
59
|
|
|
$multiplier = $xml->getAttribute('multiplier'); |
60
|
|
|
} |
61
|
|
|
$this->currencyDetails['CXV'][$xml->getAttribute('currency')] = $xml->readInnerXml() / $multiplier; |
62
|
|
|
break; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function manageCurrencyToDisplay(\Symfony\Component\HttpFoundation\Request $tCSG) |
67
|
|
|
{ |
68
|
|
|
$aCurrencyDetails = []; |
69
|
|
|
$xMoneyChosen = array_merge(['RON'], $tCSG->request->get('xMoney')); |
70
|
|
|
foreach ($this->currencyDetails['CX'] as $key2 => $value2) { |
71
|
|
|
if (in_array($key2, $xMoneyChosen)) { |
72
|
|
|
$aCurrencyDetails[$key2] = $value2; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return $aCurrencyDetails; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function setCurrencyExchangeVariables($aryRelevantCrncy, $kCX) |
79
|
|
|
{ |
80
|
|
|
$this->currencyDetails = [ |
81
|
|
|
'CX' => $aryRelevantCrncy, |
82
|
|
|
'CXD' => strtotime('now'), |
83
|
|
|
]; |
84
|
|
|
foreach ($kCX as $value) { |
85
|
|
|
$this->currencyDetails['CXV'][$value] = 1; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function setExchangeRateValues($appSettings, $aryRelevantCrncy) |
90
|
|
|
{ |
91
|
|
|
$kCX = array_keys($aryRelevantCrncy); |
92
|
|
|
$this->setCurrencyExchangeVariables($aryRelevantCrncy, $kCX); |
93
|
|
|
$this->updateCurrencyExchangeRatesFile($appSettings); |
94
|
|
|
$xml = new \XMLReader(); |
95
|
|
|
if ($xml->open($appSettings['Exchange Rate Local'], 'UTF-8')) { |
96
|
|
|
while ($xml->read()) { |
97
|
|
|
if ($xml->nodeType == \XMLReader::ELEMENT) { |
98
|
|
|
$this->getCurrencyExchangeRates($xml); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
$xml->close(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function updateCurrencyExchangeRatesFile($appSettings) |
106
|
|
|
{ |
107
|
|
|
if ($this->establishIfNewCurrencyExchangeRatesFileIsNeeded($appSettings)) { |
108
|
|
|
$fCntnt = file_get_contents($appSettings['Exchange Rate Source']); |
109
|
|
|
if ($fCntnt !== false) { |
110
|
|
|
file_put_contents($appSettings['Exchange Rate Local'], $fCntnt); |
111
|
|
|
chmod($appSettings['Exchange Rate Local'], 0666); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|