Merge pull request #23416 from Humml87/develop_REST_BOM_Ref_NextNum

Fix: #23415 REST BOM API - It's able to have a wrong value on the 'ref' Field
This commit is contained in:
Laurent Destailleur 2023-01-07 10:31:20 +01:00 committed by GitHub
commit 93f5c69b1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
}
}
}
}