From 416311a7b94013591f3d520a3a74cf9d5d9ca1e7 Mon Sep 17 00:00:00 2001 From: gauthier Date: Fri, 16 Sep 2016 11:17:19 +0200 Subject: [PATCH 1/9] NEW : allow to save a parent warehouse # Conflicts: # htdocs/product/stock/card.php --- .../install/mysql/migration/4.0.0-5.0.0.sql | 2 ++ htdocs/install/mysql/tables/llx_entrepot.sql | 3 ++- htdocs/langs/en_US/stocks.lang | 1 + .../product/class/html.formproduct.class.php | 12 ++++++--- htdocs/product/stock/card.php | 25 +++++++++++++++++++ htdocs/product/stock/class/entrepot.class.php | 8 +++--- 6 files changed, 43 insertions(+), 8 deletions(-) diff --git a/htdocs/install/mysql/migration/4.0.0-5.0.0.sql b/htdocs/install/mysql/migration/4.0.0-5.0.0.sql index 359d01fcc54..aebc549413e 100644 --- a/htdocs/install/mysql/migration/4.0.0-5.0.0.sql +++ b/htdocs/install/mysql/migration/4.0.0-5.0.0.sql @@ -135,3 +135,5 @@ ALTER TABLE llx_accounting_bookkeeping ADD COLUMN tms timestamp; ALTER TABLE llx_accounting_account ADD INDEX uk_accounting_account (account_number, entity); ALTER TABLE llx_c_payment_term change fdm type_cdr tinyint + +ALTER TABLE llx_entrepot ADD COLUMN fk_parent integer DEFAULT NULL diff --git a/htdocs/install/mysql/tables/llx_entrepot.sql b/htdocs/install/mysql/tables/llx_entrepot.sql index a8898a7ec92..827675a5c0a 100644 --- a/htdocs/install/mysql/tables/llx_entrepot.sql +++ b/htdocs/install/mysql/tables/llx_entrepot.sql @@ -34,5 +34,6 @@ create table llx_entrepot fk_pays integer DEFAULT 0, statut tinyint DEFAULT 1, -- 1 open, 0 close fk_user_author integer, - import_key varchar(14) + import_key varchar(14), + fk_parent integer DEFAULT NULL )ENGINE=innodb; diff --git a/htdocs/langs/en_US/stocks.lang b/htdocs/langs/en_US/stocks.lang index 413e6c80088..d5fa618b1e0 100644 --- a/htdocs/langs/en_US/stocks.lang +++ b/htdocs/langs/en_US/stocks.lang @@ -2,6 +2,7 @@ WarehouseCard=Warehouse card Warehouse=Warehouse Warehouses=Warehouses +ParentWarehouse=Parent warehouse NewWarehouse=New warehouse / Stock area WarehouseEdit=Modify warehouse MenuNewWarehouse=New warehouse diff --git a/htdocs/product/class/html.formproduct.class.php b/htdocs/product/class/html.formproduct.class.php index cedf40678d2..9e08ad86159 100644 --- a/htdocs/product/class/html.formproduct.class.php +++ b/htdocs/product/class/html.formproduct.class.php @@ -58,12 +58,14 @@ class FormProduct * @param boolean $sumStock sum total stock of a warehouse, default true * @return int Nb of loaded lines, 0 if already loaded, <0 if KO */ - function loadWarehouses($fk_product=0, $batch = '', $status=null, $sumStock = true) + function loadWarehouses($fk_product=0, $batch = '', $status=null, $sumStock = true, $exclude='') { global $conf, $langs; if (empty($fk_product) && count($this->cache_warehouses)) return 0; // Cache already loaded and we do not want a list with information specific to a product - + + if (is_array($exclude)) $excludeGroups = implode("','",$exclude); + $sql = "SELECT e.rowid, e.label, e.description"; if (!empty($fk_product)) { @@ -100,6 +102,8 @@ class FormProduct $sql.= " AND e.statut = 1"; } + if(!empty($exclude)) $sql.= ' AND e.rowid NOT IN('.implode(',', $exclude).')'; + if ($sumStock && empty($fk_product)) $sql.= " GROUP BY e.rowid, e.label, e.description"; $sql.= " ORDER BY e.label"; @@ -144,7 +148,7 @@ class FormProduct * @param string $morecss Add more css classes * @return string HTML select */ - function selectWarehouses($selected='',$htmlname='idwarehouse',$filtertype='',$empty=0,$disabled=0,$fk_product=0,$empty_label='', $showstock=0, $forcecombo=0, $events=array(), $morecss='minwidth200') + function selectWarehouses($selected='',$htmlname='idwarehouse',$filtertype='',$empty=0,$disabled=0,$fk_product=0,$empty_label='', $showstock=0, $forcecombo=0, $events=array(), $morecss='minwidth200', $exclude='') { global $conf,$langs,$user; @@ -152,7 +156,7 @@ class FormProduct $out=''; - $this->loadWarehouses($fk_product, '', + $filtertype); // filter on numeric status + $this->loadWarehouses($fk_product, '', + $filtertype, true, $exclude); // filter on numeric status $nbofwarehouses=count($this->cache_warehouses); if ($conf->use_javascript_ajax && ! $forcecombo) diff --git a/htdocs/product/stock/card.php b/htdocs/product/stock/card.php index 60f7938de06..5a3c5f7249b 100644 --- a/htdocs/product/stock/card.php +++ b/htdocs/product/stock/card.php @@ -31,10 +31,12 @@ require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/stock.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'; +require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php'; $langs->load("products"); $langs->load("stocks"); $langs->load("companies"); +$langs->load("categories"); $action=GETPOST('action'); $cancel=GETPOST('cancel'); @@ -64,6 +66,7 @@ $object = new Entrepot($db); if ($action == 'add' && $user->rights->stock->creer) { $object->ref = GETPOST("ref"); + $object->fk_parent = GETPOST("fk_parent"); $object->libelle = GETPOST("libelle"); $object->description = GETPOST("desc"); $object->statut = GETPOST("statut"); @@ -128,6 +131,7 @@ if ($action == 'update' && $cancel <> $langs->trans("Cancel")) if ($object->fetch($id)) { $object->libelle = GETPOST("libelle"); + $object->fk_parent = GETPOST("fk_parent"); $object->description = GETPOST("desc"); $object->statut = GETPOST("statut"); $object->lieu = GETPOST("lieu"); @@ -166,6 +170,7 @@ if ($cancel == $langs->trans("Cancel")) $productstatic=new Product($db); $form=new Form($db); +$formproduct=new FormProduct($db); $formcompany=new FormCompany($db); $help_url='EN:Module_Stocks_En|FR:Module_Stock|ES:Módulo_Stocks'; @@ -189,6 +194,11 @@ if ($action == 'create') print ''.$langs->trans("Ref").''; print ''.$langs->trans("LocationSummary").''; + + // Parent entrepot + print ''.$langs->trans("AddIn").''; + print $formproduct->selectWarehouses('', 'fk_parent', '', 1); + print ''; // Description print ''.$langs->trans("Description").''; @@ -346,6 +356,16 @@ else //print ''.$langs->trans("LocationSummary").''.$object->lieu.''; + // Parent entrepot + $e = new Entrepot($db); + if(!empty($object->fk_parent) && $e->fetch($object->fk_parent) > 0) { + + print ''.$langs->trans("ParentWarehouse").''; + print $e->getNomUrl(3); + print ''; + + } + // Description print ''.$langs->trans("Description").''.nl2br($object->description).''; @@ -623,6 +643,11 @@ else print ''.$langs->trans("Ref").''; print ''.$langs->trans("LocationSummary").''; + + // Parent entrepot + print ''.$langs->trans("AddIn").''; + print $formproduct->selectWarehouses('', 'fk_parent', '', 1, 0, 0, '', 0, 0, array(), 'minwidth200', array($object->id)); + print ''; // Description print ''.$langs->trans("Description").''; diff --git a/htdocs/product/stock/class/entrepot.class.php b/htdocs/product/stock/class/entrepot.class.php index aee38a67ccf..6bec94d9640 100644 --- a/htdocs/product/stock/class/entrepot.class.php +++ b/htdocs/product/stock/class/entrepot.class.php @@ -123,8 +123,8 @@ class Entrepot extends CommonObject $this->db->begin(); - $sql = "INSERT INTO ".MAIN_DB_PREFIX."entrepot (entity, datec, fk_user_author, label)"; - $sql .= " VALUES (".$conf->entity.",'".$this->db->idate($now)."',".$user->id.",'".$this->db->escape($this->libelle)."')"; + $sql = "INSERT INTO ".MAIN_DB_PREFIX."entrepot (entity, datec, fk_user_author, label, fk_parent)"; + $sql .= " VALUES (".$conf->entity.",'".$this->db->idate($now)."',".$user->id.",'".$this->db->escape($this->libelle)."', ".($this->fk_parent > 0 ? $this->fk_parent : 'NULL').")"; dol_syslog(get_class($this)."::create", LOG_DEBUG); $result=$this->db->query($sql); @@ -184,6 +184,7 @@ class Entrepot extends CommonObject $sql = "UPDATE ".MAIN_DB_PREFIX."entrepot "; $sql .= " SET label = '" . $this->db->escape($this->libelle) ."'"; + $sql .= ", fk_parent = '" . (($this->fk_parent > 0) ? $this->fk_parent : 'NULL') ."'"; $sql .= ", description = '" . $this->db->escape($this->description) ."'"; $sql .= ", statut = " . $this->statut; $sql .= ", lieu = '" . $this->db->escape($this->lieu) ."'"; @@ -294,7 +295,7 @@ class Entrepot extends CommonObject { global $conf; - $sql = "SELECT rowid, label, description, statut, lieu, address, zip, town, fk_pays as country_id"; + $sql = "SELECT rowid, fk_parent, label, description, statut, lieu, address, zip, town, fk_pays as country_id"; $sql .= " FROM ".MAIN_DB_PREFIX."entrepot"; if ($id) @@ -317,6 +318,7 @@ class Entrepot extends CommonObject $obj=$this->db->fetch_object($result); $this->id = $obj->rowid; + $this->fk_parent = $obj->fk_parent; $this->ref = $obj->rowid; $this->libelle = $obj->label; $this->description = $obj->description; From fe1bcb7e0a4ba9cd680d0203d82a1a85164e9759 Mon Sep 17 00:00:00 2001 From: gauthier Date: Fri, 16 Sep 2016 10:18:36 +0200 Subject: [PATCH 2/9] NEW : function to display full path to current warehouse --- .../product/class/html.formproduct.class.php | 2 +- htdocs/product/stock/class/entrepot.class.php | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/htdocs/product/class/html.formproduct.class.php b/htdocs/product/class/html.formproduct.class.php index 9e08ad86159..1d8eb4578ca 100644 --- a/htdocs/product/class/html.formproduct.class.php +++ b/htdocs/product/class/html.formproduct.class.php @@ -118,7 +118,7 @@ class FormProduct $obj = $this->db->fetch_object($resql); if ($sumStock) $obj->stock = price2num($obj->stock,5); $this->cache_warehouses[$obj->rowid]['id'] =$obj->rowid; - $this->cache_warehouses[$obj->rowid]['label']=$obj->label; + $this->cache_warehouses[$obj->rowid]['label']=$o->get_full_arbo(); $this->cache_warehouses[$obj->rowid]['description'] = $obj->description; $this->cache_warehouses[$obj->rowid]['stock'] = $obj->stock; $i++; diff --git a/htdocs/product/stock/class/entrepot.class.php b/htdocs/product/stock/class/entrepot.class.php index 6bec94d9640..539b714ec2e 100644 --- a/htdocs/product/stock/class/entrepot.class.php +++ b/htdocs/product/stock/class/entrepot.class.php @@ -606,4 +606,33 @@ class Entrepot extends CommonObject $this->country_id=1; $this->country_code='FR'; } + + function get_full_arbo() { + + global $user,$langs,$conf; + + $TArbo = array($this->libelle); + + $id = $this->id; + + while(true) { + $sql = 'SELECT fk_parent + FROM '.MAIN_DB_PREFIX.'entrepot + WHERE rowid = '.$id; + + $resql = $this->db->query($sql); + if($resql) { + $res = $this->db->fetch_object($resql); + if(empty($res->fk_parent)) break; + $id = $res->fk_parent; + $o = new Entrepot($this->db); + $o->fetch($id); + $TArbo[] = $o->libelle; + } else break; + } + + return implode(' >> ', array_reverse($TArbo)); + + } + } From 1a648cfc892d908dabd4055cc4dfd3b1312fdd32 Mon Sep 17 00:00:00 2001 From: gauthier Date: Tue, 20 Sep 2016 09:27:27 +0200 Subject: [PATCH 3/9] NEW : getNomUrl displays full path to warehouse # Conflicts: # htdocs/product/stock/class/entrepot.class.php --- htdocs/product/class/html.formproduct.class.php | 2 ++ htdocs/product/stock/card.php | 2 +- htdocs/product/stock/class/entrepot.class.php | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/htdocs/product/class/html.formproduct.class.php b/htdocs/product/class/html.formproduct.class.php index 1d8eb4578ca..9a8f6c1a94d 100644 --- a/htdocs/product/class/html.formproduct.class.php +++ b/htdocs/product/class/html.formproduct.class.php @@ -117,6 +117,8 @@ class FormProduct { $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]['description'] = $obj->description; diff --git a/htdocs/product/stock/card.php b/htdocs/product/stock/card.php index 5a3c5f7249b..203288170c8 100644 --- a/htdocs/product/stock/card.php +++ b/htdocs/product/stock/card.php @@ -646,7 +646,7 @@ else // Parent entrepot print ''.$langs->trans("AddIn").''; - print $formproduct->selectWarehouses('', 'fk_parent', '', 1, 0, 0, '', 0, 0, array(), 'minwidth200', array($object->id)); + print $formproduct->selectWarehouses($object->fk_parent, 'fk_parent', '', 1, 0, 0, '', 0, 0, array(), 'minwidth200', array($object->id)); print ''; // Description diff --git a/htdocs/product/stock/class/entrepot.class.php b/htdocs/product/stock/class/entrepot.class.php index 539b714ec2e..0132ec68fab 100644 --- a/htdocs/product/stock/class/entrepot.class.php +++ b/htdocs/product/stock/class/entrepot.class.php @@ -575,7 +575,7 @@ class Entrepot extends CommonObject $linkend=''; if ($withpicto) $result.=($link.img_object($label, 'stock', 'class="classfortooltip"').$linkend.' '); - $result.=$link.(empty($this->label)?$this->libelle:$this->label).$linkend; + $result.=$link.$this->get_full_arbo().$linkend; return $result; } From 61a318835387695ca4fb45e8e0f8e5a10428ba65 Mon Sep 17 00:00:00 2001 From: gauthier Date: Fri, 14 Oct 2016 11:59:07 +0200 Subject: [PATCH 4/9] FIX : new parameter documentation --- htdocs/product/class/html.formproduct.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/htdocs/product/class/html.formproduct.class.php b/htdocs/product/class/html.formproduct.class.php index 9a8f6c1a94d..d550daf6ecb 100644 --- a/htdocs/product/class/html.formproduct.class.php +++ b/htdocs/product/class/html.formproduct.class.php @@ -56,6 +56,7 @@ class FormProduct * @param string $batch Add quantity of batch stock in label for product with batch name batch, batch name precedes batch_id. Nothing if ''. * @param int $status additional filter on status other then 1 * @param boolean $sumStock sum total stock of a warehouse, default true + * @param array $exclude warehouses ids to exclude * @return int Nb of loaded lines, 0 if already loaded, <0 if KO */ function loadWarehouses($fk_product=0, $batch = '', $status=null, $sumStock = true, $exclude='') @@ -148,6 +149,7 @@ class FormProduct * @param int $forcecombo force combo iso ajax select2 * @param array $events events to add to select2 * @param string $morecss Add more css classes + * @param array $exclude warehouses ids to exclude * @return string HTML select */ function selectWarehouses($selected='',$htmlname='idwarehouse',$filtertype='',$empty=0,$disabled=0,$fk_product=0,$empty_label='', $showstock=0, $forcecombo=0, $events=array(), $morecss='minwidth200', $exclude='') From 805b65addda30517c3bf70bad7c89cdb6576a986 Mon Sep 17 00:00:00 2001 From: gauthier Date: Fri, 14 Oct 2016 12:02:24 +0200 Subject: [PATCH 5/9] FIX : semicolon --- htdocs/install/mysql/migration/4.0.0-5.0.0.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/install/mysql/migration/4.0.0-5.0.0.sql b/htdocs/install/mysql/migration/4.0.0-5.0.0.sql index aebc549413e..7b4581d2dce 100644 --- a/htdocs/install/mysql/migration/4.0.0-5.0.0.sql +++ b/htdocs/install/mysql/migration/4.0.0-5.0.0.sql @@ -134,6 +134,6 @@ ALTER TABLE llx_accounting_bookkeeping ADD COLUMN tms timestamp; ALTER TABLE llx_accounting_account ADD INDEX uk_accounting_account (account_number, entity); -ALTER TABLE llx_c_payment_term change fdm type_cdr tinyint +ALTER TABLE llx_c_payment_term change fdm type_cdr tinyint; -ALTER TABLE llx_entrepot ADD COLUMN fk_parent integer DEFAULT NULL +ALTER TABLE llx_entrepot ADD COLUMN fk_parent integer DEFAULT NULL; From 012be8e4003f26e58930f0d2feef935d718971f2 Mon Sep 17 00:00:00 2001 From: gauthier Date: Fri, 14 Oct 2016 12:42:42 +0200 Subject: [PATCH 6/9] NEW : security check to avoid adding a parent warehouse chich is already a child of current one --- htdocs/langs/en_US/main.lang | 1 + htdocs/product/stock/class/entrepot.class.php | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/htdocs/langs/en_US/main.lang b/htdocs/langs/en_US/main.lang index fa9a4eec9e2..05a754eb317 100644 --- a/htdocs/langs/en_US/main.lang +++ b/htdocs/langs/en_US/main.lang @@ -62,6 +62,7 @@ ErrorCantLoadUserFromDolibarrDatabase=Failed to find user %s in Dolibarr ErrorNoVATRateDefinedForSellerCountry=Error, no vat rates defined for country '%s'. ErrorNoSocialContributionForSellerCountry=Error, no social/fiscal taxes type defined for country '%s'. ErrorFailedToSaveFile=Error, failed to save file. +ErrorCannotAddThisParentWarehouse=You are trying to add a parent warehouse which is already a child of current one NotAuthorized=You are not authorized to do that. SetDate=Set date SelectDate=Select a date diff --git a/htdocs/product/stock/class/entrepot.class.php b/htdocs/product/stock/class/entrepot.class.php index 0132ec68fab..452d9a59b69 100644 --- a/htdocs/product/stock/class/entrepot.class.php +++ b/htdocs/product/stock/class/entrepot.class.php @@ -172,6 +172,16 @@ class Entrepot extends CommonObject */ function update($id, $user) { + // Check if new parent is already a child of current warehouse + if(!empty($this->fk_parent)) { + $TChildWarehouses = array(); + $TChildWarehouses = $this->get_children_warehouses($this->id, $TChildWarehouses); + if(in_array($this->fk_parent, $TChildWarehouses)) { + $this->error = 'ErrorCannotAddThisParentWarehouse'; + return -2; + } + } + $this->libelle=trim($this->libelle); $this->description=trim($this->description); @@ -634,5 +644,23 @@ class Entrepot extends CommonObject return implode(' >> ', array_reverse($TArbo)); } + + function get_children_warehouses($id, &$TChildWarehouses) { + + $sql = 'SELECT rowid + FROM '.MAIN_DB_PREFIX.'entrepot + WHERE fk_parent = '.$id; + + $resql = $this->db->query($sql); + if($resql) { + while($res = $this->db->fetch_object($resql)) { + $TChildWarehouses[] = $res->rowid; + $this->get_children_warehouses($res->rowid, $TChildWarehouses); + } + } + + return $TChildWarehouses; + + } } From d85dd69184595ae29daef0a6e1bfd713924fa30a Mon Sep 17 00:00:00 2001 From: gauthier Date: Thu, 20 Oct 2016 13:14:28 +0200 Subject: [PATCH 7/9] NEW : get_full_arbo() & get_children_warehouses() functions documentation --- htdocs/product/stock/class/entrepot.class.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/htdocs/product/stock/class/entrepot.class.php b/htdocs/product/stock/class/entrepot.class.php index 452d9a59b69..109460cfa1a 100644 --- a/htdocs/product/stock/class/entrepot.class.php +++ b/htdocs/product/stock/class/entrepot.class.php @@ -616,7 +616,12 @@ class Entrepot extends CommonObject $this->country_id=1; $this->country_code='FR'; } - + + /** + * Return full path to current warehouse + * + * @return string String full path to current warehouse separated by " >> " + */ function get_full_arbo() { global $user,$langs,$conf; @@ -645,6 +650,13 @@ class Entrepot extends CommonObject } + /** + * Return array of children warehouses ids from $id warehouse (recursive function) + * + * @param int $id id parent warehouse + * @param array() $TChildWarehouses array which will contain all children (param by reference) + * @return array() $TChildWarehouses array which will contain all children + */ function get_children_warehouses($id, &$TChildWarehouses) { $sql = 'SELECT rowid From 9a3d4031c5dd6aa8d59bacac4ed6fc1a67736d93 Mon Sep 17 00:00:00 2001 From: gauthier Date: Thu, 20 Oct 2016 13:30:47 +0200 Subject: [PATCH 8/9] FIX : param $protection - Deep counter to avoid infinite loop --- htdocs/product/stock/class/entrepot.class.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/htdocs/product/stock/class/entrepot.class.php b/htdocs/product/stock/class/entrepot.class.php index 109460cfa1a..da39522126a 100644 --- a/htdocs/product/stock/class/entrepot.class.php +++ b/htdocs/product/stock/class/entrepot.class.php @@ -620,17 +620,20 @@ class Entrepot extends CommonObject /** * Return full path to current warehouse * - * @return string String full path to current warehouse separated by " >> " + * @param int $protection Deep counter to avoid infinite loop + * @return string String full path to current warehouse separated by " >> " */ - function get_full_arbo() { + function get_full_arbo($protection=1000) { global $user,$langs,$conf; $TArbo = array($this->libelle); $id = $this->id; + + $i=0; - while(true) { + while((empty($protection) || $i < $protection)) { $sql = 'SELECT fk_parent FROM '.MAIN_DB_PREFIX.'entrepot WHERE rowid = '.$id; @@ -644,6 +647,9 @@ class Entrepot extends CommonObject $o->fetch($id); $TArbo[] = $o->libelle; } else break; + + $i++; + } return implode(' >> ', array_reverse($TArbo)); From 59941f998066ece0923d6748b645c7bc137fe2da Mon Sep 17 00:00:00 2001 From: gauthier Date: Thu, 20 Oct 2016 14:29:51 +0200 Subject: [PATCH 9/9] 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 d550daf6ecb..d1fe225ba0e 100644 --- a/htdocs/product/class/html.formproduct.class.php +++ b/htdocs/product/class/html.formproduct.class.php @@ -67,7 +67,7 @@ class FormProduct 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 @@ class FormProduct { $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 @@ class FormProduct 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 @@ class FormProduct $out.=''; } diff --git a/htdocs/product/stock/card.php b/htdocs/product/stock/card.php index 203288170c8..5adff357db1 100644 --- a/htdocs/product/stock/card.php +++ b/htdocs/product/stock/card.php @@ -646,7 +646,7 @@ else // 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 da39522126a..032a0f3a56d 100644 --- a/htdocs/product/stock/class/entrepot.class.php +++ b/htdocs/product/stock/class/entrepot.class.php @@ -174,7 +174,7 @@ class Entrepot extends CommonObject { // 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';