2016-01-18 19:45:27 +01:00
< ? php
/* Copyright ( C ) 2007 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
* 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
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
/**
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
2018-09-24 12:22:41 +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
/**
* 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
*/
public $table_element_line = " multicurrency_rate " ;
/**
* @ 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 ;
/**
* Constructor
*
* @ param DoliDb $db Database handler
*/
public function __construct ( DoliDB $db )
{
$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
*
* @ 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
*/
public function create ( User $user , $trigger = true )
{
2016-06-04 00:42:01 +02:00
global $conf , $langs ;
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
dol_syslog ( 'Currency::create' , LOG_DEBUG );
$error = 0 ;
2018-08-15 17:34:35 +02:00
2016-06-04 00:42:01 +02:00
if ( self :: checkCodeAlreadyExists ( $this -> code ))
{
$error ++ ;
$this -> errors [] = $langs -> trans ( 'multicurrency_code_already_added' );
return - 1 ;
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
if ( empty ( $this -> entity ) || $this -> entity <= 0 ) $this -> entity = $conf -> entity ;
$now = date ( 'Y-m-d H:i:s' );
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
// Insert request
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this -> table_element . '(' ;
$sql .= ' code,' ;
$sql .= ' name,' ;
$sql .= ' entity,' ;
$sql .= ' date_create,' ;
$sql .= ' fk_user' ;
$sql .= ') VALUES (' ;
$sql .= ' \'' . $this -> db -> escape ( $this -> code ) . '\',' ;
$sql .= ' \'' . $this -> db -> escape ( $this -> name ) . '\',' ;
$sql .= ' \'' . $this -> entity . '\',' ;
$sql .= ' \'' . $now . '\',' ;
$sql .= ' \'' . $user -> id . '\'' ;
$sql .= ')' ;
$this -> db -> begin ();
2016-06-30 14:24:55 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'Currency::create ' . join ( ',' , $this -> errors ), LOG_ERR );
}
if ( ! $error ) {
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . $this -> table_element );
$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 ) {
$result = $this -> call_trigger ( 'CURRENCY_CREATE' , $user );
if ( $result < 0 ) $error ++ ;
}
}
if ( $error ) {
$this -> db -> rollback ();
return - 1 * $error ;
} else {
$this -> db -> commit ();
return $this -> id ;
}
}
/**
* Load object in memory from the database
*
* @ param int $id Id object
* @ param string $code code
*
* @ return int < 0 if KO , 0 if not found , > 0 if OK
*/
public function fetch ( $id , $code = null )
{
dol_syslog ( 'Currency::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
$sql = 'SELECT' ;
$sql .= ' c.rowid, c.name, c.code, c.entity, c.date_create, c.fk_user' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element . ' AS c' ;
2016-10-25 12:07:34 +02:00
if ( ! empty ( $code )) $sql .= ' WHERE c.code = \'' . $this -> db -> escape ( $code ) . '\' AND c.entity = ' . $conf -> entity ;
2016-01-18 19:45:27 +01:00
else $sql .= ' WHERE c.rowid = ' . $id ;
2016-06-30 14:24:55 +02: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 {
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'Currency::fetch ' . join ( ',' , $this -> errors ), LOG_ERR );
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 ()
{
$sql = 'SELECT cr.rowid' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element_line . ' as cr' ;
$sql .= ' WHERE cr.fk_multicurrency = ' . $this -> id ;
$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 ();
2016-06-30 14:24:55 +02: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 {
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'Currency::fetchAllCurrencyRate ' . join ( ',' , $this -> errors ), LOG_ERR );
return - 1 ;
}
}
/**
* Update object into database
*
* @ param User $user User that modifies
* @ param bool $trigger true = launch triggers after , false = disable triggers
*
* @ return int < 0 if KO , > 0 if OK
*/
public function update ( User $user , $trigger = true )
{
$error = 0 ;
dol_syslog ( 'Currency::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 ++ ;
dol_syslog ( 'Currency::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
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element . ' SET' ;
2016-06-30 14:24:55 +02:00
$sql .= ' name=\'' . $this -> db -> escape ( $this -> name ) . '\'' ;
$sql .= ' code=\'' . $this -> db -> escape ( $this -> code ) . '\'' ;
2016-01-18 19:45:27 +01:00
$sql .= ' WHERE rowid=' . $this -> id ;
2016-06-30 14:24:55 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$this -> db -> begin ();
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'Currency::update ' . join ( ',' , $this -> errors ), LOG_ERR );
}
if ( ! $error && $trigger ) {
$result = $this -> call_trigger ( 'CURRENCY_MODIFY' , $user );
if ( $result < 0 ) $error ++ ;
}
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
return - 1 * $error ;
} 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
2016-01-18 19:45:27 +01:00
dol_syslog ( 'Currency::delete' , LOG_DEBUG );
$error = 0 ;
$this -> db -> begin ();
if ( $trigger ) {
$result = $this -> call_trigger ( 'CURRENCY_DELETE' , $user );
if ( $result < 0 ) $error ++ ;
}
if ( ! $error ) {
// Delete all rates before
if ( ! $this -> deleteRates ()) {
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'Currency::delete ' . join ( ',' , $this -> errors ), LOG_ERR );
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= ' WHERE rowid=' . $this -> id ;
2016-06-30 14:24:55 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'Currency::delete ' . join ( ',' , $this -> errors ), LOG_ERR );
}
}
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
return - 1 * $error ;
} 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 ()
{
foreach ( $this -> rates as & $rate )
{
if ( $rate -> delete () <= 0 )
{
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
/**
2018-08-15 17:34:35 +02:00
* Delete rate in database
*
2016-01-18 19:45:27 +01:00
* @ param double $rate rate value
2018-08-15 17:34:35 +02:00
*
2016-03-11 23:42:02 +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 )
{
2016-01-18 19:45:27 +01:00
$currencyRate = new CurrencyRate ( $this -> db );
$currencyRate -> rate = $rate ;
2018-08-15 17:34:35 +02:00
if ( $currencyRate -> create ( $this -> id ) > 0 )
2016-01-18 19:45:27 +01:00
{
$this -> rate = $currencyRate ;
return 1 ;
}
2018-08-15 17:34:35 +02:00
else
2016-01-18 19:45:27 +01:00
{
$this -> rate = null ;
return - 1 ;
}
}
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
*/
2016-03-11 23:42:02 +01:00
function addRateFromDolibarr ( $code , $rate )
{
global $db , $user ;
2018-08-15 17:34:35 +02:00
2016-03-11 23:42:02 +01:00
$currency = new MultiCurrency ( $db );
$currency -> code = $code ;
$currency -> name = $code ;
2018-08-15 17:34:35 +02:00
2016-06-30 14:24:55 +02:00
$sql = 'SELECT label FROM ' . MAIN_DB_PREFIX . 'c_currencies WHERE code_iso = \'' . $db -> escape ( $code ) . '\'' ;
2018-08-15 17:34:35 +02:00
2016-06-30 14:24:55 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-03-11 23:42:02 +01:00
$resql = $db -> query ( $sql );
if ( $resql && ( $line = $db -> fetch_object ( $resql )))
{
$currency -> name = $line -> label ;
}
2018-08-15 17:34:35 +02:00
2016-03-11 23:42:02 +01:00
if ( $currency -> create ( $user ) > 0 )
{
$currency -> addRate ( $rate );
2018-08-15 17:34:35 +02:00
2016-03-11 23:42:02 +01:00
if ( ! empty ( $line )) return 2 ;
else return 1 ;
}
2018-08-15 17:34:35 +02:00
return - 1 ;
2016-03-11 23:42:02 +01:00
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
/**
2016-12-11 12:01:38 +01:00
* Add new entry into llx_multicurrency_rate to historise
2018-08-15 17:34:35 +02:00
*
2016-01-18 19:45:27 +01:00
* @ param double $rate rate value
2018-08-15 17:34:35 +02:00
*
2016-01-18 19:45:27 +01:00
* @ return int < 0 if KO , > 0 if OK
*/
2018-08-14 12:23:10 +02:00
public function updateRate ( $rate )
{
2016-12-11 12:01:38 +01:00
return $this -> addRate ( $rate );
2016-01-18 19:45:27 +01:00
}
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
*/
2018-08-14 12:23:10 +02:00
public function getRate ()
{
2016-01-18 19:45:27 +01:00
$sql = 'SELECT cr.rowid' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element_line . ' as cr' ;
$sql .= ' WHERE cr.fk_multicurrency = ' . $this -> id ;
2016-12-11 12:01:38 +01:00
$sql .= ' AND cr.date_sync = (SELECT MAX(cr2.date_sync) FROM ' . MAIN_DB_PREFIX . $this -> table_element_line . ' AS cr2 WHERE cr2.fk_multicurrency = ' . $this -> id . ')' ;
2018-08-15 17:34:35 +02:00
2016-06-30 14:24:55 +02: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 );
}
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
/**
2018-08-15 17:34:35 +02:00
* Get id of currency from code
2016-01-18 19:45:27 +01:00
*
* @ param DoliDB $db object db
* @ param string $code code value search
2018-08-15 17:34:35 +02:00
*
2016-01-18 19:45:27 +01:00
* @ return 0 if not found , > 0 if OK
*/
2018-08-14 12:23:10 +02:00
public static function getIdFromCode ( & $db , $code )
{
2016-10-25 12:45:23 +02:00
global $conf ;
2018-08-15 17:34:35 +02:00
2016-10-25 12:45:23 +02:00
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . 'multicurrency WHERE code = \'' . $db -> escape ( $code ) . '\' AND entity = ' . $conf -> entity ;
2018-08-15 17:34:35 +02:00
2016-06-30 14:24:55 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $db -> query ( $sql );
if ( $resql && $obj = $db -> fetch_object ( $resql )) return $obj -> rowid ;
else return 0 ;
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
/**
2018-08-15 17:34:35 +02:00
* Get id and rate of currency from code
*
2016-01-18 19:45:27 +01:00
* @ param DoliDB $db object db
* @ param string $code code value search
2016-12-11 16:30:01 +01:00
* @ param date $date_document date from document ( propal , order , invoice , ... )
2018-08-15 17:34:35 +02:00
*
2016-01-18 19:45:27 +01:00
* @ return array [ 0 ] => id currency
* [ 1 ] => rate
*/
2018-08-14 12:23:10 +02:00
public static function getIdAndTxFromCode ( & $db , $code , $date_document = '' )
{
2016-12-11 16:30:01 +01:00
global $conf ;
2018-08-15 17:34:35 +02:00
2016-12-11 16:30:01 +01:00
$sql1 = 'SELECT m.rowid, mc.rate FROM ' . MAIN_DB_PREFIX . 'multicurrency m' ;
$sql1 .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'multicurrency_rate mc ON (m.rowid = mc.fk_multicurrency)' ;
$sql1 .= ' WHERE m.code = \'' . $db -> escape ( $code ) . '\'' ;
2017-05-30 18:50:54 +02:00
$sql1 .= " AND m.entity IN ( " . getEntity ( 'multicurrency' ) . " ) " ;
2016-12-11 16:30:01 +01:00
$sql2 = '' ;
if ( ! empty ( $conf -> global -> MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE ) && ! empty ( $date_document )) $sql2 .= ' AND DATE_FORMAT(mc.date_sync, "%Y-%m-%d") = "' . date ( 'Y-m-d' , $date_document ) . '"' ;
$sql3 .= ' ORDER BY mc.date_sync DESC LIMIT 1' ;
2018-08-15 17:34:35 +02:00
2016-06-30 14:24:55 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-12-11 16:30:01 +01:00
$resql = $db -> query ( $sql1 . $sql2 . $sql3 );
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
if ( $resql && $obj = $db -> fetch_object ( $resql )) return array ( $obj -> rowid , $obj -> rate );
2016-12-11 16:30:01 +01:00
else
{
if ( ! empty ( $conf -> global -> MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE ))
{
$resql = $db -> query ( $sql1 . $sql3 );
if ( $resql && $obj = $db -> fetch_object ( $resql )) return array ( $obj -> rowid , $obj -> rate );
}
2018-08-15 17:34:35 +02:00
2016-12-11 16:30:01 +01:00
return array ( 0 , 1 );
}
2018-08-15 17:34:35 +02:00
}
2016-02-10 23:16:38 +01:00
/**
* Get the conversion of amount with invoice rate
2018-08-15 17:34:35 +02:00
*
2016-02-10 23:16:38 +01: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 amount converted
*/
public static function getAmountConversionFromInvoiceRate ( $fk_facture , $amount , $way = 'dolibarr' , $table = 'facture' )
{
global $db ;
2018-08-15 17:34:35 +02:00
2016-02-10 23:16:38 +01:00
$multicurrency_tx = self :: getInvoiceRate ( $fk_facture , $table );
2018-08-15 17:34:35 +02:00
if ( $multicurrency_tx )
2016-02-10 23:16:38 +01:00
{
if ( $way == 'dolibarr' ) return $amount * $multicurrency_tx ;
else return $amount / $multicurrency_tx ;
}
else return $amount ;
}
2018-08-15 17:34:35 +02:00
2018-08-21 14:03:02 +02:00
/**
* Get current invoite rate
*
* @ param int $fk_facture id of facture
* @ param string $table facture or facture_fourn
2018-08-14 12:23:10 +02:00
* @ return bool
2018-08-21 14:03:02 +02:00
*/
public static function getInvoiceRate ( $fk_facture , $table = 'facture' )
{
global $db ;
2018-08-15 17:34:35 +02:00
2018-08-21 14:03:02 +02:00
$sql = 'SELECT multicurrency_tx FROM ' . MAIN_DB_PREFIX . $table . ' WHERE rowid = ' . $fk_facture ;
2018-08-15 17:34:35 +02:00
2018-08-21 14:03:02 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
$resql = $db -> query ( $sql );
if ( $resql && ( $line = $db -> fetch_object ( $resql )))
{
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
/**
* With free account we can ' t set source then recalcul all rates to force another source
2018-08-15 17:34:35 +02:00
*
* @ param stdClass $TRate Object containing all currencies rates
2016-03-11 23:42:02 +01:00
* @ return - 1 if KO , 0 if nothing , 1 if OK
*/
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
2016-03-11 23:42:02 +01:00
if ( ! empty ( $conf -> global -> MULTICURRENCY_ALTERNATE_SOURCE ))
{
$alternate_source = 'USD' . $conf -> global -> MULTICURRENCY_ALTERNATE_SOURCE ;
if ( ! empty ( $TRate -> { $alternate_source }))
{
$coef = $TRate -> USDUSD / $TRate -> { $alternate_source };
foreach ( $TRate as $attr => & $rate )
{
$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
/**
* Sync rates from api
2018-08-15 17:34:35 +02:00
*
2016-03-10 23:52:06 +01:00
* @ param array $response array of reponse from api to sync dolibarr rates
2018-08-14 12:23:10 +02:00
* @ return void
2016-03-10 23:52:06 +01:00
*/
public static function syncRates ( $response )
{
2016-03-11 23:42:02 +01:00
global $db , $conf ;
2018-08-15 17:34:35 +02:00
2018-05-30 21:14:51 +02:00
$ch = curl_init ( 'http://apilayer.net/api/live?access_key=' . $key . '' );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
$response = curl_exec ( $ch );
curl_close ( $ch );
$response = json_decode ( $response );
if ( $response -> success )
{
2018-08-15 17:34:35 +02:00
2018-05-30 21:14:51 +02:00
$TRate = $response -> quotes ;
$timestamp = $response -> timestamp ;
2018-08-15 17:34:35 +02:00
if ( self :: recalculRates ( $TRate ) >= 0 )
2016-03-11 23:42:02 +01:00
{
2018-05-30 21:14:51 +02:00
foreach ( $TRate as $currency_code => $rate )
2016-03-11 23:42:02 +01:00
{
2018-05-30 21:14:51 +02:00
$code = substr ( $currency_code , 3 , 3 );
$obj = new MultiCurrency ( $db );
if ( $obj -> fetch ( null , $code ) > 0 )
{
$obj -> updateRate ( $rate );
}
2018-08-15 17:34:35 +02:00
else
2018-05-30 21:14:51 +02:00
{
self :: addRateFromDolibarr ( $code , $rate );
}
2018-08-15 17:34:35 +02:00
}
2018-05-30 21:14:51 +02:00
}
}
else
{
setEventMessages ( $langs -> trans ( 'multicurrency_syncronize_error' , $response -> error -> info ), null , 'errors' );
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
*/
2018-08-14 12:23:10 +02:00
public static function checkCodeAlreadyExists ( $code )
{
2016-06-04 00:42:01 +02:00
global $db ;
2018-08-15 17:34:35 +02:00
2016-06-04 00:42:01 +02:00
$currency = new MultiCurrency ( $db );
if ( $currency -> fetch ( '' , $code ) > 0 ) return true ;
else return false ;
2018-08-14 12:23:10 +02: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
/**
* @ var date Date synchronisation
*/
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
2016-01-18 19:45:27 +01:00
/**
* Constructor
*
* @ param DoliDb $db Database handler
*/
public function __construct ( DoliDB $db )
{
$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
*
* @ return int < 0 if KO , Id of created object if OK
*/
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 );
if ( empty ( $this -> entity ) || $this -> entity <= 0 ) $this -> entity = $conf -> entity ;
$now = date ( 'Y-m-d H:i:s' );
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
// Insert request
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this -> table_element . '(' ;
$sql .= ' rate,' ;
$sql .= ' date_sync,' ;
$sql .= ' fk_multicurrency,' ;
$sql .= ' entity' ;
$sql .= ') VALUES (' ;
$sql .= ' ' . $this -> rate . ',' ;
$sql .= ' \'' . $now . '\',' ;
$sql .= ' \'' . $fk_multicurrency . '\',' ;
$sql .= ' \'' . $this -> entity . '\'' ;
$sql .= ')' ;
$this -> db -> begin ();
2016-06-30 14:24:55 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'CurrencyRate::create ' . join ( ',' , $this -> errors ), LOG_ERR );
}
if ( ! $error ) {
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . $this -> table_element );
$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 ) {
$result = $this -> call_trigger ( 'CURRENCYRATE_CREATE' , $user );
if ( $result < 0 ) $error ++ ;
}
}
if ( $error ) {
$this -> db -> rollback ();
return - 1 * $error ;
} else {
$this -> db -> commit ();
return $this -> id ;
}
}
/**
* Load object in memory from the database
*
* @ param int $id Id object
*
* @ return int < 0 if KO , 0 if not found , > 0 if OK
*/
public function fetch ( $id )
{
dol_syslog ( 'CurrencyRate::fetch' , LOG_DEBUG );
$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 = ' . $id ;
2016-06-30 14:24:55 +02: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 ;
$this -> date_sync = $obj -> date_sync ;
$this -> fk_multicurrency = $obj -> fk_multicurrency ;
$this -> entity = $obj -> entity ;
}
$this -> db -> free ( $resql );
if ( $numrows ) {
return 1 ;
} else {
return 0 ;
}
} else {
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'CurrencyRate::fetch ' . join ( ',' , $this -> errors ), LOG_ERR );
return - 1 ;
}
}
2018-08-15 17:34:35 +02:00
2016-01-18 19:45:27 +01:00
/**
* Update object into database
*
* @ param bool $trigger true = launch triggers after , false = disable triggers
*
* @ return int < 0 if KO , > 0 if OK
*/
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
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element . ' SET' ;
$sql .= ' rate=' . $this -> rate ;
$sql .= ' WHERE rowid=' . $this -> id ;
$this -> db -> begin ();
2016-06-30 14:24:55 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'CurrencyRate::update ' . join ( ',' , $this -> errors ), LOG_ERR );
}
if ( ! $error && $trigger ) {
$result = $this -> call_trigger ( 'CURRENCYRATE_MODIFY' , $user );
if ( $result < 0 ) $error ++ ;
}
// 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
*
* @ 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
2016-01-18 19:45:27 +01:00
dol_syslog ( 'CurrencyRate::delete' , LOG_DEBUG );
$error = 0 ;
$this -> db -> begin ();
if ( $trigger ) {
$result = $this -> call_trigger ( 'CURRENCYRATE_DELETE' , $user );
if ( $result < 0 ) $error ++ ;
}
if ( ! $error ) {
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= ' WHERE rowid=' . $this -> id ;
2016-06-30 14:24:55 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2016-01-18 19:45:27 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( 'CurrencyRate::delete ' . join ( ',' , $this -> errors ), LOG_ERR );
}
}
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
return - 1 * $error ;
} else {
$this -> db -> commit ();
return 1 ;
}
}
}