dolibarr 18.0.6
api_warehouses.class.php
1<?php
2/* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18 use Luracast\Restler\RestException;
19
20 require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
21 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
22
30{
34 public static $FIELDS = array(
35 'label',
36 );
37
41 public $warehouse;
42
46 public function __construct()
47 {
48 global $db, $conf;
49 $this->db = $db;
50 $this->warehouse = new Entrepot($this->db);
51 }
52
63 public function get($id)
64 {
65 if (!DolibarrApiAccess::$user->rights->stock->lire) {
66 throw new RestException(401);
67 }
68
69 $result = $this->warehouse->fetch($id);
70 if (!$result) {
71 throw new RestException(404, 'warehouse not found');
72 }
73
74 if (!DolibarrApi::_checkAccessToResource('warehouse', $this->warehouse->id)) {
75 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
76 }
77
78 return $this->_cleanObjectDatas($this->warehouse);
79 }
80
96 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters = '')
97 {
98 global $db, $conf;
99
100 $obj_ret = array();
101
102 if (!DolibarrApiAccess::$user->rights->stock->lire) {
103 throw new RestException(401);
104 }
105
106 $sql = "SELECT t.rowid";
107 $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
108 if ($category > 0) {
109 $sql .= ", ".$this->db->prefix()."categorie_societe as c";
110 }
111 $sql .= ' WHERE t.entity IN ('.getEntity('stock').')';
112 // Select warehouses of given category
113 if ($category > 0) {
114 $sql .= " AND c.fk_categorie = ".((int) $category);
115 $sql .= " AND c.fk_warehouse = t.rowid ";
116 }
117 // Add sql filters
118 if ($sqlfilters) {
119 $errormessage = '';
120 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
121 if ($errormessage) {
122 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
123 }
124 }
125
126 $sql .= $this->db->order($sortfield, $sortorder);
127 if ($limit) {
128 if ($page < 0) {
129 $page = 0;
130 }
131 $offset = $limit * $page;
132
133 $sql .= $this->db->plimit($limit + 1, $offset);
134 }
135
136 $result = $this->db->query($sql);
137 if ($result) {
138 $i = 0;
139 $num = $this->db->num_rows($result);
140 $min = min($num, ($limit <= 0 ? $num : $limit));
141 while ($i < $min) {
142 $obj = $this->db->fetch_object($result);
143 $warehouse_static = new Entrepot($this->db);
144 if ($warehouse_static->fetch($obj->rowid)) {
145 $obj_ret[] = $this->_cleanObjectDatas($warehouse_static);
146 }
147 $i++;
148 }
149 } else {
150 throw new RestException(503, 'Error when retrieve warehouse list : '.$this->db->lasterror());
151 }
152 if (!count($obj_ret)) {
153 throw new RestException(404, 'No warehouse found');
154 }
155 return $obj_ret;
156 }
157
158
165 public function post($request_data = null)
166 {
167 if (!DolibarrApiAccess::$user->rights->stock->creer) {
168 throw new RestException(401);
169 }
170
171 // Check mandatory fields
172 $result = $this->_validate($request_data);
173
174 foreach ($request_data as $field => $value) {
175 $this->warehouse->$field = $value;
176 }
177 if ($this->warehouse->create(DolibarrApiAccess::$user) < 0) {
178 throw new RestException(500, "Error creating warehouse", array_merge(array($this->warehouse->error), $this->warehouse->errors));
179 }
180 return $this->warehouse->id;
181 }
182
190 public function put($id, $request_data = null)
191 {
192 if (!DolibarrApiAccess::$user->rights->stock->creer) {
193 throw new RestException(401);
194 }
195
196 $result = $this->warehouse->fetch($id);
197 if (!$result) {
198 throw new RestException(404, 'warehouse not found');
199 }
200
201 if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id)) {
202 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
203 }
204
205 foreach ($request_data as $field => $value) {
206 if ($field == 'id') {
207 continue;
208 }
209 $this->warehouse->$field = $value;
210 }
211
212 if ($this->warehouse->update($id, DolibarrApiAccess::$user)) {
213 return $this->get($id);
214 }
215
216 return false;
217 }
218
225 public function delete($id)
226 {
227 if (!DolibarrApiAccess::$user->rights->stock->supprimer) {
228 throw new RestException(401);
229 }
230 $result = $this->warehouse->fetch($id);
231 if (!$result) {
232 throw new RestException(404, 'warehouse not found');
233 }
234
235 if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id)) {
236 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
237 }
238
239 if (!$this->warehouse->delete(DolibarrApiAccess::$user)) {
240 throw new RestException(401, 'error when delete warehouse');
241 }
242
243 return array(
244 'success' => array(
245 'code' => 200,
246 'message' => 'Warehouse deleted'
247 )
248 );
249 }
250
251
252 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
259 protected function _cleanObjectDatas($object)
260 {
261 // phpcs:enable
262 $object = parent::_cleanObjectDatas($object);
263
264 // Remove the subscriptions because they are handled as a subresource.
265 //unset($object->subscriptions);
266
267 return $object;
268 }
269
270
279 private function _validate($data)
280 {
281 $warehouse = array();
282 foreach (Warehouses::$FIELDS as $field) {
283 if (!isset($data[$field])) {
284 throw new RestException(400, "$field field missing");
285 }
286 $warehouse[$field] = $data[$field];
287 }
288 return $warehouse;
289 }
290}
Class for API REST v1.
Definition api.class.php:31
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
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='')
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