1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2008 Laurent Destailleur <[email protected]> |
4
|
|
|
* Copyright (C) 2009 Regis Houssin <[email protected]> |
5
|
|
|
* Copyright (C) 2016 Marcos García <[email protected]> |
6
|
|
|
* Copyright (C) 2024 Frédéric France <[email protected]> |
7
|
|
|
* Copyright (C) 2024 Rafael San José <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU General Public License as published by |
11
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
12
|
|
|
* (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Dolibarr\Code\Compta\Classes; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* \file compta/bank/class/bankcateg.class.php |
27
|
|
|
* \ingroup bank |
28
|
|
|
* \brief This file is CRUD class file (Create/Read/Update/Delete) for bank categories |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class to manage bank categories |
33
|
|
|
*/ |
34
|
|
|
class BankCateg // extends CommonObject |
35
|
|
|
{ |
36
|
|
|
//public $element='bank_categ'; //!< Id that identify managed objects |
37
|
|
|
//public $table_element='bank_categ'; //!< Name of table without prefix where object is stored |
38
|
|
|
/** |
39
|
|
|
* @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
40
|
|
|
*/ |
41
|
|
|
public $picto = 'generic'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var int ID |
45
|
|
|
*/ |
46
|
|
|
public $id; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string bank categories label |
50
|
|
|
*/ |
51
|
|
|
public $label; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var DoliDB |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
protected $db; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string error |
60
|
|
|
*/ |
61
|
|
|
public $error; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array errors |
65
|
|
|
*/ |
66
|
|
|
public $errors; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var array context |
70
|
|
|
*/ |
71
|
|
|
public $context; |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Constructor |
76
|
|
|
* |
77
|
|
|
* @param DoliDB $db Database handler |
78
|
|
|
*/ |
79
|
|
|
public function __construct(DoliDB $db) |
80
|
|
|
{ |
81
|
|
|
$this->db = $db; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Create in database |
87
|
|
|
* |
88
|
|
|
* @param User $user User that create |
89
|
|
|
* @param int $notrigger 0=launch triggers after, 1=disable triggers |
90
|
|
|
* @return int Return integer <0 if KO, Id of created object if OK |
91
|
|
|
*/ |
92
|
|
|
public function create(User $user, $notrigger = 0) |
93
|
|
|
{ |
94
|
|
|
global $conf; |
95
|
|
|
|
96
|
|
|
$error = 0; |
97
|
|
|
|
98
|
|
|
// Clean parameters |
99
|
|
|
if (isset($this->label)) { |
100
|
|
|
$this->label = trim($this->label); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Insert request |
104
|
|
|
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "bank_categ ("; |
105
|
|
|
$sql .= "label"; |
106
|
|
|
$sql .= ", entity"; |
107
|
|
|
$sql .= ") VALUES ("; |
108
|
|
|
$sql .= " " . (!isset($this->label) ? 'NULL' : "'" . $this->db->escape($this->label) . "'"); |
109
|
|
|
$sql .= ", " . ((int) $conf->entity); |
110
|
|
|
$sql .= ")"; |
111
|
|
|
|
112
|
|
|
$this->db->begin(); |
113
|
|
|
|
114
|
|
|
dol_syslog(get_class($this) . "::create", LOG_DEBUG); |
115
|
|
|
$resql = $this->db->query($sql); |
116
|
|
|
if (!$resql) { |
117
|
|
|
$error++; |
118
|
|
|
$this->errors[] = "Error " . $this->db->lasterror(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!$error) { |
122
|
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "bank_categ"); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Commit or rollback |
126
|
|
|
if ($error) { |
127
|
|
|
foreach ($this->errors as $errmsg) { |
128
|
|
|
dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR); |
129
|
|
|
$this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
130
|
|
|
} |
131
|
|
|
$this->db->rollback(); |
132
|
|
|
return -1 * $error; |
133
|
|
|
} else { |
134
|
|
|
$this->db->commit(); |
135
|
|
|
return $this->id; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Load object in memory from database |
142
|
|
|
* |
143
|
|
|
* @param int $id Id object |
144
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
145
|
|
|
*/ |
146
|
|
|
public function fetch($id) |
147
|
|
|
{ |
148
|
|
|
global $conf; |
149
|
|
|
|
150
|
|
|
$sql = "SELECT"; |
151
|
|
|
$sql .= " t.rowid,"; |
152
|
|
|
$sql .= " t.label"; |
153
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . "bank_categ as t"; |
154
|
|
|
$sql .= " WHERE t.rowid = " . ((int) $id); |
155
|
|
|
$sql .= " AND t.entity = " . $conf->entity; |
156
|
|
|
|
157
|
|
|
dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
158
|
|
|
$resql = $this->db->query($sql); |
159
|
|
|
if ($resql) { |
160
|
|
|
if ($this->db->num_rows($resql)) { |
161
|
|
|
$obj = $this->db->fetch_object($resql); |
162
|
|
|
|
163
|
|
|
$this->id = $obj->rowid; |
164
|
|
|
$this->label = $obj->label; |
165
|
|
|
} |
166
|
|
|
$this->db->free($resql); |
167
|
|
|
|
168
|
|
|
return 1; |
169
|
|
|
} else { |
170
|
|
|
$this->error = "Error " . $this->db->lasterror(); |
171
|
|
|
return -1; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Update database |
177
|
|
|
* |
178
|
|
|
* @param User|null $user User that modify |
179
|
|
|
* @param int $notrigger 0=launch triggers after, 1=disable triggers |
180
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
181
|
|
|
*/ |
182
|
|
|
public function update(User $user = null, $notrigger = 0) |
183
|
|
|
{ |
184
|
|
|
global $conf; |
185
|
|
|
$error = 0; |
186
|
|
|
|
187
|
|
|
// Clean parameters |
188
|
|
|
if (isset($this->label)) { |
189
|
|
|
$this->label = trim($this->label); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// Check parameters |
193
|
|
|
// Put here code to add control on parameters values |
194
|
|
|
|
195
|
|
|
// Update request |
196
|
|
|
$sql = "UPDATE " . MAIN_DB_PREFIX . "bank_categ SET"; |
197
|
|
|
$sql .= " label=" . (isset($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null"); |
198
|
|
|
$sql .= " WHERE rowid=" . ((int) $this->id); |
199
|
|
|
$sql .= " AND entity = " . $conf->entity; |
200
|
|
|
|
201
|
|
|
$this->db->begin(); |
202
|
|
|
|
203
|
|
|
dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
204
|
|
|
$resql = $this->db->query($sql); |
205
|
|
|
if (!$resql) { |
206
|
|
|
$error++; |
207
|
|
|
$this->errors[] = "Error " . $this->db->lasterror(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// Commit or rollback |
211
|
|
|
if ($error) { |
212
|
|
|
foreach ($this->errors as $errmsg) { |
213
|
|
|
dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR); |
214
|
|
|
$this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
215
|
|
|
} |
216
|
|
|
$this->db->rollback(); |
217
|
|
|
return -1 * $error; |
218
|
|
|
} else { |
219
|
|
|
$this->db->commit(); |
220
|
|
|
return 1; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Delete object in database |
226
|
|
|
* |
227
|
|
|
* @param User $user User that delete |
228
|
|
|
* @param int $notrigger 0=launch triggers after, 1=disable triggers |
229
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
230
|
|
|
*/ |
231
|
|
|
public function delete(User $user, $notrigger = 0) |
232
|
|
|
{ |
233
|
|
|
global $conf; |
234
|
|
|
$error = 0; |
235
|
|
|
|
236
|
|
|
$this->db->begin(); |
237
|
|
|
|
238
|
|
|
// Delete link between tag and bank account |
239
|
|
|
if (!$error) { |
240
|
|
|
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "categorie_account"; |
241
|
|
|
$sql .= " WHERE fk_categorie = " . ((int) $this->id); |
242
|
|
|
|
243
|
|
|
$resql = $this->db->query($sql); |
244
|
|
|
if (!$resql) { |
245
|
|
|
$error++; |
246
|
|
|
$this->errors[] = "Error " . $this->db->lasterror(); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// Delete link between tag and bank lines |
251
|
|
|
if (!$error) { |
252
|
|
|
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "bank_class"; |
253
|
|
|
$sql .= " WHERE fk_categ = " . ((int) $this->id); |
254
|
|
|
|
255
|
|
|
$resql = $this->db->query($sql); |
256
|
|
|
if (!$resql) { |
257
|
|
|
$error++; |
258
|
|
|
$this->errors[] = "Error " . $this->db->lasterror(); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
// Delete bank categ |
263
|
|
|
if (!$error) { |
264
|
|
|
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "bank_categ"; |
265
|
|
|
$sql .= " WHERE rowid=" . ((int) $this->id); |
266
|
|
|
|
267
|
|
|
$resql = $this->db->query($sql); |
268
|
|
|
if (!$resql) { |
269
|
|
|
$error++; |
270
|
|
|
$this->errors[] = "Error " . $this->db->lasterror(); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
// Commit or rollback |
275
|
|
|
if ($error) { |
276
|
|
|
foreach ($this->errors as $errmsg) { |
277
|
|
|
dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR); |
278
|
|
|
$this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
279
|
|
|
} |
280
|
|
|
$this->db->rollback(); |
281
|
|
|
return -1 * $error; |
282
|
|
|
} else { |
283
|
|
|
$this->db->commit(); |
284
|
|
|
return 1; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Load an object from its id and create a new one in database |
290
|
|
|
* |
291
|
|
|
* @param User $user User making the clone |
292
|
|
|
* @param int $fromid Id of object to clone |
293
|
|
|
* @return int New id of clone |
294
|
|
|
*/ |
295
|
|
|
public function createFromClone(User $user, $fromid) |
296
|
|
|
{ |
297
|
|
|
$error = 0; |
298
|
|
|
|
299
|
|
|
$object = new BankCateg($this->db); |
300
|
|
|
|
301
|
|
|
$this->db->begin(); |
302
|
|
|
|
303
|
|
|
// Load source object |
304
|
|
|
$object->fetch($fromid); |
305
|
|
|
$object->id = 0; |
306
|
|
|
// $object->statut = 0; |
307
|
|
|
|
308
|
|
|
// Create clone |
309
|
|
|
$object->context['createfromclone'] = 'createfromclone'; |
310
|
|
|
$result = $object->create($user); |
311
|
|
|
|
312
|
|
|
// Other options |
313
|
|
|
if ($result < 0) { |
314
|
|
|
$this->error = $object->error; |
315
|
|
|
$error++; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
unset($object->context['createfromclone']); |
319
|
|
|
|
320
|
|
|
// End |
321
|
|
|
if (!$error) { |
322
|
|
|
$this->db->commit(); |
323
|
|
|
return $object->id; |
324
|
|
|
} else { |
325
|
|
|
$this->db->rollback(); |
326
|
|
|
return -1; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Returns all bank categories |
332
|
|
|
* |
333
|
|
|
* @return BankCateg[] |
334
|
|
|
*/ |
335
|
|
|
public function fetchAll() |
336
|
|
|
{ |
337
|
|
|
global $conf; |
338
|
|
|
|
339
|
|
|
$return = array(); |
340
|
|
|
|
341
|
|
|
$sql = "SELECT rowid, label FROM " . MAIN_DB_PREFIX . "bank_categ WHERE entity = " . $conf->entity . " ORDER BY label"; |
342
|
|
|
$resql = $this->db->query($sql); |
343
|
|
|
|
344
|
|
|
if ($resql) { |
345
|
|
|
while ($obj = $this->db->fetch_object($resql)) { |
346
|
|
|
$tmp = new BankCateg($this->db); |
347
|
|
|
$tmp->id = $obj->rowid; |
348
|
|
|
$tmp->label = $obj->label; |
349
|
|
|
|
350
|
|
|
$return[] = $tmp; |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
return $return; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Initialise an instance with random values. |
359
|
|
|
* Used to build previews or test instances. |
360
|
|
|
* id must be 0 if object instance is a specimen. |
361
|
|
|
* |
362
|
|
|
* @return int |
363
|
|
|
*/ |
364
|
|
|
public function initAsSpecimen() |
365
|
|
|
{ |
366
|
|
|
$this->id = 0; |
367
|
|
|
$this->label = ''; |
368
|
|
|
|
369
|
|
|
return 1; |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|