From d77f7556d8f19f4f622083eebf771e75962a6acb Mon Sep 17 00:00:00 2001 From: VESSILLER Date: Wed, 9 Oct 2019 16:00:14 +0200 Subject: [PATCH 1/3] NEW add parent category id or label in import category module --- .../modules/import/import_csv.modules.php | 6 +++++- .../modules/import/import_xlsx.modules.php | 6 +++++- htdocs/core/modules/modCategorie.class.php | 20 ++++++++++++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index de428900ba9..734f516fa78 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -459,10 +459,14 @@ class ImportCsv extends ModeleImports $error++; }*/ $param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart. + } elseif ($class == 'Categorie') { + if ($objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeorlabel') { + $param_array = array('', $newval, $arrayrecord[1]['val']); + } } call_user_func_array(array($classinstance, $method), $param_array); // If not found, try the fetch from label - if (! ($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeorlabel') + if (! ($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeorlabel' && $class!='Categorie') { $param_array = array('', '', $newval); call_user_func_array(array($classinstance, $method), $param_array); diff --git a/htdocs/core/modules/import/import_xlsx.modules.php b/htdocs/core/modules/import/import_xlsx.modules.php index c4799ba557e..1a92062b15f 100644 --- a/htdocs/core/modules/import/import_xlsx.modules.php +++ b/htdocs/core/modules/import/import_xlsx.modules.php @@ -486,10 +486,14 @@ class ImportXlsx extends ModeleImports $error++; }*/ $param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart. + } elseif ($class == 'Categorie') { + if ($objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeorlabel') { + $param_array = array('', $newval, $arrayrecord[1]['val']); + } } call_user_func_array(array($classinstance, $method), $param_array); // If not found, try the fetch from label - if (! ($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeorlabel') + if (! ($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeorlabel' && $class!='Categorie') { $param_array = array('', '', $newval); call_user_func_array(array($classinstance, $method), $param_array); diff --git a/htdocs/core/modules/modCategorie.class.php b/htdocs/core/modules/modCategorie.class.php index c04a4d6b346..e8a1bd218cd 100644 --- a/htdocs/core/modules/modCategorie.class.php +++ b/htdocs/core/modules/modCategorie.class.php @@ -406,15 +406,29 @@ class modCategorie extends DolibarrModules $this->import_icon[$r]=$this->picto; $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon $this->import_tables_array[$r]=array('ca'=>MAIN_DB_PREFIX.'categorie'); - $this->import_fields_array[$r]=array('ca.label'=>"Label*",'ca.type'=>"Type*",'ca.description'=>"Description"); - + $this->import_fields_array[$r]=array( + 'ca.label'=>"Label*",'ca.type'=>"Type*",'ca.description'=>"Description", + 'ca.fk_parent' => 'Parent' + ); $this->import_regex_array[$r]=array('ca.type'=>'^[0|1|2|3]'); + $this->import_convertvalue_array[$r] = array( + 'ca.fk_parent' => array( + 'rule' => 'fetchidfromcodeorlabel', + 'classfile' => '/categories/class/categorie.class.php', + 'class' => 'Categorie', + 'method' => 'fetch', + 'element' => 'category' + ) + ); $typeexample=""; if ($conf->product->enabled) { $typeexample.=($typeexample?"/":"")."0=Product"; } if ($conf->fournisseur->enabled) { $typeexample.=($typeexample?"/":"")."1=Supplier"; } if ($conf->societe->enabled) { $typeexample.=($typeexample?"/":"")."2=Customer-Prospect"; } if ($conf->adherent->enabled) { $typeexample.=($typeexample?"/":"")."3=Member"; } - $this->import_examplevalues_array[$r]=array('ca.label'=>"Supplier Category",'ca.type'=>$typeexample,'ca.description'=>"Imported category"); + $this->import_examplevalues_array[$r] = array( + 'ca.label'=>"Supplier Category",'ca.type'=>$typeexample,'ca.description'=>"Imported category", + 'ca.fk_parent' => '0' + ); if (! empty($conf->product->enabled)) { From 2fb776ee347e3e051ff0a9e52f93773aa46cd6af Mon Sep 17 00:00:00 2001 From: VESSILLER Date: Fri, 11 Oct 2019 11:57:11 +0200 Subject: [PATCH 2/3] NEW add new rule fetchidfromcodeandlabel for categories import --- .../modules/import/import_csv.modules.php | 49 +++++++++++++++++-- .../modules/import/import_xlsx.modules.php | 49 +++++++++++++++++-- htdocs/core/modules/modCategorie.class.php | 11 +++-- 3 files changed, 94 insertions(+), 15 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 734f516fa78..cb3ae176026 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -381,6 +381,11 @@ class ImportCsv extends ModeleImports //dol_syslog("Table ".$tablename." check for entity into cache is ".$tablewithentity_cache[$tablename]); } + // array of fields to column index + $arrayfield = array(); + foreach($sort_array_match_file_to_database as $key => $val) { + $arrayfield[$val] = ($key-1); + } // Loop on each fields in the match array: $key = 1..n, $val=alias of field (s.nom) foreach($sort_array_match_file_to_database as $key => $val) @@ -459,14 +464,10 @@ class ImportCsv extends ModeleImports $error++; }*/ $param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart. - } elseif ($class == 'Categorie') { - if ($objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeorlabel') { - $param_array = array('', $newval, $arrayrecord[1]['val']); - } } call_user_func_array(array($classinstance, $method), $param_array); // If not found, try the fetch from label - if (! ($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeorlabel' && $class!='Categorie') + if (! ($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeorlabel') { $param_array = array('', '', $newval); call_user_func_array(array($classinstance, $method), $param_array); @@ -489,6 +490,44 @@ class ImportCsv extends ModeleImports } } } + elseif ($objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeandlabel') + { + $isidorref='id'; + if (! is_numeric($newval) && $newval != '' && ! preg_match('/^id:/i', $newval)) $isidorref='ref'; + $newval=preg_replace('/^(id|ref):/i', '', $newval); + + if ($isidorref == 'ref') { + $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']); + $class = $objimport->array_import_convertvalue[0][$val]['class']; + $method = $objimport->array_import_convertvalue[0][$val]['method']; + $codefromfield = $objimport->array_import_convertvalue[0][$val]['codefromfield']; + $code = $arrayrecord[$arrayfield[$codefromfield]]['val']; + if ($this->cacheconvert[$file . '_' . $class . '_' . $method . '_' . $code][$newval] != '') { + $newval = $this->cacheconvert[$file . '_' . $class . '_' . $method . '_' . $code][$newval]; + } else { + $resultload = dol_include_once($file); + if (empty($resultload)) { + dol_print_error('', 'Error trying to call file=' . $file . ', class=' . $class . ', method=' . $method . ', code=' . $code); + break; + } + $classinstance = new $class($this->db); + // Try the fetch from code and ref + $param_array = array('', $newval, $code); + call_user_func_array(array($classinstance, $method), $param_array); + $this->cacheconvert[$file . '_' . $class . '_' . $method . '_' . $code][$newval] = $classinstance->id; + if ($classinstance->id > 0) // we found record + { + $newval = $classinstance->id; + } else { + if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict'])); + else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn'; + $this->errors[$error]['type'] = 'FOREIGNKEY'; + $errorforthistable++; + $error++; + } + } + } + } elseif ($objimport->array_import_convertvalue[0][$val]['rule']=='zeroifnull') { if (empty($newval)) $newval='0'; diff --git a/htdocs/core/modules/import/import_xlsx.modules.php b/htdocs/core/modules/import/import_xlsx.modules.php index 1a92062b15f..c5fcc1441e3 100644 --- a/htdocs/core/modules/import/import_xlsx.modules.php +++ b/htdocs/core/modules/import/import_xlsx.modules.php @@ -408,6 +408,11 @@ class ImportXlsx extends ModeleImports //dol_syslog("Table ".$tablename." check for entity into cache is ".$tablewithentity_cache[$tablename]); } + // array of fields to column index + $arrayfield = array(); + foreach($sort_array_match_file_to_database as $key => $val) { + $arrayfield[$val] = ($key-1); + } // Loop on each fields in the match array: $key = 1..n, $val=alias of field (s.nom) foreach($sort_array_match_file_to_database as $key => $val) @@ -486,14 +491,10 @@ class ImportXlsx extends ModeleImports $error++; }*/ $param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart. - } elseif ($class == 'Categorie') { - if ($objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeorlabel') { - $param_array = array('', $newval, $arrayrecord[1]['val']); - } } call_user_func_array(array($classinstance, $method), $param_array); // If not found, try the fetch from label - if (! ($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeorlabel' && $class!='Categorie') + if (! ($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeorlabel') { $param_array = array('', '', $newval); call_user_func_array(array($classinstance, $method), $param_array); @@ -516,6 +517,44 @@ class ImportXlsx extends ModeleImports } } } + elseif ($objimport->array_import_convertvalue[0][$val]['rule']=='fetchidfromcodeandlabel') + { + $isidorref='id'; + if (! is_numeric($newval) && $newval != '' && ! preg_match('/^id:/i', $newval)) $isidorref='ref'; + $newval=preg_replace('/^(id|ref):/i', '', $newval); + + if ($isidorref == 'ref') { + $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']); + $class = $objimport->array_import_convertvalue[0][$val]['class']; + $method = $objimport->array_import_convertvalue[0][$val]['method']; + $codefromfield = $objimport->array_import_convertvalue[0][$val]['codefromfield']; + $code = $arrayrecord[$arrayfield[$codefromfield]]['val']; + if ($this->cacheconvert[$file . '_' . $class . '_' . $method . '_' . $code][$newval] != '') { + $newval = $this->cacheconvert[$file . '_' . $class . '_' . $method . '_' . $code][$newval]; + } else { + $resultload = dol_include_once($file); + if (empty($resultload)) { + dol_print_error('', 'Error trying to call file=' . $file . ', class=' . $class . ', method=' . $method . ', code=' . $code); + break; + } + $classinstance = new $class($this->db); + // Try the fetch from code and ref + $param_array = array('', $newval, $code); + call_user_func_array(array($classinstance, $method), $param_array); + $this->cacheconvert[$file . '_' . $class . '_' . $method . '_' . $code][$newval] = $classinstance->id; + if ($classinstance->id > 0) // we found record + { + $newval = $classinstance->id; + } else { + if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict'])); + else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn'; + $this->errors[$error]['type'] = 'FOREIGNKEY'; + $errorforthistable++; + $error++; + } + } + } + } elseif ($objimport->array_import_convertvalue[0][$val]['rule']=='zeroifnull') { if (empty($newval)) $newval='0'; diff --git a/htdocs/core/modules/modCategorie.class.php b/htdocs/core/modules/modCategorie.class.php index e8a1bd218cd..eb9e2c6b4a1 100644 --- a/htdocs/core/modules/modCategorie.class.php +++ b/htdocs/core/modules/modCategorie.class.php @@ -413,11 +413,12 @@ class modCategorie extends DolibarrModules $this->import_regex_array[$r]=array('ca.type'=>'^[0|1|2|3]'); $this->import_convertvalue_array[$r] = array( 'ca.fk_parent' => array( - 'rule' => 'fetchidfromcodeorlabel', - 'classfile' => '/categories/class/categorie.class.php', - 'class' => 'Categorie', - 'method' => 'fetch', - 'element' => 'category' + 'rule' => 'fetchidfromcodeandlabel', + 'classfile' => '/categories/class/categorie.class.php', + 'class' => 'Categorie', + 'method' => 'fetch', + 'element' => 'category', + 'codefromfield' => 'ca.type' ) ); $typeexample=""; From fa928f7dc333d3a2e086830b7e47a080a24daf6c Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 11 Oct 2019 17:33:13 +0200 Subject: [PATCH 3/3] Update modCategorie.class.php --- htdocs/core/modules/modCategorie.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/modules/modCategorie.class.php b/htdocs/core/modules/modCategorie.class.php index eb9e2c6b4a1..d157e35a555 100644 --- a/htdocs/core/modules/modCategorie.class.php +++ b/htdocs/core/modules/modCategorie.class.php @@ -427,7 +427,7 @@ class modCategorie extends DolibarrModules if ($conf->societe->enabled) { $typeexample.=($typeexample?"/":"")."2=Customer-Prospect"; } if ($conf->adherent->enabled) { $typeexample.=($typeexample?"/":"")."3=Member"; } $this->import_examplevalues_array[$r] = array( - 'ca.label'=>"Supplier Category",'ca.type'=>$typeexample,'ca.description'=>"Imported category", + 'ca.label'=>"Supplier Category",'ca.type'=>$typeexample,'ca.description'=>"My Category description", 'ca.fk_parent' => '0' );