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 public 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 ".$this->db->prefix().
"entrepot as t";
109 $sql .=
", ".$this->db->prefix().
"categorie_societe as c";
111 $sql .=
' WHERE t.entity IN ('.getEntity(
'stock').
')';
114 $sql .=
" AND c.fk_categorie = ".((int) $category);
115 $sql .=
" AND c.fk_warehouse = t.rowid ";
121 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
123 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
124 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
127 $sql .= $this->
db->order($sortfield, $sortorder);
132 $offset = $limit * $page;
134 $sql .= $this->
db->plimit($limit + 1, $offset);
137 $result = $this->
db->query($sql);
140 $num = $this->
db->num_rows($result);
141 $min = min($num, ($limit <= 0 ? $num : $limit));
143 $obj = $this->
db->fetch_object($result);
145 if ($warehouse_static->fetch($obj->rowid)) {
151 throw new RestException(503,
'Error when retrieve warehouse list : '.$this->
db->lasterror());
153 if (!count($obj_ret)) {
154 throw new RestException(404,
'No warehouse found');
166 public function post($request_data =
null)
168 if (!DolibarrApiAccess::$user->rights->stock->creer) {
169 throw new RestException(401);
173 $result = $this->
_validate($request_data);
175 foreach ($request_data as $field => $value) {
176 $this->warehouse->$field = $value;
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));
181 return $this->warehouse->id;
191 public function put($id, $request_data =
null)
193 if (!DolibarrApiAccess::$user->rights->stock->creer) {
194 throw new RestException(401);
197 $result = $this->warehouse->fetch($id);
199 throw new RestException(404,
'warehouse not found');
203 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
206 foreach ($request_data as $field => $value) {
207 if ($field ==
'id') {
210 $this->warehouse->$field = $value;
213 if ($this->warehouse->update($id, DolibarrApiAccess::$user)) {
214 return $this->
get($id);
226 public function delete($id)
228 if (!DolibarrApiAccess::$user->rights->stock->supprimer) {
229 throw new RestException(401);
231 $result = $this->warehouse->fetch($id);
233 throw new RestException(404,
'warehouse not found');
237 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
240 if (!$this->warehouse->delete(DolibarrApiAccess::$user)) {
241 throw new RestException(401,
'error when delete warehouse');
247 'message' =>
'Warehouse deleted'
263 $object = parent::_cleanObjectDatas($object);
282 $warehouse = array();
283 foreach (Warehouses::$FIELDS as $field) {
284 if (!isset($data[$field])) {
285 throw new RestException(400,
"$field field missing");
287 $warehouse[$field] = $data[$field];
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
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.