20 use Luracast\Restler\RestException;
22 require_once DOL_DOCUMENT_ROOT.
'/bom/class/bom.class.php';
51 $this->bom =
new BOM($this->
db);
65 public function get($id)
67 if (!DolibarrApiAccess::$user->rights->bom->read) {
68 throw new RestException(401);
71 $result = $this->bom->fetch($id);
73 throw new RestException(404,
'BOM not found');
77 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
98 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
103 $tmpobject =
new BOM($this->
db);
105 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
107 $restrictonsocid = 0;
111 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
113 $sql =
"SELECT t.rowid";
114 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .=
", sc.fk_soc, sc.fk_user";
115 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" as t";
117 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
118 $sql .=
" WHERE 1 = 1";
124 if ($tmpobject->ismultientitymanaged) $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
125 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .=
" AND t.fk_soc = sc.fk_soc";
126 if ($restrictonsocid && $socid) $sql .=
" AND t.fk_soc = ".$socid;
127 if ($restrictonsocid && $search_sale > 0) $sql .=
" AND t.rowid = sc.fk_soc";
129 if ($restrictonsocid && $search_sale > 0)
131 $sql .=
" AND sc.fk_user = ".$search_sale;
136 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
138 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
139 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
142 $sql .= $this->
db->order($sortfield, $sortorder);
148 $offset = $limit * $page;
150 $sql .= $this->
db->plimit($limit + 1, $offset);
153 $result = $this->
db->query($sql);
156 $num = $this->
db->num_rows($result);
160 $obj = $this->
db->fetch_object($result);
161 $bom_static =
new BOM($this->
db);
162 if ($bom_static->fetch($obj->rowid)) {
168 throw new RestException(503,
'Error when retrieve bom list');
170 if (!count($obj_ret)) {
171 throw new RestException(404,
'No bom found');
182 public function post($request_data = null)
184 if (!DolibarrApiAccess::$user->rights->bom->write) {
185 throw new RestException(401);
188 $result = $this->
_validate($request_data);
190 foreach ($request_data as $field => $value) {
191 $this->bom->$field = $value;
193 if (!$this->bom->create(DolibarrApiAccess::$user)) {
194 throw new RestException(500,
"Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
196 return $this->bom->id;
207 public function put($id, $request_data = null)
209 if (!DolibarrApiAccess::$user->rights->bom->write) {
210 throw new RestException(401);
213 $result = $this->bom->fetch($id);
215 throw new RestException(404,
'BOM not found');
219 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
222 foreach ($request_data as $field => $value) {
223 if ($field ==
'id')
continue;
224 $this->bom->$field = $value;
227 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
228 return $this->
get($id);
230 throw new RestException(500, $this->bom->error);
240 public function delete($id)
242 if (!DolibarrApiAccess::$user->rights->bom->delete) {
243 throw new RestException(401);
245 $result = $this->bom->fetch($id);
247 throw new RestException(404,
'BOM not found');
251 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
254 if (!$this->bom->delete(DolibarrApiAccess::$user))
256 throw new RestException(500,
'Error when deleting BOM : '.$this->bom->error);
262 'message' =>
'BOM deleted'
278 $object = parent::_cleanObjectDatas($object);
280 unset($object->rowid);
281 unset($object->canvas);
283 unset($object->name);
284 unset($object->lastname);
285 unset($object->firstname);
286 unset($object->civility_id);
287 unset($object->statut);
288 unset($object->state);
289 unset($object->state_id);
290 unset($object->state_code);
291 unset($object->region);
292 unset($object->region_code);
293 unset($object->country);
294 unset($object->country_id);
295 unset($object->country_code);
296 unset($object->barcode_type);
297 unset($object->barcode_type_code);
298 unset($object->barcode_type_label);
299 unset($object->barcode_type_coder);
300 unset($object->total_ht);
301 unset($object->total_tva);
302 unset($object->total_localtax1);
303 unset($object->total_localtax2);
304 unset($object->total_ttc);
305 unset($object->fk_account);
306 unset($object->comments);
307 unset($object->note);
308 unset($object->mode_reglement_id);
309 unset($object->cond_reglement_id);
310 unset($object->cond_reglement);
311 unset($object->shipping_method_id);
312 unset($object->fk_incoterms);
313 unset($object->label_incoterms);
314 unset($object->location_incoterms);
317 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
318 $nboflines = count($object->lines);
319 for ($i = 0; $i < $nboflines; $i++)
323 unset($object->lines[$i]->lines);
324 unset($object->lines[$i]->note);
342 foreach ($this->bom->fields as $field => $propfield) {
343 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1)
continue;
344 if (!isset($data[$field]))
345 throw new RestException(400,
"$field field missing");
346 $myobject[$field] = $data[$field];
__construct()
Constructor.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
post($request_data=null)
Create bom object.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
List boms.
_validate($data)
Validate fields before create or update object.
_cleanObjectDatas($object)
Clean sensible object datas.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
put($id, $request_data=null)
Update bom.