| Total Complexity | 83 |
| Total Lines | 688 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MultiCurrency often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MultiCurrency, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 735 | } |
||
| 736 | } |
||
| 737 |