From 59941f998066ece0923d6748b645c7bc137fe2da Mon Sep 17 00:00:00 2001 From: gauthier Date: Thu, 20 Oct 2016 14:29:51 +0200 Subject: [PATCH] FIX : must not get full path with recursive query --- .../product/class/html.formproduct.class.php | 38 ++++++++++++++++--- htdocs/product/stock/card.php | 2 +- htdocs/product/stock/class/entrepot.class.php | 2 +- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/htdocs/product/class/html.formproduct.class.php b/htdocs/product/class/html.formproduct.class.php index d550daf6ecbca..d1fe225ba0e4b 100644 --- a/htdocs/product/class/html.formproduct.class.php +++ b/htdocs/product/class/html.formproduct.class.php @@ -67,7 +67,7 @@ function loadWarehouses($fk_product=0, $batch = '', $status=null, $sumStock = tr if (is_array($exclude)) $excludeGroups = implode("','",$exclude); - $sql = "SELECT e.rowid, e.label, e.description"; + $sql = "SELECT e.rowid, e.label, e.description, e.fk_parent"; if (!empty($fk_product)) { if (!empty($batch)) @@ -118,14 +118,19 @@ function loadWarehouses($fk_product=0, $batch = '', $status=null, $sumStock = tr { $obj = $this->db->fetch_object($resql); if ($sumStock) $obj->stock = price2num($obj->stock,5); - $o = new Entrepot($this->db); - $o->fetch($obj->rowid); $this->cache_warehouses[$obj->rowid]['id'] =$obj->rowid; - $this->cache_warehouses[$obj->rowid]['label']=$o->get_full_arbo(); + $this->cache_warehouses[$obj->rowid]['label']=$obj->label; + $this->cache_warehouses[$obj->rowid]['parent_id']=$obj->fk_parent; $this->cache_warehouses[$obj->rowid]['description'] = $obj->description; $this->cache_warehouses[$obj->rowid]['stock'] = $obj->stock; $i++; } + + // Full label init + foreach($this->cache_warehouses as $obj_rowid=>$tab) { + $this->cache_warehouses[$obj_rowid]['full_label'] = $this->get_parent_path($tab); + } + return $num; } else @@ -134,6 +139,29 @@ function loadWarehouses($fk_product=0, $batch = '', $status=null, $sumStock = tr return -1; } } + + /** + * Return full path to current warehouse in $tab (recursive function) + * + * @param array $tab warehouse data in $this->cache_warehouses line + * @param String $final_label full label with all parents, separated by ' >> ' (completed on each call) + * @return String full label with all parents, separated by ' >> ' + */ + private function get_parent_path($tab, $final_label='') { + + if(empty($final_label)) $final_label = $tab['label']; + + if(empty($tab['parent_id'])) return $final_label; + else { + if(!empty($this->cache_warehouses[$tab['parent_id']])) { + $final_label = $this->cache_warehouses[$tab['parent_id']]['label'].' >> '.$final_label; + return $this->get_parent_path($this->cache_warehouses[$tab['parent_id']], $final_label); + } + } + + return $final_label; + + } /** * Return list of warehouses @@ -178,7 +206,7 @@ function selectWarehouses($selected='',$htmlname='idwarehouse',$filtertype='',$e $out.=''; } diff --git a/htdocs/product/stock/card.php b/htdocs/product/stock/card.php index 203288170c80d..5adff357db1c7 100644 --- a/htdocs/product/stock/card.php +++ b/htdocs/product/stock/card.php @@ -646,7 +646,7 @@ // Parent entrepot print ''.$langs->trans("AddIn").''; - print $formproduct->selectWarehouses($object->fk_parent, 'fk_parent', '', 1, 0, 0, '', 0, 0, array(), 'minwidth200', array($object->id)); + print $formproduct->selectWarehouses($object->fk_parent, 'fk_parent', '', 1); print ''; // Description diff --git a/htdocs/product/stock/class/entrepot.class.php b/htdocs/product/stock/class/entrepot.class.php index da39522126a2f..032a0f3a56d54 100644 --- a/htdocs/product/stock/class/entrepot.class.php +++ b/htdocs/product/stock/class/entrepot.class.php @@ -174,7 +174,7 @@ function update($id, $user) { // Check if new parent is already a child of current warehouse if(!empty($this->fk_parent)) { - $TChildWarehouses = array(); + $TChildWarehouses = array($id); $TChildWarehouses = $this->get_children_warehouses($this->id, $TChildWarehouses); if(in_array($this->fk_parent, $TChildWarehouses)) { $this->error = 'ErrorCannotAddThisParentWarehouse';