dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.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
26// Put here all includes required by your class file
27require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
28
29
34{
38 public $records = array();
39
43 public $element = 'cproductnbature';
44
48 public $table_element = 'c_product_nature';
49
50
56 public function __construct($db)
57 {
58 $this->db = $db;
59 }
60
61
69 public function create($user, $notrigger = 0)
70 {
71 global $conf, $langs;
72
73 // Insert request
74 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element."(";
75 $sql .= "rowid,";
76 $sql .= "code,";
77 $sql .= "label,";
78 $sql .= "active";
79 $sql .= ") VALUES (";
80 $sql .= " ".(!isset($this->id) ? 'NULL' : ((int) $this->id)).",";
81 $sql .= " ".(!isset($this->code) ? 'NULL' : ((int) $this->code)).",";
82 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape(trim($this->label))."'").",";
83 $sql .= " ".(!isset($this->active) ? 'NULL' : ((int) $this->active)).",";
84 $sql .= ")";
85
86 $this->db->begin();
87
88 dol_syslog(get_class($this)."::create", LOG_DEBUG);
89 $resql = $this->db->query($sql);
90 // Commit or rollback
91 if (!$resql) {
92 dol_syslog(get_class($this)."::create ".$this->db->lasterror(), LOG_ERR);
93 $this->error = "Error ".$this->db->lasterror();
94 $this->db->rollback();
95 return -1;
96 } else {
97 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
98 $this->db->commit();
99 return $this->id;
100 }
101 }
102
103
111 public function fetch($id, $code = '')
112 {
113 global $langs;
114
115 $sql = "SELECT";
116 $sql .= " t.rowid,";
117 $sql .= " t.code,";
118 $sql .= " t.label,";
119 $sql .= " t.active";
120 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
121 $sql_where = array();
122 if ($id) {
123 $sql_where[] = " t.rowid = ".((int) $id);
124 }
125 if ($code >= 0) {
126 $sql_where[] = " t.code = ".((int) $code);
127 }
128 if (count($sql_where) > 0) {
129 $sql .= ' WHERE '.implode(' AND ', $sql_where);
130 }
131
132 $resql = $this->db->query($sql);
133 if ($resql) {
134 if ($this->db->num_rows($resql)) {
135 $obj = $this->db->fetch_object($resql);
136
137 $this->id = $obj->rowid;
138 $this->code = $obj->code;
139 $this->label = $obj->label;
140 $this->active = $obj->active;
141 }
142 $this->db->free($resql);
143
144 return 1;
145 } else {
146 $this->error = "Error ".$this->db->lasterror();
147 return -1;
148 }
149 }
150
151
163 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
164 {
165 dol_syslog(__METHOD__, LOG_DEBUG);
166
167 $sql = "SELECT";
168 $sql .= " t.rowid,";
169 $sql .= " t.code,";
170 $sql .= " t.label,";
171 $sql .= " t.active";
172 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
173 $sql .= " WHERE 1 = 1";
174
175 // Manage filter
176 if (is_array($filter)) {
177 $sqlwhere = array();
178 if (count($filter) > 0) {
179 foreach ($filter as $key => $value) {
180 if ($key == 't.rowid' || $key == 't.active' || $key == 't.code') {
181 $sqlwhere[] = $this->db->sanitize($key)." = ".((int) $value);
182 } elseif (strpos($key, 'date') !== false) {
183 $sqlwhere[] = $this->db->sanitize($key)." = '".$this->db->idate($value)."'";
184 } elseif ($key == 't.label') {
185 $sqlwhere[] = $this->db->sanitize($key)." = '".$this->db->escape($value)."'";
186 } else {
187 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
188 }
189 }
190 }
191 if (count($sqlwhere) > 0) {
192 $sql .= " AND ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
193 }
194
195 $filter = '';
196 }
197
198 // Manage filter
199 $errormessage = '';
200 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
201 if ($errormessage) {
202 $this->errors[] = $errormessage;
203 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
204 return -1;
205 }
206
207 if (!empty($sortfield)) {
208 $sql .= $this->db->order($sortfield, $sortorder);
209 }
210 if (!empty($limit)) {
211 $sql .= $this->db->plimit($limit, $offset);
212 }
213
214 $resql = $this->db->query($sql);
215 if ($resql) {
216 $this->records = array();
217 $num = $this->db->num_rows($resql);
218 if ($num > 0) {
219 while ($obj = $this->db->fetch_object($resql)) {
220 $record = new self($this->db);
221
222 $record->id = $obj->rowid;
223 $record->code = $obj->code;
224 $record->label = $obj->label;
225 $this->records[$record->id] = $record;
226 }
227 }
228 $this->db->free($resql);
229
230 return $this->records;
231 } else {
232 $this->errors[] = 'Error '.$this->db->lasterror();
233 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
234
235 return -1;
236 }
237 }
238
239
247 public function update($user = null, $notrigger = 0)
248 {
249 global $conf, $langs;
250
251 // Update request
252 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
253 $sql .= " code=".(isset($this->code) ? ((int) $this->code) : "null").",";
254 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape(trim($this->label))."'" : "null").",";
255 $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null");
256 $sql .= " WHERE rowid=".(int) $this->id;
257
258 $this->db->begin();
259
260 dol_syslog(get_class($this)."::update", LOG_DEBUG);
261 $resql = $this->db->query($sql);
262 // Commit or rollback
263 if (!$resql) {
264 dol_syslog(get_class($this)."::update Error ".$this->db->lasterror(), LOG_ERR);
265 $this->error = "Error ".$this->db->lasterror();
266 $this->db->rollback();
267 return -1;
268 } else {
269 $this->db->commit();
270 return 1;
271 }
272 }
273
274
282 public function delete($user, $notrigger = 0)
283 {
284 global $conf, $langs;
285 $error = 0;
286
287 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
288 $sql .= " WHERE rowid=".(int) $this->id;
289
290 $this->db->begin();
291
292 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
293 $resql = $this->db->query($sql);
294 // Commit or rollback
295 if (!$resql) {
296 dol_syslog(get_class($this)."::delete Error ".$this->db->lasterror(), LOG_ERR);
297 $this->error = "Error ".$this->db->lasterror();
298 $this->db->rollback();
299 return -1;
300 } else {
301 $this->db->commit();
302 return 1;
303 }
304 }
305
306
313 public function getProductNatureFromCode($code, $mode = 'code')
314 {
315 if ($mode == 'label') {
316 return dol_getIdFromCode($this->db, $code, $this->table_element, 'label', 'code');
317 } elseif ($mode == 'code') {
318 return dol_getIdFromCode($this->db, $code, $this->table_element, 'code', 'code');
319 }
320
321 return $code;
322 }
323}
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, $filter='', $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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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.