21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/product/class/api_products.class.php';
39 public static $FIELDS = array(
55 $this->warehouse =
new Entrepot($this->db);
74 public function get(
$id)
76 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'lire')) {
77 throw new RestException(403);
80 throw new RestException(400,
'No warehouse with id=0 can exist');
82 $result = $this->warehouse->fetch(
$id);
84 throw new RestException(404,
'warehouse not found');
88 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
120 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
124 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'lire')) {
125 throw new RestException(403);
128 $sql =
"SELECT t.rowid";
129 $sql .=
" FROM ".MAIN_DB_PREFIX.
"entrepot AS t LEFT JOIN ".MAIN_DB_PREFIX.
"entrepot_extrafields AS ef ON (ef.fk_object = t.rowid)";
131 $sql .=
", ".$this->db->prefix().
"categorie_warehouse as c";
133 $sql .=
' WHERE t.entity IN ('.getEntity(
'stock').
')';
136 $sql .=
" AND c.fk_categorie = ".((int) $category);
137 $sql .=
" AND c.fk_warehouse = t.rowid ";
144 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
149 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
151 $sql .= $this->db->order($sortfield, $sortorder);
156 $offset = $limit * $page;
158 $sql .= $this->db->plimit($limit + 1, $offset);
161 $result = $this->db->query($sql);
164 $num = $this->db->num_rows($result);
165 $min = min($num, ($limit <= 0 ? $num : $limit));
167 $obj = $this->db->fetch_object($result);
168 $warehouse_static =
new Entrepot($this->db);
169 if ($warehouse_static->fetch($obj->rowid)) {
175 throw new RestException(503,
'Error when retrieve warehouse list : '.$this->db->lasterror());
179 if ($pagination_data) {
180 $totalsResult = $this->db->query($sqlTotals);
181 $total = $this->db->fetch_object($totalsResult)->total;
186 $obj_ret[
'data'] = $tmp;
187 $obj_ret[
'pagination'] = [
188 'total' => (int) $total,
190 'page_count' => ceil((
int) $total / $limit),
216 public function post($request_data =
null)
218 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'creer')) {
219 throw new RestException(403);
223 $result = $this->
_validate($request_data);
225 foreach ($request_data as $field => $value) {
226 if ($field ===
'caller') {
228 $this->warehouse->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
232 if ($field ==
'id' || $field ==
'warehouse_id') {
233 throw new RestException(400,
'Creating with id field is forbidden');
235 if ($field ==
'entity' && $value != $this->warehouse->entity) {
236 throw new RestException(403,
'Changing entity of a user using the APIs is not possible');
239 $this->warehouse->$field = $this->
_checkValForAPI($field, $value, $this->warehouse);
241 if ($this->warehouse->create(DolibarrApiAccess::$user) < 0) {
242 throw new RestException(500,
"Error creating warehouse", array_merge(array($this->warehouse->error), $this->warehouse->errors));
244 return $this->warehouse->id;
266 public function put(
$id, $request_data =
null)
268 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'creer')) {
269 throw new RestException(403);
272 throw new RestException(400,
'No warehouse with id=0 can exist');
274 $result = $this->warehouse->fetch(
$id);
276 throw new RestException(404,
'warehouse not found');
280 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
283 foreach ($request_data as $field => $value) {
284 if ($field ==
'id' || $field ==
'warehouse_id') {
285 throw new RestException(400,
'Updating with id field is forbidden');
287 if ($field ==
'entity' && $value != $this->warehouse->entity) {
288 throw new RestException(403,
'Changing entity of a user using the APIs is not possible');
290 if ($field ==
'ref') {
291 throw new RestException(400,
'Deprecated, use label, not ref');
294 if ($field ===
'caller') {
296 $this->warehouse->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
300 if ($field ==
'array_options' && is_array($value)) {
301 foreach ($value as $index => $val) {
302 $this->warehouse->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->warehouse);
307 $this->warehouse->$field = $this->
_checkValForAPI($field, $value, $this->warehouse);
310 $updateresult = $this->warehouse->update(
$id, DolibarrApiAccess::$user);
311 if ($updateresult > 0) {
312 return $this->
get(
$id);
314 throw new RestException(500, $this->warehouse->error);
336 public function delete(
$id)
338 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'supprimer')) {
339 throw new RestException(403);
342 throw new RestException(400,
'No warehouse with id=0 can exist');
344 $result = $this->warehouse->fetch(
$id);
346 throw new RestException(404,
'warehouse not found');
350 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
353 if (!$this->warehouse->delete(DolibarrApiAccess::$user)) {
354 throw new RestException(500,
'error when delete warehouse');
360 'message' =>
'Warehouse deleted'
396 public function listProducts(
$id = 0, $sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includetrans =
false, $properties =
'', $pagination_data =
false)
398 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'lire')) {
399 throw new RestException(403);
401 if ((
int)
$id == 0) {
402 throw new RestException(400,
'No warehouse with id=0 can exist');
404 $existsresult = $this->warehouse->fetch(
$id);
405 if (!$existsresult) {
406 throw new RestException(404,
'warehouse not found');
410 $sql =
"SELECT t.rowid FROM ".MAIN_DB_PREFIX.
"product_stock as ps";
411 $sql.=
" INNER JOIN ".MAIN_DB_PREFIX.
"product as t ON ps.fk_product = t.rowid";
412 $sql.=
" WHERE ps.fk_entrepot =".((int)
$id);
413 $sql.=
" AND t.entity IN (".getEntity(
'stock').
")";
416 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
418 $sql .= $this->db->order($sortfield, $sortorder);
423 $offset = $limit * $page;
425 $sql .= $this->db->plimit($limit + 1, $offset);
428 $result = $this->db->query($sql);
431 $num = $this->db->num_rows($result);
433 $obj = $this->db->fetch_object($result);
434 $api_products_static =
new Products();
435 if ($api_products_static->get($obj->rowid, $includestockdata, $includesubproducts, $includeparentid, $includetrans)) {
441 throw new RestException(500,
'Error when retrieve warehouse product list : '.$this->db->lasterror());
445 if ($pagination_data) {
446 $totalsResult = $this->db->query($sqlTotals);
447 $total = $this->db->fetch_object($totalsResult)->total;
452 $obj_ret[
'data'] = $tmp;
453 $obj_ret[
'pagination'] = [
454 'total' => (int) $total,
456 'page_count' => ceil((
int) $total / $limit),
479 unset(
$object->actiontypecode);
483 unset(
$object->cond_reglement_id);
484 unset(
$object->cond_reglement_supplier_id);
487 unset(
$object->contacts_ids_internal);
490 unset(
$object->date_validation);
491 unset(
$object->demand_reason_id);
492 unset(
$object->deposit_percent);
496 unset(
$object->fk_multicurrency);
503 unset(
$object->linkedObjectsIds);
504 unset(
$object->mode_reglement_id);
506 unset(
$object->multicurrency_code);
507 unset(
$object->multicurrency_total_ht);
508 unset(
$object->multicurrency_total_localtax1);
509 unset(
$object->multicurrency_total_localtax2);
510 unset(
$object->multicurrency_total_ttc);
511 unset(
$object->multicurrency_total_tva);
512 unset(
$object->multicurrency_tx);
522 unset(
$object->retained_warranty_fk_cond_reglement);
523 unset(
$object->shipping_method);
524 unset(
$object->shipping_method_id);
529 unset(
$object->total_localtax1);
530 unset(
$object->total_localtax2);
534 unset(
$object->totalpaid_multicurrency);
535 unset(
$object->transport_mode_id);
536 unset(
$object->TRIGGER_PREFIX);
538 unset(
$object->user_closing_id);
539 unset(
$object->user_modification_id);
540 unset(
$object->user_validation_id);
556 if ($data ===
null) {
559 $warehouse = array();
560 foreach (Warehouses::$FIELDS as $field) {
561 if (!isset($data[$field])) {
562 throw new RestException(400,
"$field field missing");
564 $warehouse[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $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 @phpstan-template T.
__construct()
Constructor.
put($id, $request_data=null)
Update a warehouse.
post($request_data=null)
Create a warehouse.
listProducts($id=0, $sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includetrans=false, $properties='', $pagination_data=false)
List products in a warehouse.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List warehouses.
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.