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->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 LEFT JOIN ".MAIN_DB_PREFIX.
"entrepot_extrafields AS ef ON (ef.fk_object = t.rowid)";
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 ";
122 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
126 $sql .= $this->db->order($sortfield, $sortorder);
131 $offset = $limit * $page;
133 $sql .= $this->db->plimit($limit + 1, $offset);
136 $result = $this->db->query($sql);
139 $num = $this->db->num_rows($result);
140 $min = min($num, ($limit <= 0 ? $num : $limit));
142 $obj = $this->db->fetch_object($result);
143 $warehouse_static =
new Entrepot($this->db);
144 if ($warehouse_static->fetch($obj->rowid)) {
150 throw new RestException(503,
'Error when retrieve warehouse list : '.$this->db->lasterror());
152 if (!count($obj_ret)) {
153 throw new RestException(404,
'No warehouse found');
165 public function post($request_data =
null)
167 if (!DolibarrApiAccess::$user->rights->stock->creer) {
168 throw new RestException(401);
172 $result = $this->
_validate($request_data);
174 foreach ($request_data as $field => $value) {
175 $this->warehouse->$field = $value;
177 if ($this->warehouse->create(DolibarrApiAccess::$user) < 0) {
178 throw new RestException(500,
"Error creating warehouse", array_merge(array($this->warehouse->error), $this->warehouse->errors));
180 return $this->warehouse->id;
190 public function put($id, $request_data =
null)
192 if (!DolibarrApiAccess::$user->rights->stock->creer) {
193 throw new RestException(401);
196 $result = $this->warehouse->fetch($id);
198 throw new RestException(404,
'warehouse not found');
202 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
205 foreach ($request_data as $field => $value) {
206 if ($field ==
'id') {
209 $this->warehouse->$field = $value;
212 if ($this->warehouse->update($id, DolibarrApiAccess::$user)) {
213 return $this->
get($id);
225 public function delete($id)
227 if (!DolibarrApiAccess::$user->rights->stock->supprimer) {
228 throw new RestException(401);
230 $result = $this->warehouse->fetch($id);
232 throw new RestException(404,
'warehouse not found');
236 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
239 if (!$this->warehouse->delete(DolibarrApiAccess::$user)) {
240 throw new RestException(401,
'error when delete warehouse');
246 'message' =>
'Warehouse deleted'
262 $object = parent::_cleanObjectDatas($object);
281 $warehouse = array();
282 foreach (Warehouses::$FIELDS as $field) {
283 if (!isset($data[$field])) {
284 throw new RestException(400,
"$field field missing");
286 $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.
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria