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).
")";
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);
296 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
297 throw new RestException(403);
299 $result = $this->bom->fetch(
$id);
301 throw new RestException(404,
'Bom not found');
304 $result = $this->bom->validate(DolibarrApiAccess::$user, $notrigger);
306 throw new RestException(304,
'Error nothing done. May be object is already validated');
309 throw new RestException(500,
'Error when validating BOM: '.$this->bom->error);
311 $result = $this->bom->fetch(
$id);
328 public function delete(
$id)
330 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'delete')) {
331 throw new RestException(403);
333 $result = $this->bom->fetch(
$id);
335 throw new RestException(404,
'BOM not found');
339 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
342 if (!$this->bom->delete(DolibarrApiAccess::$user)) {
343 throw new RestException(500,
'Error when deleting BOM : '.$this->bom->error);
349 'message' =>
'BOM deleted'
370 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'read')) {
371 throw new RestException(403);
374 $result = $this->bom->fetch(
$id);
376 throw new RestException(404,
'BOM not found');
380 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
382 $this->bom->getLinesArray();
384 foreach ($this->bom->lines as $line) {
408 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
409 throw new RestException(403);
412 $result = $this->bom->fetch(
$id);
414 throw new RestException(404,
'BOM not found');
418 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
421 $request_data = (object) $request_data;
423 $updateRes = $this->bom->addLine(
424 $request_data->fk_product,
426 $request_data->qty_frozen,
427 $request_data->disable_stock_change,
428 $request_data->efficiency,
429 $request_data->position,
430 $request_data->fk_bom_child,
431 $request_data->import_key,
432 $request_data->fk_unit,
433 $request_data->array_options,
434 $request_data->fk_default_workstation
437 if ($updateRes > 0) {
440 throw new RestException(500, $this->bom->error);
462 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
463 throw new RestException(403);
466 $result = $this->bom->fetch(
$id);
468 throw new RestException(404,
'BOM not found');
472 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
475 $request_data = (object) $request_data;
477 $updateRes = $this->bom->updateLine(
480 $request_data->qty_frozen,
481 $request_data->disable_stock_change,
482 $request_data->efficiency,
483 $request_data->position,
484 $request_data->import_key,
485 $request_data->fk_unit,
486 $request_data->array_options,
487 $request_data->fk_default_workstation
490 if ($updateRes > 0) {
491 $result = $this->
get(
$id);
492 unset($result->line);
517 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
518 throw new RestException(403);
521 $result = $this->bom->fetch(
$id);
523 throw new RestException(404,
'BOM not found');
527 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
531 $lineIdIsFromObject =
false;
532 foreach ($this->bom->lines as $bl) {
533 if ($bl->id == $lineid) {
534 $lineIdIsFromObject =
true;
538 if (!$lineIdIsFromObject) {
539 throw new RestException(500,
'Line to delete (rowid: '.$lineid.
') is not a line of BOM (id: '.$this->bom->id.
')');
542 $updateRes = $this->bom->deleteLine(DolibarrApiAccess::$user, $lineid);
543 if ($updateRes > 0) {
547 'message' =>
'line ' .$lineid.
' deleted'
551 throw new RestException(500, $this->bom->error);
587 unset(
$object->barcode_type_code);
588 unset(
$object->barcode_type_label);
589 unset(
$object->barcode_type_coder);
592 unset(
$object->total_localtax1);
593 unset(
$object->total_localtax2);
598 unset(
$object->mode_reglement_id);
599 unset(
$object->cond_reglement_id);
600 unset(
$object->cond_reglement);
601 unset(
$object->shipping_method_id);
603 unset(
$object->label_incoterms);
604 unset(
$object->location_incoterms);
605 unset(
$object->multicurrency_code);
606 unset(
$object->multicurrency_tx);
607 unset(
$object->multicurrency_total_ht);
608 unset(
$object->multicurrency_total_ttc);
609 unset(
$object->multicurrency_total_tva);
610 unset(
$object->multicurrency_total_localtax1);
611 unset(
$object->multicurrency_total_localtax2);
616 $nboflines = count(
$object->lines);
617 for ($i = 0; $i < $nboflines; $i++) {
620 unset(
$object->lines[$i]->lines);
621 unset(
$object->lines[$i]->note);
638 if ($data ===
null) {
642 foreach ($this->bom->fields as $field => $propfield) {
643 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || empty($propfield[
'notnull']) || $propfield[
'notnull'] != 1) {
646 if (!isset($data[$field])) {
647 throw new RestException(400,
"$field field missing");
649 $myobject[$field] = $data[$field];
661 $ref = substr($this->bom->ref, 1, 4);
662 if ($this->bom->status > BOM::STATUS_DRAFT && $ref ==
'PROV') {
663 throw new RestException(400,
"Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
666 if (strtolower($this->bom->ref) ==
'auto') {
667 if (empty($this->bom->id) && $this->bom->status == BOM::STATUS_DRAFT) {
668 $this->bom->ref =
'';
670 $res = $this->bom->fetch_product();
671 if ($res > 0 && $this->bom->product instanceof
Product) {
672 $numref = $this->bom->getNextNumRef($this->bom->product);
673 $this->bom->ref = $numref;
675 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
validate($id, $notrigger=0)
Validate BOM.
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.
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.