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(
50 $this->warehouse =
new Entrepot($this->db);
63 public function get(
$id)
65 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'lire')) {
66 throw new RestException(403);
69 $result = $this->warehouse->fetch(
$id);
71 throw new RestException(404,
'warehouse not found');
75 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
97 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'', $properties =
'')
103 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'lire')) {
104 throw new RestException(403);
107 $sql =
"SELECT t.rowid";
108 $sql .=
" FROM ".MAIN_DB_PREFIX.
"entrepot AS t LEFT JOIN ".MAIN_DB_PREFIX.
"entrepot_extrafields AS ef ON (ef.fk_object = t.rowid)";
110 $sql .=
", ".$this->db->prefix().
"categorie_warehouse as c";
112 $sql .=
' WHERE t.entity IN ('.getEntity(
'stock').
')';
115 $sql .=
" AND c.fk_categorie = ".((int) $category);
116 $sql .=
" AND c.fk_warehouse = t.rowid ";
123 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
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);
144 $warehouse_static =
new Entrepot($this->db);
145 if ($warehouse_static->fetch($obj->rowid)) {
151 throw new RestException(503,
'Error when retrieve warehouse list : '.$this->db->lasterror());
164 public function post($request_data =
null)
166 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'creer')) {
167 throw new RestException(403);
171 $result = $this->
_validate($request_data);
173 foreach ($request_data as $field => $value) {
174 if ($field ===
'caller') {
176 $this->warehouse->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
180 $this->warehouse->$field = $this->
_checkValForAPI($field, $value, $this->warehouse);
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->hasRight(
'stock',
'creer')) {
198 throw new RestException(403);
201 $result = $this->warehouse->fetch(
$id);
203 throw new RestException(404,
'warehouse not found');
207 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
210 foreach ($request_data as $field => $value) {
211 if ($field ==
'id') {
214 if ($field ===
'caller') {
216 $this->warehouse->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
220 $this->warehouse->$field = $this->
_checkValForAPI($field, $value, $this->warehouse);
223 if ($this->warehouse->update(
$id, DolibarrApiAccess::$user)) {
224 return $this->
get(
$id);
226 throw new RestException(500, $this->warehouse->error);
236 public function delete(
$id)
238 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'supprimer')) {
239 throw new RestException(403);
241 $result = $this->warehouse->fetch(
$id);
243 throw new RestException(404,
'warehouse not found');
247 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
250 if (!$this->warehouse->delete(DolibarrApiAccess::$user)) {
251 throw new RestException(403,
'error when delete warehouse');
257 'message' =>
'Warehouse deleted'
292 $warehouse = array();
293 foreach (Warehouses::$FIELDS as $field) {
294 if (!isset($data[$field])) {
295 throw new RestException(400,
"$field field missing");
297 $warehouse[$field] = $data[$field];
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
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='', $properties='')
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
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.