Passed
Push — GENERAL_BUG_REVIEW_240911 ( 8cbbee...d4fdcf )
by Rafael
64:20
created

CurrencyRate::update()   B

Complexity

Conditions 8
Paths 48

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
nc 48
nop 2
dl 0
loc 45
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2007-2020  Laurent Destailleur         <[email protected]>
4
 * Copyright (C) 2014       Juanjo Menent               <[email protected]>
5
 * Copyright (C) 2015       Florian Henry               <[email protected]>
6
 * Copyright (C) 2015       Raphaël Doursenaud          <[email protected]>
7
 * Copyright (C) 2016       Pierre-Henry Favre          <[email protected]>
8
 * Copyright (C) 2024       Frédéric France             <[email protected]>
9
 * Copyright (C) 2024		MDW							<[email protected]>
10
 * Copyright (C) 2024       Rafael San José             <[email protected]>
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
26
namespace Dolibarr\Code\MultiCurrency\Classes;
27
28
use Dolibarr\Code\User\Classes\User;
29
use Dolibarr\Core\Base\CommonObjectLine;
30
use DoliDB;
31
32
/**
33
 * \file    htdocs/multicurrency/class/multicurrency.class.php
34
 * \ingroup multicurrency
35
 * \brief   This file is a CRUD class file (Create/Read/Update/Delete) for multicurrency
36
 */
37
38
/**
39
 * Class CurrencyRate
40
 */
