dolibarr  13.0.2
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 ".MAIN_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 .= " ".$this->value;
97  $sql .= ")";
98 
99  $this->db->begin();
100 
101  dol_syslog(__METHOD__);
102  $resql = $this->db->query($sql);
103  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
104 
105  if (!$error)
106  {
107  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
108 
109  if (!$notrigger)
110  {
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  {
124  foreach ($this->errors as $errmsg)
125  {
126  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
127  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
128  }
129  $this->db->rollback();
130  return -1 * $error;
131  } else {
132  $this->db->commit();
133  return $this->id;
134  }
135  }
136 
137 
144  public function fetch($id)
145  {
146  $sql = "SELECT code, description, value";
147  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
148  $sql .= " WHERE rowid = ".$id;
149 
150  dol_syslog(__METHOD__);
151  $resql = $this->db->query($sql);
152  if ($resql)
153  {
154  $obj = $this->db->fetch_object($resql);
155  if ($obj)
156  {
157  $this->id = $id;
158  $this->code = $obj->code;
159  $this->description = $obj->description;
160  $this->value = $obj->value;
161  $this->checkParameters();
162  return 1;
163  } else {
164  return 0;
165  }
166  } else {
167  $this->error = "Error ".$this->db->lasterror();
168  return -1;
169  }
170  }
171 
179  public function update($user = 0, $notrigger = 0)
180  {
181  $error = 0;
182 
183  $this->checkParameters();
184 
185  // Update request
186  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
187  $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").",";
188  $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
189  $sql .= " value = ".$this->value;
190  $sql .= " WHERE rowid = ".$this->id;
191 
192  $this->db->begin();
193 
194  dol_syslog(__METHOD__);
195  $resql = $this->db->query($sql);
196  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
197 
198  // if (! $error)
199  // {
200  // if (! $notrigger)
201  // {
202  // // Uncomment this and change MYOBJECT to your own tag if you
203  // // want this action calls a trigger.
204 
205  // //// Call triggers
206  // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
207  // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
208  // //// End call triggers
209  // }
210  // }
211 
212  // Commit or rollback
213  if ($error)
214  {
215  foreach ($this->errors as $errmsg)
216  {
217  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
218  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
219  }
220  $this->db->rollback();
221  return -1 * $error;
222  } else {
223  $this->db->commit();
224  return 1;
225  }
226  }
227 
228 
237  public function delete($rowid, $user, $notrigger = 0)
238  {
239  $error = 0;
240 
241  $this->db->begin();
242 
243  if (!$error)
244  {
245  if (!$notrigger)
246  {
247  // Uncomment this and change MYOBJECT to your own tag if you
248  // want this action calls a trigger.
249 
251  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
252  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
254  }
255  }
256 
257  if (!$error)
258  {
259  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
260  $sql .= " WHERE rowid = ".$rowid;
261 
262  dol_syslog(__METHOD__);
263  $resql = $this->db->query($sql);
264  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
265  }
266 
267  // Commit or rollback
268  if ($error)
269  {
270  foreach ($this->errors as $errmsg)
271  {
272  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
273  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
274  }
275  $this->db->rollback();
276  return -1 * $error;
277  } else {
278  $this->db->commit();
279  return 1;
280  }
281  }
282 
289  public function initAsSpecimen()
290  {
291  $this->id = 0;
292  $this->code = '';
293  $this->description = '';
294  $this->value = '';
295  }
296 
302  public function checkParameters()
303  {
304  // Clean parameters
305  if (isset($this->code)) $this->code = trim($this->code);
306  if (isset($this->description)) $this->description = trim($this->description);
307 
308  // Check parameters
309  if (empty($this->value) || !is_numeric($this->value)) $this->value = 0;
310  }
311 
317  public function listGlobalVariables()
318  {
319  $sql = "SELECT rowid, code, description, value";
320  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
321  $sql .= " ORDER BY code";
322 
323  dol_syslog(__METHOD__, LOG_DEBUG);
324  $resql = $this->db->query($sql);
325  if ($resql)
326  {
327  $retarray = array();
328 
329  while ($record = $this->db->fetch_array($resql))
330  {
331  $variable_obj = new PriceGlobalVariable($this->db);
332  $variable_obj->id = $record["rowid"];
333  $variable_obj->code = $record["code"];
334  $variable_obj->description = $record["description"];
335  $variable_obj->value = $record["value"];
336  $variable_obj->checkParameters();
337  $retarray[] = $variable_obj;
338  }
339 
340  $this->db->free($resql);
341  return $retarray;
342  } else {
343  $this->error = $this->db->error();
344  return -1;
345  }
346  }
347 }
create($user, $notrigger=0)
Create object into database.
update($user=0, $notrigger=0)
Update object into database.
if(!empty($arrayfields['country.code_iso']['checked'])) print_liste_field_titre($arrayfields['country.code_iso']['label'] country if(!empty($arrayfields['typent.code']['checked'])) print_liste_field_titre($arrayfields['typent.code']['label'] typent code
Definition: list.php:566
listGlobalVariables()
List all price global variables.
</td > param sortfield sortorder printFieldListOption< tdclass="liste_titremaxwidthsearchright"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration center DesiredStock p desiredstock right StockLimitShort p seuil_stock_alerte right stock_physique right stock_real_warehouse right Ordered right StockToBuy right SupplierRef right param sortfield sortorder printFieldListTitle warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow StockTooLow help help help< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"> stock</td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:750
fetch($id)
Load object in memory from the database.
$conf db
API class for accounts.
Definition: inc.php:54
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
checkParameters()
Checks if all parameters are in order.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
Class for accesing price global variables table.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.