diff --git a/htdocs/bom/class/api_boms.class.php b/htdocs/bom/class/api_boms.class.php index fb7d175a229..e199650be30 100644 --- a/htdocs/bom/class/api_boms.class.php +++ b/htdocs/bom/class/api_boms.class.php @@ -205,6 +205,9 @@ class Boms extends DolibarrApi foreach ($request_data as $field => $value) { $this->bom->$field = $value; } + + $this->checkRefNumbering(); + if (!$this->bom->create(DolibarrApiAccess::$user)) { throw new RestException(500, "Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors)); } @@ -241,6 +244,8 @@ class Boms extends DolibarrApi $this->bom->$field = $value; } + $this->checkRefNumbering(); + if ($this->bom->update(DolibarrApiAccess::$user) > 0) { return $this->get($id); } else { @@ -536,4 +541,27 @@ class Boms extends DolibarrApi } return $myobject; } + + /** + * Validate the ref field and get the next Number if it's necessary. + * + * @return void + */ + private function checkRefNumbering(): void + { + $ref = substr($this->bom->ref, 1, 4); + if ($this->bom->status > 0 && $ref == 'PROV') { + throw new RestException(400, "Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field."); + } + + if (strtolower($this->bom->ref) == 'auto') { + if (empty($this->bom->id) && $this->bom->status == 0) { + $this->bom->ref = ''; // 'ref' will auto incremented with '(PROV' + newID + ')' + } else { + $this->bom->fetch_product(); + $numref = $this->bom->getNextNumRef($this->bom->product); + $this->bom->ref = $numref; + } + } + } }