dolibarr  13.0.2
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 
29 class Warehouses extends DolibarrApi
30 {
34  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";
108  if ($category > 0) {
109  $sql .= ", ".MAIN_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 = ".$this->db->escape($category);
115  $sql .= " AND c.fk_warehouse = t.rowid ";
116  }
117  // Add sql filters
118  if ($sqlfilters)
119  {
120  if (!DolibarrApi::_checkFilters($sqlfilters))
121  {
122  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
123  }
124  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
125  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
126  }
127 
128  $sql .= $this->db->order($sortfield, $sortorder);
129  if ($limit) {
130  if ($page < 0)
131  {
132  $page = 0;
133  }
134  $offset = $limit * $page;
135 
136  $sql .= $this->db->plimit($limit + 1, $offset);
137  }
138 
139  $result = $this->db->query($sql);
140  if ($result)
141  {
142  $i = 0;
143  $num = $this->db->num_rows($result);
144  $min = min($num, ($limit <= 0 ? $num : $limit));
145  while ($i < $min)
146  {
147  $obj = $this->db->fetch_object($result);
148  $warehouse_static = new Entrepot($this->db);
149  if ($warehouse_static->fetch($obj->rowid)) {
150  $obj_ret[] = $this->_cleanObjectDatas($warehouse_static);
151  }
152  $i++;
153  }
154  } else {
155  throw new RestException(503, 'Error when retrieve warehouse list : '.$this->db->lasterror());
156  }
157  if (!count($obj_ret)) {
158  throw new RestException(404, 'No warehouse found');
159  }
160  return $obj_ret;
161  }
162 
163 
170  public function post($request_data = null)
171  {
172  if (!DolibarrApiAccess::$user->rights->stock->creer) {
173  throw new RestException(401);
174  }
175 
176  // Check mandatory fields
177  $result = $this->_validate($request_data);
178 
179  foreach ($request_data as $field => $value) {
180  $this->warehouse->$field = $value;
181  }
182  if ($this->warehouse->create(DolibarrApiAccess::$user) < 0) {
183  throw new RestException(500, "Error creating warehouse", array_merge(array($this->warehouse->error), $this->warehouse->errors));
184  }
185  return $this->warehouse->id;
186  }
187 
195  public function put($id, $request_data = null)
196  {
197  if (!DolibarrApiAccess::$user->rights->stock->creer) {
198  throw new RestException(401);
199  }
200 
201  $result = $this->warehouse->fetch($id);
202  if (!$result) {
203  throw new RestException(404, 'warehouse not found');
204  }
205 
206  if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id)) {
207  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
208  }
209 
210  foreach ($request_data as $field => $value) {
211  if ($field == 'id') continue;
212  $this->warehouse->$field = $value;
213  }
214 
215  if ($this->warehouse->update($id, DolibarrApiAccess::$user))
216  return $this->get($id);
217 
218  return false;
219  }
220 
227  public function delete($id)
228  {
229  if (!DolibarrApiAccess::$user->rights->stock->supprimer) {
230  throw new RestException(401);
231  }
232  $result = $this->warehouse->fetch($id);
233  if (!$result) {
234  throw new RestException(404, 'warehouse not found');
235  }
236 
237  if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id)) {
238  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
239  }
240 
241  if (!$this->warehouse->delete(DolibarrApiAccess::$user)) {
242  throw new RestException(401, 'error when delete warehouse');
243  }
244 
245  return array(
246  'success' => array(
247  'code' => 200,
248  'message' => 'Warehouse deleted'
249  )
250  );
251  }
252 
253 
254  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
261  protected function _cleanObjectDatas($object)
262  {
263  // phpcs:enable
264  $object = parent::_cleanObjectDatas($object);
265 
266  // Remove the subscriptions because they are handled as a subresource.
267  //unset($object->subscriptions);
268 
269  return $object;
270  }
271 
272 
281  private function _validate($data)
282  {
283  $warehouse = array();
284  foreach (Warehouses::$FIELDS as $field) {
285  if (!isset($data[$field]))
286  throw new RestException(400, "$field field missing");
287  $warehouse[$field] = $data[$field];
288  }
289  return $warehouse;
290  }
291 }
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $category=0, $sqlfilters= '')
List warehouses.
put($id, $request_data=null)
Update warehouse.
$conf db
API class for accounts.
Definition: inc.php:54
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
Class for API REST v1.
Definition: api.class.php:30
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Validate fields before create or update object.
post($request_data=null)
Create warehouse object.
__construct()
Constructor.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
Definition: api.class.php:252
Class to manage warehouses.