dolibarr  16.0.5
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 
29 class 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 
48 
54  public function __construct(DoliDB $db)
55  {
56  $this->db = $db;
57  }
58 
59 
67  public function create(User $user, $notrigger = 0)
68  {
69  global $conf;
70 
71  $error = 0;
72 
73  // Clean parameters
74  if (isset($this->label)) {
75  $this->label = trim($this->label);
76  }
77 
78  // Insert request
79  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_categ (";
80  $sql .= "label";
81  $sql .= ", entity";
82  $sql .= ") VALUES (";
83  $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'")."";
84  $sql .= ", ".((int) $conf->entity);
85  $sql .= ")";
86 
87  $this->db->begin();
88 
89  dol_syslog(get_class($this)."::create", LOG_DEBUG);
90  $resql = $this->db->query($sql);
91  if (!$resql) {
92  $error++;
93  $this->errors[] = "Error ".$this->db->lasterror();
94  }
95 
96  if (!$error) {
97  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_categ");
98  }
99 
100  // Commit or rollback
101  if ($error) {
102  foreach ($this->errors as $errmsg) {
103  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
104  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
105  }
106  $this->db->rollback();
107  return -1 * $error;
108  } else {
109  $this->db->commit();
110  return $this->id;
111  }
112  }
113 
114 
121  public function fetch($id)
122  {
123  global $conf;
124 
125  $sql = "SELECT";
126  $sql .= " t.rowid,";
127  $sql .= " t.label";
128  $sql .= " FROM ".MAIN_DB_PREFIX."bank_categ as t";
129  $sql .= " WHERE t.rowid = ".((int) $id);
130  $sql .= " AND t.entity = ".$conf->entity;
131 
132  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
133  $resql = $this->db->query($sql);
134  if ($resql) {
135  if ($this->db->num_rows($resql)) {
136  $obj = $this->db->fetch_object($resql);
137 
138  $this->id = $obj->rowid;
139  $this->label = $obj->label;
140  }
141  $this->db->free($resql);
142 
143  return 1;
144  } else {
145  $this->error = "Error ".$this->db->lasterror();
146  return -1;
147  }
148  }
149 
157  public function update(User $user = null, $notrigger = 0)
158  {
159  global $conf;
160  $error = 0;
161 
162  // Clean parameters
163  if (isset($this->label)) {
164  $this->label = trim($this->label);
165  }
166 
167  // Check parameters
168  // Put here code to add control on parameters values
169 
170  // Update request
171  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_categ SET";
172  $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null")."";
173  $sql .= " WHERE rowid=".((int) $this->id);
174  $sql .= " AND entity = ".$conf->entity;
175 
176  $this->db->begin();
177 
178  dol_syslog(get_class($this)."::update", LOG_DEBUG);
179  $resql = $this->db->query($sql);
180  if (!$resql) {
181  $error++;
182  $this->errors[] = "Error ".$this->db->lasterror();
183  }
184 
185  // Commit or rollback
186  if ($error) {
187  foreach ($this->errors as $errmsg) {
188  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
189  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
190  }
191  $this->db->rollback();
192  return -1 * $error;
193  } else {
194  $this->db->commit();
195  return 1;
196  }
197  }
198 
206  public function delete(User $user, $notrigger = 0)
207  {
208  global $conf;
209  $error = 0;
210 
211  $this->db->begin();
212 
213  // Delete link between tag and bank account
214  if (!$error) {
215  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
216  $sql .= " WHERE fk_categorie = ".((int) $this->id);
217 
218  $resql = $this->db->query($sql);
219  if (!$resql) {
220  $error++;
221  $this->errors[] = "Error ".$this->db->lasterror();
222  }
223  }
224 
225  // Delete link between tag and bank lines
226  if (!$error) {
227  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class";
228  $sql .= " WHERE fk_categ = ".((int) $this->id);
229 
230  $resql = $this->db->query($sql);
231  if (!$resql) {
232  $error++;
233  $this->errors[] = "Error ".$this->db->lasterror();
234  }
235  }
236 
237  // Delete bank categ
238  if (!$error) {
239  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_categ";
240  $sql .= " WHERE rowid=".((int) $this->id);
241 
242  $resql = $this->db->query($sql);
243  if (!$resql) {
244  $error++;
245  $this->errors[] = "Error ".$this->db->lasterror();
246  }
247  }
248 
249  // Commit or rollback
250  if ($error) {
251  foreach ($this->errors as $errmsg) {
252  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
253  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
254  }
255  $this->db->rollback();
256  return -1 * $error;
257  } else {
258  $this->db->commit();
259  return 1;
260  }
261  }
262 
270  public function createFromClone(User $user, $fromid)
271  {
272  $error = 0;
273 
274  $object = new BankCateg($this->db);
275 
276  $this->db->begin();
277 
278  // Load source object
279  $object->fetch($fromid);
280  $object->id = 0;
281  $object->statut = 0;
282 
283  // Create clone
284  $object->context['createfromclone'] = 'createfromclone';
285  $result = $object->create($user);
286 
287  // Other options
288  if ($result < 0) {
289  $this->error = $object->error;
290  $error++;
291  }
292 
293  unset($object->context['createfromclone']);
294 
295  // End
296  if (!$error) {
297  $this->db->commit();
298  return $object->id;
299  } else {
300  $this->db->rollback();
301  return -1;
302  }
303  }
304 
310  public function fetchAll()
311  {
312  global $conf;
313 
314  $return = array();
315 
316  $sql = "SELECT rowid, label FROM ".MAIN_DB_PREFIX."bank_categ WHERE entity = ".$conf->entity." ORDER BY label";
317  $resql = $this->db->query($sql);
318 
319  if ($resql) {
320  while ($obj = $this->db->fetch_object($resql)) {
321  $tmp = new BankCateg($this->db);
322  $tmp->id = $obj->rowid;
323  $tmp->label = $obj->label;
324 
325  $return[] = $tmp;
326  }
327  }
328 
329  return $return;
330  }
331 
339  public function initAsSpecimen()
340  {
341  $this->id = 0;
342  $this->label = '';
343  }
344 }
db
$conf db
API class for accounts.
Definition: inc.php:41
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
BankCateg\createFromClone
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
Definition: bankcateg.class.php:270
BankCateg\fetchAll
fetchAll()
Returns all bank categories.
Definition: bankcateg.class.php:310
BankCateg\fetch
fetch($id)
Load object in memory from database.
Definition: bankcateg.class.php:121
BankCateg\create
create(User $user, $notrigger=0)
Create in database.
Definition: bankcateg.class.php:67
BankCateg
Class to manage bank categories.
Definition: bankcateg.class.php:29
BankCateg\__construct
__construct(DoliDB $db)
Constructor.
Definition: bankcateg.class.php:54
BankCateg\update
update(User $user=null, $notrigger=0)
Update database.
Definition: bankcateg.class.php:157
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
BankCateg\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: bankcateg.class.php:339
User
Class to manage Dolibarr users.
Definition: user.class.php:44
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742