dolibarr  13.0.2
price_expression.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 $title;
53  public $expression;
54 
58  public $table_element = "c_price_expression";
59 
65  public function __construct($db)
66  {
67  $this->db = $db;
68  }
69 
70 
78  public function create($user, $notrigger = 0)
79  {
80  $error = 0;
81 
82  // Clean parameters
83  if (isset($this->title)) $this->title = trim($this->title);
84  if (isset($this->expression)) $this->expression = trim($this->expression);
85 
86  // Insert request
87  $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
88  $sql .= "title, expression";
89  $sql .= ") VALUES (";
90  $sql .= " ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").",";
91  $sql .= " ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''");
92  $sql .= ")";
93 
94  $this->db->begin();
95 
96  dol_syslog(__METHOD__, LOG_DEBUG);
97  $resql = $this->db->query($sql);
98  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
99 
100  if (!$error)
101  {
102  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
103 
104  //if (! $notrigger)
105  //{
106  // Uncomment this and change MYOBJECT to your own tag if you
107  // want this action calls a trigger.
108 
110  //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
111  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
113  //}
114  }
115 
116  // Commit or rollback
117  if ($error)
118  {
119  foreach ($this->errors as $errmsg)
120  {
121  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
122  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
123  }
124  $this->db->rollback();
125  return -1 * $error;
126  } else {
127  $this->db->commit();
128  return $this->id;
129  }
130  }
131 
132 
139  public function fetch($id)
140  {
141  // Check parameters
142  if (empty($id))
143  {
144  $this->error = 'ErrorWrongParameters';
145  return -1;
146  }
147 
148  $sql = "SELECT title, expression";
149  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
150  $sql .= " WHERE rowid = ".$id;
151 
152  dol_syslog(__METHOD__);
153  $resql = $this->db->query($sql);
154  if ($resql)
155  {
156  $obj = $this->db->fetch_object($resql);
157  if ($obj)
158  {
159  $this->id = $id;
160  $this->title = $obj->title;
161  $this->expression = $obj->expression;
162  return 1;
163  } else {
164  return 0;
165  }
166  } else {
167  $this->error = "Error ".$this->db->lasterror();
168  return -1;
169  }
170  }
171 
172  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
178  public function list_price_expression()
179  {
180  // phpcs:enable
181  $sql = "SELECT rowid, title, expression";
182  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
183  $sql .= " ORDER BY title";
184 
185  dol_syslog(__METHOD__, LOG_DEBUG);
186  $resql = $this->db->query($sql);
187  if ($resql)
188  {
189  $retarray = array();
190 
191  while ($record = $this->db->fetch_array($resql))
192  {
193  $price_expression_obj = new PriceExpression($this->db);
194  $price_expression_obj->id = $record["rowid"];
195  $price_expression_obj->title = $record["title"];
196  $price_expression_obj->expression = $record["expression"];
197  $retarray[] = $price_expression_obj;
198  }
199 
200  $this->db->free($resql);
201  return $retarray;
202  } else {
203  $this->error = $this->db->error();
204  return -1;
205  }
206  }
207 
208 
209  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
216  public function find_title($title)
217  {
218  // phpcs:enable
219  $sql = "SELECT rowid";
220  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
221  $sql .= " WHERE title = '".$this->db->escape($title)."'";
222 
223  dol_syslog(__METHOD__, LOG_DEBUG);
224  $resql = $this->db->query($sql);
225  if ($resql)
226  {
227  $obj = $this->db->fetch_object($resql);
228  if ($obj)
229  {
230  return (int) $obj->rowid;
231  } else {
232  return 0;
233  }
234  } else {
235  $this->error = "Error ".$this->db->lasterror();
236  return -1;
237  }
238  }
239 
240 
248  public function update($user = 0, $notrigger = 0)
249  {
250  $error = 0;
251 
252  // Clean parameters
253  if (isset($this->title)) $this->title = trim($this->title);
254  if (isset($this->expression)) $this->expression = trim($this->expression);
255 
256  // Update request
257  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
258  $sql .= " title = ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").",";
259  $sql .= " expression = ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''")."";
260  $sql .= " WHERE rowid = ".$this->id;
261 
262  $this->db->begin();
263 
264  dol_syslog(__METHOD__);
265  $resql = $this->db->query($sql);
266  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
267 
268  // if (! $error)
269  // {
270  // if (! $notrigger)
271  // {
272  // // Uncomment this and change MYOBJECT to your own tag if you
273  // // want this action calls a trigger.
274 
275  // //// Call triggers
276  // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
277  // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
278  // //// End call triggers
279  // }
280  // }
281 
282  // Commit or rollback
283  if ($error)
284  {
285  foreach ($this->errors as $errmsg)
286  {
287  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
288  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
289  }
290  $this->db->rollback();
291  return -1 * $error;
292  } else {
293  $this->db->commit();
294  return 1;
295  }
296  }
297 
298 
306  public function delete(User $user, $notrigger = 0)
307  {
308  $error = 0;
309 
310  $rowid = $this->id;
311 
312  $this->db->begin();
313 
314  //if (! $error)
315  //{
316  // if (! $notrigger)
317  // {
318  // Uncomment this and change MYOBJECT to your own tag if you
319  // want this action calls a trigger.
320 
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}
325  // }
326  //}
327 
328  if (!$error)
329  {
330  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
331  $sql .= " WHERE rowid = ".$rowid;
332 
333  dol_syslog(__METHOD__);
334  $resql = $this->db->query($sql);
335  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
336  }
337 
338  // Commit or rollback
339  if ($error)
340  {
341  foreach ($this->errors as $errmsg)
342  {
343  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
344  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
345  }
346  $this->db->rollback();
347  return -1 * $error;
348  } else {
349  $this->db->commit();
350  return 1;
351  }
352  }
353 
360  public function initAsSpecimen()
361  {
362  $this->id = 0;
363  $this->expression = '';
364  }
365 }
list_price_expression()
List all price expressions.
Class to manage Dolibarr users.
Definition: user.class.php:44
fetch($id)
Load object in memory from the database.
$conf db
API class for accounts.
Definition: inc.php:54
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Class for accesing price expression table.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
find_title($title)
Returns any existing rowid with specified title.
update($user=0, $notrigger=0)
Update object into database.
create($user, $notrigger=0)
Create object into database.
__construct($db)
Constructor.
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