NEW add new rule fetchidfromcodeandlabel for categories import

This commit is contained in:
VESSILLER 2019-10-11 11:57:11 +02:00
parent d77f7556d8
commit 2fb776ee34
3 changed files with 94 additions and 15 deletions

View File

@ -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';

View File

@ -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';

View File

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