dolibarr  9.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  *
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 <http://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 .= ", ".$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 = ".$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=".$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  {
216  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
217  $sql.= " WHERE fk_categorie = ".$this->id;
218 
219  $resql = $this->db->query($sql);
220  if (!$resql)
221  {
222  $error++;
223  $this->errors[] = "Error ".$this->db->lasterror();
224  }
225  }
226 
227  // Delete link between tag and bank lines
228  if (! $error)
229  {
230  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class";
231  $sql.= " WHERE fk_categ = ".$this->id;
232 
233  $resql = $this->db->query($sql);
234  if (!$resql)
235  {
236  $error++;
237  $this->errors[] = "Error ".$this->db->lasterror();
238  }
239  }
240 
241  // Delete bank categ
242  if (! $error)
243  {
244  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_categ";
245  $sql .= " WHERE rowid=".$this->id;
246 
247  $resql = $this->db->query($sql);
248  if (!$resql)
249  {
250  $error++;
251  $this->errors[] = "Error ".$this->db->lasterror();
252  }
253  }
254 
255  // Commit or rollback
256  if ($error) {
257  foreach ($this->errors as $errmsg) {
258  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
259  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
260  }
261  $this->db->rollback();
262  return -1 * $error;
263  } else {
264  $this->db->commit();
265  return 1;
266  }
267  }
268 
275  public function createFromClone($fromid)
276  {
277  global $user;
278 
279  $error = 0;
280 
281  $object = new BankCateg($this->db);
282 
283  $this->db->begin();
284 
285  // Load source object
286  $object->fetch($fromid);
287  $object->id = 0;
288  $object->statut = 0;
289 
290  // Create clone
291  $object->context['createfromclone'] = 'createfromclone';
292  $result = $object->create($user);
293 
294  // Other options
295  if ($result < 0) {
296  $this->error = $object->error;
297  $error++;
298  }
299 
300  unset($object->context['createfromclone']);
301 
302  // End
303  if (!$error) {
304  $this->db->commit();
305  return $object->id;
306  } else {
307  $this->db->rollback();
308  return -1;
309  }
310  }
311 
317  public function fetchAll()
318  {
319  global $conf;
320 
321  $return = array();
322 
323  $sql = "SELECT rowid, label FROM ".MAIN_DB_PREFIX."bank_categ WHERE entity = ".$conf->entity." ORDER BY label";
324  $resql = $this->db->query($sql);
325 
326  if ($resql) {
327  while ($obj = $this->db->fetch_object($resql)) {
328  $tmp = new BankCateg($this->db);
329  $tmp->id = $obj->rowid;
330  $tmp->label = $obj->label;
331 
332  $return[] = $tmp;
333  }
334  }
335 
336  return $return;
337  }
338 
346  public function initAsSpecimen()
347  {
348  $this->id = 0;
349  $this->label = '';
350  }
351 }
print $object label
hash of file content (md5_file(dol_osencode($destfull))
Definition: edit.php:153
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
createFromClone($fromid)
Load an object from its id and create a new one in database.
fetchAll()
Returns all bank categories.
Class to manage Dolibarr users.
Definition: user.class.php:41
Class to manage Dolibarr database access.
fetch($id)
Load object in memory from database.
create(User $user, $notrigger=0)
Create in database.
__construct(DoliDB $db)
Constructor.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage bank categories.
update(User $user=null, $notrigger=0)
Update database.
initAsSpecimen()
Initialise an instance with random values.