dolibarr 19.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
25// Put here all includes required by your class file
26require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
27
28
33{
37 public $records = array();
38
42 public $element = 'cproductnbature';
43
47 public $table_element = 'c_product_nature';
48
49
55 public function __construct($db)
56 {
57 $this->db = $db;
58 }
59
60
68 public function create($user, $notrigger = 0)
69 {
70 global $conf, $langs;
71
72 // Insert request
73 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element."(";
74 $sql .= "rowid,";
75 $sql .= "code,";
76 $sql .= "label,";
77 $sql .= "active";
78 $sql .= ") VALUES (";
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)).",";
83 $sql .= ")";
84
85 $this->db->begin();
86
87 dol_syslog(get_class($this)."::create", LOG_DEBUG);
88 $resql = $this->db->query($sql);
89 // Commit or rollback
90 if (!$resql) {
91 dol_syslog(get_class($this)."::create ".$this->db->lasterror(), LOG_ERR);
92 $this->error = "Error ".$this->db->lasterror();
93 $this->db->rollback();
94 return -1;
95 } else {
96 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
97 $this->db->commit();
98 return $this->id;
99 }
100 }
101
102
110 public function fetch($id, $code = '')
111 {
112 global $langs;
113
114 $sql = "SELECT";
115 $sql .= " t.rowid,";
116 $sql .= " t.code,";
117 $sql .= " t.label,";
118 $sql .= " t.active";
119 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
120 $sql_where = array();
121 if ($id) {
122 $sql_where[] = " t.rowid = ".((int) $id);
123 }
124 if ($code >= 0) {
125 $sql_where[] = " t.code = ".((int) $code);
126 }
127 if (count($sql_where) > 0) {
128 $sql .= ' WHERE '.implode(' AND ', $sql_where);
129 }
130
131 $resql = $this->db->query($sql);
132 if ($resql) {
133 if ($this->db->num_rows($resql)) {
134 $obj = $this->db->fetch_object($resql);
135
136 $this->id = $obj->rowid;
137 $this->code = $obj->code;
138 $this->label = $obj->label;
139 $this->active = $obj->active;
140 }
141 $this->db->free($resql);
142
143 return 1;
144 } else {
145 $this->error = "Error ".$this->db->lasterror();
146 return -1;
147 }
148 }
149
150
162 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
163 {
164 global $conf;
165
166 dol_syslog(__METHOD__, LOG_DEBUG);
167
168 $sql = "SELECT";
169 $sql .= " t.rowid,";
170 $sql .= " t.code,";
171 $sql .= " t.label,";
172 $sql .= " t.active";
173 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
174 // Manage filter
175 $sqlwhere = array();
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)."'";
184 } else {
185 $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
186 }
187 }
188 }
189 if (count($sqlwhere) > 0) {
190 $sql .= ' WHERE ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
191 }
192
193 if (!empty($sortfield)) {
194 $sql .= $this->db->order($sortfield, $sortorder);
195 }
196 if (!empty($limit)) {
197 $sql .= $this->db->plimit($limit, $offset);
198 }
199
200 $resql = $this->db->query($sql);
201 if ($resql) {
202 $this->records = array();
203 $num = $this->db->num_rows($resql);
204 if ($num > 0) {
205 while ($obj = $this->db->fetch_object($resql)) {
206 $record = new self($this->db);
207
208 $record->id = $obj->rowid;
209 $record->code = $obj->code;
210 $record->label = $obj->label;
211 $this->records[$record->id] = $record;
212 }
213 }
214 $this->db->free($resql);
215
216 return $this->records;
217 } else {
218 $this->errors[] = 'Error '.$this->db->lasterror();
219 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
220
221 return -1;
222 }
223 }
224
225
233 public function update($user = null, $notrigger = 0)
234 {
235 global $conf, $langs;
236
237 // Update request
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;
243
244 $this->db->begin();
245
246 dol_syslog(get_class($this)."::update", LOG_DEBUG);
247 $resql = $this->db->query($sql);
248 // Commit or rollback
249 if (!$resql) {
250 dol_syslog(get_class($this)."::update Error ".$this->db->lasterror(), LOG_ERR);
251 $this->error = "Error ".$this->db->lasterror();
252 $this->db->rollback();
253 return -1;
254 } else {
255 $this->db->commit();
256 return 1;
257 }
258 }
259
260
268 public function delete($user, $notrigger = 0)
269 {
270 global $conf, $langs;
271 $error = 0;
272
273 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
274 $sql .= " WHERE rowid=".(int) $this->id;
275
276 $this->db->begin();
277
278 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
279 $resql = $this->db->query($sql);
280 // Commit or rollback
281 if (!$resql) {
282 dol_syslog(get_class($this)."::delete 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
299 public function getProductNatureFromCode($code, $mode = 'code')
300 {
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');
305 }
306
307 return $code;
308 }
309}
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.