|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* Copyright (C) 2007-2020 Laurent Destailleur <[email protected]> |
|
4
|
|
|
* Copyright (C) 2014 Juanjo Menent <[email protected]> |
|
5
|
|
|
* Copyright (C) 2015 Florian Henry <[email protected]> |
|
6
|
|
|
* Copyright (C) 2015 Raphaël Doursenaud <[email protected]> |
|
7
|
|
|
* Copyright (C) 2016 Pierre-Henry Favre <[email protected]> |
|
8
|
|
|
* Copyright (C) 2024 Frédéric France <[email protected]> |
|
9
|
|
|
* Copyright (C) 2024 MDW <[email protected]> |
|
10
|
|
|
* Copyright (C) 2024 Rafael San José <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU General Public License as published by |
|
14
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
15
|
|
|
* (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU General Public License |
|
23
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace Dolibarr\Code\MultiCurrency\Classes; |
|
27
|
|
|
|
|
28
|
|
|
use CommonObject; |
|
29
|
|
|
use Dolibarr\Classes\stdClass; |
|
30
|
|
|
use DoliDB; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* \file htdocs/multicurrency/class/multicurrency.class.php |
|
34
|
|
|
* \ingroup multicurrency |
|
35
|
|
|
* \brief This file is a CRUD class file (Create/Read/Update/Delete) for multicurrency |
|
36
|
|
|
*/ |
|
37
|
|
|
|
|
38
|
|
|
// Put here all includes required by your class file |
|
39
|
|
|
require_once constant('DOL_DOCUMENT_ROOT') . '/core/class/commonobject.class.php'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Class Currency |
|
43
|
|
|
* |
|
44
|
|
|
* Put here description of your class |
|
45
|
|
|
* @see CommonObject |
|
46
|
|
|
*/ |
|
47
|
|
|
class MultiCurrency extends CommonObject |
|
48
|
|
|
{ |
|
49
|
|
|
/** |
|
50
|
|
|
* @var string Id to identify managed objects |
|
51
|
|
|
*/ |
|
52
|
|
|
public $element = 'multicurrency'; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string Name of table without prefix where object is stored |
|
56
|
|
|
*/ |
|
57
|
|
|
public $table_element = 'multicurrency'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var string Name of table without prefix where object is stored |
|
61
|
|
|
*/ |
|
62
|
|
|
public $table_element_line = "multicurrency_rate"; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var CurrencyRate[] Currency rates |
|
66
|
|
|
*/ |
|
67
|
|
|
public $rates = array(); |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var int The environment ID when using a multicompany module |
|
71
|
|
|
*/ |
|
72
|
|
|
public $id; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var string The currency code |
|
76
|
|
|
*/ |
|
77
|
|
|
public $code; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var string The currency name |
|
81
|
|
|
*/ |
|
82
|
|
|
public $name; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var int The environment ID when using a multicompany module |
|
86
|
|
|
*/ |
|
87
|
|
|
public $entity; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @var mixed Sample property 2 |
|
91
|
|
|
*/ |
|
92
|
|
|
public $date_create; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @var mixed Sample property 2 |
|
96
|
|
|
*/ |
|
97
|
|
|
public $fk_user; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @var ?CurrencyRate The currency rate |
|
101
|
|
|
*/ |
|
102
|
|
|
public $rate; |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Constructor |
|
107
|
|
|
* |
|
108
|
|
|
* @param DoliDB $db Database handler |
|
109
|
|
|
*/ |
|
110
|
|
|
public function __construct(DoliDB $db) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->db = $db; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get id of currency from code |
|
117
|
|
|
* |
|
118
|
|
|
* @param DoliDB $dbs object db |
|
119
|
|
|
* @param string $code code value search |
|
120
|
|
|
* |
|
121
|
|
|
* @return int 0 if not found, >0 if OK |
|
122
|
|
|
*/ |
|
123
|
|
|
public static function getIdFromCode($dbs, $code) |
|
124
|
|
|
{ |
|
125
|
|
|
global $conf; |
|
126
|
|
|
|
|
127
|
|
|
$sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "multicurrency WHERE code = '" . $dbs->escape($code) . "' AND entity = " . ((int)$conf->entity); |
|
128
|
|
|
|
|
129
|
|
|
dol_syslog(__METHOD__, LOG_DEBUG); |
|
130
|
|
|
$resql = $dbs->query($sql); |
|
131
|
|
|
if ($resql && $obj = $dbs->fetch_object($resql)) { |
|
132
|
|
|
return $obj->rowid; |
|
133
|
|
|
} else { |
|
134
|
|
|
return 0; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get id and rate of currency from code |
|
140
|
|
|
* |
|
141
|
|
|
* @param DoliDB $dbs Object db |
|
142
|
|
|
* @param string $code Code value search |
|
143
|
|
|
* @param integer|string $date_document Date from document (propal, order, invoice, ...) |
|
144
|
|
|
* |
|
145
|
|
|
* @return array [0] => id currency |
|
146
|
|
|
* [1] => rate |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function getIdAndTxFromCode($dbs, $code, $date_document = '') |
|
149
|
|
|
{ |
|
150
|
|
|
global $conf; |
|
151
|
|
|
|
|
152
|
|
|
$sql1 = "SELECT m.rowid, mc.rate FROM " . MAIN_DB_PREFIX . "multicurrency m"; |
|
153
|
|
|
|
|
154
|
|
|
$sql1 .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'multicurrency_rate mc ON (m.rowid = mc.fk_multicurrency)'; |
|
155
|
|
|
$sql1 .= " WHERE m.code = '" . $dbs->escape($code) . "'"; |
|
156
|
|
|
$sql1 .= " AND m.entity IN (" . getEntity('multicurrency') . ")"; |
|
157
|
|
|
$sql2 = ''; |
|
158
|
|
|
if (getDolGlobalString('MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE') && !empty($date_document)) { // Use last known rate compared to document date |
|
159
|
|
|
$tmparray = dol_getdate($date_document); |
|
160
|
|
|
$sql2 .= " AND mc.date_sync <= '" . $dbs->idate(dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year'], true)) . "'"; |
|
161
|
|
|
} |
|
162
|
|
|
$sql3 = " ORDER BY mc.date_sync DESC LIMIT 1"; |
|
163
|
|
|
|
|
164
|
|
|
dol_syslog(__METHOD__, LOG_DEBUG); |
|
165
|
|
|
$resql = $dbs->query($sql1 . $sql2 . $sql3); |
|
166
|
|
|
|
|
167
|
|
|
if ($resql && $obj = $dbs->fetch_object($resql)) { |
|
168
|
|
|
return array($obj->rowid, $obj->rate); |
|
169
|
|
|
} else { |
|
170
|
|
|
if (getDolGlobalString('MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE')) { |
|
171
|
|
|
$resql = $dbs->query($sql1 . $sql3); |
|
172
|
|
|
if ($resql && $obj = $dbs->fetch_object($resql)) { |
|
173
|
|
|
return array($obj->rowid, $obj->rate); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return array(0, 1); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get the conversion of amount with invoice rate |
|
183
|
|
|
* |
|
184
|
|
|
* @param int $fk_facture Id of invoice |
|
185
|
|
|
* @param double $amount amount to convert |
|
186
|
|
|
* @param string $way 'dolibarr' mean the amount is in dolibarr currency |
|
187
|
|
|
* @param string $table 'facture' or 'facture_fourn' |
|
188
|
|
|
* @param float|null $invoice_rate Invoice rate if known (to avoid to make the getInvoiceRate call) |
|
189
|
|
|
* @return float|false amount converted or false if conversion fails |
|
190
|
|
|
*/ |
|
191
|
|
|
public static function getAmountConversionFromInvoiceRate($fk_facture, $amount, $way = 'dolibarr', $table = 'facture', $invoice_rate = null) |
|
192
|
|
|
{ |
|
193
|
|
|
if (!is_null($invoice_rate)) { |
|
194
|
|
|
$multicurrency_tx = $invoice_rate; |
|
195
|
|
|
} else { |
|
196
|
|
|
$multicurrency_tx = self::getInvoiceRate($fk_facture, $table); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
if ($multicurrency_tx) { |
|
200
|
|
|
if ($way == 'dolibarr') { |
|
201
|
|
|
return (float)price2num($amount * $multicurrency_tx, 'MU'); |
|
202
|
|
|
} else { |
|
203
|
|
|
return (float)price2num($amount / $multicurrency_tx, 'MU'); |
|
204
|
|
|
} |
|
205
|
|
|
} else { |
|
206
|
|
|
return false; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Get current invoite rate |
|
212
|
|
|
* |
|
213
|
|
|
* @param int $fk_facture id of facture |
|
214
|
|
|
* @param string $table facture or facture_fourn |
|
215
|
|
|
* @return float|bool Rate of currency or false if error |
|
216
|
|
|
*/ |
|
217
|
|
|
public static function getInvoiceRate($fk_facture, $table = 'facture') |
|
218
|
|
|
{ |
|
219
|
|
|
global $db; |
|
220
|
|
|
|
|
221
|
|
|
$sql = "SELECT multicurrency_tx FROM " . MAIN_DB_PREFIX . $table . " WHERE rowid = " . ((int)$fk_facture); |
|
222
|
|
|
|
|
223
|
|
|
dol_syslog(__METHOD__, LOG_DEBUG); |
|
224
|
|
|
$resql = $db->query($sql); |
|
225
|
|
|
if ($resql && ($line = $db->fetch_object($resql))) { |
|
226
|
|
|
return $line->multicurrency_tx; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return false; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Update object into database |
|
234
|
|
|
* |
|
235
|
|
|
* @param User $user User that modifies |
|
236
|
|
|
* @param int $notrigger 0=launch triggers after, 1=disable triggers |
|
237
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
|
238
|
|
|
*/ |
|
239
|
|
|
public function update(User $user, $notrigger = 0) |
|
|
|
|
|
|
240
|
|
|
{ |
|
241
|
|
|
$error = 0; |
|
242
|
|
|
|
|
243
|
|
|
dol_syslog('MultiCurrency::update', LOG_DEBUG); |
|
244
|
|
|
|
|
245
|
|
|
// Clean parameters |
|
246
|
|
|
$this->name = trim($this->name); |
|
247
|
|
|
$this->code = trim($this->code); |
|
248
|
|
|
|
|
249
|
|
|
// Check parameters |
|
250
|
|
|
if (empty($this->code)) { |
|
251
|
|
|
$error++; |
|
252
|
|
|
dol_syslog('MultiCurrency::update $this->code can not be empty', LOG_ERR); |
|
253
|
|
|
|
|
254
|
|
|
return -1; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
// Update request |
|
258
|
|
|
$sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET"; |
|
259
|
|
|
$sql .= " name = '" . $this->db->escape($this->name) . "',"; |
|
260
|
|
|
$sql .= " code = '" . $this->db->escape($this->code) . "'"; |
|
261
|
|
|
$sql .= " WHERE rowid = " . ((int)$this->id); |
|
262
|
|
|
|
|
263
|
|
|
$this->db->begin(); |
|
264
|
|
|
|
|
265
|
|
|
$resql = $this->db->query($sql); |
|
266
|
|
|
if (!$resql) { |
|
267
|
|
|
$error++; |
|
268
|
|
|
$this->errors[] = 'Error ' . $this->db->lasterror(); |
|
269
|
|
|
dol_syslog('MultiCurrency::update ' . implode(',', $this->errors), LOG_ERR); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
if (!$error && empty($notrigger)) { |
|
273
|
|
|
$result = $this->call_trigger('CURRENCY_MODIFY', $user); |
|
274
|
|
|
if ($result < 0) { |
|
275
|
|
|
$error++; |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
// Commit or rollback |
|
280
|
|
|
if ($error) { |
|
281
|
|
|
$this->db->rollback(); |
|
282
|
|
|
|
|
283
|
|
|
return -1 * $error; |
|
284
|
|
|
} else { |
|
285
|
|
|
$this->db->commit(); |
|
286
|
|
|
|
|
287
|
|
|
return 1; |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Delete object in database |
|
293
|
|
|
* |
|
294
|
|
|
* @param User $user User making the deletion |
|
295
|
|
|
* @param int $notrigger 0=launch triggers after, 1=disable triggers |
|
296
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
|
297
|
|
|
*/ |
|
298
|
|
|
public function delete(User $user, $notrigger = 0) |
|
299
|
|
|
{ |
|
300
|
|
|
dol_syslog('MultiCurrency::delete', LOG_DEBUG); |
|
301
|
|
|
|
|
302
|
|
|
$error = 0; |
|
303
|
|
|
|
|
304
|
|
|
$this->db->begin(); |
|
305
|
|
|
|
|
306
|
|
|
if (empty($notrigger)) { |
|
307
|
|
|
$result = $this->call_trigger('CURRENCY_DELETE', $user); |
|
308
|
|
|
if ($result < 0) { |
|
309
|
|
|
$error++; |
|
310
|
|
|
} |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
if (!$error) { |
|
314
|
|
|
// Delete all rates before |
|
315
|
|
|
if (!$this->deleteRates()) { |
|
316
|
|
|
$error++; |
|
317
|
|
|
$this->errors[] = 'Error ' . $this->db->lasterror(); |
|
318
|
|
|
dol_syslog('Currency::delete ' . implode(',', $this->errors), LOG_ERR); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
$sql = "DELETE FROM " . MAIN_DB_PREFIX . $this->table_element; |
|
322
|
|
|
$sql .= " WHERE rowid = " . ((int)$this->id); |
|
323
|
|
|
|
|
324
|
|
|
dol_syslog(__METHOD__, LOG_DEBUG); |
|
325
|
|
|
$resql = $this->db->query($sql); |
|
326
|
|
|
if (!$resql) { |
|
327
|
|
|
$error++; |
|
328
|
|
|
$this->errors[] = 'Error ' . $this->db->lasterror(); |
|
329
|
|
|
dol_syslog('MultiCurrency::delete ' . implode(',', $this->errors), LOG_ERR); |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
// Commit or rollback |
|
334
|
|
|
if ($error) { |
|
335
|
|
|
$this->db->rollback(); |
|
336
|
|
|
|
|
337
|
|
|
return -1 * $error; |
|
338
|
|
|
} else { |
|
339
|
|
|
$this->db->commit(); |
|
340
|
|
|
|
|
341
|
|
|
return 1; |
|
342
|
|
|
} |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Delete rates in database |
|
347
|
|
|
* |
|
348
|
|
|
* @return bool false if KO, true if OK |
|
349
|
|
|
*/ |
|
350
|
|
|
public function deleteRates() |
|
351
|
|
|
{ |
|
352
|
|
|
global $user; |
|
353
|
|
|
|
|
354
|
|
|
foreach ($this->rates as &$rate) { |
|
355
|
|
|
if ($rate->delete($user) <= 0) { |
|
356
|
|
|
return false; |
|
357
|
|
|
} |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
return true; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* Sync rates from API |
|
365
|
|
|
* |
|
366
|
|
|
* @param string $key Key to use. Come from getDolGlobalString("MULTICURRENCY_APP_ID") |
|
367
|
|
|
* @param int $addifnotfound Add if not found |
|
368
|
|
|
* @param string $mode "" for standard use, "cron" to use it in a cronjob |
|
369
|
|
|
* @return int Return integer <0 if KO, >0 if OK, if mode = "cron" OK is 0 |
|
370
|
|
|
*/ |
|
371
|
|
|
public function syncRates($key, $addifnotfound = 0, $mode = "") |
|
372
|
|
|
{ |
|
373
|
|
|
global $conf, $db, $langs; |
|
374
|
|
|
|
|
375
|
|
|
if (getDolGlobalString('MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER')) { |
|
376
|
|
|
if ($mode == "cron") { |
|
377
|
|
|
$this->output = $langs->trans('Use of API for currency update is disabled by option MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER'); |
|
378
|
|
|
} else { |
|
379
|
|
|
setEventMessages($langs->trans('Use of API for currency update is disabled by option MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER'), null, 'errors'); |
|
380
|
|
|
} |
|
381
|
|
|
return -1; |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
if (empty($key)) { |
|
385
|
|
|
$key = getDolGlobalString("MULTICURRENCY_APP_ID"); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
include_once DOL_DOCUMENT_ROOT . '/core/lib/geturl.lib.php'; |
|
389
|
|
|
|
|
390
|
|
|
$urlendpoint = 'http://api.currencylayer.com/live?access_key=' . $key; |
|
391
|
|
|
$urlendpoint .= '&source=' . (!getDolGlobalString('MULTICURRENCY_APP_SOURCE') ? 'USD' : $conf->global->MULTICURRENCY_APP_SOURCE); |
|
392
|
|
|
|
|
393
|
|
|
dol_syslog("Call url endpoint " . $urlendpoint); |
|
394
|
|
|
|
|
395
|
|
|
$resget = getURLContent($urlendpoint); |
|
396
|
|
|
|
|
397
|
|
|
if ($resget['content']) { |
|
398
|
|
|
$response = $resget['content']; |
|
399
|
|
|
$response = json_decode($response); |
|
400
|
|
|
|
|
401
|
|
|
if ($response->success) { |
|
402
|
|
|
$TRate = $response->quotes; |
|
403
|
|
|
//$timestamp = $response->timestamp; |
|
404
|
|
|
|
|
405
|
|
|
if ($this->recalculRates($TRate) >= 0) { |
|
406
|
|
|
foreach ($TRate as $currency_code => $rate) { |
|
407
|
|
|
$code = substr($currency_code, 3, 3); |
|
408
|
|
|
$obj = new MultiCurrency($db); |
|
409
|
|
|
if ($obj->fetch(0, $code) > 0) { |
|
410
|
|
|
$obj->updateRate($rate); |
|
411
|
|
|
} elseif ($addifnotfound) { |
|
412
|
|
|
$this->addRateFromDolibarr($code, $rate); |
|
413
|
|
|
} |
|
414
|
|
|
} |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
if ($mode == "cron") { |
|
418
|
|
|
return 0; |
|
419
|
|
|
} |
|
420
|
|
|
return 1; |
|
421
|
|
|
} else { |
|
422
|
|
|
dol_syslog("Failed to call endpoint " . $response->error->info, LOG_WARNING); |
|
423
|
|
|
if ($mode == "cron") { |
|
424
|
|
|
$this->output = $langs->trans('multicurrency_syncronize_error', $response->error->info); |
|
425
|
|
|
} else { |
|
426
|
|
|
setEventMessages($langs->trans('multicurrency_syncronize_error', $response->error->info), null, 'errors'); |
|
427
|
|
|
} |
|
428
|
|
|
return -1; |
|
429
|
|
|
} |
|
430
|
|
|
} else { |
|
431
|
|
|
return -1; |
|
432
|
|
|
} |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* With free account we can't set source then recalcul all rates to force another source. |
|
437
|
|
|
* This modify the array &$TRate. |
|
438
|
|
|
* |
|
439
|
|
|
* @param stdClass $TRate Object containing all currencies rates |
|
440
|
|
|
* @return int -1 if KO, 0 if nothing, 1 if OK |
|
441
|
|
|
*/ |
|
442
|
|
|
public function recalculRates(&$TRate) |
|
443
|
|
|
{ |
|
444
|
|
|
global $conf; |
|
445
|
|
|
|
|
446
|
|
|
if ($conf->currency != getDolGlobalString('MULTICURRENCY_APP_SOURCE')) { |
|
447
|
|
|
$alternate_source = 'USD' . $conf->currency; |
|
448
|
|
|
if (!empty($TRate->$alternate_source)) { |
|
449
|
|
|
$coef = 1 / $TRate->$alternate_source; |
|
450
|
|
|
foreach ($TRate as $attr => &$rate) { |
|
451
|
|
|
$rate *= $coef; |
|
452
|
|
|
} |
|
453
|
|
|
$TRate->USDUSD = $coef; |
|
454
|
|
|
return 1; |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
return -1; // Alternate source not found |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
return 0; // Nothing to do |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
/** |
|
464
|
|
|
* Load object in memory from the database |
|
465
|
|
|
* |
|
466
|
|
|
* @param int $id Id object |
|
467
|
|
|
* @param string $code code |
|
468
|
|
|
* @return int Return integer <0 if KO, 0 if not found, >0 if OK |
|
469
|
|
|
*/ |
|
470
|
|
|
public function fetch($id, $code = null) |
|
471
|
|
|
{ |
|
472
|
|
|
dol_syslog('MultiCurrency::fetch', LOG_DEBUG); |
|
473
|
|
|
|
|
474
|
|
|
global $conf; |
|
475
|
|
|
|
|
476
|
|
|
$sql = "SELECT"; |
|
477
|
|
|
$sql .= ' c.rowid, c.name, c.code, c.entity, c.date_create, c.fk_user'; |
|
478
|
|
|
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' AS c'; |
|
479
|
|
|
if (!empty($code)) { |
|
480
|
|
|
$sql .= ' WHERE c.code = \'' . $this->db->escape($code) . '\' AND c.entity = ' . $conf->entity; |
|
481
|
|
|
} else { |
|
482
|
|
|
$sql .= ' WHERE c.rowid = ' . ((int)$id); |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
dol_syslog(__METHOD__, LOG_DEBUG); |
|
486
|
|
|
$resql = $this->db->query($sql); |
|
487
|
|
|
|
|
488
|
|
|
if ($resql) { |
|
489
|
|
|
$numrows = $this->db->num_rows($resql); |
|
490
|
|
|
if ($numrows) { |
|
491
|
|
|
$obj = $this->db->fetch_object($resql); |
|
492
|
|
|
|
|
493
|
|
|
$this->id = $obj->rowid; |
|
494
|
|
|
$this->name = $obj->name; |
|
495
|
|
|
$this->code = $obj->code; |
|
496
|
|
|
$this->entity = $obj->entity; |
|
497
|
|
|
$this->date_create = $obj->date_create; |
|
498
|
|
|
$this->fk_user = $obj->fk_user; |
|
499
|
|
|
|
|
500
|
|
|
$this->fetchAllCurrencyRate(); |
|
501
|
|
|
$this->getRate(); |
|
502
|
|
|
} |
|
503
|
|
|
$this->db->free($resql); |
|
504
|
|
|
|
|
505
|
|
|
if ($numrows) { |
|
506
|
|
|
return 1; |
|
507
|
|
|
} else { |
|
508
|
|
|
return 0; |
|
509
|
|
|
} |
|
510
|
|
|
} else { |
|
511
|
|
|
$this->errors[] = 'Error ' . $this->db->lasterror(); |
|
512
|
|
|
dol_syslog('MultiCurrency::fetch ' . implode(',', $this->errors), LOG_ERR); |
|
513
|
|
|
|
|
514
|
|
|
return -1; |
|
515
|
|
|
} |
|
516
|
|
|
} |
|
517
|
|
|
|
|
518
|
|
|
/** |
|
519
|
|
|
* Load all rates in object from the database |
|
520
|
|
|
* |
|
521
|
|
|
* @return int Return integer <0 if KO, >=0 if OK |
|
522
|
|
|
*/ |
|
523
|
|
|
public function fetchAllCurrencyRate() |
|
524
|
|
|
{ |
|
525
|
|
|
$sql = "SELECT cr.rowid"; |
|
526
|
|
|
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element_line . ' as cr'; |
|
527
|
|
|
$sql .= ' WHERE cr.fk_multicurrency = ' . ((int)$this->id); |
|
528
|
|
|
$sql .= ' ORDER BY cr.date_sync DESC'; |
|
529
|
|
|
|
|
530
|
|
|
$this->rates = array(); |
|
531
|
|
|
|
|
532
|
|
|
dol_syslog(__METHOD__, LOG_DEBUG); |
|
533
|
|
|
$resql = $this->db->query($sql); |
|
534
|
|
|
if ($resql) { |
|
535
|
|
|
$num = $this->db->num_rows($resql); |
|
536
|
|
|
|
|
537
|
|
|
while ($obj = $this->db->fetch_object($resql)) { |
|
538
|
|
|
$rate = new CurrencyRate($this->db); |
|
539
|
|
|
$rate->fetch($obj->rowid); |
|
540
|
|
|
|
|
541
|
|
|
$this->rates[] = $rate; |
|
542
|
|
|
} |
|
543
|
|
|
$this->db->free($resql); |
|
544
|
|
|
|
|
545
|
|
|
return $num; |
|
546
|
|
|
} else { |
|
547
|
|
|
$this->errors[] = 'Error ' . $this->db->lasterror(); |
|
548
|
|
|
dol_syslog('MultiCurrency::fetchAllCurrencyRate ' . implode(',', $this->errors), LOG_ERR); |
|
549
|
|
|
|
|
550
|
|
|
return -1; |
|
551
|
|
|
} |
|
552
|
|
|
} |
|
553
|
|
|
|
|
554
|
|
|
/** |
|
555
|
|
|
* Fetch CurrencyRate object in $this->rate |
|
556
|
|
|
* |
|
557
|
|
|
* @return int Return integer <0 if KO, 0 if not found, >0 if OK |
|
558
|
|
|
*/ |
|
559
|
|
|
public function getRate() |
|
560
|
|
|
{ |
|
561
|
|
|
$sql = "SELECT cr.rowid"; |
|
562
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element_line . " as cr"; |
|
563
|
|
|
$sql .= " WHERE cr.fk_multicurrency = " . ((int)$this->id); |
|
564
|
|
|
$sql .= " AND cr.date_sync = (SELECT MAX(cr2.date_sync) FROM " . MAIN_DB_PREFIX . $this->table_element_line . " AS cr2 WHERE cr2.fk_multicurrency = " . ((int)$this->id) . ")"; |
|
565
|
|
|
|
|
566
|
|
|
dol_syslog(__METHOD__, LOG_DEBUG); |
|
567
|
|
|
$resql = $this->db->query($sql); |
|
568
|
|
|
if ($resql && ($obj = $this->db->fetch_object($resql))) { |
|
569
|
|
|
$this->rate = new CurrencyRate($this->db); |
|
570
|
|
|
return $this->rate->fetch($obj->rowid); |
|
571
|
|
|
} |
|
572
|
|
|
|
|
573
|
|
|
return -1; |
|
574
|
|
|
} |
|
575
|
|
|
|
|
576
|
|
|
/** |
|
577
|
|
|
* Add new entry into llx_multicurrency_rate |
|
578
|
|
|
* |
|
579
|
|
|
* @param double $rate rate value |
|
580
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
|
581
|
|
|
*/ |
|
582
|
|
|
public function updateRate($rate) |
|
583
|
|
|
{ |
|
584
|
|
|
return $this->addRate($rate); |
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
/** |
|
588
|
|
|
* Add a Rate into database |
|
589
|
|
|
* |
|
590
|
|
|
* @param double $rate rate value |
|
591
|
|
|
* @return int -1 if KO, 1 if OK |
|
592
|
|
|
*/ |
|
593
|
|
|
public function addRate($rate) |
|
594
|
|
|
{ |
|
595
|
|
|
global $user; |
|
596
|
|
|
|
|
597
|
|
|
$currencyRate = new CurrencyRate($this->db); |
|
598
|
|
|
$currencyRate->rate = (float)price2num($rate); |
|
599
|
|
|
|
|
600
|
|
|
if ($currencyRate->create($user, $this->id) > 0) { |
|
601
|
|
|
$this->rate = $currencyRate; |
|
602
|
|
|
return 1; |
|
603
|
|
|
} else { |
|
604
|
|
|
$this->rate = null; |
|
605
|
|
|
$this->errors = $currencyRate->errors; |
|
606
|
|
|
return -1; |
|
607
|
|
|
} |
|
608
|
|
|
} |
|
609
|
|
|
|
|
610
|
|
|
/** |
|
611
|
|
|
* Create object into database |
|
612
|
|
|
* |
|
613
|
|
|
* @param User $user User that creates |
|
614
|
|
|
* @param int $notrigger 0=launch triggers after, 1=disable triggers |
|
615
|
|
|
* @return int Return integer <0 if KO, Id of created object if OK |
|
616
|
|
|
*/ |
|
617
|
|
|
public function create(User $user, $notrigger = 0) |
|
618
|
|
|
{ |
|
619
|
|
|
global $conf, $langs; |
|
620
|
|
|
|
|
621
|
|
|
dol_syslog('MultiCurrency::create', LOG_DEBUG); |
|
622
|
|
|
|
|
623
|
|
|
$error = 0; |
|
624
|
|
|
|
|
625
|
|
|
if (self::checkCodeAlreadyExists($this->code)) { |
|
626
|
|
|
$error++; |
|
627
|
|
|
$this->errors[] = $langs->trans('multicurrency_code_already_added'); |
|
628
|
|
|
return -1; |
|
629
|
|
|
} |
|
630
|
|
|
|
|
631
|
|
|
if (empty($this->entity) || $this->entity <= 0) { |
|
632
|
|
|
$this->entity = $conf->entity; |
|
633
|
|
|
} |
|
634
|
|
|
$now = dol_now(); |
|
635
|
|
|
|
|
636
|
|
|
// Insert request |
|
637
|
|
|
$sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . "("; |
|
638
|
|
|
$sql .= ' code,'; |
|
639
|
|
|
$sql .= ' name,'; |
|
640
|
|
|
$sql .= ' entity,'; |
|
641
|
|
|
$sql .= ' date_create,'; |
|
642
|
|
|
$sql .= ' fk_user'; |
|
643
|
|
|
$sql .= ') VALUES ('; |
|
644
|
|
|
$sql .= " '" . $this->db->escape($this->code) . "',"; |
|
645
|
|
|
$sql .= " '" . $this->db->escape($this->name) . "',"; |
|
646
|
|
|
$sql .= " " . ((int)$this->entity) . ","; |
|
647
|
|
|
$sql .= " '" . $this->db->idate($now) . "',"; |
|
648
|
|
|
$sql .= " " . ((int)$user->id); |
|
649
|
|
|
$sql .= ')'; |
|
650
|
|
|
|
|
651
|
|
|
$this->db->begin(); |
|
652
|
|
|
|
|
653
|
|
|
dol_syslog(__METHOD__, LOG_DEBUG); |
|
654
|
|
|
$resql = $this->db->query($sql); |
|
655
|
|
|
if (!$resql) { |
|
656
|
|
|
$error++; |
|
657
|
|
|
$this->errors[] = 'Error ' . $this->db->lasterror(); |
|
658
|
|
|
dol_syslog('MultiCurrency::create ' . implode(',', $this->errors), LOG_ERR); |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
if (!$error) { |
|
662
|
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element); |
|
663
|
|
|
$this->date_create = $now; |
|
664
|
|
|
$this->fk_user = $user->id; |
|
665
|
|
|
|
|
666
|
|
|
if (empty($notrigger)) { |
|
667
|
|
|
$result = $this->call_trigger('CURRENCY_CREATE', $user); |
|
668
|
|
|
if ($result < 0) { |
|
669
|
|
|
$error++; |
|
670
|
|
|
} |
|
671
|
|
|
} |
|
672
|
|
|
} |
|
673
|
|
|
|
|
674
|
|
|
if ($error) { |
|
675
|
|
|
$this->db->rollback(); |
|
676
|
|
|
|
|
677
|
|
|
return -1 * $error; |
|
678
|
|
|
} else { |
|
679
|
|
|
$this->db->commit(); |
|
680
|
|
|
|
|
681
|
|
|
return $this->id; |
|
682
|
|
|
} |
|
683
|
|
|
} |
|
684
|
|
|
|
|
685
|
|
|
/** |
|
686
|
|
|
* Check in database if the current code already exists |
|
687
|
|
|
* |
|
688
|
|
|
* @param string $code current code to search |
|
689
|
|
|
* @return boolean True if exists, false if not exists |
|
690
|
|
|
*/ |
|
691
|
|
|
public function checkCodeAlreadyExists($code) |
|
692
|
|
|
{ |
|
693
|
|
|
$currencytmp = new MultiCurrency($this->db); |
|
694
|
|
|
if ($currencytmp->fetch('', $code) > 0) { |
|
695
|
|
|
return true; |
|
696
|
|
|
} else { |
|
697
|
|
|
return false; |
|
698
|
|
|
} |
|
699
|
|
|
} |
|
700
|
|
|
|
|
701
|
|
|
/** |
|
702
|
|
|
* Try get label of code in llx_currency then add rate. |
|
703
|
|
|
* |
|
704
|
|
|
* @param string $code currency code |
|
705
|
|
|
* @param double $rate new rate |
|
706
|
|
|
* @return int -1 if KO, 1 if OK, 2 if label found and OK |
|
707
|
|
|
*/ |
|
708
|
|
|
public function addRateFromDolibarr($code, $rate) |
|
709
|
|
|
{ |
|
710
|
|
|
global $user; |
|
711
|
|
|
|
|
712
|
|
|
$currency = new MultiCurrency($this->db); |
|
713
|
|
|
$currency->code = $code; |
|
714
|
|
|
$currency->name = $code; |
|
715
|
|
|
|
|
716
|
|
|
$sql = "SELECT label FROM " . MAIN_DB_PREFIX . "c_currencies WHERE code_iso = '" . $this->db->escape($code) . "'"; |
|
717
|
|
|
|
|
718
|
|
|
dol_syslog(__METHOD__, LOG_DEBUG); |
|
719
|
|
|
$resql = $this->db->query($sql); |
|
720
|
|
|
if ($resql && ($line = $this->db->fetch_object($resql))) { |
|
721
|
|
|
$currency->name = $line->label; |
|
722
|
|
|
} |
|
723
|
|
|
|
|
724
|
|
|
if ($currency->create($user) > 0) { |
|
725
|
|
|
$currency->addRate($rate); |
|
726
|
|
|
|
|
727
|
|
|
if (!empty($line)) { |
|
728
|
|
|
return 2; |
|
729
|
|
|
} else { |
|
730
|
|
|
return 1; |
|
731
|
|
|
} |
|
732
|
|
|
} |
|
733
|
|
|
|
|
734
|
|
|
return -1; |
|
735
|
|
|
} |
|
736
|
|
|
} |
|
737
|
|
|
|