dolibarr  19.0.0-dev
price_global_variable.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 Ion Agorria <ion@agorria.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 
31 {
35  public $db;
36 
40  public $error = '';
41 
45  public $errors = array();
46 
50  public $id;
51 
52  public $code;
53 
57  public $description;
58 
59  public $value;
60 
64  public $table_element = "c_price_global_variable";
65 
71  public function __construct($db)
72  {
73  $this->db = $db;
74  }
75 
76 
84  public function create($user, $notrigger = 0)
85  {
86  $error = 0;
87 
88  $this->checkParameters();
89 
90  // Insert request
91  $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
92  $sql .= "code, description, value";
93  $sql .= ") VALUES (";
94  $sql .= " ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").",";
95  $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
96  $sql .= " ".((float) $this->value);
97  $sql .= ")";
98 
99  $this->db->begin();
100 
101  dol_syslog(__METHOD__);
102  $resql = $this->db->query($sql);
103  if (!$resql) {
104  $error++; $this->errors[] = "Error ".$this->db->lasterror();
105  }
106 
107  if (!$error) {
108  $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
109 
110  if (!$notrigger) {
111  // Uncomment this and change MYOBJECT to your own tag if you
112  // want this action calls a trigger.
113 
115  //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
116  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
118  }
119  }
120 
121  // Commit or rollback
122  if ($error) {
123  foreach ($this->errors as $errmsg) {
124  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
125  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
126  }
127  $this->db->rollback();
128  return -1 * $error;
129  } else {
130  $this->db->commit();
131  return $this->id;
132  }
133  }
134 
135 
142  public function fetch($id)
143  {
144  $sql = "SELECT code, description, value";
145  $sql .= " FROM ".$this->db->prefix().$this->table_element;
146  $sql .= " WHERE rowid = ".((int) $id);
147 
148  dol_syslog(__METHOD__);
149  $resql = $this->db->query($sql);
150  if ($resql) {
151  $obj = $this->db->fetch_object($resql);
152  if ($obj) {
153  $this->id = $id;
154  $this->code = $obj->code;
155  $this->description = $obj->description;
156  $this->value = $obj->value;
157  $this->checkParameters();
158  return 1;
159  } else {
160  return 0;
161  }
162  } else {
163  $this->error = "Error ".$this->db->lasterror();
164  return -1;
165  }
166  }
167 
175  public function update($user = 0, $notrigger = 0)
176  {
177  $error = 0;
178 
179  $this->checkParameters();
180 
181  // Update request
182  $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
183  $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").",";
184  $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
185  $sql .= " value = ".((float) $this->value);
186  $sql .= " WHERE rowid = ".((int) $this->id);
187 
188  $this->db->begin();
189 
190  dol_syslog(__METHOD__);
191  $resql = $this->db->query($sql);
192  if (!$resql) {
193  $error++; $this->errors[] = "Error ".$this->db->lasterror();
194  }
195 
196  // if (! $error)
197  // {
198  // if (! $notrigger)
199  // {
200  // // Uncomment this and change MYOBJECT to your own tag if you
201  // // want this action calls a trigger.
202 
203  // //// Call triggers
204  // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
205  // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
206  // //// End call triggers
207  // }
208  // }
209 
210  // Commit or rollback
211  if ($error) {
212  foreach ($this->errors as $errmsg) {
213  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
214  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
215  }
216  $this->db->rollback();
217  return -1 * $error;
218  } else {
219  $this->db->commit();
220  return 1;
221  }
222  }
223 
224 
233  public function delete($rowid, $user, $notrigger = 0)
234  {
235  $error = 0;
236 
237  $this->db->begin();
238 
239  if (!$error) {
240  if (!$notrigger) {
241  // Uncomment this and change MYOBJECT to your own tag if you
242  // want this action calls a trigger.
243 
245  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
246  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
248  }
249  }
250 
251  if (!$error) {
252  $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
253  $sql .= " WHERE rowid = ".((int) $rowid);
254 
255  dol_syslog(__METHOD__);
256  $resql = $this->db->query($sql);
257  if (!$resql) {
258  $error++; $this->errors[] = "Error ".$this->db->lasterror();
259  }
260  }
261 
262  // Commit or rollback
263  if ($error) {
264  foreach ($this->errors as $errmsg) {
265  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
266  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
267  }
268  $this->db->rollback();
269  return -1 * $error;
270  } else {
271  $this->db->commit();
272  return 1;
273  }
274  }
275 
282  public function initAsSpecimen()
283  {
284  $this->id = 0;
285  $this->code = '';
286  $this->description = '';
287  $this->value = '';
288  }
289 
295  public function checkParameters()
296  {
297  // Clean parameters
298  if (isset($this->code)) {
299  $this->code = trim($this->code);
300  }
301  if (isset($this->description)) {
302  $this->description = trim($this->description);
303  }
304 
305  // Check parameters
306  if (empty($this->value) || !is_numeric($this->value)) {
307  $this->value = 0;
308  }
309  }
310 
316  public function listGlobalVariables()
317  {
318  $sql = "SELECT rowid, code, description, value";
319  $sql .= " FROM ".$this->db->prefix().$this->table_element;
320  $sql .= " ORDER BY code";
321 
322  dol_syslog(__METHOD__, LOG_DEBUG);
323  $resql = $this->db->query($sql);
324  if ($resql) {
325  $retarray = array();
326 
327  while ($record = $this->db->fetch_array($resql)) {
328  $variable_obj = new PriceGlobalVariable($this->db);
329  $variable_obj->id = $record["rowid"];
330  $variable_obj->code = $record["code"];
331  $variable_obj->description = $record["description"];
332  $variable_obj->value = $record["value"];
333  $variable_obj->checkParameters();
334  $retarray[] = $variable_obj;
335  }
336 
337  $this->db->free($resql);
338  return $retarray;
339  } else {
340  $this->error = $this->db->error();
341  return -1;
342  }
343  }
344 }
Class for accesing price global variables table.
listGlobalVariables()
List all price global variables.
checkParameters()
Checks if all parameters are in order.
create($user, $notrigger=0)
Create object into database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
update($user=0, $notrigger=0)
Update object into database.
fetch($id)
Load object in memory from the database.
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
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...