1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright YetiForce S.A. |
4
|
|
|
* @license YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class for connection to Nation Bank of Romania currency exchange rates. |
9
|
|
|
*/ |
10
|
|
|
class Settings_CurrencyUpdate_NBR_BankModel extends Settings_CurrencyUpdate_AbstractBank_Model |
11
|
|
|
{ |
12
|
|
|
// Returns bank name |
13
|
|
|
|
14
|
1 |
|
public function getName() |
15
|
|
|
{ |
16
|
1 |
|
return 'NBR'; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
// Returns url sources from where exchange rates are taken from |
20
|
|
|
|
21
|
1 |
|
public function getSource($year = '') |
22
|
|
|
{ |
23
|
1 |
|
if ('' == $year) { |
24
|
1 |
|
$source = 'http://www.bnr.ro/nbrfxrates10days.xml'; |
25
|
|
|
} else { |
26
|
|
|
$source = 'http://www.bnr.ro/files/xml/years/nbrfxrates' . $year . '.xml'; |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
return simplexml_load_file($source); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// Returns list of currencies supported by this bank |
33
|
|
|
|
34
|
1 |
|
public function getSupportedCurrencies() |
35
|
|
|
{ |
36
|
1 |
|
$supportedCurrencies = []; |
37
|
1 |
|
$supportedCurrencies[Settings_CurrencyUpdate_Module_Model::getCRMCurrencyName($this->getMainCurrencyCode())] = $this->getMainCurrencyCode(); |
38
|
1 |
|
$xml = $this->getSource(); |
39
|
|
|
|
40
|
1 |
|
foreach ($xml->Body->Cube[0] as $currency) { |
41
|
1 |
|
$currencyCode = (string) $currency['currency']; |
42
|
1 |
|
$supportedCurrencies[Settings_CurrencyUpdate_Module_Model::getCRMCurrencyName($currencyCode)] = $currencyCode; |
43
|
|
|
} |
44
|
1 |
|
return $supportedCurrencies; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Returns banks main currency |
48
|
|
|
|
49
|
1 |
|
public function getMainCurrencyCode() |
50
|
|
|
{ |
51
|
1 |
|
return 'RON'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Fetch exchange rates. |
56
|
|
|
* |
57
|
|
|
* @param <Array> $currencies - list of systems active currencies |
|
|
|
|
58
|
|
|
* @param <Date> $date - date for which exchange is fetched |
59
|
|
|
* @param bool $cron - if true then it is fired by server and crms currency conversion rates are updated |
60
|
|
|
* @param mixed $otherCurrencyCode |
61
|
1 |
|
* @param mixed $dateParam |
62
|
|
|
*/ |
63
|
1 |
|
public function getRates($otherCurrencyCode, $dateParam, $cron = false) |
64
|
1 |
|
{ |
65
|
1 |
|
$moduleModel = Settings_CurrencyUpdate_Module_Model::getCleanInstance(); |
66
|
|
|
$selectedBank = $moduleModel->getActiveBankId(); |
67
|
|
|
$yesterday = date('Y-m-d', strtotime('-1 day')); |
68
|
1 |
|
|
69
|
|
|
// check if data is correct, currency rates can be retrieved only for working days |
70
|
1 |
|
$lastWorkingDay = vtlib\Functions::getLastWorkingDay($yesterday); |
71
|
1 |
|
|
72
|
|
|
$today = date('Y-m-d'); |
73
|
|
|
$mainCurrency = \App\Fields\Currency::getDefault()['currency_code']; |
74
|
1 |
|
|
75
|
1 |
|
// how old is the currency rate |
76
|
1 |
|
$now = time(); // or your date as well |
77
|
1 |
|
$rateDay = strtotime($dateParam); |
78
|
|
|
$datediff = $now - $rateDay; |
79
|
|
|
if (floor($datediff / (60 * 60 * 24)) >= 10) { |
80
|
|
|
$year = date('Y', $rateDay); |
81
|
1 |
|
$xml = $this->getSource($year); |
82
|
|
|
} else { |
83
|
|
|
$xml = $this->getSource(); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
if (false === $xml) { |
87
|
|
|
return false; |
88
|
1 |
|
} |
89
|
1 |
|
|
90
|
|
|
$datePublicationOfFile = $dateParam; |
91
|
|
|
$exchangeRate = 1.0; |
92
|
1 |
|
|
93
|
1 |
|
// if currency is diffrent than RON we need to calculate rate for converting other currencies to this one from RON |
94
|
1 |
|
if ($mainCurrency != $this->getMainCurrencyCode()) { |
95
|
1 |
|
$foundRate = false; |
96
|
1 |
|
foreach ($xml->Body->Cube as $time) { |
97
|
1 |
|
if ($time['date'] == $dateParam) { |
98
|
1 |
|
foreach ($time->Rate as $rate) { |
99
|
1 |
|
if ($rate['currency'] == $mainCurrency) { |
100
|
|
|
$exchangeRate = $rate; |
101
|
1 |
|
$foundRate = true; |
102
|
1 |
|
} |
103
|
|
|
if ($foundRate) { |
104
|
|
|
break; |
105
|
|
|
} |
106
|
1 |
|
} |
107
|
1 |
|
} |
108
|
|
|
if ($foundRate) { |
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
} |
112
|
1 |
|
} |
113
|
1 |
|
|
114
|
1 |
|
$foundRate = false; |
115
|
1 |
|
foreach ($xml->Body->Cube as $time) { |
116
|
1 |
|
if ($time['date'] == $dateParam) { |
117
|
1 |
|
$num = \count($time->Rate); |
118
|
1 |
|
for ($i = 0; $i < $num; ++$i) { |
119
|
1 |
|
$currency = (string) $time->Rate[$i]['currency']; // currency code |
120
|
1 |
|
foreach ($otherCurrencyCode as $key => $currId) { |
121
|
1 |
|
if ($key == $currency && $currency != $mainCurrency) { |
122
|
|
|
$exchange = $time->Rate[$i]; |
123
|
|
|
if ($time->Rate[$i]['multiplier']) { |
124
|
1 |
|
$exchange = (float) $time->Rate[$i] / (float) $time->Rate[$i]['multiplier']; |
125
|
1 |
|
} |
126
|
|
|
$exchangeVtiger = (float) $exchangeRate / (float) $exchange; |
127
|
1 |
|
$exchange = (float) $exchange / (float) $exchangeRate; |
128
|
1 |
|
|
129
|
|
|
if (true === $cron || ((strtotime($dateParam) == strtotime($today)) || (strtotime($dateParam) == strtotime($lastWorkingDay)))) { |
130
|
1 |
|
$moduleModel->setCRMConversionRate($currency, $exchangeVtiger); |
131
|
1 |
|
} |
132
|
1 |
|
$existingId = $moduleModel->getCurrencyRateId($currId, $datePublicationOfFile, $selectedBank); |
133
|
|
|
if ($existingId > 0) { |
134
|
1 |
|
$moduleModel->updateCurrencyRate($existingId, $exchange); |
135
|
|
|
} else { |
136
|
|
|
$moduleModel->addCurrencyRate($currId, $datePublicationOfFile, $exchange, $selectedBank); |
137
|
|
|
} |
138
|
|
|
} |
139
|
1 |
|
} |
140
|
|
|
} |
141
|
1 |
|
$foundRate = true; |
142
|
1 |
|
} |
143
|
|
|
if ($foundRate) { |
144
|
|
|
break; |
145
|
|
|
} |
146
|
|
|
} |
147
|
1 |
|
|
148
|
1 |
|
// currency diffrent than RON, we need to add manually RON rates |
149
|
1 |
|
if ($mainCurrency != $this->getMainCurrencyCode()) { |
150
|
1 |
|
$exchange = 1.00000 / (float) $exchangeRate; |
151
|
1 |
|
$mainCurrencyId = false; |
152
|
|
|
foreach ($otherCurrencyCode as $code => $id) { |
153
|
|
|
if ($code == $this->getMainCurrencyCode()) { |
154
|
|
|
$mainCurrencyId = $id; |
155
|
|
|
} |
156
|
1 |
|
} |
157
|
|
|
|
158
|
|
|
if ($mainCurrencyId) { |
159
|
|
|
if (true === $cron || ((strtotime($dateParam) == strtotime($today)) || (strtotime($dateParam) == strtotime($lastWorkingDay)))) { |
160
|
|
|
$moduleModel->setCRMConversionRate($this->getMainCurrencyCode(), $exchange); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$existingId = $moduleModel->getCurrencyRateId($mainCurrencyId, $datePublicationOfFile, $selectedBank); |
164
|
|
|
|
165
|
|
|
if ($existingId > 0) { |
166
|
|
|
$moduleModel->updateCurrencyRate($existingId, $exchange); |
167
|
|
|
} else { |
168
|
|
|
$moduleModel->addCurrencyRate($mainCurrencyId, $datePublicationOfFile, $exchange, $selectedBank); |
169
|
|
|
} |
170
|
1 |
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|