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