22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT.
'/bom/class/bom.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
55 $this->bom =
new BOM($this->db);
73 public function get(
$id)
75 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'read')) {
76 throw new RestException(403);
79 $result = $this->bom->fetch(
$id);
81 throw new RestException(404,
'BOM not found');
85 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
111 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
113 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'read')) {
114 throw new RestException(403);
118 $tmpobject =
new BOM($this->db);
120 $socid = DolibarrApiAccess::$user->socid ?:
'';
122 $restrictonsocid = 0;
126 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
127 $search_sale = DolibarrApiAccess::$user->id;
130 $sql =
"SELECT t.rowid";
131 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t";
132 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
133 $sql .=
" WHERE 1 = 1";
134 if ($tmpobject->ismultientitymanaged) {
135 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
137 if ($restrictonsocid && $socid) {
138 $sql .=
" AND t.fk_soc = ".((int) $socid);
141 if ($search_sale && $search_sale !=
'-1') {
142 if ($search_sale == -2) {
143 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
144 } elseif ($search_sale > 0) {
145 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
152 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
156 $sql .= $this->db->order($sortfield, $sortorder);
161 $offset = $limit * $page;
163 $sql .= $this->db->plimit($limit + 1, $offset);
166 $result = $this->db->query($sql);
169 $num = $this->db->num_rows($result);
170 $min = min($num, ($limit <= 0 ? $num : $limit));
172 $obj = $this->db->fetch_object($result);
173 $bom_static =
new BOM($this->db);
174 if ($bom_static->fetch($obj->rowid)) {
180 throw new RestException(503,
'Error when retrieve bom list');
197 public function post($request_data =
null)
199 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
200 throw new RestException(403);
203 $result = $this->
_validate($request_data);
205 foreach ($request_data as $field => $value) {
206 if ($field ===
'caller') {
208 $this->bom->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
212 $this->bom->$field = $this->
_checkValForAPI($field, $value, $this->bom);
217 if (!$this->bom->create(DolibarrApiAccess::$user)) {
218 throw new RestException(500,
"Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
220 return $this->bom->id;
238 public function put(
$id, $request_data =
null)
240 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
241 throw new RestException(403);
244 $result = $this->bom->fetch(
$id);
246 throw new RestException(404,
'BOM not found');
250 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
253 foreach ($request_data as $field => $value) {
254 if ($field ==
'id') {
257 if ($field ===
'caller') {
259 $this->bom->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
263 if ($field ==
'array_options' && is_array($value)) {
264 foreach ($value as $index => $val) {
269 $this->bom->$field = $this->
_checkValForAPI($field, $value, $this->bom);
274 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
275 return $this->
get(
$id);
277 throw new RestException(500, $this->bom->error);
297 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
298 throw new RestException(403);
300 $result = $this->bom->fetch(
$id);
302 throw new RestException(404,
'Bom not found');
305 $result = $this->bom->validate(DolibarrApiAccess::$user, $notrigger);
307 throw new RestException(304,
'Error nothing done. May be object is already validated');
310 throw new RestException(500,
'Error when validating BOM: '.$this->bom->error);
312 $result = $this->bom->fetch(
$id);
329 public function delete(
$id)
331 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'delete')) {
332 throw new RestException(403);
334 $result = $this->bom->fetch(
$id);
336 throw new RestException(404,
'BOM not found');
340 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
343 if (!$this->bom->delete(DolibarrApiAccess::$user)) {
344 throw new RestException(500,
'Error when deleting BOM : '.$this->bom->error);
350 'message' =>
'BOM deleted'
371 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'read')) {
372 throw new RestException(403);
375 $result = $this->bom->fetch(
$id);
377 throw new RestException(404,
'BOM not found');
381 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
383 $this->bom->getLinesArray();
385 foreach ($this->bom->lines as $line) {
409 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
410 throw new RestException(403);
413 $result = $this->bom->fetch(
$id);
415 throw new RestException(404,
'BOM not found');
419 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
422 $request_data = (object) $request_data;
424 $updateRes = $this->bom->addLine(
425 $request_data->fk_product,
427 $request_data->qty_frozen,
428 $request_data->disable_stock_change,
429 $request_data->efficiency,
430 $request_data->position,
431 $request_data->fk_bom_child,
432 $request_data->import_key,
433 $request_data->fk_unit,
434 $request_data->array_options,
435 $request_data->fk_default_workstation
438 if ($updateRes > 0) {
441 throw new RestException(500, $this->bom->error);
463 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
464 throw new RestException(403);
467 $result = $this->bom->fetch(
$id);
469 throw new RestException(404,
'BOM not found');
473 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
476 $request_data = (object) $request_data;
478 $updateRes = $this->bom->updateLine(
481 $request_data->qty_frozen,
482 $request_data->disable_stock_change,
483 $request_data->efficiency,
484 $request_data->position,
485 $request_data->import_key,
486 $request_data->fk_unit,
487 $request_data->array_options,
488 $request_data->fk_default_workstation
491 if ($updateRes > 0) {
492 $result = $this->
get(
$id);
493 unset($result->line);
518 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
519 throw new RestException(403);
522 $result = $this->bom->fetch(
$id);
524 throw new RestException(404,
'BOM not found');
528 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
532 $lineIdIsFromObject =
false;
533 foreach ($this->bom->lines as $bl) {
534 if ($bl->id == $lineid) {
535 $lineIdIsFromObject =
true;
539 if (!$lineIdIsFromObject) {
540 throw new RestException(500,
'Line to delete (rowid: '.$lineid.
') is not a line of BOM (id: '.$this->bom->id.
')');
543 $updateRes = $this->bom->deleteLine(DolibarrApiAccess::$user, $lineid);
544 if ($updateRes > 0) {
548 'message' =>
'line ' .$lineid.
' deleted'
552 throw new RestException(500, $this->bom->error);
589 unset(
$object->barcode_type_code);
590 unset(
$object->barcode_type_label);
591 unset(
$object->barcode_type_coder);
592 unset(
$object->demand_reason_id);
593 unset(
$object->transport_mode_id);
594 unset(
$object->shipping_method);
596 unset(
$object->actiontypecode);
601 unset(
$object->total_localtax1);
602 unset(
$object->total_localtax2);
608 unset(
$object->totalpaid_multicurrency);
609 unset(
$object->deposit_percent);
610 unset(
$object->cond_reglement_supplier_id);
615 unset(
$object->mode_reglement_id);
616 unset(
$object->cond_reglement_id);
617 unset(
$object->cond_reglement);
618 unset(
$object->shipping_method_id);
620 unset(
$object->label_incoterms);
621 unset(
$object->location_incoterms);
622 unset(
$object->multicurrency_code);
623 unset(
$object->multicurrency_tx);
624 unset(
$object->multicurrency_total_ht);
625 unset(
$object->multicurrency_total_ttc);
626 unset(
$object->multicurrency_total_tva);
627 unset(
$object->multicurrency_total_localtax1);
628 unset(
$object->multicurrency_total_localtax2);
633 $nboflines = count(
$object->lines);
634 for ($i = 0; $i < $nboflines; $i++) {
637 unset(
$object->lines[$i]->lines);
638 unset(
$object->lines[$i]->note);
655 if ($data ===
null) {
659 foreach ($this->bom->fields as $field => $propfield) {
660 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || empty($propfield[
'notnull']) || $propfield[
'notnull'] != 1) {
663 if (!isset($data[$field])) {
664 throw new RestException(400,
"$field field missing");
666 $myobject[$field] = $data[$field];
678 $ref = substr($this->bom->ref, 1, 4);
679 if ($this->bom->status > BOM::STATUS_DRAFT && $ref ==
'PROV') {
680 throw new RestException(400,
"Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
683 if (strtolower($this->bom->ref) ==
'auto') {
684 if (empty($this->bom->id) && $this->bom->status == BOM::STATUS_DRAFT) {
685 $this->bom->ref =
'';
687 $res = $this->bom->fetch_product();
688 if ($res > 0 && $this->bom->product instanceof
Product) {
689 $numref = $this->bom->getNextNumRef($this->bom->product);
690 $this->bom->ref = $numref;
692 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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
Class to manage products or services.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.