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);
167 $num = $this->db->num_rows($result);
170 $obj = $this->db->fetch_object($result);
171 $bom_static =
new BOM($this->db);
172 if ($bom_static->fetch($obj->rowid)) {
178 throw new RestException(503,
'Error when retrieve bom list');
195 public function post($request_data =
null)
197 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
198 throw new RestException(403);
201 $result = $this->
_validate($request_data);
203 foreach ($request_data as $field => $value) {
204 if ($field ===
'caller') {
206 $this->bom->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
210 $this->bom->$field = $this->
_checkValForAPI($field, $value, $this->bom);
215 if (!$this->bom->create(DolibarrApiAccess::$user)) {
216 throw new RestException(500,
"Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
218 return $this->bom->id;
236 public function put(
$id, $request_data =
null)
238 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
239 throw new RestException(403);
242 $result = $this->bom->fetch(
$id);
244 throw new RestException(404,
'BOM not found');
248 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
251 foreach ($request_data as $field => $value) {
252 if ($field ==
'id') {
255 if ($field ===
'caller') {
257 $this->bom->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
261 if ($field ==
'array_options' && is_array($value)) {
262 foreach ($value as $index => $val) {
263 $this->bom->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->bom);
267 $this->bom->$field = $this->
_checkValForAPI($field, $value, $this->bom);
272 if ($this->bom->update(DolibarrApiAccess::$user) > 0) {
273 return $this->
get(
$id);
275 throw new RestException(500, $this->bom->error);
291 public function delete(
$id)
293 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'delete')) {
294 throw new RestException(403);
296 $result = $this->bom->fetch(
$id);
298 throw new RestException(404,
'BOM not found');
302 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
305 if (!$this->bom->delete(DolibarrApiAccess::$user)) {
306 throw new RestException(500,
'Error when deleting BOM : '.$this->bom->error);
312 'message' =>
'BOM deleted'
333 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'read')) {
334 throw new RestException(403);
337 $result = $this->bom->fetch(
$id);
339 throw new RestException(404,
'BOM not found');
343 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
345 $this->bom->getLinesArray();
347 foreach ($this->bom->lines as $line) {
371 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
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);
384 $request_data = (object) $request_data;
386 $updateRes = $this->bom->addLine(
387 $request_data->fk_product,
389 $request_data->qty_frozen,
390 $request_data->disable_stock_change,
391 $request_data->efficiency,
392 $request_data->position,
393 $request_data->fk_bom_child,
394 $request_data->import_key,
395 $request_data->fk_unit,
396 $request_data->array_options,
397 $request_data->fk_default_workstation
400 if ($updateRes > 0) {
403 throw new RestException(500, $this->bom->error);
425 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
426 throw new RestException(403);
429 $result = $this->bom->fetch(
$id);
431 throw new RestException(404,
'BOM not found');
435 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
438 $request_data = (object) $request_data;
440 $updateRes = $this->bom->updateLine(
443 $request_data->qty_frozen,
444 $request_data->disable_stock_change,
445 $request_data->efficiency,
446 $request_data->position,
447 $request_data->import_key,
448 $request_data->fk_unit,
449 $request_data->array_options,
450 $request_data->fk_default_workstation
453 if ($updateRes > 0) {
454 $result = $this->
get(
$id);
455 unset($result->line);
480 if (!DolibarrApiAccess::$user->hasRight(
'bom',
'write')) {
481 throw new RestException(403);
484 $result = $this->bom->fetch(
$id);
486 throw new RestException(404,
'BOM not found');
490 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
494 $lineIdIsFromObject =
false;
495 foreach ($this->bom->lines as $bl) {
496 if ($bl->id == $lineid) {
497 $lineIdIsFromObject =
true;
501 if (!$lineIdIsFromObject) {
502 throw new RestException(500,
'Line to delete (rowid: '.$lineid.
') is not a line of BOM (id: '.$this->bom->id.
')');
505 $updateRes = $this->bom->deleteLine(DolibarrApiAccess::$user, $lineid);
506 if ($updateRes > 0) {
510 'message' =>
'line ' .$lineid.
' deleted'
514 throw new RestException(500, $this->bom->error);
547 unset(
$object->barcode_type_code);
548 unset(
$object->barcode_type_label);
549 unset(
$object->barcode_type_coder);
552 unset(
$object->total_localtax1);
553 unset(
$object->total_localtax2);
558 unset(
$object->mode_reglement_id);
559 unset(
$object->cond_reglement_id);
560 unset(
$object->cond_reglement);
561 unset(
$object->shipping_method_id);
563 unset(
$object->label_incoterms);
564 unset(
$object->location_incoterms);
565 unset(
$object->multicurrency_code);
566 unset(
$object->multicurrency_tx);
567 unset(
$object->multicurrency_total_ht);
568 unset(
$object->multicurrency_total_ttc);
569 unset(
$object->multicurrency_total_tva);
570 unset(
$object->multicurrency_total_localtax1);
571 unset(
$object->multicurrency_total_localtax2);
576 $nboflines = count(
$object->lines);
577 for ($i = 0; $i < $nboflines; $i++) {
580 unset(
$object->lines[$i]->lines);
581 unset(
$object->lines[$i]->note);
598 if ($data ===
null) {
602 foreach ($this->bom->fields as $field => $propfield) {
603 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || empty($propfield[
'notnull']) || $propfield[
'notnull'] != 1) {
606 if (!isset($data[$field])) {
607 throw new RestException(400,
"$field field missing");
609 $myobject[$field] = $data[$field];
621 $ref = substr($this->bom->ref, 1, 4);
622 if ($this->bom->status > 0 && $ref ==
'PROV') {
623 throw new RestException(400,
"Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field.");
626 if (strtolower($this->bom->ref) ==
'auto') {
627 if (empty($this->bom->id) && $this->bom->status == 0) {
628 $this->bom->ref =
'';
630 $this->bom->fetch_product();
631 $numref = $this->bom->getNextNumRef($this->bom->product);
632 $this->bom->ref = $numref;
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $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.
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.
_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.
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.