41
class CurrencyRate extends CommonObjectLine
42
{
43
    /**
44
     * @var string Id to identify managed objects
45
     */
46
    public $element = 'multicurrency_rate';
47
48
    /**
49
     * @var string Name of table without prefix where object is stored
50
     */
51
    public $table_element = 'multicurrency_rate';
52
53
    /**
54
     * @var int ID
55
     */
56
    public $id;
57
58
    /**
59
     * @var double Rate
60
     */
61
    public $rate;
62
63
    /**
64
     * @var double Rate Indirect
65
     */
66
    public $rate_indirect;
67
68
    /**
69
     * @var integer    Date synchronisation
70
     */
71
    public $date_sync;
72
73
    /**
74
     * @var int Id of currency
75
     */
76
    public $fk_multicurrency;
77
78
    /**
79
     * @var int Id of entity
80
     */
81
    public $entity;
82
83
84
    /**
85
     * Constructor
86
     *
87
     * @param DoliDB $db Database handler
88
     */
89
    public function __construct(DoliDB $db)
90
    {
91
        $this->db = $db;
92
    }
93
94
    /**
95
     * Create object into database
96
     *
97
     * @param   User    $user               User making the deletion
98
     * @param   int     $fk_multicurrency   Id of currency
99
     * @param   int     $notrigger          0=launch triggers after, 1=disable triggers
100
     * @return  int                         Return integer <0 if KO, Id of created object if OK
101
     */
102
    public function create(User $user, int $fk_multicurrency, $notrigger = 0)
103
    {
104
        global $conf;
105
106
        dol_syslog('CurrencyRate::create', LOG_DEBUG);
107
108
        $error = 0;
109
        $this->rate = (float) price2num($this->rate);
110
        if (empty($this->entity) || $this->entity <= 0) {
111
            $this->entity = $conf->entity;
112
        }
113
        $now = empty($this->date_sync) ? dol_now() : $this->date_sync;
114
115
        // Insert request
116
        $sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . "(";
117
        $sql .= ' rate,';
118
        $sql .= ' rate_indirect,';
119
        $sql .= ' date_sync,';
120
        $sql .= ' fk_multicurrency,';
121
        $sql .= ' entity';
122
        $sql .= ') VALUES (';
123
        $sql .= ' ' . ((float) $this->rate) . ',';
124
        $sql .= ' ' . ((float) $this->rate_indirect) . ',';
125
        $sql .= " '" . $this->db->idate($now) . "',";
126
        $sql .= " " . ((int) $fk_multicurrency) . ",";
127
        $sql .= " " . ((int) $this->entity);
128
        $sql .= ')';
129
130
        $this->db->begin();
131
132
        dol_syslog(__METHOD__, LOG_DEBUG);
133
        $resql = $this->db->query($sql);
134
        if (!$resql) {
135
            $error++;
136
            $this->errors[] = 'Error ' . $this->db->lasterror();
137
            dol_syslog('CurrencyRate::create ' . implode(',', $this->errors), LOG_ERR);
138
        }
139
140
        if (!$error) {
141
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
142
            $this->fk_multicurrency = $fk_multicurrency;
143
            $this->date_sync = $now;
144
145
            if (empty($notrigger)) {
146
                $result = $this->call_trigger('CURRENCYRATE_CREATE', $user);
147
                if ($result < 0) {
148
                    $error++;
149
                }
150
            }
151
        }
152
153
        if ($error) {
154
            $this->db->rollback();
155
156
            return -1 * $error;
157
        } else {
158
            $this->db->commit();
159
160
            return $this->id;
161
        }
162
    }
163
164
    /**
165
     * Load object in memory from the database
166
     *
167
     * @param   int    $id  Id object
168
     * @return  int         Return integer <0 if KO, 0 if not found, >0 if OK
169
     */
170
    public function fetch($id)
171
    {
172
        dol_syslog('CurrencyRate::fetch', LOG_DEBUG);
173
174
        $sql = "SELECT cr.rowid, cr.rate, cr.rate_indirect, cr.date_sync, cr.fk_multicurrency, cr.entity";
175
        $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element . " AS cr";
176
        $sql .= " WHERE cr.rowid = " . ((int) $id);
177
178
        dol_syslog(__METHOD__, LOG_DEBUG);
179
        $resql = $this->db->query($sql);
180
        if ($resql) {
181
            $numrows = $this->db->num_rows($resql);
182
            if ($numrows) {
183
                $obj = $this->db->fetch_object($resql);
184
185
                $this->id = $obj->rowid;
186
                $this->rate = $obj->rate;
187
                $this->rate_indirect = $obj->rate_indirect;
188
                $this->date_sync = $this->db->jdate($obj->date_sync);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->db->jdate($obj->date_sync) can also be of type string. However, the property $date_sync is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
189
                $this->fk_multicurrency = $obj->fk_multicurrency;
190
                $this->entity = $obj->entity;
191
            }
192
            $this->db->free($resql);
193
194
            if ($numrows) {
195
                return 1;
196
            } else {
197
                return 0;
198
            }
199
        } else {
200
            $this->errors[] = 'Error ' . $this->db->lasterror();
201
            dol_syslog('CurrencyRate::fetch ' . implode(',', $this->errors), LOG_ERR);
202
203
            return -1;
204
        }
205
    }
206
207
    /**
208
     * Update object into database
209
     *
210
     * @param   User    $user       User making the deletion
211
     * @param   int     $notrigger  0=launch triggers after, 1=disable triggers
212
     * @return  int                 Return integer <0 if KO, >0 if OK
213
     */
214
    public function update(User $user, $notrigger = 0)
215
    {
216
        $error = 0;
217
218
        dol_syslog('CurrencyRate::update', LOG_DEBUG);
219
220
        $this->rate = (float) price2num($this->rate);
221
222
        // Update request
223
        $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element;
224
        $sql .= "SET rate = " . ((float) $this->rate);
225
        if (!empty($this->date_sync)) {
226
            $sql .= ", date_sync = '" . $this->db->idate($this->date_sync) . "'";
227
        }
228
        if (!empty($this->fk_multicurrency)) {
229
            $sql .= ', fk_multicurrency = ' . ((int) $this->fk_multicurrency);
230
        }
231
        $sql .= " WHERE rowid =" . ((int) $this->id);
232
233
        $this->db->begin();
234
235
        dol_syslog(__METHOD__, LOG_DEBUG);
236
        $resql = $this->db->query($sql);
237
        if (!$resql) {
238
            $error++;
239
            $this->errors[] = 'Error ' . $this->db->lasterror();
240
            dol_syslog('CurrencyRate::update ' . implode(',', $this->errors), LOG_ERR);
241
        }
242
243
        if (!$error && empty($notrigger)) {
244
            $result = $this->call_trigger('CURRENCYRATE_MODIFY', $user);
245
            if ($result < 0) {
246
                $error++;
247
            }
248
        }
249
250
        // Commit or rollback
251
        if ($error) {
252
            $this->db->rollback();
253
254
            return -1 * $error;
255
        } else {
256
            $this->db->commit();
257
258
            return 1;
259
        }
260
    }
261
262
    /**
263
     * Delete object in database
264
     *
265
     * @param   User    $user       User making the deletion
266
     * @param   int     $notrigger  0=launch triggers after, 1=disable triggers
267
     * @return  int                 Return integer <0 if KO, >0 if OK
268
     */
269
    public function delete(User $user, $notrigger = 0)
270
    {
271
        dol_syslog('CurrencyRate::delete', LOG_DEBUG);
272
273
        $error = 0;
274
275
        $this->db->begin();
276
277
        if (empty($notrigger)) {
278
            $result = $this->call_trigger('CURRENCYRATE_DELETE', $user);
279
            if ($result < 0) {
280
                $error++;
281
            }
282
        }
283
284
        if (!$error) {
285
            $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
286
            $sql .= ' WHERE rowid=' . ((int) $this->id);
287
288
            dol_syslog(__METHOD__, LOG_DEBUG);
289
            $resql = $this->db->query($sql);
290
            if (!$resql) {
291
                $error++;
292
                $this->errors[] = 'Error ' . $this->db->lasterror();
293
                dol_syslog('CurrencyRate::delete ' . implode(',', $this->errors), LOG_ERR);
294
            }
295
        }
296
297
        // Commit or rollback
298
        if ($error) {
299
            $this->db->rollback();
300
301
            return -1 * $error;
302
        } else {
303
            $this->db->commit();
304
305
            return 1;
306
        }
307
    }
308
}
309