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