dolibarr  9.0.0
cpaiement.class.php
1 <?php
2 /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
4  * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
5  * Copyright (C) 2015 RaphaĆ«l Doursenaud <rdoursenaud@gpcsolutions.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
31 class Cpaiement
32 {
36  public $element = 'cpaiement';
37 
41  public $table_element = 'c_paiement';
42 
43  public $code;
44  public $libelle;
45  public $type;
46  public $active;
47  public $accountancy_code;
48  public $module;
49 
50 
56  public function __construct(DoliDB $db)
57  {
58  $this->db = $db;
59  }
60 
69  public function create(User $user, $notrigger = false)
70  {
71  dol_syslog(__METHOD__, LOG_DEBUG);
72 
73  $error = 0;
74 
75  // Clean parameters
76 
77  if (isset($this->code)) {
78  $this->code = trim($this->code);
79  }
80  if (isset($this->libelle)) {
81  $this->libelle = trim($this->libelle);
82  }
83  if (isset($this->type)) {
84  $this->type = trim($this->type);
85  }
86  if (isset($this->active)) {
87  $this->active = trim($this->active);
88  }
89  if (isset($this->accountancy_code)) {
90  $this->accountancy_code = trim($this->accountancy_code);
91  }
92  if (isset($this->module)) {
93  $this->module = trim($this->module);
94  }
95 
96 
97 
98  // Check parameters
99  // Put here code to add control on parameters values
100 
101  // Insert request
102  $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
103 
104  $sql.= 'entity,';
105  $sql.= 'code,';
106  $sql.= 'libelle,';
107  $sql.= 'type,';
108  $sql.= 'active,';
109  $sql.= 'accountancy_code,';
110  $sql.= 'module';
111 
112 
113  $sql .= ') VALUES (';
114 
115  $sql .= ' '.(! isset($this->entity)?getEntity('c_paiement'):$this->entity).',';
116  $sql .= ' '.(! isset($this->code)?'NULL':"'".$this->db->escape($this->code)."'").',';
117  $sql .= ' '.(! isset($this->libelle)?'NULL':"'".$this->db->escape($this->libelle)."'").',';
118  $sql .= ' '.(! isset($this->type)?'NULL':$this->type).',';
119  $sql .= ' '.(! isset($this->active)?'NULL':$this->active).',';
120  $sql .= ' '.(! isset($this->accountancy_code)?'NULL':"'".$this->db->escape($this->accountancy_code)."'").',';
121  $sql .= ' '.(! isset($this->module)?'NULL':"'".$this->db->escape($this->module)."'");
122 
123 
124  $sql .= ')';
125 
126  $this->db->begin();
127 
128  $resql = $this->db->query($sql);
129  if (!$resql) {
130  $error ++;
131  $this->errors[] = 'Error ' . $this->db->lasterror();
132  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
133  }
134 
135  if (!$error) {
136  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
137 
138  // Uncomment this and change MYOBJECT to your own tag if you
139  // want this action to call a trigger.
140  //if (!$notrigger) {
141 
142  // // Call triggers
143  // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
144  // if ($result < 0) $error++;
145  // // End call triggers
146  //}
147  }
148 
149  // Commit or rollback
150  if ($error) {
151  $this->db->rollback();
152 
153  return - 1 * $error;
154  } else {
155  $this->db->commit();
156 
157  return $this->id;
158  }
159  }
160 
169  public function fetch($id, $ref = null)
170  {
171  dol_syslog(__METHOD__, LOG_DEBUG);
172 
173  $sql = 'SELECT';
174  $sql .= ' t.id,';
175  $sql .= " t.code,";
176  $sql .= " t.libelle,";
177  $sql .= " t.type,";
178  $sql .= " t.active,";
179  $sql .= " t.accountancy_code,";
180  $sql .= " t.module";
181  $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
182  if (null !== $ref) {
183  $sql .= ' WHERE t.entity IN ('.getEntity('c_paiement').')';
184  $sql .= ' AND t.code = ' . '\'' . $ref . '\'';
185  } else {
186  $sql .= ' WHERE t.id = ' . $id;
187  }
188 
189  $resql = $this->db->query($sql);
190  if ($resql) {
191  $numrows = $this->db->num_rows($resql);
192  if ($numrows) {
193  $obj = $this->db->fetch_object($resql);
194 
195  $this->id = $obj->id;
196 
197  $this->code = $obj->code;
198  $this->libelle = $obj->libelle;
199  $this->type = $obj->type;
200  $this->active = $obj->active;
201  $this->accountancy_code = $obj->accountancy_code;
202  $this->module = $obj->module;
203  }
204  $this->db->free($resql);
205 
206  if ($numrows) {
207  return 1;
208  } else {
209  return 0;
210  }
211  } else {
212  $this->errors[] = 'Error ' . $this->db->lasterror();
213  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
214 
215  return - 1;
216  }
217  }
218 
227  public function update(User $user, $notrigger = false)
228  {
229  $error = 0;
230 
231  dol_syslog(__METHOD__, LOG_DEBUG);
232 
233  // Clean parameters
234 
235  if (isset($this->code)) {
236  $this->code = trim($this->code);
237  }
238  if (isset($this->libelle)) {
239  $this->libelle = trim($this->libelle);
240  }
241  if (isset($this->type)) {
242  $this->type = trim($this->type);
243  }
244  if (isset($this->active)) {
245  $this->active = trim($this->active);
246  }
247  if (isset($this->accountancy_code)) {
248  $this->accountancy_code = trim($this->accountancy_code);
249  }
250  if (isset($this->module)) {
251  $this->module = trim($this->module);
252  }
253 
254 
255 
256  // Check parameters
257  // Put here code to add a control on parameters values
258 
259  // Update request
260  $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
261  $sql .= ' id = '.(isset($this->id)?$this->id:"null").',';
262  $sql .= ' code = '.(isset($this->code)?"'".$this->db->escape($this->code)."'":"null").',';
263  $sql .= ' libelle = '.(isset($this->libelle)?"'".$this->db->escape($this->libelle)."'":"null").',';
264  $sql .= ' type = '.(isset($this->type)?$this->type:"null").',';
265  $sql .= ' active = '.(isset($this->active)?$this->active:"null").',';
266  $sql .= ' accountancy_code = '.(isset($this->accountancy_code)?"'".$this->db->escape($this->accountancy_code)."'":"null").',';
267  $sql .= ' module = '.(isset($this->module)?"'".$this->db->escape($this->module)."'":"null");
268  $sql .= ' WHERE id=' . $this->id;
269 
270  $this->db->begin();
271 
272  $resql = $this->db->query($sql);
273  if (!$resql) {
274  $error ++;
275  $this->errors[] = 'Error ' . $this->db->lasterror();
276  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
277  }
278 
279  // Uncomment this and change MYOBJECT to your own tag if you
280  // want this action calls a trigger.
281  //if (!$error && !$notrigger) {
282 
283  // // Call triggers
284  // $result=$this->call_trigger('MYOBJECT_MODIFY',$user);
285  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
286  // // End call triggers
287  //}
288 
289  // Commit or rollback
290  if ($error) {
291  $this->db->rollback();
292 
293  return - 1 * $error;
294  } else {
295  $this->db->commit();
296 
297  return 1;
298  }
299  }
300 
309  public function delete(User $user, $notrigger = false)
310  {
311  dol_syslog(__METHOD__, LOG_DEBUG);
312 
313  $error = 0;
314 
315  $this->db->begin();
316 
317  // Uncomment this and change MYOBJECT to your own tag if you
318  // want this action calls a trigger.
319  //if (!$error && !$notrigger) {
320 
321  // // Call triggers
322  // $result=$this->call_trigger('MYOBJECT_DELETE',$user);
323  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
324  // // End call triggers
325  //}
326 
327  if (!$error) {
328  $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
329  $sql .= ' WHERE id=' . $this->id;
330 
331  $resql = $this->db->query($sql);
332  if (!$resql) {
333  $error ++;
334  $this->errors[] = 'Error ' . $this->db->lasterror();
335  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
336  }
337  }
338 
339  // Commit or rollback
340  if ($error) {
341  $this->db->rollback();
342 
343  return - 1 * $error;
344  } else {
345  $this->db->commit();
346 
347  return 1;
348  }
349  }
350 
351 
358  public function initAsSpecimen()
359  {
360  $this->id = 0;
361 
362  $this->code = '';
363  $this->libelle = '';
364  $this->type = '';
365  $this->active = '';
366  $this->accountancy_code = '';
367  $this->module = '';
368  }
369 }
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
__construct(DoliDB $db)
Constructor.
create(User $user, $notrigger=false)
Create object into database.
Class to manage Dolibarr users.
Definition: user.class.php:41
Class Cpaiement.
Class to manage Dolibarr database access.
fetch($id, $ref=null)
Load object in memory from the database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
type
Definition: viewcat.php:284
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
update(User $user, $notrigger=false)
Update object into database.