diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 16568288aa4..08eb9a6f42d 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -819,11 +819,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(); @@ -842,6 +849,7 @@ class ImportCsv extends ModeleImports if ($num_rows == 1) { $res = $this->db->fetch_object($resql); $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 ($num_rows > 1) { $this->errors[$error]['lib'] = $langs->trans('MultipleRecordFoundWithTheseFilters', implode(', ', $filters)); @@ -903,6 +911,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 diff --git a/htdocs/core/modules/import/import_xlsx.modules.php b/htdocs/core/modules/import/import_xlsx.modules.php index d16a9a764eb..7be4ee8894a 100644 --- a/htdocs/core/modules/import/import_xlsx.modules.php +++ b/htdocs/core/modules/import/import_xlsx.modules.php @@ -863,11 +863,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(); @@ -886,6 +894,7 @@ class ImportXlsx extends ModeleImports if ($num_rows == 1) { $res = $this->db->fetch_object($resql); $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 ($num_rows > 1) { $this->errors[$error]['lib'] = $langs->trans('MultipleRecordFoundWithTheseFilters', implode(', ', $filters)); @@ -947,6 +956,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 diff --git a/htdocs/core/modules/modCategorie.class.php b/htdocs/core/modules/modCategorie.class.php index 6ee294eff58..9e138c6ccef 100644 --- a/htdocs/core/modules/modCategorie.class.php +++ b/htdocs/core/modules/modCategorie.class.php @@ -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