dolibarr  x.y.z
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  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 ".$this->db->prefix()."entrepot as t";
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  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
121  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
122  }
123  $regexstring = '\‍(([^:\'\‍(\‍)]+:[^:\'\‍(\‍)]+:[^\‍(\‍)]+)\‍)';
124  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
125  }
126 
127  $sql .= $this->db->order($sortfield, $sortorder);
128  if ($limit) {
129  if ($page < 0) {
130  $page = 0;
131  }
132  $offset = $limit * $page;
133 
134  $sql .= $this->db->plimit($limit + 1, $offset);
135  }
136 
137  $result = $this->db->query($sql);
138  if ($result) {
139  $i = 0;
140  $num = $this->db->num_rows($result);
141  $min = min($num, ($limit <= 0 ? $num : $limit));
142  while ($i < $min) {
143  $obj = $this->db->fetch_object($result);
144  $warehouse_static = new Entrepot($this->db);
145  if ($warehouse_static->fetch($obj->rowid)) {
146  $obj_ret[] = $this->_cleanObjectDatas($warehouse_static);
147  }
148  $i++;
149  }
150  } else {
151  throw new RestException(503, 'Error when retrieve warehouse list : '.$this->db->lasterror());
152  }
153  if (!count($obj_ret)) {
154  throw new RestException(404, 'No warehouse found');
155  }
156  return $obj_ret;
157  }
158 
159 
166  public function post($request_data = null)
167  {
168  if (!DolibarrApiAccess::$user->rights->stock->creer) {
169  throw new RestException(401);
170  }
171 
172  // Check mandatory fields
173  $result = $this->_validate($request_data);
174 
175  foreach ($request_data as $field => $value) {
176  $this->warehouse->$field = $value;
177  }
178  if ($this->warehouse->create(DolibarrApiAccess::$user) < 0) {
179  throw new RestException(500, "Error creating warehouse", array_merge(array($this->warehouse->error), $this->warehouse->errors));
180  }
181  return $this->warehouse->id;
182  }
183 
191  public function put($id, $request_data = null)
192  {
193  if (!DolibarrApiAccess::$user->rights->stock->creer) {
194  throw new RestException(401);
195  }
196 
197  $result = $this->warehouse->fetch($id);
198  if (!$result) {
199  throw new RestException(404, 'warehouse not found');
200  }
201 
202  if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id)) {
203  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
204  }
205 
206  foreach ($request_data as $field => $value) {
207  if ($field == 'id') {
208  continue;
209  }
210  $this->warehouse->$field = $value;
211  }
212 
213  if ($this->warehouse->update($id, DolibarrApiAccess::$user)) {
214  return $this->get($id);
215  }
216 
217  return false;
218  }
219 
226  public function delete($id)
227  {
228  if (!DolibarrApiAccess::$user->rights->stock->supprimer) {
229  throw new RestException(401);
230  }
231  $result = $this->warehouse->fetch($id);
232  if (!$result) {
233  throw new RestException(404, 'warehouse not found');
234  }
235 
236  if (!DolibarrApi::_checkAccessToResource('stock', $this->warehouse->id)) {
237  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
238  }
239 
240  if (!$this->warehouse->delete(DolibarrApiAccess::$user)) {
241  throw new RestException(401, 'error when delete warehouse');
242  }
243 
244  return array(
245  'success' => array(
246  'code' => 200,
247  'message' => 'Warehouse deleted'
248  )
249  );
250  }
251 
252 
253  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
260  protected function _cleanObjectDatas($object)
261  {
262  // phpcs:enable
263  $object = parent::_cleanObjectDatas($object);
264 
265  // Remove the subscriptions because they are handled as a subresource.
266  //unset($object->subscriptions);
267 
268  return $object;
269  }
270 
271 
280  private function _validate($data)
281  {
282  $warehouse = array();
283  foreach (Warehouses::$FIELDS as $field) {
284  if (!isset($data[$field])) {
285  throw new RestException(400, "$field field missing");
286  }
287  $warehouse[$field] = $data[$field];
288  }
289  return $warehouse;
290  }
291 }
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.
Definition: api.class.php:283
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
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.
$conf db
API class for accounts.
Definition: inc.php:41