18 use Luracast\Restler\RestException;
20 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
21 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
34 static $FIELDS = array(
63 public function get($id)
65 if (!DolibarrApiAccess::$user->rights->stock->lire) {
66 throw new RestException(401);
69 $result = $this->warehouse->fetch($id);
71 throw new RestException(404,
'warehouse not found');
75 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
96 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'')
102 if (!DolibarrApiAccess::$user->rights->stock->lire) {
103 throw new RestException(401);
106 $sql =
"SELECT t.rowid";
107 $sql .=
" FROM ".MAIN_DB_PREFIX.
"entrepot as t";
109 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
111 $sql .=
' WHERE t.entity IN ('.getEntity(
'stock').
')';
114 $sql .=
" AND c.fk_categorie = ".$this->db->escape($category);
115 $sql .=
" AND c.fk_warehouse = t.rowid ";
122 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
124 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
125 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
128 $sql .= $this->
db->order($sortfield, $sortorder);
134 $offset = $limit * $page;
136 $sql .= $this->
db->plimit($limit + 1, $offset);
139 $result = $this->
db->query($sql);
143 $num = $this->
db->num_rows($result);
144 $min = min($num, ($limit <= 0 ? $num : $limit));
147 $obj = $this->
db->fetch_object($result);
149 if ($warehouse_static->fetch($obj->rowid)) {
155 throw new RestException(503,
'Error when retrieve warehouse list : '.$this->
db->lasterror());
157 if (!count($obj_ret)) {
158 throw new RestException(404,
'No warehouse found');
170 public function post($request_data = null)
172 if (!DolibarrApiAccess::$user->rights->stock->creer) {
173 throw new RestException(401);
177 $result = $this->
_validate($request_data);
179 foreach ($request_data as $field => $value) {
180 $this->warehouse->$field = $value;
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));
185 return $this->warehouse->id;
195 public function put($id, $request_data = null)
197 if (!DolibarrApiAccess::$user->rights->stock->creer) {
198 throw new RestException(401);
201 $result = $this->warehouse->fetch($id);
203 throw new RestException(404,
'warehouse not found');
207 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
210 foreach ($request_data as $field => $value) {
211 if ($field ==
'id')
continue;
212 $this->warehouse->$field = $value;
215 if ($this->warehouse->update($id, DolibarrApiAccess::$user))
216 return $this->
get($id);
227 public function delete($id)
229 if (!DolibarrApiAccess::$user->rights->stock->supprimer) {
230 throw new RestException(401);
232 $result = $this->warehouse->fetch($id);
234 throw new RestException(404,
'warehouse not found');
238 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
241 if (!$this->warehouse->delete(DolibarrApiAccess::$user)) {
242 throw new RestException(401,
'error when delete warehouse');
248 'message' =>
'Warehouse deleted'
264 $object = parent::_cleanObjectDatas($object);
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];
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.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
_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.
Class to manage warehouses.