2016-01-18 19:45:27 +01:00
< ? php
2020-03-25 17:30:54 +01:00
/* Copyright ( C ) 2007 - 2020 Laurent Destailleur < eldy @ users . sourceforge . net >
2016-01-18 19:45:27 +01:00
* Copyright ( C ) 2014 Juanjo Menent < jmenent @ 2 byte . es >
* Copyright ( C ) 2015 Florian Henry < florian . henry @ open - concept . pro >
* Copyright ( C ) 2015 Raphaël Doursenaud < rdoursenaud @ gpcsolutions . fr >
* Copyright ( C ) 2016 Pierre - Henry Favre < phf @ atm - consulting . fr >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2016-01-18 19:45:27 +01:00
*/
/**
2018-09-24 12:22:41 +02:00
* \file htdocs / multicurrency / class / multicurrency . class . php
* \ingroup multicurrency
* \brief This file is a CRUD class file ( Create / Read / Update / Delete ) for multicurrency
2016-01-18 19:45:27 +01:00
*/
// Put here all includes required by your class file
2020-04-10 10:59:32 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobjectline.class.php' ;
2016-01-18 19:45:27 +01:00
2020-03-25 17:30:54 +01:00
2016-01-18 19:45:27 +01:00
/**
* Class Currency
*
* Put here description of your class
* @ see CommonObject
*/
class MultiCurrency extends CommonObject
{
/**
* @ var string Id to identify managed objects
*/
public $element = 'multicurrency' ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
* @ var string Name of table without prefix where object is stored
*/
public $table_element = 'multicurrency' ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
* @ var string Name of table without prefix where object is stored
*/
2020-04-10 10:59:32 +02:00
public $table_element_line = " multicurrency_rate " ;
2016-01-18 19:45:27 +01:00
/**
* @ var CurrencyRate [] rates
*/
public $rates = array ();
/**
* @ var mixed Sample property 1
*/
public $id ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
* @ var mixed Sample property 1
*/
public $code ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
* @ var mixed Sample property 2
*/
public $name ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
2018-08-31 18:27:16 +02:00
* @ var int Entity
2016-01-18 19:45:27 +01:00
*/
public $entity ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
* @ var mixed Sample property 2
*/
public $date_create ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
* @ var mixed Sample property 2
*/
public $fk_user ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
* @ var mixed Sample property 2
*/
public $rate ;
2020-03-25 17:30:54 +01:00
2016-01-18 19:45:27 +01:00
/**
* Constructor
*
* @ param DoliDb $db Database handler
*/
public function __construct ( DoliDB $db )
{
2020-03-25 17:30:54 +01:00
$this -> db = $db ;
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
return 1 ;
}
/**
* Create object into database
*
2020-03-25 17:30:54 +01:00
* @ param User $user User that creates
* @ param bool $trigger true = launch triggers after , false = disable triggers
* @ return int < 0 if KO , Id of created object if OK
2016-01-18 19:45:27 +01:00
*/
public function create ( User $user , $trigger = true )
{
2020-04-10 10:59:32 +02:00
global $conf , $langs ;
2018-08-15 17:34:35 +02:00
2020-03-25 17:30:54 +01:00
dol_syslog ( 'MultiCurrency::create' , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$error = 0 ;
2018-08-15 17:34:35 +02:00
2021-02-26 18:26:44 +01:00
if ( self :: checkCodeAlreadyExists ( $this -> code )) {
2016-06-04 00:42:01 +02:00
$error ++ ;
$this -> errors [] = $langs -> trans ( 'multicurrency_code_already_added' );
return - 1 ;
}
2018-08-15 17:34:35 +02:00
2021-02-26 18:26:44 +01:00
if ( empty ( $this -> entity ) || $this -> entity <= 0 ) {
$this -> entity = $conf -> entity ;
}
2021-08-28 03:09:18 +02:00
$now = dol_now ();
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
// Insert request
2021-09-30 15:59:47 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . $this -> table_element . " ( " ;
2016-01-18 19:45:27 +01:00
$sql .= ' code,' ;
$sql .= ' name,' ;
$sql .= ' entity,' ;
$sql .= ' date_create,' ;
$sql .= ' fk_user' ;
$sql .= ') VALUES (' ;
2021-08-28 03:09:18 +02:00
$sql .= " ' " . $this -> db -> escape ( $this -> code ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> name ) . " ', " ;
$sql .= " " . (( int ) $this -> entity ) . " , " ;
$sql .= " ' " . $this -> db -> idate ( $now ) . " ', " ;
$sql .= " " . (( int ) $user -> id );
2016-01-18 19:45:27 +01:00
$sql .= ')' ;
$this -> db -> begin ();
2019-01-27 11:55:16 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2020-04-10 10:59:32 +02:00
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'MultiCurrency::create ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-01-18 19:45:27 +01:00
}
if ( ! $error ) {
2020-04-10 10:59:32 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . $this -> table_element );
2016-01-18 19:45:27 +01:00
$this -> date_create = $now ;
$this -> fk_user = $user -> id ;
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
if ( $trigger ) {
2020-04-10 10:59:32 +02:00
$result = $this -> call_trigger ( 'CURRENCY_CREATE' , $user );
2021-02-26 18:26:44 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2016-01-18 19:45:27 +01:00
}
}
if ( $error ) {
$this -> db -> rollback ();
2020-04-10 10:59:32 +02:00
return - 1 * $error ;
2016-01-18 19:45:27 +01:00
} else {
$this -> db -> commit ();
return $this -> id ;
}
}
/**
* Load object in memory from the database
*
2020-03-25 17:30:54 +01:00
* @ param int $id Id object
* @ param string $code code
* @ return int < 0 if KO , 0 if not found , > 0 if OK
2016-01-18 19:45:27 +01:00
*/
public function fetch ( $id , $code = null )
{
2020-03-25 17:30:54 +01:00
dol_syslog ( 'MultiCurrency::fetch' , LOG_DEBUG );
2018-08-15 17:34:35 +02:00
2016-10-25 12:07:34 +02:00
global $conf ;
2016-01-18 19:45:27 +01:00
2021-09-30 15:59:47 +02:00
$sql = " SELECT " ;
2016-01-18 19:45:27 +01:00
$sql .= ' c.rowid, c.name, c.code, c.entity, c.date_create, c.fk_user' ;
2020-04-10 10:59:32 +02:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element . ' AS c' ;
2021-02-26 18:26:44 +01:00
if ( ! empty ( $code )) {
$sql .= ' WHERE c.code = \'' . $this -> db -> escape ( $code ) . '\' AND c.entity = ' . $conf -> entity ;
} else {
2021-03-14 11:48:39 +01:00
$sql .= ' WHERE c.rowid = ' . (( int ) $id );
2021-02-26 18:26:44 +01:00
}
2016-01-18 19:45:27 +01:00
2019-01-27 11:55:16 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
if ( $resql ) {
$numrows = $this -> db -> num_rows ( $resql );
if ( $numrows ) {
$obj = $this -> db -> fetch_object ( $resql );
$this -> id = $obj -> rowid ;
$this -> name = $obj -> name ;
$this -> code = $obj -> code ;
$this -> entity = $obj -> entity ;
$this -> date_create = $obj -> date_create ;
$this -> fk_user = $obj -> fk_user ;
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
$this -> fetchAllCurrencyRate ();
$this -> getRate ();
}
$this -> db -> free ( $resql );
if ( $numrows ) {
return 1 ;
} else {
return 0 ;
}
} else {
2020-04-10 10:59:32 +02:00
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'MultiCurrency::fetch ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-01-18 19:45:27 +01:00
2016-03-11 23:42:02 +01:00
return - 1 ;
2016-01-18 19:45:27 +01:00
}
}
/**
* Load all rates in object from the database
*
* @ return int < 0 if KO , >= 0 if OK
*/
public function fetchAllCurrencyRate ()
{
2021-09-30 15:59:47 +02:00
$sql = " SELECT cr.rowid " ;
2020-04-10 10:59:32 +02:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element_line . ' as cr' ;
2021-08-27 23:36:06 +02:00
$sql .= ' WHERE cr.fk_multicurrency = ' . (( int ) $this -> id );
2020-04-10 10:59:32 +02:00
$sql .= ' ORDER BY cr.date_sync DESC' ;
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
$this -> rates = array ();
2019-01-27 11:55:16 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$num = $this -> db -> num_rows ( $resql );
while ( $obj = $this -> db -> fetch_object ( $resql )) {
$rate = new CurrencyRate ( $this -> db );
$rate -> fetch ( $obj -> rowid );
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
$this -> rates [] = $rate ;
}
$this -> db -> free ( $resql );
return $num ;
} else {
2020-04-10 10:59:32 +02:00
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'MultiCurrency::fetchAllCurrencyRate ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-01-18 19:45:27 +01:00
2020-04-10 10:59:32 +02:00
return - 1 ;
2016-01-18 19:45:27 +01:00
}
}
/**
* Update object into database
*
2020-03-25 17:30:54 +01:00
* @ param User $user User that modifies
* @ param bool $trigger true = launch triggers after , false = disable triggers
* @ return int < 0 if KO , > 0 if OK
2016-01-18 19:45:27 +01:00
*/
public function update ( User $user , $trigger = true )
{
$error = 0 ;
2020-03-25 17:30:54 +01:00
dol_syslog ( 'MultiCurrency::update' , LOG_DEBUG );
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
// Clean parameters
$this -> name = trim ( $this -> name );
$this -> code = trim ( $this -> code );
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
// Check parameters
if ( empty ( $this -> code )) {
$error ++ ;
2020-03-25 17:30:54 +01:00
dol_syslog ( 'MultiCurrency::update $this->code can not be empty' , LOG_ERR );
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
return - 1 ;
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
// Update request
2021-09-30 15:59:47 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element . " SET " ;
$sql .= " name = ' " . $this -> db -> escape ( $this -> name ) . " ' " ;
$sql .= " code = ' " . $this -> db -> escape ( $this -> code ) . " ' " ;
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2016-01-18 19:45:27 +01:00
$this -> db -> begin ();
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2020-04-10 10:59:32 +02:00
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'MultiCurrency::update ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-01-18 19:45:27 +01:00
}
if ( ! $error && $trigger ) {
2020-04-10 10:59:32 +02:00
$result = $this -> call_trigger ( 'CURRENCY_MODIFY' , $user );
2021-02-26 18:26:44 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2016-01-18 19:45:27 +01:00
}
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
2020-04-10 10:59:32 +02:00
return - 1 * $error ;
2016-01-18 19:45:27 +01:00
} else {
$this -> db -> commit ();
return 1 ;
}
}
/**
* Delete object in database
*
* @ param bool $trigger true = launch triggers after , false = disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
public function delete ( $trigger = true )
{
global $user ;
2018-08-15 17:34:35 +02:00
2020-03-25 17:30:54 +01:00
dol_syslog ( 'MultiCurrency::delete' , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$error = 0 ;
$this -> db -> begin ();
if ( $trigger ) {
2020-04-10 10:59:32 +02:00
$result = $this -> call_trigger ( 'CURRENCY_DELETE' , $user );
2021-02-26 18:26:44 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2016-01-18 19:45:27 +01:00
}
if ( ! $error ) {
// Delete all rates before
if ( ! $this -> deleteRates ()) {
2020-04-10 10:59:32 +02:00
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'Currency::delete ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-01-18 19:45:27 +01:00
}
2018-08-15 17:34:35 +02:00
2021-09-30 15:59:47 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2016-01-18 19:45:27 +01:00
2019-01-27 11:55:16 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2020-04-10 10:59:32 +02:00
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'MultiCurrency::delete ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-01-18 19:45:27 +01:00
}
}
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
2020-04-10 10:59:32 +02:00
return - 1 * $error ;
2016-01-18 19:45:27 +01:00
} else {
$this -> db -> commit ();
return 1 ;
}
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
/**
* Delete rates in database
*
* @ return bool false if KO , true if OK
*/
public function deleteRates ()
{
2021-02-26 18:26:44 +01:00
foreach ( $this -> rates as & $rate ) {
if ( $rate -> delete () <= 0 ) {
2016-01-18 19:45:27 +01:00
return false ;
}
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
return true ;
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
/**
2020-03-25 17:30:54 +01:00
* Add a Rate into database
2018-08-15 17:34:35 +02:00
*
2016-01-18 19:45:27 +01:00
* @ param double $rate rate value
2020-03-25 17:30:54 +01:00
* @ return int - 1 if KO , 1 if OK
2016-01-18 19:45:27 +01:00
*/
2018-08-14 12:23:10 +02:00
public function addRate ( $rate )
{
2021-02-26 18:26:44 +01:00
$currencyRate = new CurrencyRate ( $this -> db );
2020-03-25 17:30:54 +01:00
$currencyRate -> rate = price2num ( $rate );
2018-08-15 17:34:35 +02:00
2021-02-26 18:26:44 +01:00
if ( $currencyRate -> create ( $this -> id ) > 0 ) {
2016-01-18 19:45:27 +01:00
$this -> rate = $currencyRate ;
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2016-01-18 19:45:27 +01:00
$this -> rate = null ;
2020-04-10 10:59:32 +02:00
$this -> errors = $currencyRate -> errors ;
2016-01-18 19:45:27 +01:00
return - 1 ;
}
2019-02-03 14:29:45 +01:00
}
2018-08-15 17:34:35 +02:00
2018-08-14 12:23:10 +02:00
/**
2018-08-16 00:47:09 +02:00
* Try get label of code in llx_currency then add rate .
2018-08-14 12:23:10 +02:00
*
* @ param string $code currency code
* @ param double $rate new rate
* @ return int - 1 if KO , 1 if OK , 2 if label found and OK
*/
2020-09-07 10:18:17 +02:00
public function addRateFromDolibarr ( $code , $rate )
{
2021-09-27 15:41:58 +02:00
global $user ;
2018-08-15 17:34:35 +02:00
2020-09-20 02:57:15 +02:00
$currency = new MultiCurrency ( $this -> db );
2016-03-11 23:42:02 +01:00
$currency -> code = $code ;
$currency -> name = $code ;
2018-08-15 17:34:35 +02:00
2021-09-30 15:59:47 +02:00
$sql = " SELECT label FROM " . MAIN_DB_PREFIX . " c_currencies WHERE code_iso = ' " . $this -> db -> escape ( $code ) . " ' " ;
2018-08-15 17:34:35 +02:00
2021-02-26 18:26:44 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2021-09-27 15:41:58 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql && ( $line = $this -> db -> fetch_object ( $resql ))) {
2016-03-11 23:42:02 +01:00
$currency -> name = $line -> label ;
}
2018-08-15 17:34:35 +02:00
2021-02-26 18:26:44 +01:00
if ( $currency -> create ( $user ) > 0 ) {
2016-03-11 23:42:02 +01:00
$currency -> addRate ( $rate );
2018-08-15 17:34:35 +02:00
2021-02-26 18:26:44 +01:00
if ( ! empty ( $line )) {
return 2 ;
} else {
return 1 ;
}
2016-03-11 23:42:02 +01:00
}
2018-08-15 17:34:35 +02:00
return - 1 ;
2020-09-07 10:18:17 +02:00
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
/**
2020-09-07 10:18:17 +02:00
* Add new entry into llx_multicurrency_rate
*
* @ param double $rate rate value
* @ return int < 0 if KO , > 0 if OK
*/
public function updateRate ( $rate )
{
return $this -> addRate ( $rate );
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
/**
2018-08-15 17:34:35 +02:00
* Fetch CurrencyRate object in $this -> rate
*
2016-01-18 19:45:27 +01:00
* @ return int < 0 if KO , 0 if not found , > 0 if OK
*/
2020-09-07 10:18:17 +02:00
public function getRate ()
{
2021-09-30 15:59:47 +02:00
$sql = " SELECT cr.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element_line . " as cr " ;
2021-08-28 03:09:18 +02:00
$sql .= " WHERE cr.fk_multicurrency = " . (( int ) $this -> id );
$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 ) . " ) " ;
2018-08-15 17:34:35 +02:00
2019-01-27 11:55:16 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql && ( $obj = $this -> db -> fetch_object ( $resql ))) {
$this -> rate = new CurrencyRate ( $this -> db );
return $this -> rate -> fetch ( $obj -> rowid );
}
2020-09-07 10:18:17 +02:00
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
/**
2020-09-07 10:18:17 +02:00
* Get id of currency from code
*
2022-03-07 19:31:06 +01:00
* @ param DoliDB $dbs object db
2020-09-20 02:57:15 +02:00
* @ param string $code code value search
2020-09-07 10:18:17 +02:00
*
* @ return int 0 if not found , > 0 if OK
*/
2022-03-07 19:31:06 +01:00
public static function getIdFromCode ( $dbs , $code )
2020-09-07 10:18:17 +02:00
{
2021-02-26 18:26:44 +01:00
global $conf ;
2018-08-15 17:34:35 +02:00
2022-03-07 19:31:06 +01:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " multicurrency WHERE code = ' " . $dbs -> escape ( $code ) . " ' AND entity = " . (( int ) $conf -> entity );
2018-08-15 17:34:35 +02:00
2021-02-26 18:26:44 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2022-03-07 19:31:06 +01:00
$resql = $dbs -> query ( $sql );
if ( $resql && $obj = $dbs -> fetch_object ( $resql )) {
2021-02-26 18:26:44 +01:00
return $obj -> rowid ;
} else {
return 0 ;
}
2020-09-07 10:18:17 +02:00
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
/**
2020-09-07 10:18:17 +02:00
* Get id and rate of currency from code
*
2022-03-07 19:31:06 +01:00
* @ param DoliDB $dbs Object db
2020-09-07 10:18:17 +02:00
* @ param string $code Code value search
* @ param integer $date_document Date from document ( propal , order , invoice , ... )
*
* @ return array [ 0 ] => id currency
2021-09-27 15:41:58 +02:00
* [ 1 ] => rate
2020-09-07 10:18:17 +02:00
*/
2022-03-07 19:31:06 +01:00
public static function getIdAndTxFromCode ( $dbs , $code , $date_document = '' )
2020-09-07 10:18:17 +02:00
{
2016-12-11 16:30:01 +01:00
global $conf ;
2018-08-15 17:34:35 +02:00
2021-09-30 15:59:47 +02:00
$sql1 = " SELECT m.rowid, mc.rate FROM " . MAIN_DB_PREFIX . " multicurrency m " ;
2020-07-25 00:22:47 +02:00
2020-04-10 10:59:32 +02:00
$sql1 .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'multicurrency_rate mc ON (m.rowid = mc.fk_multicurrency)' ;
2022-03-07 19:31:06 +01:00
$sql1 .= " WHERE m.code = ' " . $dbs -> escape ( $code ) . " ' " ;
2020-04-10 10:59:32 +02:00
$sql1 .= " AND m.entity IN ( " . getEntity ( 'multicurrency' ) . " ) " ;
$sql2 = '' ;
2020-03-12 17:01:07 +01:00
if ( ! empty ( $conf -> global -> MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE ) && ! empty ( $date_document )) { // Use last known rate compared to document date
$tmparray = dol_getdate ( $date_document );
2022-03-07 19:31:06 +01:00
$sql2 .= " AND mc.date_sync <= ' " . $dbs -> idate ( dol_mktime ( 23 , 59 , 59 , $tmparray [ 'mon' ], $tmparray [ 'mday' ], $tmparray [ 'year' ], true )) . " ' " ;
2020-03-12 17:01:07 +01:00
}
2021-09-30 15:59:47 +02:00
$sql3 = " ORDER BY mc.date_sync DESC LIMIT 1 " ;
2018-08-15 17:34:35 +02:00
2019-01-27 11:55:16 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2022-03-07 19:31:06 +01:00
$resql = $dbs -> query ( $sql1 . $sql2 . $sql3 );
2018-08-15 17:34:35 +02:00
2022-03-07 19:31:06 +01:00
if ( $resql && $obj = $dbs -> fetch_object ( $resql )) {
2021-02-26 18:26:44 +01:00
return array ( $obj -> rowid , $obj -> rate );
} else {
if ( ! empty ( $conf -> global -> MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE )) {
2022-03-07 19:31:06 +01:00
$resql = $dbs -> query ( $sql1 . $sql3 );
if ( $resql && $obj = $dbs -> fetch_object ( $resql )) {
2021-02-26 18:26:44 +01:00
return array ( $obj -> rowid , $obj -> rate );
}
2016-12-11 16:30:01 +01:00
}
2018-08-15 17:34:35 +02:00
2016-12-11 16:30:01 +01:00
return array ( 0 , 1 );
}
2020-09-07 10:18:17 +02:00
}
/**
* Get the conversion of amount with invoice rate
*
2022-04-08 19:52:40 +02:00
* @ param int $fk_facture id of facture
* @ param double $amount amount to convert
* @ param string $way 'dolibarr' mean the amount is in dolibarr currency
* @ param string $table facture or facture_fourn
* @ return double | boolean amount converted or false if conversion fails
2020-09-07 10:18:17 +02:00
*/
public static function getAmountConversionFromInvoiceRate ( $fk_facture , $amount , $way = 'dolibarr' , $table = 'facture' )
{
$multicurrency_tx = self :: getInvoiceRate ( $fk_facture , $table );
2021-02-26 18:26:44 +01:00
if ( $multicurrency_tx ) {
if ( $way == 'dolibarr' ) {
return price2num ( $amount * $multicurrency_tx , 'MU' );
} else {
return price2num ( $amount / $multicurrency_tx , 'MU' );
}
} else {
2022-04-08 19:52:40 +02:00
return false ;
2021-02-26 18:26:44 +01:00
}
2020-09-07 10:18:17 +02:00
}
2018-08-15 17:34:35 +02:00
2018-08-21 14:03:02 +02:00
/**
* Get current invoite rate
*
2022-04-08 19:52:40 +02:00
* @ param int $fk_facture id of facture
* @ param string $table facture or facture_fourn
* @ return float | bool Rate of currency or false if error
2018-08-21 14:03:02 +02:00
*/
2019-01-27 15:20:16 +01:00
public static function getInvoiceRate ( $fk_facture , $table = 'facture' )
2018-08-21 14:03:02 +02:00
{
global $db ;
2018-08-15 17:34:35 +02:00
2021-09-30 15:59:47 +02:00
$sql = " SELECT multicurrency_tx FROM " . MAIN_DB_PREFIX . $table . " WHERE rowid = " . (( int ) $fk_facture );
2018-08-15 17:34:35 +02:00
2019-01-27 11:55:16 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2018-08-21 14:03:02 +02:00
$resql = $db -> query ( $sql );
2021-02-26 18:26:44 +01:00
if ( $resql && ( $line = $db -> fetch_object ( $resql ))) {
2018-08-21 14:03:02 +02:00
return $line -> multicurrency_tx ;
}
2016-03-11 23:42:02 +01:00
2018-08-21 14:03:02 +02:00
return false ;
}
2016-03-11 23:42:02 +01:00
/**
2020-05-28 21:24:46 +02:00
* With free account we can ' t set source then recalcul all rates to force another source .
* This modify the array & $TRate .
2018-08-15 17:34:35 +02:00
*
* @ param stdClass $TRate Object containing all currencies rates
2020-05-28 21:24:46 +02:00
* @ return int - 1 if KO , 0 if nothing , 1 if OK
2016-03-11 23:42:02 +01:00
*/
2018-08-15 17:34:35 +02:00
public static function recalculRates ( & $TRate )
2016-03-11 23:42:02 +01:00
{
global $conf ;
2018-08-15 17:34:35 +02:00
2021-02-26 18:26:44 +01:00
if ( $conf -> currency != $conf -> global -> MULTICURRENCY_APP_SOURCE ) {
2020-09-07 10:18:17 +02:00
$alternate_source = 'USD' . $conf -> currency ;
2021-02-26 18:26:44 +01:00
if ( ! empty ( $TRate -> { $alternate_source })) {
2016-03-11 23:42:02 +01:00
$coef = $TRate -> USDUSD / $TRate -> { $alternate_source };
2021-02-26 18:26:44 +01:00
foreach ( $TRate as $attr => & $rate ) {
2016-03-11 23:42:02 +01:00
$rate *= $coef ;
}
2018-08-15 17:34:35 +02:00
2016-03-11 23:42:02 +01:00
return 1 ;
}
2018-08-15 17:34:35 +02:00
2016-03-11 23:42:02 +01:00
return - 1 ; // Alternate souce not found
}
2018-08-15 17:34:35 +02:00
2016-03-11 23:42:02 +01:00
return 0 ; // Nothing to do
}
2018-08-15 17:34:35 +02:00
2016-03-10 23:52:06 +01:00
/**
2020-03-25 17:24:20 +01:00
* Sync rates from API
2018-08-15 17:34:35 +02:00
*
2019-05-02 13:31:47 +02:00
* @ param string $key Key to use . Come from $conf -> global -> MULTICURRENCY_APP_ID .
* @ param int $addifnotfound Add if not found
2020-09-07 10:18:17 +02:00
* @ return int < 0 if KO , > 0 if OK
2016-03-10 23:52:06 +01:00
*/
2019-05-02 13:31:47 +02:00
public static function syncRates ( $key , $addifnotfound = 0 )
2016-03-10 23:52:06 +01:00
{
2019-02-19 14:01:08 +01:00
global $conf , $db , $langs ;
2018-08-15 17:34:35 +02:00
2020-11-09 11:20:23 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/geturl.lib.php' ;
2021-10-23 23:42:06 +02:00
$urlendpoint = 'http://api.currencylayer.com/live?access_key=' . $key ;
$urlendpoint .= '&source=' . ( empty ( $conf -> global -> MULTICURRENCY_APP_SOURCE ) ? 'USD' : $conf -> global -> MULTICURRENCY_APP_SOURCE );
2019-05-02 13:31:47 +02:00
2019-05-02 13:24:28 +02:00
dol_syslog ( " Call url endpoint " . $urlendpoint );
2021-10-23 23:42:06 +02:00
$resget = getURLContent ( $urlendpoint );
2020-09-07 10:18:17 +02:00
2020-11-09 11:20:23 +01:00
if ( $resget [ 'content' ]) {
$response = $resget [ 'content' ];
$response = json_decode ( $response );
2018-05-30 21:14:51 +02:00
2021-02-26 18:26:44 +01:00
if ( $response -> success ) {
2020-11-09 11:20:23 +01:00
$TRate = $response -> quotes ;
$timestamp = $response -> timestamp ;
2021-02-26 18:26:44 +01:00
if ( self :: recalculRates ( $TRate ) >= 0 ) {
foreach ( $TRate as $currency_code => $rate ) {
2020-11-09 11:20:23 +01:00
$code = substr ( $currency_code , 3 , 3 );
$obj = new MultiCurrency ( $db );
2021-02-26 18:26:44 +01:00
if ( $obj -> fetch ( null , $code ) > 0 ) {
2020-11-09 11:20:23 +01:00
$obj -> updateRate ( $rate );
2021-02-26 18:26:44 +01:00
} elseif ( $addifnotfound ) {
2020-11-09 11:20:23 +01:00
self :: addRateFromDolibarr ( $code , $rate );
}
2018-05-30 21:14:51 +02:00
}
2018-08-15 17:34:35 +02:00
}
2020-03-25 17:24:20 +01:00
2020-11-09 11:20:23 +01:00
return 1 ;
} else {
dol_syslog ( " Failed to call endpoint " . $response -> error -> info , LOG_WARNING );
setEventMessages ( $langs -> trans ( 'multicurrency_syncronize_error' , $response -> error -> info ), null , 'errors' );
2020-03-25 17:24:20 +01:00
2020-11-09 11:20:23 +01:00
return - 1 ;
}
2016-03-11 23:42:02 +01:00
}
2016-03-10 23:52:06 +01:00
}
2018-08-15 17:34:35 +02:00
2016-06-04 00:42:01 +02:00
/**
* Check in database if the current code already exists
2018-08-15 17:34:35 +02:00
*
2016-06-04 00:42:01 +02:00
* @ param string $code current code to search
2016-06-20 02:34:00 +02:00
* @ return boolean True if exists , false if not exists
2016-06-04 00:42:01 +02:00
*/
2021-09-27 15:41:58 +02:00
public function checkCodeAlreadyExists ( $code )
2018-08-14 12:23:10 +02:00
{
2021-09-27 15:41:58 +02:00
$currencytmp = new MultiCurrency ( $this -> db );
if ( $currencytmp -> fetch ( '' , $code ) > 0 ) {
2021-02-26 18:26:44 +01:00
return true ;
} else {
return false ;
}
2018-08-14 12:23:10 +02:00
}
2016-01-18 19:45:27 +01:00
}
2020-10-31 14:24:00 +01:00
2016-01-18 19:45:27 +01:00
/**
* Class CurrencyRate
*/
class CurrencyRate extends CommonObjectLine
{
/**
* @ var string Id to identify managed objects
*/
public $element = 'multicurrency_rate' ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
* @ var string Name of table without prefix where object is stored
*/
public $table_element = 'multicurrency_rate' ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
* @ var int ID
*/
public $id ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
* @ var double Rate
*/
public $rate ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
2019-04-04 18:33:12 +02:00
* @ var integer Date synchronisation
2016-01-18 19:45:27 +01:00
*/
public $date_sync ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
* @ var int Id of currency
*/
public $fk_multicurrency ;
2018-08-31 18:27:16 +02:00
2016-01-18 19:45:27 +01:00
/**
* @ var int Id of entity
*/
public $entity ;
2018-08-15 17:34:35 +02:00
2020-10-31 14:24:00 +01:00
2016-01-18 19:45:27 +01:00
/**
* Constructor
*
* @ param DoliDb $db Database handler
*/
public function __construct ( DoliDB $db )
{
2022-03-07 19:31:06 +01:00
$this -> db = $db ;
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
return 1 ;
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
/**
* Create object into database
*
* @ param int $fk_multicurrency Id of currency
* @ param bool $trigger true = launch triggers after , false = disable triggers
2020-10-31 14:24:00 +01:00
* @ return int < 0 if KO , Id of created object if OK
2016-01-18 19:45:27 +01:00
*/
public function create ( $fk_multicurrency , $trigger = true )
{
global $conf , $user ;
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
dol_syslog ( 'CurrencyRate::create' , LOG_DEBUG );
$error = 0 ;
$this -> rate = price2num ( $this -> rate );
2021-02-26 18:26:44 +01:00
if ( empty ( $this -> entity ) || $this -> entity <= 0 ) {
$this -> entity = $conf -> entity ;
}
2020-10-31 14:24:00 +01:00
$now = empty ( $this -> date_sync ) ? dol_now () : $this -> date_sync ;
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
// Insert request
2021-09-30 15:59:47 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . $this -> table_element . " ( " ;
2016-01-18 19:45:27 +01:00
$sql .= ' rate,' ;
$sql .= ' date_sync,' ;
$sql .= ' fk_multicurrency,' ;
$sql .= ' entity' ;
$sql .= ') VALUES (' ;
2021-08-28 03:09:18 +02:00
$sql .= ' ' . (( float ) $this -> rate ) . ',' ;
2020-10-31 14:24:00 +01:00
$sql .= " ' " . $this -> db -> idate ( $now ) . " ', " ;
$sql .= " " . (( int ) $fk_multicurrency ) . " , " ;
$sql .= " " . (( int ) $this -> entity );
2016-01-18 19:45:27 +01:00
$sql .= ')' ;
$this -> db -> begin ();
2019-01-27 11:55:16 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2020-04-10 10:59:32 +02:00
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'CurrencyRate::create ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-01-18 19:45:27 +01:00
}
if ( ! $error ) {
2020-04-10 10:59:32 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . $this -> table_element );
2016-01-18 19:45:27 +01:00
$this -> fk_multicurrency = $fk_multicurrency ;
$this -> date_sync = $now ;
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
if ( $trigger ) {
2020-04-10 10:59:32 +02:00
$result = $this -> call_trigger ( 'CURRENCYRATE_CREATE' , $user );
2021-02-26 18:26:44 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2016-01-18 19:45:27 +01:00
}
}
if ( $error ) {
$this -> db -> rollback ();
2020-04-10 10:59:32 +02:00
return - 1 * $error ;
2016-01-18 19:45:27 +01:00
} else {
$this -> db -> commit ();
return $this -> id ;
}
}
/**
* Load object in memory from the database
*
2020-10-31 14:24:00 +01:00
* @ param int $id Id object
* @ return int < 0 if KO , 0 if not found , > 0 if OK
2016-01-18 19:45:27 +01:00
*/
public function fetch ( $id )
{
dol_syslog ( 'CurrencyRate::fetch' , LOG_DEBUG );
2021-09-30 15:59:47 +02:00
$sql = " SELECT cr.rowid, cr.rate, cr.date_sync, cr.fk_multicurrency, cr.entity " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " AS cr " ;
$sql .= " WHERE cr.rowid = " . (( int ) $id );
2016-01-18 19:45:27 +01:00
2019-01-27 11:55:16 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$numrows = $this -> db -> num_rows ( $resql );
if ( $numrows ) {
$obj = $this -> db -> fetch_object ( $resql );
$this -> id = $obj -> rowid ;
$this -> rate = $obj -> rate ;
2020-10-31 14:24:00 +01:00
$this -> date_sync = $this -> db -> jdate ( $obj -> date_sync );
2016-01-18 19:45:27 +01:00
$this -> fk_multicurrency = $obj -> fk_multicurrency ;
$this -> entity = $obj -> entity ;
}
$this -> db -> free ( $resql );
if ( $numrows ) {
return 1 ;
} else {
return 0 ;
}
} else {
2020-04-10 10:59:32 +02:00
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'CurrencyRate::fetch ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-01-18 19:45:27 +01:00
2020-04-10 10:59:32 +02:00
return - 1 ;
2016-01-18 19:45:27 +01:00
}
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
/**
* Update object into database
*
2020-10-31 14:24:00 +01:00
* @ param bool $trigger true = launch triggers after , false = disable triggers
* @ return int < 0 if KO , > 0 if OK
2016-01-18 19:45:27 +01:00
*/
public function update ( $trigger = true )
{
global $user ;
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
$error = 0 ;
dol_syslog ( 'CurrencyRate::update' , LOG_DEBUG );
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
$this -> rate = price2num ( $this -> rate );
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
// Update request
2021-09-30 15:59:47 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " SET rate = " . (( float ) $this -> rate );
2021-02-26 18:26:44 +01:00
if ( ! empty ( $this -> date_sync )) {
2021-09-30 15:59:47 +02:00
$sql .= " , date_sync = ' " . $this -> db -> idate ( $this -> date_sync ) . " ' " ;
2021-02-26 18:26:44 +01:00
}
if ( ! empty ( $this -> fk_multicurrency )) {
2021-09-30 15:59:47 +02:00
$sql .= ', fk_multicurrency = ' . (( int ) $this -> fk_multicurrency );
2021-02-26 18:26:44 +01:00
}
2021-09-30 15:59:47 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2016-01-18 19:45:27 +01:00
$this -> db -> begin ();
2019-01-27 11:55:16 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2020-04-10 10:59:32 +02:00
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'CurrencyRate::update ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-01-18 19:45:27 +01:00
}
if ( ! $error && $trigger ) {
2020-04-10 10:59:32 +02:00
$result = $this -> call_trigger ( 'CURRENCYRATE_MODIFY' , $user );
2021-02-26 18:26:44 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2016-01-18 19:45:27 +01:00
}
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
2016-03-11 23:42:02 +01:00
return - 1 * $error ;
2016-01-18 19:45:27 +01:00
} else {
$this -> db -> commit ();
return 1 ;
}
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
/**
* Delete object in database
*
2020-10-31 14:24:00 +01:00
* @ param bool $trigger true = launch triggers after , false = disable triggers
* @ return int < 0 if KO , > 0 if OK
2016-01-18 19:45:27 +01:00
*/
public function delete ( $trigger = true )
{
global $user ;
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
dol_syslog ( 'CurrencyRate::delete' , LOG_DEBUG );
$error = 0 ;
$this -> db -> begin ();
if ( $trigger ) {
2020-04-10 10:59:32 +02:00
$result = $this -> call_trigger ( 'CURRENCYRATE_DELETE' , $user );
2021-02-26 18:26:44 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2016-01-18 19:45:27 +01:00
}
if ( ! $error ) {
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this -> table_element ;
2021-03-14 12:20:23 +01:00
$sql .= ' WHERE rowid=' . (( int ) $this -> id );
2016-01-18 19:45:27 +01:00
2019-01-27 11:55:16 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2020-04-10 10:59:32 +02:00
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'CurrencyRate::delete ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-01-18 19:45:27 +01:00
}
}
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
2020-04-10 10:59:32 +02:00
return - 1 * $error ;
2016-01-18 19:45:27 +01:00
} else {
$this -> db -> commit ();
return 1 ;
}
}
}