From 78b12c8298ca412e5ddd1c5d193fccdd9ee7fd41 Mon Sep 17 00:00:00 2001 From: Christian Humpel Date: Wed, 4 Jan 2023 16:33:39 +0100 Subject: [PATCH 1/6] 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; + } + } } From 7d5dec6bb7f8fcc37915ede68a3d9dde05e40ab0 Mon Sep 17 00:00:00 2001 From: Christian Humpel Date: Wed, 4 Jan 2023 16:39:50 +0100 Subject: [PATCH 2/6] Remove comment. --- htdocs/bom/class/api_boms.class.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/htdocs/bom/class/api_boms.class.php b/htdocs/bom/class/api_boms.class.php index 36cc2850db0..37008475160 100644 --- a/htdocs/bom/class/api_boms.class.php +++ b/htdocs/bom/class/api_boms.class.php @@ -206,8 +206,6 @@ class Boms extends DolibarrApi $this->bom->$field = $value; } - // Check Ref Numbering - // We check that object has a temporary ref $this->CheckRefNumbering(); if (!$this->bom->create(DolibarrApiAccess::$user)) { @@ -246,8 +244,6 @@ 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) { From b72a1c9964286e7735fd07887d6324807ccc8e39 Mon Sep 17 00:00:00 2001 From: Christian Humpel Date: Wed, 4 Jan 2023 17:41:26 +0100 Subject: [PATCH 3/6] Rename Methode "checkRefNumbering". --- htdocs/bom/class/api_boms.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/bom/class/api_boms.class.php b/htdocs/bom/class/api_boms.class.php index 37008475160..e7376dfbdfa 100644 --- a/htdocs/bom/class/api_boms.class.php +++ b/htdocs/bom/class/api_boms.class.php @@ -206,7 +206,7 @@ class Boms extends DolibarrApi $this->bom->$field = $value; } - $this->CheckRefNumbering(); + $this->checkRefNumbering(); if (!$this->bom->create(DolibarrApiAccess::$user)) { throw new RestException(500, "Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors)); @@ -244,7 +244,7 @@ class Boms extends DolibarrApi $this->bom->$field = $value; } - $this->CheckRefNumbering(); + $this->checkRefNumbering(); if ($this->bom->update(DolibarrApiAccess::$user) > 0) { return $this->get($id); @@ -547,7 +547,7 @@ class Boms extends DolibarrApi * * @return void */ - private function CheckRefNumbering(): void + private function checkRefNumbering(): void { $ref = substr($this->bom->ref, 1, 4); if ($this->bom->status > 0 && (empty($this->bom->ref) || $ref == 'PROV')) { From de4986a57047b4cef6ba52be0318c8c80c170f8b Mon Sep 17 00:00:00 2001 From: Christian Humpel Date: Wed, 4 Jan 2023 18:14:58 +0100 Subject: [PATCH 4/6] integrate auto increment on 'auto' keyword. --- htdocs/bom/class/api_boms.class.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/htdocs/bom/class/api_boms.class.php b/htdocs/bom/class/api_boms.class.php index e7376dfbdfa..302054ceb86 100644 --- a/htdocs/bom/class/api_boms.class.php +++ b/htdocs/bom/class/api_boms.class.php @@ -531,7 +531,7 @@ class Boms extends DolibarrApi { $myobject = array(); foreach ($this->bom->fields as $field => $propfield) { - if (in_array($field, array('rowid', 'entity', 'ref', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) { + if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) { continue; // Not a mandatory field } if (!isset($data[$field])) { @@ -550,10 +550,18 @@ class Boms extends DolibarrApi 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; + 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 ($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; + } } } } From 18550211c8179b29a4159a5f05abf1b1434a414c Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Wed, 4 Jan 2023 17:32:30 +0000 Subject: [PATCH 5/6] Fixing style errors. --- htdocs/bom/class/api_boms.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/bom/class/api_boms.class.php b/htdocs/bom/class/api_boms.class.php index 302054ceb86..e0e6cfd69dd 100644 --- a/htdocs/bom/class/api_boms.class.php +++ b/htdocs/bom/class/api_boms.class.php @@ -554,8 +554,8 @@ class Boms extends DolibarrApi throw new RestException(400, "Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field."); } - if ($this->bom->ref == 'auto'){ - if (empty($this->bom->id) && $this->bom->status == 0){ + if ($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(); From 5b2c2335b00e095814073989c2cfb35c95d53787 Mon Sep 17 00:00:00 2001 From: Christian Humpel Date: Thu, 5 Jan 2023 11:55:37 +0100 Subject: [PATCH 6/6] strtolower for keyword 'auto' --- htdocs/bom/class/api_boms.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/bom/class/api_boms.class.php b/htdocs/bom/class/api_boms.class.php index e0e6cfd69dd..e199650be30 100644 --- a/htdocs/bom/class/api_boms.class.php +++ b/htdocs/bom/class/api_boms.class.php @@ -554,7 +554,7 @@ class Boms extends DolibarrApi throw new RestException(400, "Wrong naming scheme '(PROV%)' is only allowed on 'DRAFT' status. For automatic increment use 'auto' on the 'ref' field."); } - if ($this->bom->ref == 'auto') { + 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 {