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="";