19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
22require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
35 public static $FIELDS = array(
51 $this->warehouse =
new Entrepot($this->db);
64 public function get(
$id)
66 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'lire')) {
67 throw new RestException(403);
70 $result = $this->warehouse->fetch(
$id);
72 throw new RestException(404,
'warehouse not found');
76 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
100 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'', $properties =
'')
104 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'lire')) {
105 throw new RestException(403);
108 $sql =
"SELECT t.rowid";
109 $sql .=
" FROM ".MAIN_DB_PREFIX.
"entrepot AS t LEFT JOIN ".MAIN_DB_PREFIX.
"entrepot_extrafields AS ef ON (ef.fk_object = t.rowid)";
111 $sql .=
", ".$this->db->prefix().
"categorie_warehouse as c";
113 $sql .=
' WHERE t.entity IN ('.getEntity(
'stock').
')';
116 $sql .=
" AND c.fk_categorie = ".((int) $category);
117 $sql .=
" AND c.fk_warehouse = t.rowid ";
124 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
128 $sql .= $this->db->order($sortfield, $sortorder);
133 $offset = $limit * $page;
135 $sql .= $this->db->plimit($limit + 1, $offset);
138 $result = $this->db->query($sql);
141 $num = $this->db->num_rows($result);
142 $min = min($num, ($limit <= 0 ? $num : $limit));
144 $obj = $this->db->fetch_object($result);
145 $warehouse_static =
new Entrepot($this->db);
146 if ($warehouse_static->fetch($obj->rowid)) {
152 throw new RestException(503,
'Error when retrieve warehouse list : '.$this->db->lasterror());
167 public function post($request_data =
null)
169 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'creer')) {
170 throw new RestException(403);
174 $result = $this->
_validate($request_data);
176 foreach ($request_data as $field => $value) {
177 if ($field ===
'caller') {
179 $this->warehouse->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
183 $this->warehouse->$field = $this->
_checkValForAPI($field, $value, $this->warehouse);
185 if ($this->warehouse->create(DolibarrApiAccess::$user) < 0) {
186 throw new RestException(500,
"Error creating warehouse", array_merge(array($this->warehouse->error), $this->warehouse->errors));
188 return $this->warehouse->id;
200 public function put(
$id, $request_data =
null)
202 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'creer')) {
203 throw new RestException(403);
206 $result = $this->warehouse->fetch(
$id);
208 throw new RestException(404,
'warehouse not found');
212 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
215 foreach ($request_data as $field => $value) {
216 if ($field ==
'id') {
219 if ($field ===
'caller') {
221 $this->warehouse->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
225 if ($field ==
'array_options' && is_array($value)) {
226 foreach ($value as $index => $val) {
227 $this->warehouse->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->warehouse);
232 $this->warehouse->$field = $this->
_checkValForAPI($field, $value, $this->warehouse);
235 if ($this->warehouse->update(
$id, DolibarrApiAccess::$user)) {
236 return $this->
get(
$id);
238 throw new RestException(500, $this->warehouse->error);
250 public function delete(
$id)
252 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'supprimer')) {
253 throw new RestException(403);
255 $result = $this->warehouse->fetch(
$id);
257 throw new RestException(404,
'warehouse not found');
261 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
264 if (!$this->warehouse->delete(DolibarrApiAccess::$user)) {
265 throw new RestException(403,
'error when delete warehouse');
271 'message' =>
'Warehouse deleted'
303 if ($data ===
null) {
306 $warehouse = array();
307 foreach (Warehouses::$FIELDS as $field) {
308 if (!isset($data[$field])) {
309 throw new RestException(400,
"$field field missing");
311 $warehouse[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
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.