Fix: REST MO API - It's able to have a wrong value on the 'ref' Field

This commit is contained in:
Christian Humpel 2023-01-08 21:43:12 +01:00
parent 3e12a8ab21
commit f8aa861a77
2 changed files with 29 additions and 0 deletions

View File

@ -203,6 +203,9 @@ class Mos extends DolibarrApi
foreach ($request_data as $field => $value) {
$this->mo->$field = $value;
}
$this->checkRefNumbering();
if (!$this->mo->create(DolibarrApiAccess::$user)) {
throw new RestException(500, "Error creating MO", array_merge(array($this->mo->error), $this->mo->errors));
}
@ -239,6 +242,8 @@ class Mos extends DolibarrApi
$this->mo->$field = $value;
}
$this->checkRefNumbering();
if ($this->mo->update(DolibarrApiAccess::$user) > 0) {
return $this->get($id);
} else {
@ -723,4 +728,27 @@ class Mos 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->mo->ref, 1, 4);
if ($this->mo->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->mo->ref) == 'auto') {
if (empty($this->mo->id) && $this->mo->status == 0) {
$this->mo->ref = ''; // 'ref' will auto incremented with '(PROV' + newID + ')'
} else {
$this->mo->fetch_product();
$numref = $this->mo->getNextNumRef($this->mo->product);
$this->mo->ref = $numref;
}
}
}
}

View File

@ -277,6 +277,7 @@ class Mo extends CommonObject
if ($this->fk_bom > 0) {
// If there is a nown BOM, we force the type of MO to the type of BOM
include_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
$tmpbom = new BOM($this->db);
$tmpbom->fetch($this->fk_bom);