dolibarr  19.0.0-dev
cpaiement.class.php
Go to the documentation of this file.
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  * Copyright (C) 2023 Frédéric France <frederic.france@netlogic.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
32 class Cpaiement
33 {
37  public $db;
38 
42  public $element = 'cpaiement';
43 
47  public $table_element = 'c_paiement';
48 
49  public $code;
50 
55  public $libelle;
56  public $label;
57 
58  public $type;
59  public $active;
60  public $accountancy_code;
61  public $module;
62 
63 
69  public function __construct(DoliDB $db)
70  {
71  $this->db = $db;
72  }
73 
82  public function create(User $user, $notrigger = false)
83  {
84  dol_syslog(__METHOD__, LOG_DEBUG);
85 
86  $error = 0;
87 
88  // Clean parameters
89 
90  if (isset($this->code)) {
91  $this->code = trim($this->code);
92  }
93  if (isset($this->libelle)) {
94  $this->libelle = trim($this->libelle);
95  }
96  if (isset($this->label)) {
97  $this->label = trim($this->label);
98  }
99  if (isset($this->type)) {
100  $this->type = trim($this->type);
101  }
102  if (isset($this->active)) {
103  $this->active = trim($this->active);
104  }
105  if (isset($this->accountancy_code)) {
106  $this->accountancy_code = trim($this->accountancy_code);
107  }
108  if (isset($this->module)) {
109  $this->module = trim($this->module);
110  }
111 
112 
113 
114  // Check parameters
115  // Put here code to add control on parameters values
116 
117  // Insert request
118  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
119  $sql .= 'entity,';
120  $sql .= 'code,';
121  $sql .= 'libelle,';
122  $sql .= 'type,';
123  $sql .= 'active,';
124  $sql .= 'accountancy_code,';
125  $sql .= 'module';
126  $sql .= ') VALUES (';
127  $sql .= ' '.(!isset($this->entity) ?getEntity('c_paiement') : $this->entity).',';
128  $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
129  $sql .= ' '.(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").',';
130  $sql .= ' '.(!isset($this->type) ? 'NULL' : $this->type).',';
131  $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active).',';
132  $sql .= ' '.(!isset($this->accountancy_code) ? 'NULL' : "'".$this->db->escape($this->accountancy_code)."'").',';
133  $sql .= ' '.(!isset($this->module) ? 'NULL' : "'".$this->db->escape($this->module)."'");
134  $sql .= ')';
135 
136  $this->db->begin();
137 
138  $resql = $this->db->query($sql);
139  if (!$resql) {
140  $error++;
141  $this->errors[] = 'Error '.$this->db->lasterror();
142  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
143  }
144 
145  if (!$error) {
146  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
147 
148  // Uncomment this and change MYOBJECT to your own tag if you
149  // want this action to call a trigger.
150  //if (!$notrigger) {
151 
152  // // Call triggers
153  // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
154  // if ($result < 0) $error++;
155  // // End call triggers
156  //}
157  }
158 
159  // Commit or rollback
160  if ($error) {
161  $this->db->rollback();
162 
163  return -1 * $error;
164  } else {
165  $this->db->commit();
166 
167  return $this->id;
168  }
169  }
170 
179  public function fetch($id, $ref = null)
180  {
181  dol_syslog(__METHOD__, LOG_DEBUG);
182 
183  $sql = 'SELECT';
184  $sql .= ' t.id,';
185  $sql .= " t.code,";
186  $sql .= " t.libelle as label,";
187  $sql .= " t.type,";
188  $sql .= " t.active,";
189  $sql .= " t.accountancy_code,";
190  $sql .= " t.module";
191  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
192  if (null !== $ref) {
193  $sql .= ' WHERE t.entity IN ('.getEntity('c_paiement').')';
194  $sql .= " AND t.code = '".$this->db->escape($ref)."'";
195  } else {
196  $sql .= ' WHERE t.id = '.((int) $id);
197  }
198 
199  $resql = $this->db->query($sql);
200  if ($resql) {
201  $numrows = $this->db->num_rows($resql);
202  if ($numrows) {
203  $obj = $this->db->fetch_object($resql);
204 
205  $this->id = $obj->id;
206 
207  $this->code = $obj->code;
208  $this->libelle = $obj->label;
209  $this->label = $obj->label;
210  $this->type = $obj->type;
211  $this->active = $obj->active;
212  $this->accountancy_code = $obj->accountancy_code;
213  $this->module = $obj->module;
214  }
215  $this->db->free($resql);
216 
217  if ($numrows) {
218  return 1;
219  } else {
220  return 0;
221  }
222  } else {
223  $this->errors[] = 'Error '.$this->db->lasterror();
224  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
225 
226  return -1;
227  }
228  }
229 
238  public function update(User $user, $notrigger = false)
239  {
240  $error = 0;
241 
242  dol_syslog(__METHOD__, LOG_DEBUG);
243 
244  // Clean parameters
245 
246  if (isset($this->code)) {
247  $this->code = trim($this->code);
248  }
249  if (isset($this->libelle)) {
250  $this->libelle = trim($this->libelle);
251  }
252  if (isset($this->label)) {
253  $this->label = trim($this->label);
254  }
255  if (isset($this->type)) {
256  $this->type = trim($this->type);
257  }
258  if (isset($this->active)) {
259  $this->active = trim($this->active);
260  }
261  if (isset($this->accountancy_code)) {
262  $this->accountancy_code = trim($this->accountancy_code);
263  }
264  if (isset($this->module)) {
265  $this->module = trim($this->module);
266  }
267 
268 
269 
270  // Check parameters
271  // Put here code to add a control on parameters values
272 
273  // Update request
274  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
275  $sql .= ' id = '.(isset($this->id) ? $this->id : "null").',';
276  $sql .= ' code = '.(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
277  $sql .= ' libelle = '.(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").',';
278  $sql .= ' type = '.(isset($this->type) ? $this->type : "null").',';
279  $sql .= ' active = '.(isset($this->active) ? $this->active : "null").',';
280  $sql .= ' accountancy_code = '.(isset($this->accountancy_code) ? "'".$this->db->escape($this->accountancy_code)."'" : "null").',';
281  $sql .= ' module = '.(isset($this->module) ? "'".$this->db->escape($this->module)."'" : "null");
282  $sql .= ' WHERE id = '.((int) $this->id);
283 
284  $this->db->begin();
285 
286  $resql = $this->db->query($sql);
287  if (!$resql) {
288  $error++;
289  $this->errors[] = 'Error '.$this->db->lasterror();
290  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
291  }
292 
293  // Uncomment this and change MYOBJECT to your own tag if you
294  // want this action calls a trigger.
295  //if (!$error && !$notrigger) {
296 
297  // // Call triggers
298  // $result=$this->call_trigger('MYOBJECT_MODIFY',$user);
299  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
300  // // End call triggers
301  //}
302 
303  // Commit or rollback
304  if ($error) {
305  $this->db->rollback();
306 
307  return -1 * $error;
308  } else {
309  $this->db->commit();
310 
311  return 1;
312  }
313  }
314 
323  public function delete(User $user, $notrigger = false)
324  {
325  dol_syslog(__METHOD__, LOG_DEBUG);
326 
327  $error = 0;
328 
329  $this->db->begin();
330 
331  // Uncomment this and change MYOBJECT to your own tag if you
332  // want this action calls a trigger.
333  //if (!$error && !$notrigger) {
334 
335  // // Call triggers
336  // $result=$this->call_trigger('MYOBJECT_DELETE',$user);
337  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
338  // // End call triggers
339  //}
340 
341  if (!$error) {
342  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
343  $sql .= ' WHERE id = '.((int) $this->id);
344 
345  $resql = $this->db->query($sql);
346  if (!$resql) {
347  $error++;
348  $this->errors[] = 'Error '.$this->db->lasterror();
349  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
350  }
351  }
352 
353  // Commit or rollback
354  if ($error) {
355  $this->db->rollback();
356 
357  return -1 * $error;
358  } else {
359  $this->db->commit();
360 
361  return 1;
362  }
363  }
364 
365 
372  public function initAsSpecimen()
373  {
374  $this->id = 0;
375 
376  $this->code = '';
377  $this->libelle = '';
378  $this->label = '';
379  $this->type = '';
380  $this->active = '';
381  $this->accountancy_code = '';
382  $this->module = '';
383  }
384 }
Cpaiement\update
update(User $user, $notrigger=false)
Update object into database.
Definition: cpaiement.class.php:238
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
Cpaiement
Class Cpaiement.
Definition: cpaiement.class.php:32
getEntity
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Definition: functions.lib.php:266
code
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...
Definition: sync_members_ldap2dolibarr.php:60
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1741
Cpaiement\create
create(User $user, $notrigger=false)
Create object into database.
Definition: cpaiement.class.php:82
Cpaiement\__construct
__construct(DoliDB $db)
Constructor.
Definition: cpaiement.class.php:69
Cpaiement\$libelle
$libelle
Definition: cpaiement.class.php:55
$sql
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
User
Class to manage Dolibarr users.
Definition: user.class.php:47
Cpaiement\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: cpaiement.class.php:372
Cpaiement\fetch
fetch($id, $ref=null)
Load object in memory from the database.
Definition: cpaiement.class.php:179
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:120