26require_once DOL_DOCUMENT_ROOT.
'/core/class/commondict.class.php';
37 public $records = array();
42 public $element =
'cproductnbature';
47 public $table_element =
'c_product_nature';
68 public function create($user, $notrigger = 0)
73 $sql =
"INSERT INTO ".$this->db->prefix().$this->table_element.
"(";
79 $sql .=
" ".(!isset($this->
id) ?
'NULL' : ((int) $this->
id)).
",";
80 $sql .=
" ".(!isset($this->code) ?
'NULL' : ((int) $this->code)).
",";
81 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape(trim($this->label)).
"'").
",";
82 $sql .=
" ".(!isset($this->active) ?
'NULL' : ((int) $this->active)).
",";
87 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
88 $resql = $this->db->query($sql);
91 dol_syslog(get_class($this).
"::create ".$this->db->lasterror(), LOG_ERR);
92 $this->error =
"Error ".$this->db->lasterror();
93 $this->db->rollback();
96 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
110 public function fetch($id, $code =
'')
119 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
120 $sql_where = array();
122 $sql_where[] =
" t.rowid = ".((int) $id);
125 $sql_where[] =
" t.code = ".((int) $code);
127 if (count($sql_where) > 0) {
128 $sql .=
' WHERE '.implode(
' AND ', $sql_where);
131 $resql = $this->db->query($sql);
133 if ($this->db->num_rows($resql)) {
134 $obj = $this->db->fetch_object($resql);
136 $this->
id = $obj->rowid;
137 $this->code = $obj->code;
138 $this->label = $obj->label;
139 $this->active = $obj->active;
141 $this->db->free($resql);
145 $this->error =
"Error ".$this->db->lasterror();
162 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
173 $sql .=
" FROM ".$this->db->prefix().$this->table_element.
" as t";
176 if (count($filter) > 0) {
177 foreach ($filter as $key => $value) {
178 if ($key ==
't.rowid' || $key ==
't.active' || $key ==
't.code') {
179 $sqlwhere[] = $key.
" = ".((int) $value);
180 } elseif (strpos($key,
'date') !==
false) {
181 $sqlwhere[] = $key.
" = '".$this->db->idate($value).
"'";
182 } elseif ($key ==
't.label') {
183 $sqlwhere[] = $key.
" = '".$this->db->escape($value).
"'";
185 $sqlwhere[] = $key.
" LIKE '%".$this->db->escape($value).
"%'";
189 if (count($sqlwhere) > 0) {
190 $sql .=
' WHERE ('.implode(
' '.$this->db->escape($filtermode).
' ', $sqlwhere).
')';
193 if (!empty($sortfield)) {
194 $sql .= $this->db->order($sortfield, $sortorder);
196 if (!empty($limit)) {
197 $sql .= $this->db->plimit($limit, $offset);
200 $resql = $this->db->query($sql);
202 $this->records = array();
203 $num = $this->db->num_rows($resql);
205 while ($obj = $this->db->fetch_object($resql)) {
206 $record =
new self($this->db);
208 $record->id = $obj->rowid;
209 $record->code = $obj->code;
210 $record->label = $obj->label;
211 $this->records[$record->id] = $record;
214 $this->db->free($resql);
216 return $this->records;
218 $this->errors[] =
'Error '.$this->db->lasterror();
219 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
233 public function update($user =
null, $notrigger = 0)
235 global $conf, $langs;
238 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
239 $sql .=
" code=".(isset($this->code) ? ((int) $this->code) :
"null").
",";
240 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape(trim($this->label)).
"'" :
"null").
",";
241 $sql .=
" active=".(isset($this->active) ? ((int) $this->active) :
"null");
242 $sql .=
" WHERE rowid=".(int) $this->
id;
246 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
247 $resql = $this->db->query($sql);
250 dol_syslog(get_class($this).
"::update Error ".$this->db->lasterror(), LOG_ERR);
251 $this->error =
"Error ".$this->db->lasterror();
252 $this->db->rollback();
268 public function delete($user, $notrigger = 0)
270 global $conf, $langs;
273 $sql =
"DELETE FROM ".$this->db->prefix().$this->table_element;
274 $sql .=
" WHERE rowid=".(int) $this->
id;
278 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
279 $resql = $this->db->query($sql);
282 dol_syslog(get_class($this).
"::delete Error ".$this->db->lasterror(), LOG_ERR);
283 $this->error =
"Error ".$this->db->lasterror();
284 $this->db->rollback();
301 if ($mode ==
'label') {
302 return dol_getIdFromCode($this->db, $code, $this->table_element,
'label',
'code');
303 } elseif ($mode ==
'code') {
304 return dol_getIdFromCode($this->db, $code, $this->table_element,
'code',
'code');
Class of dictionary of nature of product (used by imports)
fetch($id, $code='')
Load object in memory from database.
getProductNatureFromCode($code, $mode='code')
Get unit from code.
update($user=null, $notrigger=0)
Update object into database.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load list of objects in memory from the database.
create($user, $notrigger=0)
Create object into database.
__construct($db)
Constructor.
Parent class of all other dictionary classes.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.