dolibarr 20.0.0
bankcateg.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2008 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2009 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
30class BankCateg // extends CommonObject
31{
32 //public $element='bank_categ'; //!< Id that identify managed objects
33 //public $table_element='bank_categ'; //!< Name of table without prefix where object is stored
37 public $picto = 'generic';
38
42 public $id;
43
47 public $label;
48
52 protected $db;
53
57 public $error;
58
62 public $errors;
63
67 public $context;
68
69
75 public function __construct(DoliDB $db)
76 {
77 $this->db = $db;
78 }
79
80
88 public function create(User $user, $notrigger = 0)
89 {
90 global $conf;
91
92 $error = 0;
93
94 // Clean parameters
95 if (isset($this->label)) {
96 $this->label = trim($this->label);
97 }
98
99 // Insert request
100 $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_categ (";
101 $sql .= "label";
102 $sql .= ", entity";
103 $sql .= ") VALUES (";
104 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'");
105 $sql .= ", ".((int) $conf->entity);
106 $sql .= ")";
107
108 $this->db->begin();
109
110 dol_syslog(get_class($this)."::create", LOG_DEBUG);
111 $resql = $this->db->query($sql);
112 if (!$resql) {
113 $error++;
114 $this->errors[] = "Error ".$this->db->lasterror();
115 }
116
117 if (!$error) {
118 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_categ");
119 }
120
121 // Commit or rollback
122 if ($error) {
123 foreach ($this->errors as $errmsg) {
124 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
125 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
126 }
127 $this->db->rollback();
128 return -1 * $error;
129 } else {
130 $this->db->commit();
131 return $this->id;
132 }
133 }
134
135
142 public function fetch($id)
143 {
144 global $conf;
145
146 $sql = "SELECT";
147 $sql .= " t.rowid,";
148 $sql .= " t.label";
149 $sql .= " FROM ".MAIN_DB_PREFIX."bank_categ as t";
150 $sql .= " WHERE t.rowid = ".((int) $id);
151 $sql .= " AND t.entity = ".$conf->entity;
152
153 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
154 $resql = $this->db->query($sql);
155 if ($resql) {
156 if ($this->db->num_rows($resql)) {
157 $obj = $this->db->fetch_object($resql);
158
159 $this->id = $obj->rowid;
160 $this->label = $obj->label;
161 }
162 $this->db->free($resql);
163
164 return 1;
165 } else {
166 $this->error = "Error ".$this->db->lasterror();
167 return -1;
168 }
169 }
170
178 public function update(User $user = null, $notrigger = 0)
179 {
180 global $conf;
181 $error = 0;
182
183 // Clean parameters
184 if (isset($this->label)) {
185 $this->label = trim($this->label);
186 }
187
188 // Check parameters
189 // Put here code to add control on parameters values
190
191 // Update request
192 $sql = "UPDATE ".MAIN_DB_PREFIX."bank_categ SET";
193 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
194 $sql .= " WHERE rowid=".((int) $this->id);
195 $sql .= " AND entity = ".$conf->entity;
196
197 $this->db->begin();
198
199 dol_syslog(get_class($this)."::update", LOG_DEBUG);
200 $resql = $this->db->query($sql);
201 if (!$resql) {
202 $error++;
203 $this->errors[] = "Error ".$this->db->lasterror();
204 }
205
206 // Commit or rollback
207 if ($error) {
208 foreach ($this->errors as $errmsg) {
209 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
210 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
211 }
212 $this->db->rollback();
213 return -1 * $error;
214 } else {
215 $this->db->commit();
216 return 1;
217 }
218 }
219
227 public function delete(User $user, $notrigger = 0)
228 {
229 global $conf;
230 $error = 0;
231
232 $this->db->begin();
233
234 // Delete link between tag and bank account
235 if (!$error) {
236 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
237 $sql .= " WHERE fk_categorie = ".((int) $this->id);
238
239 $resql = $this->db->query($sql);
240 if (!$resql) {
241 $error++;
242 $this->errors[] = "Error ".$this->db->lasterror();
243 }
244 }
245
246 // Delete link between tag and bank lines
247 if (!$error) {
248 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class";
249 $sql .= " WHERE fk_categ = ".((int) $this->id);
250
251 $resql = $this->db->query($sql);
252 if (!$resql) {
253 $error++;
254 $this->errors[] = "Error ".$this->db->lasterror();
255 }
256 }
257
258 // Delete bank categ
259 if (!$error) {
260 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_categ";
261 $sql .= " WHERE rowid=".((int) $this->id);
262
263 $resql = $this->db->query($sql);
264 if (!$resql) {
265 $error++;
266 $this->errors[] = "Error ".$this->db->lasterror();
267 }
268 }
269
270 // Commit or rollback
271 if ($error) {
272 foreach ($this->errors as $errmsg) {
273 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
274 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
275 }
276 $this->db->rollback();
277 return -1 * $error;
278 } else {
279 $this->db->commit();
280 return 1;
281 }
282 }
283
291 public function createFromClone(User $user, $fromid)
292 {
293 $error = 0;
294
295 $object = new BankCateg($this->db);
296
297 $this->db->begin();
298
299 // Load source object
300 $object->fetch($fromid);
301 $object->id = 0;
302 // $object->statut = 0;
303
304 // Create clone
305 $object->context['createfromclone'] = 'createfromclone';
306 $result = $object->create($user);
307
308 // Other options
309 if ($result < 0) {
310 $this->error = $object->error;
311 $error++;
312 }
313
314 unset($object->context['createfromclone']);
315
316 // End
317 if (!$error) {
318 $this->db->commit();
319 return $object->id;
320 } else {
321 $this->db->rollback();
322 return -1;
323 }
324 }
325
331 public function fetchAll()
332 {
333 global $conf;
334
335 $return = array();
336
337 $sql = "SELECT rowid, label FROM ".MAIN_DB_PREFIX."bank_categ WHERE entity = ".$conf->entity." ORDER BY label";
338 $resql = $this->db->query($sql);
339
340 if ($resql) {
341 while ($obj = $this->db->fetch_object($resql)) {
342 $tmp = new BankCateg($this->db);
343 $tmp->id = $obj->rowid;
344 $tmp->label = $obj->label;
345
346 $return[] = $tmp;
347 }
348 }
349
350 return $return;
351 }
352
360 public function initAsSpecimen()
361 {
362 $this->id = 0;
363 $this->label = '';
364
365 return 1;
366 }
367}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage bank categories.
create(User $user, $notrigger=0)
Create in database.
update(User $user=null, $notrigger=0)
Update database.
fetch($id)
Load object in memory from database.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
__construct(DoliDB $db)
Constructor.
fetchAll()
Returns all bank categories.
initAsSpecimen()
Initialise an instance with random values.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.