dolibarr 24.0.0-beta
currencyrate.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2020 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
5 * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
6 * Copyright (C) 2016 Pierre-Henry Favre <phf@atm-consulting.fr>
7 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
8 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
9 * Copyright (C) 2024 William Mead <william.mead@manchenumerique.fr>
10 * Copyright (C) 2026 Lenin Rivas <lenin.rivas777@gmail.com>
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
32require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
33
38{
42 public $element = 'multicurrency_rate';
43
47 public $table_element = 'multicurrency_rate';
48
52 public $id;
53
57 public $rate;
58
62 public $rate_direct;
63
67 public $date_sync;
68
72 public $fk_multicurrency;
73
77 public $entity;
78
79
85 public function __construct(DoliDB $db)
86 {
87 $this->db = $db;
88 }
89
98 public function create(User $user, int $fk_multicurrency, $notrigger = 0)
99 {
100 global $conf;
101
102 dol_syslog('CurrencyRate::create', LOG_DEBUG);
103
104 $error = 0;
105 $this->rate = (float) price2num($this->rate);
106 if (empty($this->entity) || $this->entity <= 0) {
107 $this->entity = $conf->entity;
108 }
109 $now = empty($this->date_sync) ? dol_now() : $this->date_sync;
110
111 // Insert request
112 $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element."(";
113 $sql .= ' rate,';
114 $sql .= ' rate_direct,';
115 $sql .= ' date_sync,';
116 $sql .= ' fk_multicurrency,';
117 $sql .= ' entity';
118 $sql .= ') VALUES (';
119 $sql .= ' '.((float) $this->rate).',';
120 $sql .= ' '.((float) $this->rate_direct).',';
121 $sql .= " '".$this->db->idate($now)."',";
122 $sql .= " ".((int) $fk_multicurrency).",";
123 $sql .= " ".((int) $this->entity);
124 $sql .= ')';
125
126 $this->db->begin();
127
128 dol_syslog(__METHOD__, LOG_DEBUG);
129 $resql = $this->db->query($sql);
130 if (!$resql) {
131 $error++;
132 $this->errors[] = 'Error '.$this->db->lasterror();
133 dol_syslog('CurrencyRate::create '.implode(',', $this->errors), LOG_ERR);
134 }
135
136 if (!$error) {
137 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
138 $this->fk_multicurrency = $fk_multicurrency;
139 $this->date_sync = $now;
140
141 if (empty($notrigger)) {
142 $result = $this->call_trigger('CURRENCYRATE_CREATE', $user);
143 if ($result < 0) {
144 $error++;
145 }
146 }
147 }
148
149 if ($error) {
150 $this->db->rollback();
151
152 return -1 * $error;
153 } else {
154 $this->db->commit();
155
156 return $this->id;
157 }
158 }
159
166 public function fetch($id)
167 {
168 dol_syslog('CurrencyRate::fetch', LOG_DEBUG);
169
170 $sql = "SELECT cr.rowid, cr.rate, cr.rate_direct, cr.date_sync, cr.fk_multicurrency, cr.entity";
171 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." AS cr";
172 $sql .= " WHERE cr.rowid = ".((int) $id);
173
174 dol_syslog(__METHOD__, LOG_DEBUG);
175 $resql = $this->db->query($sql);
176 if ($resql) {
177 $numrows = $this->db->num_rows($resql);
178 if ($numrows) {
179 $obj = $this->db->fetch_object($resql);
180
181 $this->id = $obj->rowid;
182 $this->rate = $obj->rate;
183 $this->rate_direct = $obj->rate_direct;
184 $this->date_sync = $this->db->jdate($obj->date_sync);
185 $this->fk_multicurrency = $obj->fk_multicurrency;
186 $this->entity = $obj->entity;
187 }
188 $this->db->free($resql);
189
190 if ($numrows) {
191 return 1;
192 } else {
193 return 0;
194 }
195 } else {
196 $this->errors[] = 'Error '.$this->db->lasterror();
197 dol_syslog('CurrencyRate::fetch '.implode(',', $this->errors), LOG_ERR);
198
199 return -1;
200 }
201 }
202
210 public function update(User $user, $notrigger = 0)
211 {
212 $error = 0;
213
214 dol_syslog('CurrencyRate::update', LOG_DEBUG);
215
216 $this->rate = (float) price2num($this->rate);
217
218 // Update request
219 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
220 $sql .= "SET rate = ".((float) $this->rate);
221 if (!empty($this->rate_direct)) {
222 $sql .= ", rate_direct = ".((float) $this->rate_direct);
223 }
224 if (!empty($this->date_sync)) {
225 $sql .= ", date_sync = '".$this->db->idate($this->date_sync)."'";
226 }
227 if (!empty($this->fk_multicurrency)) {
228 $sql .= ', fk_multicurrency = '.((int) $this->fk_multicurrency);
229 }
230 $sql .= " WHERE rowid =".((int) $this->id);
231
232 $this->db->begin();
233
234 dol_syslog(__METHOD__, LOG_DEBUG);
235 $resql = $this->db->query($sql);
236 if (!$resql) {
237 $error++;
238 $this->errors[] = 'Error '.$this->db->lasterror();
239 dol_syslog('CurrencyRate::update '.implode(',', $this->errors), LOG_ERR);
240 }
241
242 if (!$error && empty($notrigger)) {
243 $result = $this->call_trigger('CURRENCYRATE_MODIFY', $user);
244 if ($result < 0) {
245 $error++;
246 }
247 }
248
249 // Commit or rollback
250 if ($error) {
251 $this->db->rollback();
252
253 return -1 * $error;
254 } else {
255 $this->db->commit();
256
257 return 1;
258 }
259 }
260
268 public function delete(User $user, $notrigger = 0)
269 {
270 dol_syslog('CurrencyRate::delete', LOG_DEBUG);
271
272 $error = 0;
273
274 $this->db->begin();
275
276 if (empty($notrigger)) {
277 $result = $this->call_trigger('CURRENCYRATE_DELETE', $user);
278 if ($result < 0) {
279 $error++;
280 }
281 }
282
283 if (!$error) {
284 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
285 $sql .= ' WHERE rowid='.((int) $this->id);
286
287 dol_syslog(__METHOD__, LOG_DEBUG);
288 $resql = $this->db->query($sql);
289 if (!$resql) {
290 $error++;
291 $this->errors[] = 'Error '.$this->db->lasterror();
292 dol_syslog('CurrencyRate::delete '.implode(',', $this->errors), LOG_ERR);
293 }
294 }
295
296 // Commit or rollback
297 if ($error) {
298 $this->db->rollback();
299
300 return -1 * $error;
301 } else {
302 $this->db->commit();
303
304 return 1;
305 }
306 }
307}
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Class CurrencyRate.
fetch($id)
Load object in memory from the database.
update(User $user, $notrigger=0)
Update object into database.
create(User $user, int $fk_multicurrency, $notrigger=0)
Create object into database.
__construct(DoliDB $db)
Constructor.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.