dolibarr 21.0.0-alpha
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 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
31require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
32
37{
41 public $element = 'multicurrency_rate';
42
46 public $table_element = 'multicurrency_rate';
47
51 public $id;
52
56 public $rate;
57
61 public $rate_indirect;
62
66 public $date_sync;
67
71 public $fk_multicurrency;
72
76 public $entity;
77
78
84 public function __construct(DoliDB $db)
85 {
86 $this->db = $db;
87 }
88
97 public function create(User $user, int $fk_multicurrency, $notrigger = 0)
98 {
99 global $conf;
100
101 dol_syslog('CurrencyRate::create', LOG_DEBUG);
102
103 $error = 0;
104 $this->rate = (float) price2num($this->rate);
105 if (empty($this->entity) || $this->entity <= 0) {
106 $this->entity = $conf->entity;
107 }
108 $now = empty($this->date_sync) ? dol_now() : $this->date_sync;
109
110 // Insert request
111 $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element."(";
112 $sql .= ' rate,';
113 $sql .= ' rate_indirect,';
114 $sql .= ' date_sync,';
115 $sql .= ' fk_multicurrency,';
116 $sql .= ' entity';
117 $sql .= ') VALUES (';
118 $sql .= ' '.((float) $this->rate).',';
119 $sql .= ' '.((float) $this->rate_indirect).',';
120 $sql .= " '".$this->db->idate($now)."',";
121 $sql .= " ".((int) $fk_multicurrency).",";
122 $sql .= " ".((int) $this->entity);
123 $sql .= ')';
124
125 $this->db->begin();
126
127 dol_syslog(__METHOD__, LOG_DEBUG);
128 $resql = $this->db->query($sql);
129 if (!$resql) {
130 $error++;
131 $this->errors[] = 'Error '.$this->db->lasterror();
132 dol_syslog('CurrencyRate::create '.implode(',', $this->errors), LOG_ERR);
133 }
134
135 if (!$error) {
136 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
137 $this->fk_multicurrency = $fk_multicurrency;
138 $this->date_sync = $now;
139
140 if (empty($notrigger)) {
141 $result = $this->call_trigger('CURRENCYRATE_CREATE', $user);
142 if ($result < 0) {
143 $error++;
144 }
145 }
146 }
147
148 if ($error) {
149 $this->db->rollback();
150
151 return -1 * $error;
152 } else {
153 $this->db->commit();
154
155 return $this->id;
156 }
157 }
158
165 public function fetch($id)
166 {
167 dol_syslog('CurrencyRate::fetch', LOG_DEBUG);
168
169 $sql = "SELECT cr.rowid, cr.rate, cr.rate_indirect, cr.date_sync, cr.fk_multicurrency, cr.entity";
170 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." AS cr";
171 $sql .= " WHERE cr.rowid = ".((int) $id);
172
173 dol_syslog(__METHOD__, LOG_DEBUG);
174 $resql = $this->db->query($sql);
175 if ($resql) {
176 $numrows = $this->db->num_rows($resql);
177 if ($numrows) {
178 $obj = $this->db->fetch_object($resql);
179
180 $this->id = $obj->rowid;
181 $this->rate = $obj->rate;
182 $this->rate_indirect = $obj->rate_indirect;
183 $this->date_sync = $this->db->jdate($obj->date_sync);
184 $this->fk_multicurrency = $obj->fk_multicurrency;
185 $this->entity = $obj->entity;
186 }
187 $this->db->free($resql);
188
189 if ($numrows) {
190 return 1;
191 } else {
192 return 0;
193 }
194 } else {
195 $this->errors[] = 'Error '.$this->db->lasterror();
196 dol_syslog('CurrencyRate::fetch '.implode(',', $this->errors), LOG_ERR);
197
198 return -1;
199 }
200 }
201
209 public function update(User $user, $notrigger = 0)
210 {
211 $error = 0;
212
213 dol_syslog('CurrencyRate::update', LOG_DEBUG);
214
215 $this->rate = (float) price2num($this->rate);
216
217 // Update request
218 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
219 $sql .= "SET rate = ".((float) $this->rate);
220 if (!empty($this->date_sync)) {
221 $sql .= ", date_sync = '".$this->db->idate($this->date_sync)."'";
222 }
223 if (!empty($this->fk_multicurrency)) {
224 $sql .= ', fk_multicurrency = '.((int) $this->fk_multicurrency);
225 }
226 $sql .= " WHERE rowid =".((int) $this->id);
227
228 $this->db->begin();
229
230 dol_syslog(__METHOD__, LOG_DEBUG);
231 $resql = $this->db->query($sql);
232 if (!$resql) {
233 $error++;
234 $this->errors[] = 'Error '.$this->db->lasterror();
235 dol_syslog('CurrencyRate::update '.implode(',', $this->errors), LOG_ERR);
236 }
237
238 if (!$error && empty($notrigger)) {
239 $result = $this->call_trigger('CURRENCYRATE_MODIFY', $user);
240 if ($result < 0) {
241 $error++;
242 }
243 }
244
245 // Commit or rollback
246 if ($error) {
247 $this->db->rollback();
248
249 return -1 * $error;
250 } else {
251 $this->db->commit();
252
253 return 1;
254 }
255 }
256
264 public function delete(User $user, $notrigger = 0)
265 {
266 dol_syslog('CurrencyRate::delete', LOG_DEBUG);
267
268 $error = 0;
269
270 $this->db->begin();
271
272 if (empty($notrigger)) {
273 $result = $this->call_trigger('CURRENCYRATE_DELETE', $user);
274 if ($result < 0) {
275 $error++;
276 }
277 }
278
279 if (!$error) {
280 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
281 $sql .= ' WHERE rowid='.((int) $this->id);
282
283 dol_syslog(__METHOD__, LOG_DEBUG);
284 $resql = $this->db->query($sql);
285 if (!$resql) {
286 $error++;
287 $this->errors[] = 'Error '.$this->db->lasterror();
288 dol_syslog('CurrencyRate::delete '.implode(',', $this->errors), LOG_ERR);
289 }
290 }
291
292 // Commit or rollback
293 if ($error) {
294 $this->db->rollback();
295
296 return -1 * $error;
297 } else {
298 $this->db->commit();
299
300 return 1;
301 }
302 }
303}
call_trigger($triggerName, $user)
Call trigger based on this instance.
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.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_now($mode='auto')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.