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] 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