dolibarr  13.0.2
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 ".MAIN_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(MAIN_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 ".MAIN_DB_PREFIX.$this->table_element." as t";
152  $sql_where = array();
153  if ($id) $sql_where[] = " t.rowid = ".$id;
154  if ($code >= 0) $sql_where[] = " t.code = ".((int) $code);
155  if (count($sql_where) > 0) {
156  $sql .= ' WHERE '.implode(' AND ', $sql_where);
157  }
158 
159  $resql = $this->db->query($sql);
160  if ($resql)
161  {
162  if ($this->db->num_rows($resql))
163  {
164  $obj = $this->db->fetch_object($resql);
165 
166  $this->id = $obj->rowid;
167  $this->code = $obj->code;
168  $this->label = $obj->label;
169  $this->active = $obj->active;
170  }
171  $this->db->free($resql);
172 
173  return 1;
174  } else {
175  $this->error = "Error ".$this->db->lasterror();
176  return -1;
177  }
178  }
179 
180 
192  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
193  {
194  global $conf;
195 
196  dol_syslog(__METHOD__, LOG_DEBUG);
197 
198  $sql = 'SELECT';
199  $sql .= " t.rowid,";
200  $sql .= " t.code,";
201  $sql .= " t.label,";
202  $sql .= " t.active";
203  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
204  // Manage filter
205  $sqlwhere = array();
206  if (count($filter) > 0) {
207  foreach ($filter as $key => $value) {
208  if ($key == 't.rowid' || $key == 't.active' || $key == 't.code') {
209  $sqlwhere[] = $key.'='.(int) $value;
210  } elseif (strpos($key, 'date') !== false) {
211  $sqlwhere[] = $key.' = \''.$this->db->idate($value).'\'';
212  } elseif ($key == 't.label') {
213  $sqlwhere[] = $key.' = \''.$this->db->escape($value).'\'';
214  } else {
215  $sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\'';
216  }
217  }
218  }
219  if (count($sqlwhere) > 0) {
220  $sql .= ' WHERE ('.implode(' '.$filtermode.' ', $sqlwhere).')';
221  }
222 
223  if (!empty($sortfield)) {
224  $sql .= $this->db->order($sortfield, $sortorder);
225  }
226  if (!empty($limit)) {
227  $sql .= ' '.$this->db->plimit($limit, $offset);
228  }
229 
230  $resql = $this->db->query($sql);
231  if ($resql) {
232  $this->records = array();
233  $num = $this->db->num_rows($resql);
234  if ($num > 0) {
235  while ($obj = $this->db->fetch_object($resql))
236  {
237  $record = new self($this->db);
238 
239  $record->id = $obj->rowid;
240  $record->code = $obj->code;
241  $record->label = $obj->label;
242  $this->records[$record->id] = $record;
243  }
244  }
245  $this->db->free($resql);
246 
247  return $this->records;
248  } else {
249  $this->errors[] = 'Error '.$this->db->lasterror();
250  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
251 
252  return -1;
253  }
254  }
255 
256 
264  public function update($user = null, $notrigger = 0)
265  {
266  global $conf, $langs;
267 
268  // Update request
269  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
270  $sql .= " code=".(isset($this->code) ? ((int) $this->code) : "null").",";
271  $sql .= " label=".(isset($this->label) ? "'".$this->db->escape(trim($this->label))."'" : "null").",";
272  $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null");
273  $sql .= " WHERE rowid=".(int) $this->id;
274 
275  $this->db->begin();
276 
277  dol_syslog(get_class($this)."::update", LOG_DEBUG);
278  $resql = $this->db->query($sql);
279  // Commit or rollback
280  if (!$resql) {
281  dol_syslog(get_class($this)."::update Error ".$this->db->lasterror(), LOG_ERR);
282  $this->error = "Error ".$this->db->lasterror();
283  $this->db->rollback();
284  return -1;
285  } else {
286  $this->db->commit();
287  return 1;
288  }
289  }
290 
291 
299  public function delete($user, $notrigger = 0)
300  {
301  global $conf, $langs;
302  $error = 0;
303 
304  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
305  $sql .= " WHERE rowid=".(int) $this->id;
306 
307  $this->db->begin();
308 
309  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
310  $resql = $this->db->query($sql);
311  // Commit or rollback
312  if (!$resql) {
313  dol_syslog(get_class($this)."::delete Error ".$this->db->lasterror(), LOG_ERR);
314  $this->error = "Error ".$this->db->lasterror();
315  $this->db->rollback();
316  return -1;
317  } else {
318  $this->db->commit();
319  return 1;
320  }
321  }
322 
323 
330  public function getProductNatureFromCode($code, $mode = 'code')
331  {
332  if ($mode == 'label') {
333  return dol_getIdFromCode($this->db, $code, $this->table_element, 'label', 'code');
334  } elseif ($mode == 'code') {
335  return dol_getIdFromCode($this->db, $code, $this->table_element, 'code', 'code');
336  }
337 
338  return $code;
339  }
340 }
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
$conf db
API class for accounts.
Definition: inc.php:54
update($user=null, $notrigger=0)
Update object into database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
fetch($id, $code= '')
Load object in memory from database.
__construct($db)
Constructor.
dol_getIdFromCode($db, $key, $tablename, $fieldkey= 'code', $fieldid= 'id', $entityfilter=0)
Return an id or code from a code or id.
Class of dictionary of nature of product (used by imports)
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
create($user, $notrigger=0)
Create object into database.
fetchAll($sortorder= '', $sortfield= '', $limit=0, $offset=0, array $filter=array(), $filtermode= 'AND')
Load list of objects in memory from the database.
getProductNatureFromCode($code, $mode= 'code')
Get unit from code.