dolibarr 21.0.0-beta
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 include_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
94 $cats = new Categorie($this->db);
95 $catTypeID = $cats->getMapId()[Categorie::TYPE_BANK_LINE];
96
97 $error = 0;
98
99 // Clean parameters
100 if (isset($this->label)) {
101 $this->label = trim($this->label);
102 }
103
104 // Insert request
105 $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie (";
106 $sql .= "label";
107 $sql .= ", entity";
108 $sql .= ", type";
109 $sql .= ") VALUES (";
110 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'");
111 $sql .= ", ".((int) $conf->entity);
112 $sql .= ", ".((int) $catTypeID);
113 $sql .= ")";
114
115 $this->db->begin();
116
117 dol_syslog(get_class($this)."::create", LOG_DEBUG);
118 $resql = $this->db->query($sql);
119 if (!$resql) {
120 $error++;
121 $this->errors[] = "Error ".$this->db->lasterror();
122 }
123
124 if (!$error) {
125 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."categorie");
126 }
127
128 // Commit or rollback
129 if ($error) {
130 foreach ($this->errors as $errmsg) {
131 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
132 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
133 }
134 $this->db->rollback();
135 return -1 * $error;
136 } else {
137 $this->db->commit();
138 return $this->id;
139 }
140 }
141
142
149 public function fetch($id)
150 {
151 global $conf;
152
153 include_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
154 $cats = new Categorie($this->db);
155 $catTypeID = $cats->getMapId()[Categorie::TYPE_BANK_LINE];
156
157 $sql = "SELECT";
158 $sql .= " t.rowid,";
159 $sql .= " t.label";
160 $sql .= " FROM ".MAIN_DB_PREFIX."categorie as t";
161 $sql .= " WHERE t.rowid = ".((int) $id);
162 $sql .= " AND t.entity = ".$conf->entity." AND t.type = " . ((int) $catTypeID);
163
164 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
165 $resql = $this->db->query($sql);
166 if ($resql) {
167 if ($this->db->num_rows($resql)) {
168 $obj = $this->db->fetch_object($resql);
169
170 $this->id = $obj->rowid;
171 $this->label = $obj->label;
172 }
173 $this->db->free($resql);
174
175 return 1;
176 } else {
177 $this->error = "Error ".$this->db->lasterror();
178 return -1;
179 }
180 }
181
189 public function update($user = null, $notrigger = 0)
190 {
191 global $conf;
192 $error = 0;
193
194 // Clean parameters
195 if (isset($this->label)) {
196 $this->label = trim($this->label);
197 }
198
199 // Check parameters
200 // Put here code to add control on parameters values
201
202 // Update request
203 $sql = "UPDATE ".MAIN_DB_PREFIX."categorie SET";
204 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
205 $sql .= " WHERE rowid=".((int) $this->id);
206 $sql .= " AND entity = ".$conf->entity;
207
208 $this->db->begin();
209
210 dol_syslog(get_class($this)."::update", LOG_DEBUG);
211 $resql = $this->db->query($sql);
212 if (!$resql) {
213 $error++;
214 $this->errors[] = "Error ".$this->db->lasterror();
215 }
216
217 // Commit or rollback
218 if ($error) {
219 foreach ($this->errors as $errmsg) {
220 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
221 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
222 }
223 $this->db->rollback();
224 return -1 * $error;
225 } else {
226 $this->db->commit();
227 return 1;
228 }
229 }
230
238 public function delete(User $user, $notrigger = 0)
239 {
240 global $conf;
241 $error = 0;
242
243 $this->db->begin();
244
245 // Delete link between tag and bank account
246 if (!$error) {
247 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
248 $sql .= " WHERE fk_categorie = ".((int) $this->id);
249
250 $resql = $this->db->query($sql);
251 if (!$resql) {
252 $error++;
253 $this->errors[] = "Error ".$this->db->lasterror();
254 }
255 }
256
257 // Delete link between tag and bank lines
258 if (!$error) {
259 $sql = "DELETE FROM ".MAIN_DB_PREFIX."category_bankline";
260 $sql .= " WHERE fk_categ = ".((int) $this->id);
261
262 $resql = $this->db->query($sql);
263 if (!$resql) {
264 $error++;
265 $this->errors[] = "Error ".$this->db->lasterror();
266 }
267 }
268
269 // Delete bank categ
270 if (!$error) {
271 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie";
272 $sql .= " WHERE rowid=".((int) $this->id);
273
274 $resql = $this->db->query($sql);
275 if (!$resql) {
276 $error++;
277 $this->errors[] = "Error ".$this->db->lasterror();
278 }
279 }
280
281 // Commit or rollback
282 if ($error) {
283 foreach ($this->errors as $errmsg) {
284 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
285 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
286 }
287 $this->db->rollback();
288 return -1 * $error;
289 } else {
290 $this->db->commit();
291 return 1;
292 }
293 }
294
302 public function createFromClone(User $user, $fromid)
303 {
304 $error = 0;
305
306 $object = new BankCateg($this->db);
307
308 $this->db->begin();
309
310 // Load source object
311 $object->fetch($fromid);
312 $object->id = 0;
313 // $object->statut = 0;
314
315 // Create clone
316 $object->context['createfromclone'] = 'createfromclone';
317 $result = $object->create($user);
318
319 // Other options
320 if ($result < 0) {
321 $this->error = $object->error;
322 $error++;
323 }
324
325 unset($object->context['createfromclone']);
326
327 // End
328 if (!$error) {
329 $this->db->commit();
330 return $object->id;
331 } else {
332 $this->db->rollback();
333 return -1;
334 }
335 }
336
342 public function fetchAll()
343 {
344 global $conf;
345
346 include_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
347 $cats = new Categorie($this->db);
348 $catTypeID = $cats->getMapId()[Categorie::TYPE_BANK_LINE];
349
350 $return = array();
351
352 $sql = "SELECT rowid, label FROM ".MAIN_DB_PREFIX."categorie WHERE entity = ".$conf->entity." AND type = ".((int) $catTypeID)." ORDER BY label";
353 $resql = $this->db->query($sql);
354
355 if ($resql) {
356 while ($obj = $this->db->fetch_object($resql)) {
357 $tmp = new BankCateg($this->db);
358 $tmp->id = $obj->rowid;
359 $tmp->label = $obj->label;
360
361 $return[] = $tmp;
362 }
363 }
364
365 return $return;
366 }
367
375 public function initAsSpecimen()
376 {
377 $this->id = 0;
378 $this->label = '';
379
380 return 1;
381 }
382}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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 categories.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79