dolibarr  7.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@capnetworks.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
33  public $picto='generic';
34 
35  public $id;
36  public $label;
37 
38 
44  public function __construct(DoliDB $db)
45  {
46  $this->db = $db;
47  }
48 
49 
57  public function create(User $user, $notrigger = 0)
58  {
59  global $conf;
60 
61  $error = 0;
62 
63  // Clean parameters
64  if (isset($this->label)) {
65  $this->label = trim($this->label);
66  }
67 
68  // Insert request
69  $sql = "INSERT INTO ".MAIN_DB_PREFIX."bank_categ (";
70  $sql .= "label";
71  $sql .= ", entity";
72  $sql .= ") VALUES (";
73  $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'")."";
74  $sql .= ", ".$conf->entity;
75  $sql .= ")";
76 
77  $this->db->begin();
78 
79  dol_syslog(get_class($this)."::create", LOG_DEBUG);
80  $resql = $this->db->query($sql);
81  if (!$resql) {
82  $error++;
83  $this->errors[] = "Error ".$this->db->lasterror();
84  }
85 
86  if (!$error) {
87  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."bank_categ");
88  }
89 
90  // Commit or rollback
91  if ($error) {
92  foreach ($this->errors as $errmsg) {
93  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
94  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
95  }
96  $this->db->rollback();
97  return -1 * $error;
98  } else {
99  $this->db->commit();
100  return $this->id;
101  }
102  }
103 
104 
111  public function fetch($id)
112  {
113  global $conf;
114 
115  $sql = "SELECT";
116  $sql .= " t.rowid,";
117  $sql .= " t.label";
118  $sql .= " FROM ".MAIN_DB_PREFIX."bank_categ as t";
119  $sql .= " WHERE t.rowid = ".$id;
120  $sql .= " AND t.entity = ".$conf->entity;
121 
122  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
123  $resql = $this->db->query($sql);
124  if ($resql) {
125  if ($this->db->num_rows($resql)) {
126  $obj = $this->db->fetch_object($resql);
127 
128  $this->id = $obj->rowid;
129  $this->label = $obj->label;
130  }
131  $this->db->free($resql);
132 
133  return 1;
134  } else {
135  $this->error = "Error ".$this->db->lasterror();
136  return -1;
137  }
138  }
139 
147  public function update(User $user = null, $notrigger = 0)
148  {
149  global $conf;
150  $error = 0;
151 
152  // Clean parameters
153  if (isset($this->label)) {
154  $this->label = trim($this->label);
155  }
156 
157  // Check parameters
158  // Put here code to add control on parameters values
159 
160  // Update request
161  $sql = "UPDATE ".MAIN_DB_PREFIX."bank_categ SET";
162  $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null")."";
163  $sql .= " WHERE rowid=".$this->id;
164  $sql .= " AND entity = ".$conf->entity;
165 
166  $this->db->begin();
167 
168  dol_syslog(get_class($this)."::update", LOG_DEBUG);
169  $resql = $this->db->query($sql);
170  if (!$resql) {
171  $error++;
172  $this->errors[] = "Error ".$this->db->lasterror();
173  }
174 
175  // Commit or rollback
176  if ($error) {
177  foreach ($this->errors as $errmsg) {
178  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
179  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
180  }
181  $this->db->rollback();
182  return -1 * $error;
183  } else {
184  $this->db->commit();
185  return 1;
186  }
187  }
188 
196  public function delete(User $user, $notrigger = 0)
197  {
198  global $conf;
199  $error = 0;
200 
201  $this->db->begin();
202 
203  // Delete link between tag and bank account
204  if (! $error)
205  {
206  $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_account";
207  $sql.= " WHERE fk_categorie = ".$this->id;
208 
209  $resql = $this->db->query($sql);
210  if (!$resql)
211  {
212  $error++;
213  $this->errors[] = "Error ".$this->db->lasterror();
214  }
215  }
216 
217  // Delete link between tag and bank lines
218  if (! $error)
219  {
220  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class";
221  $sql.= " WHERE fk_categ = ".$this->id;
222 
223  $resql = $this->db->query($sql);
224  if (!$resql)
225  {
226  $error++;
227  $this->errors[] = "Error ".$this->db->lasterror();
228  }
229  }
230 
231  // Delete bank categ
232  if (! $error)
233  {
234  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_categ";
235  $sql .= " WHERE rowid=".$this->id;
236 
237  $resql = $this->db->query($sql);
238  if (!$resql)
239  {
240  $error++;
241  $this->errors[] = "Error ".$this->db->lasterror();
242  }
243  }
244 
245  // Commit or rollback
246  if ($error) {
247  foreach ($this->errors as $errmsg) {
248  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
249  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
250  }
251  $this->db->rollback();
252  return -1 * $error;
253  } else {
254  $this->db->commit();
255  return 1;
256  }
257  }
258 
265  public function createFromClone($fromid)
266  {
267  global $user;
268 
269  $error = 0;
270 
271  $object = new BankCateg($this->db);
272 
273  $object->context['createfromclone'] = 'createfromclone';
274 
275  $this->db->begin();
276 
277  // Load source object
278  $object->fetch($fromid);
279  $object->id = 0;
280  $object->statut = 0;
281 
282  // Create clone
283  $result = $object->create($user);
284 
285  // Other options
286  if ($result < 0) {
287  $this->error = $object->error;
288  $error++;
289  }
290 
291  unset($object->context['createfromclone']);
292 
293  // End
294  if (!$error) {
295  $this->db->commit();
296  return $object->id;
297  } else {
298  $this->db->rollback();
299  return -1;
300  }
301  }
302 
308  public function fetchAll()
309  {
310  global $conf;
311 
312  $return = array();
313 
314  $sql = "SELECT rowid, label FROM ".MAIN_DB_PREFIX."bank_categ WHERE entity = ".$conf->entity." ORDER BY label";
315  $resql = $this->db->query($sql);
316 
317  if ($resql) {
318  while ($obj = $this->db->fetch_object($resql)) {
319  $tmp = new BankCateg($this->db);
320  $tmp->id = $obj->rowid;
321  $tmp->label = $obj->label;
322 
323  $return[] = $tmp;
324  }
325  }
326 
327  return $return;
328  }
329 
337  public function initAsSpecimen()
338  {
339  $this->id = 0;
340  $this->label = '';
341  }
342 
343 }
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:39
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.
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:1013