dolibarr  17.0.3
cproductnature.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2020 Florian HENRY <florian.henry@scopen.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
29 class CProductNature // extends CommonObject
30 {
34  public $db;
35 
39  public $error = '';
40 
44  public $errors = array();
45 
49  public $records = array();
50 
54  public $element = 'cproductnbature';
55 
59  public $table_element = 'c_product_nature';
60 
64  public $id;
65 
69  public $code;
70 
74  public $label;
75 
79  public $active;
80 
81 
87  public function __construct($db)
88  {
89  $this->db = $db;
90  }
91 
92 
100  public function create($user, $notrigger = 0)
101  {
102  global $conf, $langs;
103 
104  // Insert request
105  $sql = "INSERT INTO ".$this->db->prefix().$this->table_element."(";
106  $sql .= "rowid,";
107  $sql .= "code,";
108  $sql .= "label,";
109  $sql .= "active";
110  $sql .= ") VALUES (";
111  $sql .= " ".(!isset($this->id) ? 'NULL' : ((int) $this->id)).",";
112  $sql .= " ".(!isset($this->code) ? 'NULL' : ((int) $this->code)).",";
113  $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape(trim($this->label))."'").",";
114  $sql .= " ".(!isset($this->active) ? 'NULL' : ((int) $this->active)).",";
115  $sql .= ")";
116 
117  $this->db->begin();
118 
119  dol_syslog(get_class($this)."::create", LOG_DEBUG);
120  $resql = $this->db->query($sql);
121  // Commit or rollback
122  if (!$resql) {
123  dol_syslog(get_class($this)."::create ".$this->db->lasterror(), LOG_ERR);
124  $this->error = "Error ".$this->db->lasterror();
125  $this->db->rollback();
126  return -1;
127  } else {
128  $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
129  $this->db->commit();
130  return $this->id;
131  }
132  }
133 
134 
142  public function fetch($id, $code = '')
143  {
144  global $langs;
145 
146  $sql = "SELECT";
147  $sql .= " t.rowid,";
148  $sql .= " t.code,";
149  $sql .= " t.label,";
150  $sql .= " t.active";
151  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
152  $sql_where = array();
153  if ($id) {
154  $sql_where[] = " t.rowid = ".((int) $id);
155  }
156  if ($code >= 0) {
157  $sql_where[] = " t.code = ".((int) $code);
158  }
159  if (count($sql_where) > 0) {
160  $sql .= ' WHERE '.implode(' AND ', $sql_where);
161  }
162 
163  $resql = $this->db->query($sql);
164  if ($resql) {
165  if ($this->db->num_rows($resql)) {
166  $obj = $this->db->fetch_object($resql);
167 
168  $this->id = $obj->rowid;
169  $this->code = $obj->code;
170  $this->label = $obj->label;
171  $this->active = $obj->active;
172  }
173  $this->db->free($resql);
174 
175  return 1;
176  } else {
177  $this->error = "Error ".$this->db->lasterror();
178  return -1;
179  }
180  }
181 
182 
194  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
195  {
196  global $conf;
197 
198  dol_syslog(__METHOD__, LOG_DEBUG);
199 
200  $sql = "SELECT";
201  $sql .= " t.rowid,";
202  $sql .= " t.code,";
203  $sql .= " t.label,";
204  $sql .= " t.active";
205  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
206  // Manage filter
207  $sqlwhere = array();
208  if (count($filter) > 0) {
209  foreach ($filter as $key => $value) {
210  if ($key == 't.rowid' || $key == 't.active' || $key == 't.code') {
211  $sqlwhere[] = $key." = ".((int) $value);
212  } elseif (strpos($key, 'date') !== false) {
213  $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
214  } elseif ($key == 't.label') {
215  $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
216  } else {
217  $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
218  }
219  }
220  }
221  if (count($sqlwhere) > 0) {
222  $sql .= ' WHERE ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
223  }
224 
225  if (!empty($sortfield)) {
226  $sql .= $this->db->order($sortfield, $sortorder);
227  }
228  if (!empty($limit)) {
229  $sql .= $this->db->plimit($limit, $offset);
230  }
231 
232  $resql = $this->db->query($sql);
233  if ($resql) {
234  $this->records = array();
235  $num = $this->db->num_rows($resql);
236  if ($num > 0) {
237  while ($obj = $this->db->fetch_object($resql)) {
238  $record = new self($this->db);
239 
240  $record->id = $obj->rowid;
241  $record->code = $obj->code;
242  $record->label = $obj->label;
243  $this->records[$record->id] = $record;
244  }
245  }
246  $this->db->free($resql);
247 
248  return $this->records;
249  } else {
250  $this->errors[] = 'Error '.$this->db->lasterror();
251  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
252 
253  return -1;
254  }
255  }
256 
257 
265  public function update($user = null, $notrigger = 0)
266  {
267  global $conf, $langs;
268 
269  // Update request
270  $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
271  $sql .= " code=".(isset($this->code) ? ((int) $this->code) : "null").",";
272  $sql .= " label=".(isset($this->label) ? "'".$this->db->escape(trim($this->label))."'" : "null").",";
273  $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null");
274  $sql .= " WHERE rowid=".(int) $this->id;
275 
276  $this->db->begin();
277 
278  dol_syslog(get_class($this)."::update", LOG_DEBUG);
279  $resql = $this->db->query($sql);
280  // Commit or rollback
281  if (!$resql) {
282  dol_syslog(get_class($this)."::update Error ".$this->db->lasterror(), LOG_ERR);
283  $this->error = "Error ".$this->db->lasterror();
284  $this->db->rollback();
285  return -1;
286  } else {
287  $this->db->commit();
288  return 1;
289  }
290  }
291 
292 
300  public function delete($user, $notrigger = 0)
301  {
302  global $conf, $langs;
303  $error = 0;
304 
305  $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
306  $sql .= " WHERE rowid=".(int) $this->id;
307 
308  $this->db->begin();
309 
310  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
311  $resql = $this->db->query($sql);
312  // Commit or rollback
313  if (!$resql) {
314  dol_syslog(get_class($this)."::delete Error ".$this->db->lasterror(), LOG_ERR);
315  $this->error = "Error ".$this->db->lasterror();
316  $this->db->rollback();
317  return -1;
318  } else {
319  $this->db->commit();
320  return 1;
321  }
322  }
323 
324 
331  public function getProductNatureFromCode($code, $mode = 'code')
332  {
333  if ($mode == 'label') {
334  return dol_getIdFromCode($this->db, $code, $this->table_element, 'label', 'code');
335  } elseif ($mode == 'code') {
336  return dol_getIdFromCode($this->db, $code, $this->table_element, 'code', 'code');
337  }
338 
339  return $code;
340  }
341 }
db
$conf db
API class for accounts.
Definition: inc.php:41
CProductNature\create
create($user, $notrigger=0)
Create object into database.
Definition: cproductnature.class.php:100
CProductNature
Class of dictionary of nature of product (used by imports)
Definition: cproductnature.class.php:29
CProductNature\fetchAll
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load list of objects in memory from the database.
Definition: cproductnature.class.php:194
CProductNature\update
update($user=null, $notrigger=0)
Update object into database.
Definition: cproductnature.class.php:265
dol_getIdFromCode
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
Definition: functions.lib.php:8880
code
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...
Definition: sync_members_ldap2dolibarr.php:60
$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
CProductNature\__construct
__construct($db)
Constructor.
Definition: cproductnature.class.php:87
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
CProductNature\fetch
fetch($id, $code='')
Load object in memory from database.
Definition: cproductnature.class.php:142
CProductNature\getProductNatureFromCode
getProductNatureFromCode($code, $mode='code')
Get unit from code.
Definition: cproductnature.class.php:331