22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT.
'/bom/class/bom.class.php';
54 $this->bom =
new BOM($this->db);
72 public function get(
$id)
74 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'read')) {
75 throw new RestException(403);
78 $result = $this->bom->fetch(
$id);
80 throw new RestException(404,
'BOM not found');
84 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
110 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
112 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'read')) {
113 throw new RestException(403);
117 $tmpobject =
new BOM($this->db);
119 $socid = DolibarrApiAccess::$user->socid ?:
'';
121 $restrictonsocid = 0;
125 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
126 $search_sale = DolibarrApiAccess::$user->id;
129 $sql =
"SELECT t.rowid";
130 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t";
131 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
132 $sql .=
" WHERE 1 = 1";
133 if ($tmpobject->ismultientitymanaged) {
134 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
136 if ($restrictonsocid && $socid) {
137 $sql .=
" AND t.fk_soc = ".((int) $socid);
140 if ($search_sale && $search_sale !=
'-1') {
141 if ($search_sale == -2) {
142 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
143 } elseif ($search_sale > 0) {
144 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
149 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
151 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
155 $sql .= $this->db->order($sortfield, $sortorder);
160 $offset = $limit * $page;
162 $sql .= $this->db->plimit($limit + 1, $offset);
165 $result = $this->db->query($sql);
168 $num = $this->db->num_rows($result);
169 $min = min($num, ($limit <= 0 ? $num : $limit));
171 $obj = $this->db->fetch_object($result);
172 $bom_static =
new BOM($this->db);
173 if ($bom_static->fetch($obj->rowid)) {
179 throw new RestException(503,
'Error when retrieve bom list');
196 public function post($request_data =
null)
198 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
199 throw new RestException(403);
202 $result = $this->
_validate($request_data);
204 foreach ($request_data as $field => $value) {
205 if ($field ===
'caller') {
207 $this->bom->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
211 $this->bom->$field = $this->
_checkValForAPI($field, $value, $this->bom);
216 if (!$this->bom->create(DolibarrApiAccess::$user)) {
217 throw new RestException(500,
"Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
219 return $this->bom->id;
237 public function put(
$id, $request_data =
null)
239 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
240 throw new RestException(403);
243 $result = $this->bom->fetch(
$id);
245 throw new RestException(404,
'BOM not found');
249 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
252 foreach ($request_data as $field => $value) {
253 if ($field ==
'id') {
256 if ($field ===
'caller') {
258 $this->bom->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
262 if ($field ==
'array_options' && is_array($value)) {
263 foreach ($value as $index => $val) {
268 $this->bom->$field = $this->
_checkValForAPI($field, $value, $this->bom);
273 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
274 return $this->
get(
$id);
276 throw new RestException(500, $this->bom->error);
292 public function delete(
$id)
294 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'delete')) {
295 throw new RestException(403);
297 $result = $this->bom->fetch(
$id);
299 throw new RestException(404,
'BOM not found');
303 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
306 if (!$this->bom->delete(DolibarrApiAccess::$user)) {
307 throw new RestException(500,
'Error when deleting BOM : '.$this->bom->error);
313 'message' =>
'BOM deleted'
334 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'read')) {
335 throw new RestException(403);
338 $result = $this->bom->fetch(
$id);
340 throw new RestException(404,
'BOM not found');
344 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
346 $this->bom->getLinesArray();
348 foreach ($this->bom->lines as $line) {
372 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
373 throw new RestException(403);
376 $result = $this->bom->fetch(
$id);
378 throw new RestException(404,
'BOM not found');
382 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
385 $request_data = (object) $request_data;
387 $updateRes = $this->bom->addLine(
388 $request_data->fk_product,
390 $request_data->qty_frozen,
391 $request_data->disable_stock_change,
392 $request_data->efficiency,
393 $request_data->position,
394 $request_data->fk_bom_child,
395 $request_data->import_key,
396 $request_data->fk_unit,
397 $request_data->array_options,
398 $request_data->fk_default_workstation
401 if ($updateRes > 0) {
404 throw new RestException(500, $this->bom->error);
426 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
427 throw new RestException(403);
430 $result = $this->bom->fetch(
$id);
432 throw new RestException(404,
'BOM not found');
436 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
439 $request_data = (object) $request_data;
441 $updateRes = $this->bom->updateLine(
444 $request_data->qty_frozen,
445 $request_data->disable_stock_change,
446 $request_data->efficiency,
447 $request_data->position,
448 $request_data->import_key,
449 $request_data->fk_unit,
450 $request_data->array_options,
451 $request_data->fk_default_workstation
454 if ($updateRes > 0) {
455 $result = $this->
get(
$id);
456 unset($result->line);
481 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
482 throw new RestException(403);
485 $result = $this->bom->fetch(
$id);
487 throw new RestException(404,
'BOM not found');
491 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 $lineIdIsFromObject =
false;
496 foreach ($this->bom->lines as $bl) {
497 if ($bl->id == $lineid) {
498 $lineIdIsFromObject =
true;
502 if (!$lineIdIsFromObject) {
503 throw new RestException(500,
'Line to delete (rowid: '.$lineid.
') is not a line of BOM (id: '.$this->bom->id.
')');
506 $updateRes = $this->bom->deleteLine(DolibarrApiAccess::$user, $lineid);
507 if ($updateRes > 0) {
511 'message' =>
'line ' .$lineid.
' deleted'
515 throw new RestException(500, $this->bom->error);
551 unset(
$object->barcode_type_code);
552 unset(
$object->barcode_type_label);
553 unset(
$object->barcode_type_coder);
556 unset(
$object->total_localtax1);
557 unset(
$object->total_localtax2);
562 unset(
$object->mode_reglement_id);
563 unset(
$object->cond_reglement_id);
564 unset(
$object->cond_reglement);
565 unset(
$object->shipping_method_id);
567 unset(
$object->label_incoterms);
568 unset(
$object->location_incoterms);
569 unset(
$object->multicurrency_code);
570 unset(
$object->multicurrency_tx);
571 unset(
$object->multicurrency_total_ht);
572 unset(
$object->multicurrency_total_ttc);
573 unset(
$object->multicurrency_total_tva);
574 unset(
$object->multicurrency_total_localtax1);
575 unset(
$object->multicurrency_total_localtax2);
580 $nboflines = count(
$object->lines);
581 for ($i = 0; $i < $nboflines; $i++) {
584 unset(
$object->lines[$i]->lines);
585 unset(
$object->lines[$i]->note);
602 if ($data ===
null) {
606 foreach ($this->bom->fields as $field => $propfield) {
607 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || empty($propfield[
'notnull']) || $propfield[
'notnull'] != 1) {
610 if (!isset($data[$field])) {
611 throw new RestException(400,
"$field field missing");
613 $myobject[$field] = $data[$field];
625 $ref = substr($this->bom->ref, 1, 4);
626 if ($this->bom->status > BOM::STATUS_DRAFT && $ref ==
'PROV') {
627 throw new RestException(400,
"Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
630 if (strtolower($this->bom->ref) ==
'auto') {
631 if (empty($this->bom->id) && $this->bom->status == BOM::STATUS_DRAFT) {
632 $this->bom->ref =
'';
634 $res = $this->bom->fetch_product();
635 if ($res > 0 && $this->bom->product instanceof
Product) {
636 $numref = $this->bom->getNextNumRef($this->bom->product);
637 $this->bom->ref = $numref;
639 throw new RestException(400,
"Error when generating automatic increment on the 'ref' field.");
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
checkRefNumbering()
Validate the ref field and get the next Number if it's necessary.
put($id, $request_data=null)
Update bom.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List boms.
__construct()
Constructor.
deleteLine($id, $lineid)
Delete a line to given BOM.
postLine($id, $request_data=null)
Add a line to given BOM.
_validate($data)
Validate fields before create or update object.
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_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 products or services.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.