From f8aa861a7785e76bb43298358910a321815eb232 Mon Sep 17 00:00:00 2001 From: Christian Humpel Date: Sun, 8 Jan 2023 21:43:12 +0100 Subject: [PATCH] Fix: REST MO API - It's able to have a wrong value on the 'ref' Field --- htdocs/mrp/class/api_mos.class.php | 28 ++++++++++++++++++++++++++++ htdocs/mrp/class/mo.class.php | 1 + 2 files changed, 29 insertions(+) diff --git a/htdocs/mrp/class/api_mos.class.php b/htdocs/mrp/class/api_mos.class.php index 48396025957..794ab3d5378 100644 --- a/htdocs/mrp/class/api_mos.class.php +++ b/htdocs/mrp/class/api_mos.class.php @@ -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; + } + } + } } diff --git a/htdocs/mrp/class/mo.class.php b/htdocs/mrp/class/mo.class.php index c977a14d9ca..223605bffe3 100644 --- a/htdocs/mrp/class/mo.class.php +++ b/htdocs/mrp/class/mo.class.php @@ -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);