From 08a3ef5bbf5e1aa7b38a9340d183880f1e14ffa1 Mon Sep 17 00:00:00 2001 From: Gauthier PC portable 024 Date: Tue, 10 May 2022 12:25:38 +0200 Subject: [PATCH] NEW : method avoid being blocked when entry already exists for import in llx_categorie_product (use update keys) --- htdocs/core/modules/import/import_csv.modules.php | 14 +++++++++++++- .../core/modules/import/import_xlsx.modules.php | 15 ++++++++++++++- htdocs/core/modules/modCategorie.class.php | 1 + 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 78c8f5e104f..8a99dfe90d2 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -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 diff --git a/htdocs/core/modules/import/import_xlsx.modules.php b/htdocs/core/modules/import/import_xlsx.modules.php index 38c9cd8c56a..c972dcbcb28 100644 --- a/htdocs/core/modules/import/import_xlsx.modules.php +++ b/htdocs/core/modules/import/import_xlsx.modules.php @@ -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 diff --git a/htdocs/core/modules/modCategorie.class.php b/htdocs/core/modules/modCategorie.class.php index 776c5c76681..ab48a702250 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