NEW : method avoid being blocked when entry already exists for import in llx_categorie_product (use update keys)

This commit is contained in:
Gauthier PC portable 024 2022-05-10 12:25:38 +02:00
parent b4e1bac32d
commit 08a3ef5bbf
3 changed files with 28 additions and 2 deletions

View File

@ -776,11 +776,18 @@ class ImportCsv extends ModeleImports
$updatedone = false;
$insertdone = false;
$is_table_category_link = false;
$fname = 'rowid';
if(strpos($tablename, '_categorie_') !== false) {
$is_table_category_link = true;
$fname='*';
}
if (!empty($updatekeys)) {
// We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields)
if (empty($lastinsertid)) { // No insert done yet for a parent table
$sqlSelect = 'SELECT rowid FROM '.$tablename;
$sqlSelect = 'SELECT '.$fname.' FROM '.$tablename;
$data = array_combine($listfields, $listvalues);
$where = array();
@ -798,6 +805,7 @@ class ImportCsv extends ModeleImports
$res = $this->db->fetch_object($resql);
if ($resql->num_rows == 1) {
$lastinsertid = $res->rowid;
if($is_table_category_link) $lastinsertid = 'linktable'; // used to apply update on tables like llx_categorie_product and avoid being blocked for all file content if at least one entry already exists
$last_insert_id_array[$tablename] = $lastinsertid;
} elseif ($resql->num_rows > 1) {
$this->errors[$error]['lib'] = $langs->trans('MultipleRecordFoundWithTheseFilters', implode(', ', $filters));
@ -859,6 +867,10 @@ class ImportCsv extends ModeleImports
}
$sqlend = ' WHERE '.$keyfield.' = '.((int) $lastinsertid);
if($is_table_category_link) {
$sqlend = ' WHERE ' . implode(' AND ', $where);
}
$sql = $sqlstart.$sqlend;
// Run update request

View File

@ -816,11 +816,19 @@ class ImportXlsx extends ModeleImports
if (!empty($listfields)) {
$updatedone = false;
$insertdone = false;
$is_table_category_link = false;
$fname = 'rowid';
if(strpos($tablename, '_categorie_') !== false) {
$is_table_category_link = true;
$fname='*';
}
if (!empty($updatekeys)) {
// We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields)
if (empty($lastinsertid)) { // No insert done yet for a parent table
$sqlSelect = 'SELECT rowid FROM ' . $tablename;
$sqlSelect = 'SELECT '.$fname.' FROM ' . $tablename;
$data = array_combine($listfields, $listvalues);
$where = array();
@ -838,6 +846,7 @@ class ImportXlsx extends ModeleImports
$res = $this->db->fetch_object($resql);
if ($resql->num_rows == 1) {
$lastinsertid = $res->rowid;
if($is_table_category_link) $lastinsertid = 'linktable'; // used to apply update on tables like llx_categorie_product and avoid being blocked for all file content if at least one entry already exists
$last_insert_id_array[$tablename] = $lastinsertid;
} elseif ($resql->num_rows > 1) {
$this->errors[$error]['lib'] = $langs->trans('MultipleRecordFoundWithTheseFilters', implode(', ', $filters));
@ -899,6 +908,10 @@ class ImportXlsx extends ModeleImports
}
$sqlend = ' WHERE ' . $keyfield . ' = '.((int) $lastinsertid);
if($is_table_category_link) {
$sqlend = ' WHERE ' . implode(' AND ', $where);
}
$sql = $sqlstart . $sqlend;
// Run update request

View File

@ -481,6 +481,7 @@ class modCategorie extends DolibarrModules
'cp.fk_product'=>array('rule'=>'fetchidfromref', 'classfile'=>'/product/class/product.class.php', 'class'=>'Product', 'method'=>'fetch', 'element'=>'Product')
);
$this->import_examplevalues_array[$r] = array('cp.fk_categorie'=>"rowid or label", 'cp.fk_product'=>"rowid or ref");
$this->import_updatekeys_array[$r] = array('cp.fk_categorie' => 'Category', 'cp.fk_product' => 'ProductRef');
}
// 1 Suppliers