dolibarr  17.0.3
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)) {
84  $this->title = trim($this->title);
85  }
86  if (isset($this->expression)) {
87  $this->expression = trim($this->expression);
88  }
89 
90  // Insert request
91  $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
92  $sql .= "title, expression";
93  $sql .= ") VALUES (";
94  $sql .= " ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").",";
95  $sql .= " ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''");
96  $sql .= ")";
97 
98  $this->db->begin();
99 
100  dol_syslog(__METHOD__, LOG_DEBUG);
101  $resql = $this->db->query($sql);
102  if (!$resql) {
103  $error++; $this->errors[] = "Error ".$this->db->lasterror();
104  }
105 
106  if (!$error) {
107  $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
108  }
109 
110  // Commit or rollback
111  if ($error) {
112  foreach ($this->errors as $errmsg) {
113  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
114  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
115  }
116  $this->db->rollback();
117  return -1 * $error;
118  } else {
119  $this->db->commit();
120  return $this->id;
121  }
122  }
123 
124 
131  public function fetch($id)
132  {
133  // Check parameters
134  if (empty($id)) {
135  $this->error = 'ErrorWrongParameters';
136  return -1;
137  }
138 
139  $sql = "SELECT title, expression";
140  $sql .= " FROM ".$this->db->prefix().$this->table_element;
141  $sql .= " WHERE rowid = ".((int) $id);
142 
143  dol_syslog(__METHOD__);
144  $resql = $this->db->query($sql);
145  if ($resql) {
146  $obj = $this->db->fetch_object($resql);
147  if ($obj) {
148  $this->id = $id;
149  $this->title = $obj->title;
150  $this->expression = $obj->expression;
151  return 1;
152  } else {
153  return 0;
154  }
155  } else {
156  $this->error = "Error ".$this->db->lasterror();
157  return -1;
158  }
159  }
160 
161  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
167  public function list_price_expression()
168  {
169  // phpcs:enable
170  $sql = "SELECT rowid, title, expression";
171  $sql .= " FROM ".$this->db->prefix().$this->table_element;
172  $sql .= " ORDER BY title";
173 
174  dol_syslog(__METHOD__, LOG_DEBUG);
175  $resql = $this->db->query($sql);
176  if ($resql) {
177  $retarray = array();
178 
179  while ($record = $this->db->fetch_array($resql)) {
180  $price_expression_obj = new PriceExpression($this->db);
181  $price_expression_obj->id = $record["rowid"];
182  $price_expression_obj->title = $record["title"];
183  $price_expression_obj->expression = $record["expression"];
184  $retarray[] = $price_expression_obj;
185  }
186 
187  $this->db->free($resql);
188  return $retarray;
189  } else {
190  $this->error = $this->db->error();
191  return -1;
192  }
193  }
194 
195 
196  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
203  public function find_title($title)
204  {
205  // phpcs:enable
206  $sql = "SELECT rowid";
207  $sql .= " FROM ".$this->db->prefix().$this->table_element;
208  $sql .= " WHERE title = '".$this->db->escape($title)."'";
209 
210  dol_syslog(__METHOD__, LOG_DEBUG);
211  $resql = $this->db->query($sql);
212  if ($resql) {
213  $obj = $this->db->fetch_object($resql);
214  if ($obj) {
215  return (int) $obj->rowid;
216  } else {
217  return 0;
218  }
219  } else {
220  $this->error = "Error ".$this->db->lasterror();
221  return -1;
222  }
223  }
224 
225 
233  public function update($user = 0, $notrigger = 0)
234  {
235  $error = 0;
236 
237  // Clean parameters
238  if (isset($this->title)) {
239  $this->title = trim($this->title);
240  }
241  if (isset($this->expression)) {
242  $this->expression = trim($this->expression);
243  }
244 
245  // Update request
246  $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
247  $sql .= " title = ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").",";
248  $sql .= " expression = ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''")."";
249  $sql .= " WHERE rowid = ".((int) $this->id);
250 
251  $this->db->begin();
252 
253  dol_syslog(__METHOD__);
254  $resql = $this->db->query($sql);
255  if (!$resql) {
256  $error++; $this->errors[] = "Error ".$this->db->lasterror();
257  }
258 
259  // Commit or rollback
260  if ($error) {
261  foreach ($this->errors as $errmsg) {
262  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
263  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
264  }
265  $this->db->rollback();
266  return -1 * $error;
267  } else {
268  $this->db->commit();
269  return 1;
270  }
271  }
272 
273 
281  public function delete(User $user, $notrigger = 0)
282  {
283  $error = 0;
284 
285  $rowid = $this->id;
286 
287  $this->db->begin();
288 
289  if (!$error) {
290  $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
291  $sql .= " WHERE rowid = ".((int) $rowid);
292 
293  dol_syslog(__METHOD__);
294  $resql = $this->db->query($sql);
295  if (!$resql) {
296  $error++; $this->errors[] = "Error ".$this->db->lasterror();
297  }
298  }
299 
300  // Commit or rollback
301  if ($error) {
302  foreach ($this->errors as $errmsg) {
303  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
304  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
305  }
306  $this->db->rollback();
307  return -1 * $error;
308  } else {
309  $this->db->commit();
310  return 1;
311  }
312  }
313 
320  public function initAsSpecimen()
321  {
322  $this->id = 0;
323  $this->expression = '';
324  }
325 }
db
$conf db
API class for accounts.
Definition: inc.php:41
PriceExpression\list_price_expression
list_price_expression()
List all price expressions.
Definition: price_expression.class.php:167
PriceExpression
Class for accesing price expression table.
Definition: price_expression.class.php:30
PriceExpression\fetch
fetch($id)
Load object in memory from the database.
Definition: price_expression.class.php:131
PriceExpression\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: price_expression.class.php:320
$resql
if(isModEnabled('facture') &&!empty($user->rights->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') &&!empty($user->rights->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)) $resql
Social contributions to pay.
Definition: index.php:745
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
PriceExpression\find_title
find_title($title)
Returns any existing rowid with specified title.
Definition: price_expression.class.php:203
PriceExpression\update
update($user=0, $notrigger=0)
Update object into database.
Definition: price_expression.class.php:233
PriceExpression\create
create($user, $notrigger=0)
Create object into database.
Definition: price_expression.class.php:78
User
Class to manage Dolibarr users.
Definition: user.class.php:46
PriceExpression\__construct
__construct($db)
Constructor.
Definition: price_expression.class.php:65