dolibarr 22.0.5
api_warehouses.class.php
1<?php
2/* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
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
19use Luracast\Restler\RestException;
20
21require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
22require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
23
31{
35 public static $FIELDS = array(
36 'label',
37 );
38
42 public $warehouse;
43
47 public function __construct()
48 {
49 global $db;
50 $this->db = $db;
51 $this->warehouse = new Entrepot($this->db);
52 }
53
64 public function get($id)
65 {
66 if (!DolibarrApiAccess::$user->hasRight('stock', 'lire')) {
67 throw new RestException(403);
68 }
69
70 $result = $this->warehouse->fetch($id);
71 if (!$result) {
72 throw new RestException(404, 'warehouse not found');
73 }
74
75 if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id, 'entrepot')) {
76 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
77 }
78
79 return $this->_cleanObjectDatas($this->warehouse);
80 }
81
100 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters = '', $properties = '')
101 {
102 $obj_ret = array();
103
104 if (!DolibarrApiAccess::$user->hasRight('stock', 'lire')) {
105 throw new RestException(403);
106 }
107
108 $sql = "SELECT t.rowid";
109 $sql .= " FROM ".MAIN_DB_PREFIX."entrepot AS t LEFT JOIN ".MAIN_DB_PREFIX."entrepot_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
110 if ($category > 0) {
111 $sql .= ", ".$this->db->prefix()."categorie_warehouse as c";
112 }
113 $sql .= ' WHERE t.entity IN ('.getEntity('stock').')';
114 // Select warehouses of given category
115 if ($category > 0) {
116 $sql .= " AND c.fk_categorie = ".((int) $category);
117 $sql .= " AND c.fk_warehouse = t.rowid ";
118 }
119 // Add sql filters
120 if ($sqlfilters) {
121 $errormessage = '';
122 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
123 if ($errormessage) {
124 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
125 }
126 }
127
128 $sql .= $this->db->order($sortfield, $sortorder);
129 if ($limit) {
130 if ($page < 0) {
131 $page = 0;
132 }
133 $offset = $limit * $page;
134
135 $sql .= $this->db->plimit($limit + 1, $offset);
136 }
137
138 $result = $this->db->query($sql);
139 if ($result) {
140 $i = 0;
141 $num = $this->db->num_rows($result);
142 $min = min($num, ($limit <= 0 ? $num : $limit));
143 while ($i < $min) {
144 $obj = $this->db->fetch_object($result);
145 $warehouse_static = new Entrepot($this->db);
146 if ($warehouse_static->fetch($obj->rowid)) {
147 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($warehouse_static), $properties);
148 }
149 $i++;
150 }
151 } else {
152 throw new RestException(503, 'Error when retrieve warehouse list : '.$this->db->lasterror());
153 }
154
155 return $obj_ret;
156 }
157
158
167 public function post($request_data = null)
168 {
169 if (!DolibarrApiAccess::$user->hasRight('stock', 'creer')) {
170 throw new RestException(403);
171 }
172
173 // Check mandatory fields
174 $result = $this->_validate($request_data);
175
176 foreach ($request_data as $field => $value) {
177 if ($field === 'caller') {
178 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
179 $this->warehouse->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
180 continue;
181 }
182
183 $this->warehouse->$field = $this->_checkValForAPI($field, $value, $this->warehouse);
184 }
185 if ($this->warehouse->create(DolibarrApiAccess::$user) < 0) {
186 throw new RestException(500, "Error creating warehouse", array_merge(array($this->warehouse->error), $this->warehouse->errors));
187 }
188 return $this->warehouse->id;
189 }
190
200 public function put($id, $request_data = null)
201 {
202 if (!DolibarrApiAccess::$user->hasRight('stock', 'creer')) {
203 throw new RestException(403);
204 }
205
206 $result = $this->warehouse->fetch($id);
207 if (!$result) {
208 throw new RestException(404, 'warehouse not found');
209 }
210
211 if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id)) {
212 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
213 }
214
215 foreach ($request_data as $field => $value) {
216 if ($field == 'id') {
217 continue;
218 }
219 if ($field === 'caller') {
220 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
221 $this->warehouse->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
222 continue;
223 }
224
225 if ($field == 'array_options' && is_array($value)) {
226 foreach ($value as $index => $val) {
227 $this->warehouse->array_options[$index] = $this->_checkValForAPI($field, $val, $this->warehouse);
228 }
229 continue;
230 }
231
232 $this->warehouse->$field = $this->_checkValForAPI($field, $value, $this->warehouse);
233 }
234
235 if ($this->warehouse->update($id, DolibarrApiAccess::$user)) {
236 return $this->get($id);
237 } else {
238 throw new RestException(500, $this->warehouse->error);
239 }
240 }
241
250 public function delete($id)
251 {
252 if (!DolibarrApiAccess::$user->hasRight('stock', 'supprimer')) {
253 throw new RestException(403);
254 }
255 $result = $this->warehouse->fetch($id);
256 if (!$result) {
257 throw new RestException(404, 'warehouse not found');
258 }
259
260 if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id)) {
261 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
262 }
263
264 if (!$this->warehouse->delete(DolibarrApiAccess::$user)) {
265 throw new RestException(403, 'error when delete warehouse');
266 }
267
268 return array(
269 'success' => array(
270 'code' => 200,
271 'message' => 'Warehouse deleted'
272 )
273 );
274 }
275
276
277 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
284 protected function _cleanObjectDatas($object)
285 {
286 // phpcs:enable
287 $object = parent::_cleanObjectDatas($object);
288
289 return $object;
290 }
291
292
301 private function _validate($data)
302 {
303 if ($data === null) {
304 $data = array();
305 }
306 $warehouse = array();
307 foreach (Warehouses::$FIELDS as $field) {
308 if (!isset($data[$field])) {
309 throw new RestException(400, "$field field missing");
310 }
311 $warehouse[$field] = $data[$field];
312 }
313 return $warehouse;
314 }
315}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
Class for API REST v1.
Definition api.class.php:33
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:98
Class to manage warehouses.
_validate($data)
Validate fields before create or update object.
_cleanObjectDatas($object)
Clean sensible object datas.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $category=0, $sqlfilters='', $properties='')
List warehouses.
__construct()
Constructor.
put($id, $request_data=null)
Update warehouse.
post($request_data=null)
Create warehouse object.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.