From 78b12c8298ca412e5ddd1c5d193fccdd9ee7fd41 Mon Sep 17 00:00:00 2001 From: Christian Humpel Date: Wed, 4 Jan 2023 16:33:39 +0100 Subject: [PATCH] Fix: #23415 correct numbering on 'ref'. --- htdocs/bom/class/api_boms.class.php | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/htdocs/bom/class/api_boms.class.php b/htdocs/bom/class/api_boms.class.php index fb7d175a229..36cc2850db0 100644 --- a/htdocs/bom/class/api_boms.class.php +++ b/htdocs/bom/class/api_boms.class.php @@ -205,6 +205,11 @@ class Boms extends DolibarrApi foreach ($request_data as $field => $value) { $this->bom->$field = $value; } + + // Check Ref Numbering + // We check that object has a temporary ref + $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 +246,10 @@ class Boms extends DolibarrApi $this->bom->$field = $value; } + // Check Ref Numbering + // We check that object has a temporary ref + $this->CheckRefNumbering(); + if ($this->bom->update(DolibarrApiAccess::$user) > 0) { return $this->get($id); } else { @@ -526,7 +535,7 @@ class Boms extends DolibarrApi { $myobject = array(); foreach ($this->bom->fields as $field => $propfield) { - if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) { + if (in_array($field, array('rowid', 'entity', 'ref', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) { continue; // Not a mandatory field } if (!isset($data[$field])) { @@ -536,4 +545,19 @@ 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 && (empty($this->bom->ref) || $ref == 'PROV')) { + $this->bom->fetch_product(); + $numref = $this->bom->getNextNumRef($this->bom->product); + $this->bom->ref = $numref; + } + } }