From e1bf5a5e6b53e7ac6da8453c916293d1c8d1e619 Mon Sep 17 00:00:00 2001 From: Gauthier PC portable 024 Date: Thu, 1 Sep 2022 17:16:59 +0200 Subject: [PATCH 1/6] FIX : Recursively retrieves all parent bom in the tree that leads to the $bom_id bom to check if we're allowed to add bom as bomline --- htdocs/bom/bom_card.php | 10 ++++++++++ htdocs/bom/class/bom.class.php | 28 ++++++++++++++++++++++++++++ htdocs/langs/en_US/mrp.lang | 1 + htdocs/langs/fr_FR/mrp.lang | 1 + 4 files changed, 40 insertions(+) diff --git a/htdocs/bom/bom_card.php b/htdocs/bom/bom_card.php index ee619a589e3..11d2b3b4e31 100644 --- a/htdocs/bom/bom_card.php +++ b/htdocs/bom/bom_card.php @@ -180,6 +180,16 @@ if (empty($reshook)) { $error++; } + // We check if we're allowed to add this bom + $TParentBom=array(); + $object->getParentBomTreeRecursive($TParentBom, $object->id); + if($bom_child_id > 0 && !empty($TParentBom) && in_array($bom_child_id, $TParentBom)) { + $n_child = new BOM($db); + $n_child->fetch($bom_child_id); + setEventMessages($langs->transnoentities('BomCantAddChildBom', $n_child->getNomUrl(1), $object->getNomUrl(1)), null, 'errors'); + $error++; + } + if (!$error) { $bomline = new BOMLine($db); $bomline->fk_bom = $id; diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index 8adcac855c2..e178c76c024 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -1147,6 +1147,34 @@ class BOM extends CommonObject } } } + + /** + * Recursively retrieves all parent bom in the tree that leads to the $bom_id bom + * + * @param array $TParentBom We put all found parent bom in $TParentBom + * @param int $bom_id ID of bom from which we want to get parent bom ids + * @return void + */ + public function getParentBomTreeRecursive(&$TParentBom, $bom_id) { + + $sql = 'SELECT l.fk_bom, b.label + FROM '.MAIN_DB_PREFIX.'bom_bomline l + INNER JOIN '.MAIN_DB_PREFIX.$this->table_element.' b ON b.rowid = l.fk_bom + WHERE fk_bom_child = '.$bom_id; + + $resql = $this->db->query($sql); + if(!empty($resql)) { + + while($res = $this->db->fetch_object($resql)) { + + $TParentBom[$res->fk_bom] = $res->fk_bom; + $this->getParentBomTreeRecursive($TParentBom, $res->fk_bom); + + } + + } + + } } diff --git a/htdocs/langs/en_US/mrp.lang b/htdocs/langs/en_US/mrp.lang index 7f29b774b29..9e1c8bb64ce 100644 --- a/htdocs/langs/en_US/mrp.lang +++ b/htdocs/langs/en_US/mrp.lang @@ -112,3 +112,4 @@ MOAndLines=Manufacturing Orders and lines MoChildGenerate=Generate Child Mo ParentMo=MO Parent MOChild=MO Child +BomCantAddChildBom=The nomenclature %s is already present in the tree leading to the nomenclature %s diff --git a/htdocs/langs/fr_FR/mrp.lang b/htdocs/langs/fr_FR/mrp.lang index e65244b584f..709ff68b963 100644 --- a/htdocs/langs/fr_FR/mrp.lang +++ b/htdocs/langs/fr_FR/mrp.lang @@ -112,3 +112,4 @@ MOAndLines=Ordres de fabrication et lignes MoChildGenerate=Generate Child Mo ParentMo=MO Parent MOChild=MO Child +BomCantAddChildBom=La nomenclature %s est déjà présente dans l'arborescence qui mène à la nomenclature %s From d4122278bdfe3144a77f16a59cb6fca56285af6a Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 1 Sep 2022 15:23:40 +0000 Subject: [PATCH 2/6] Fixing style errors. --- htdocs/bom/bom_card.php | 2 +- htdocs/bom/class/bom.class.php | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/htdocs/bom/bom_card.php b/htdocs/bom/bom_card.php index 11d2b3b4e31..669eb70d069 100644 --- a/htdocs/bom/bom_card.php +++ b/htdocs/bom/bom_card.php @@ -183,7 +183,7 @@ if (empty($reshook)) { // We check if we're allowed to add this bom $TParentBom=array(); $object->getParentBomTreeRecursive($TParentBom, $object->id); - if($bom_child_id > 0 && !empty($TParentBom) && in_array($bom_child_id, $TParentBom)) { + if ($bom_child_id > 0 && !empty($TParentBom) && in_array($bom_child_id, $TParentBom)) { $n_child = new BOM($db); $n_child->fetch($bom_child_id); setEventMessages($langs->transnoentities('BomCantAddChildBom', $n_child->getNomUrl(1), $object->getNomUrl(1)), null, 'errors'); diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index e178c76c024..458e51cdf2c 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -1155,7 +1155,8 @@ class BOM extends CommonObject * @param int $bom_id ID of bom from which we want to get parent bom ids * @return void */ - public function getParentBomTreeRecursive(&$TParentBom, $bom_id) { + public function getParentBomTreeRecursive(&$TParentBom, $bom_id) + { $sql = 'SELECT l.fk_bom, b.label FROM '.MAIN_DB_PREFIX.'bom_bomline l @@ -1163,17 +1164,12 @@ class BOM extends CommonObject WHERE fk_bom_child = '.$bom_id; $resql = $this->db->query($sql); - if(!empty($resql)) { - - while($res = $this->db->fetch_object($resql)) { - + if (!empty($resql)) { + while ($res = $this->db->fetch_object($resql)) { $TParentBom[$res->fk_bom] = $res->fk_bom; $this->getParentBomTreeRecursive($TParentBom, $res->fk_bom); - } - } - } } From 1269e1b86e23973110ef4f1a933ae4013797f5a5 Mon Sep 17 00:00:00 2001 From: Gauthier PC portable 024 Date: Fri, 2 Sep 2022 08:44:16 +0200 Subject: [PATCH 3/6] FIX : cast --- htdocs/bom/class/bom.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index e178c76c024..1a21c73baee 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -1160,7 +1160,7 @@ class BOM extends CommonObject $sql = 'SELECT l.fk_bom, b.label FROM '.MAIN_DB_PREFIX.'bom_bomline l INNER JOIN '.MAIN_DB_PREFIX.$this->table_element.' b ON b.rowid = l.fk_bom - WHERE fk_bom_child = '.$bom_id; + WHERE fk_bom_child = '.((int) $bom_id); $resql = $this->db->query($sql); if(!empty($resql)) { From c28407a1c9b0a45de91f34cf3f27ae5c0da591a3 Mon Sep 17 00:00:00 2001 From: Gauthier PC portable 024 Date: Fri, 2 Sep 2022 10:28:19 +0200 Subject: [PATCH 4/6] FIX : default value for bom_id with $this->id and protection against infinite loop --- htdocs/bom/bom_card.php | 2 +- htdocs/bom/class/bom.class.php | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/htdocs/bom/bom_card.php b/htdocs/bom/bom_card.php index 669eb70d069..573478a6b3d 100644 --- a/htdocs/bom/bom_card.php +++ b/htdocs/bom/bom_card.php @@ -182,7 +182,7 @@ if (empty($reshook)) { // We check if we're allowed to add this bom $TParentBom=array(); - $object->getParentBomTreeRecursive($TParentBom, $object->id); + $object->getParentBomTreeRecursive($TParentBom); if ($bom_child_id > 0 && !empty($TParentBom) && in_array($bom_child_id, $TParentBom)) { $n_child = new BOM($db); $n_child->fetch($bom_child_id); diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index 6cd1d1d42d8..374d4af248b 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -1153,11 +1153,19 @@ class BOM extends CommonObject * * @param array $TParentBom We put all found parent bom in $TParentBom * @param int $bom_id ID of bom from which we want to get parent bom ids + * @param int $level Protection against infinite loop * @return void */ - public function getParentBomTreeRecursive(&$TParentBom, $bom_id) + public function getParentBomTreeRecursive(&$TParentBom, $bom_id='', $level=1) { + // Protection against infinite loop + if ($level > 1000) { + return 0; + } + + if(empty($bom_id)) $bom_id=$this->id; + $sql = 'SELECT l.fk_bom, b.label FROM '.MAIN_DB_PREFIX.'bom_bomline l INNER JOIN '.MAIN_DB_PREFIX.$this->table_element.' b ON b.rowid = l.fk_bom @@ -1167,7 +1175,7 @@ class BOM extends CommonObject if (!empty($resql)) { while ($res = $this->db->fetch_object($resql)) { $TParentBom[$res->fk_bom] = $res->fk_bom; - $this->getParentBomTreeRecursive($TParentBom, $res->fk_bom); + $this->getParentBomTreeRecursive($TParentBom, $res->fk_bom, $level+1); } } } From 21517cf5789de767087a02c5ca99861e3f889aa3 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Fri, 2 Sep 2022 08:33:27 +0000 Subject: [PATCH 5/6] Fixing style errors. --- htdocs/bom/class/bom.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index 374d4af248b..5fa2957e663 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -1156,7 +1156,7 @@ class BOM extends CommonObject * @param int $level Protection against infinite loop * @return void */ - public function getParentBomTreeRecursive(&$TParentBom, $bom_id='', $level=1) + public function getParentBomTreeRecursive(&$TParentBom, $bom_id = '', $level = 1) { // Protection against infinite loop @@ -1164,7 +1164,7 @@ class BOM extends CommonObject return 0; } - if(empty($bom_id)) $bom_id=$this->id; + if (empty($bom_id)) $bom_id=$this->id; $sql = 'SELECT l.fk_bom, b.label FROM '.MAIN_DB_PREFIX.'bom_bomline l From 360bfed601f018bf9234aafc9f6d44e3cd282479 Mon Sep 17 00:00:00 2001 From: Gauthier PC portable 024 Date: Fri, 2 Sep 2022 11:57:29 +0200 Subject: [PATCH 6/6] FIX : return void --- htdocs/bom/class/bom.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index 374d4af248b..5ddbf05eca4 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -1161,7 +1161,7 @@ class BOM extends CommonObject // Protection against infinite loop if ($level > 1000) { - return 0; + return; } if(empty($bom_id)) $bom_id=$this->id;