21 use Luracast\Restler\RestException;
23 require_once DOL_DOCUMENT_ROOT.
'/bom/class/bom.class.php';
52 $this->bom =
new BOM($this->
db);
66 public function get($id)
68 if (!DolibarrApiAccess::$user->rights->bom->read) {
69 throw new RestException(401);
72 $result = $this->bom->fetch($id);
74 throw new RestException(404,
'BOM not found');
78 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
99 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
103 if (!DolibarrApiAccess::$user->rights->bom->read) {
104 throw new RestException(401);
108 $tmpobject =
new BOM($this->
db);
110 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
112 $restrictonsocid = 0;
116 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
117 $search_sale = DolibarrApiAccess::$user->id;
120 $sql =
"SELECT t.rowid";
121 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
122 $sql .=
", sc.fk_soc, sc.fk_user";
124 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" as t";
126 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
127 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
129 $sql .=
" WHERE 1 = 1";
135 if ($tmpobject->ismultientitymanaged) {
136 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
138 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
139 $sql .=
" AND t.fk_soc = sc.fk_soc";
141 if ($restrictonsocid && $socid) {
142 $sql .=
" AND t.fk_soc = ".((int) $socid);
144 if ($restrictonsocid && $search_sale > 0) {
145 $sql .=
" AND t.rowid = sc.fk_soc";
148 if ($restrictonsocid && $search_sale > 0) {
149 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
154 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
156 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
157 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
160 $sql .= $this->
db->order($sortfield, $sortorder);
165 $offset = $limit * $page;
167 $sql .= $this->
db->plimit($limit + 1, $offset);
170 $result = $this->
db->query($sql);
172 $num = $this->
db->num_rows($result);
175 $obj = $this->
db->fetch_object($result);
176 $bom_static =
new BOM($this->
db);
177 if ($bom_static->fetch($obj->rowid)) {
183 throw new RestException(503,
'Error when retrieve bom list');
185 if (!count($obj_ret)) {
186 throw new RestException(404,
'No bom found');
197 public function post($request_data =
null)
199 if (!DolibarrApiAccess::$user->rights->bom->write) {
200 throw new RestException(401);
203 $result = $this->
_validate($request_data);
205 foreach ($request_data as $field => $value) {
206 $this->bom->$field = $value;
208 if (!$this->bom->create(DolibarrApiAccess::$user)) {
209 throw new RestException(500,
"Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
211 return $this->bom->id;
222 public function put($id, $request_data =
null)
224 if (!DolibarrApiAccess::$user->rights->bom->write) {
225 throw new RestException(401);
228 $result = $this->bom->fetch($id);
230 throw new RestException(404,
'BOM not found');
234 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
237 foreach ($request_data as $field => $value) {
238 if ($field ==
'id') {
241 $this->bom->$field = $value;
244 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
245 return $this->
get($id);
247 throw new RestException(500, $this->bom->error);
257 public function delete($id)
259 if (!DolibarrApiAccess::$user->rights->bom->delete) {
260 throw new RestException(401);
262 $result = $this->bom->fetch($id);
264 throw new RestException(404,
'BOM not found');
268 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
271 if (!$this->bom->delete(DolibarrApiAccess::$user)) {
272 throw new RestException(500,
'Error when deleting BOM : '.$this->bom->error);
278 'message' =>
'BOM deleted'
294 if (!DolibarrApiAccess::$user->rights->bom->read) {
295 throw new RestException(401);
298 $result = $this->bom->fetch($id);
300 throw new RestException(404,
'BOM not found');
304 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
306 $this->bom->getLinesArray();
308 foreach ($this->bom->lines as $line) {
324 public function postLine($id, $request_data =
null)
326 if (!DolibarrApiAccess::$user->rights->bom->write) {
327 throw new RestException(401);
330 $result = $this->bom->fetch($id);
332 throw new RestException(404,
'BOM not found');
336 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
339 $request_data = (object) $request_data;
341 $updateRes = $this->bom->addLine(
342 $request_data->fk_product,
344 $request_data->qty_frozen,
345 $request_data->disable_stock_change,
346 $request_data->efficiency,
347 $request_data->position,
348 $request_data->fk_bom_child,
349 $request_data->import_key
352 if ($updateRes > 0) {
355 throw new RestException(400, $this->bom->error);
370 public function putLine($id, $lineid, $request_data =
null)
372 if (!DolibarrApiAccess::$user->rights->bom->write) {
373 throw new RestException(401);
376 $result = $this->bom->fetch($id);
378 throw new RestException(404,
'BOM not found');
382 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
385 $request_data = (object) $request_data;
387 $updateRes = $this->bom->updateLine(
390 $request_data->qty_frozen,
391 $request_data->disable_stock_change,
392 $request_data->efficiency,
393 $request_data->position,
394 $request_data->import_key
397 if ($updateRes > 0) {
398 $result = $this->
get($id);
399 unset($result->line);
422 if (!DolibarrApiAccess::$user->rights->bom->write) {
423 throw new RestException(401);
426 $result = $this->bom->fetch($id);
428 throw new RestException(404,
'BOM not found');
432 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
436 $lineIdIsFromObject =
false;
437 foreach ($this->bom->lines as $bl) {
438 if ($bl->id == $lineid) {
439 $lineIdIsFromObject =
true;
443 if (!$lineIdIsFromObject) {
444 throw new RestException(500,
'Line to delete (rowid: '.$lineid.
') is not a line of BOM (id: '.$this->bom->id.
')');
447 $updateRes = $this->bom->deleteline(DolibarrApiAccess::$user, $lineid);
448 if ($updateRes > 0) {
449 return $this->
get($id);
451 throw new RestException(405, $this->bom->error);
465 $object = parent::_cleanObjectDatas($object);
467 unset($object->rowid);
468 unset($object->canvas);
470 unset($object->name);
471 unset($object->lastname);
472 unset($object->firstname);
473 unset($object->civility_id);
474 unset($object->statut);
475 unset($object->state);
476 unset($object->state_id);
477 unset($object->state_code);
478 unset($object->region);
479 unset($object->region_code);
480 unset($object->country);
481 unset($object->country_id);
482 unset($object->country_code);
483 unset($object->barcode_type);
484 unset($object->barcode_type_code);
485 unset($object->barcode_type_label);
486 unset($object->barcode_type_coder);
487 unset($object->total_ht);
488 unset($object->total_tva);
489 unset($object->total_localtax1);
490 unset($object->total_localtax2);
491 unset($object->total_ttc);
492 unset($object->fk_account);
493 unset($object->comments);
494 unset($object->note);
495 unset($object->mode_reglement_id);
496 unset($object->cond_reglement_id);
497 unset($object->cond_reglement);
498 unset($object->shipping_method_id);
499 unset($object->fk_incoterms);
500 unset($object->label_incoterms);
501 unset($object->location_incoterms);
504 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
505 $nboflines = count($object->lines);
506 for ($i = 0; $i < $nboflines; $i++) {
509 unset($object->lines[$i]->lines);
510 unset($object->lines[$i]->note);
528 foreach ($this->bom->fields as $field => $propfield) {
529 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1) {
532 if (!isset($data[$field])) {
533 throw new RestException(400,
"$field field missing");
535 $myobject[$field] = $data[$field];
put($id, $request_data=null)
Update bom.
_cleanObjectDatas($object)
Clean sensible object datas.
putLine($id, $lineid, $request_data=null)
Update a line to given BOM.
post($request_data=null)
Create bom object.
getLines($id)
Get lines of an BOM.
__construct()
Constructor.
deleteLine($id, $lineid)
Delete a line to given BOM.
postLine($id, $request_data=null)
Add a line to given BOM.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List boms.
_validate($data)
Validate fields before create or update object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
$conf db
API class for accounts.