diff --git a/modules/anagrafiche/src/Import/CSV.php b/modules/anagrafiche/src/Import/CSV.php index d581b703d..822001564 100644 --- a/modules/anagrafiche/src/Import/CSV.php +++ b/modules/anagrafiche/src/Import/CSV.php @@ -235,7 +235,7 @@ class CSV extends CSVImporter ]; } - public function import($record) + public function import($record, $update_record = true, $add_record = true) { $database = database(); $primary_key = $this->getPrimaryKey(); @@ -248,6 +248,21 @@ class CSV extends CSVImporter unset($record['cognome']); unset($record['nome']); + // Ricerca di eventuale anagrafica corrispondente sulla base del campo definito come primary_key (es. codice) + if (!empty($primary_key)) { + $anagrafica = Anagrafica::where($primary_key, '=', trim((string) $record[$primary_key]))->first(); + } + + // Controllo se creare o aggiornare il record + if (($anagrafica && !$update_record) || (!$anagrafica && !$add_record)) { + return; + } + + // Se non trovo nessuna anagrafica corrispondente, allora la creo + if (empty($anagrafica)) { + $anagrafica = Anagrafica::build($record['ragione_sociale']); + } + // Individuazione del tipo dell'anagrafica $tipologie = []; if (!empty($record['tipologia'])) { @@ -331,16 +346,6 @@ class CSV extends CSVImporter } } - // Ricerca di eventuale anagrafica corrispondente sulla base del campo definito come primary_key (es. codice) - if (!empty($primary_key)) { - $anagrafica = Anagrafica::where($primary_key, '=', trim((string) $record[$primary_key]))->first(); - } - - // Se non trovo nessuna anagrafica corrispondente, allora la creo - if (empty($anagrafica)) { - $anagrafica = Anagrafica::build($record['ragione_sociale']); - } - // Impedisco di aggiornare l'anagrafica Azienda if ($anagrafica->id == $id_azienda) { return false; diff --git a/modules/articoli/src/Import/CSV.php b/modules/articoli/src/Import/CSV.php index 2af82ba19..8e1c4aae8 100644 --- a/modules/articoli/src/Import/CSV.php +++ b/modules/articoli/src/Import/CSV.php @@ -260,10 +260,23 @@ class CSV extends CSVImporter } } - public function import($record) + public function import($record, $update_record = true, $add_record = true) { $database = database(); $primary_key = $this->getPrimaryKey(); + + // Individuazione articolo e generazione + $articolo = null; + // Ricerca sulla base della chiave primaria se presente + if (!empty($primary_key)) { + $articolo = Articolo::where($primary_key, $record[$primary_key])->withTrashed()->first(); + } + + // Controllo se creare o aggiornare il record + if (($articolo && !$update_record) || (!$articolo && !$add_record)) { + return; + } + $url = $record['immagine']; unset($record['immagine']); @@ -308,12 +321,6 @@ class CSV extends CSVImporter } } - // Individuazione articolo e generazione - $articolo = null; - // Ricerca sulla base della chiave primaria se presente - if (!empty($primary_key)) { - $articolo = Articolo::where($primary_key, $record[$primary_key])->withTrashed()->first(); - } if (empty($articolo)) { $articolo = Articolo::build($record['codice'], $categoria, $sottocategoria); $articolo->setTranslation('title', $record['descrizione']); diff --git a/modules/impianti/src/Import/CSV.php b/modules/impianti/src/Import/CSV.php index c0dfc97e3..9a31da3f0 100644 --- a/modules/impianti/src/Import/CSV.php +++ b/modules/impianti/src/Import/CSV.php @@ -98,7 +98,7 @@ class CSV extends CSVImporter ]; } - public function import($record) + public function import($record, $update_record = true, $add_record = true) { $database = database(); $primary_key = $this->getPrimaryKey(); @@ -110,6 +110,17 @@ class CSV extends CSVImporter } if (!empty($anagrafica)) { + $impianto = null; + // Ricerca sulla base della chiave primaria se presente + if (!empty($primary_key)) { + $impianto = Impianto::where($primary_key, $record[$primary_key])->first(); + } + + // Controllo se creare o aggiornare il record + if (($impianto && !$update_record) || (!$impianto && !$add_record)) { + return; + } + $url = $record['immagine']; unset($record['immagine']); @@ -154,21 +165,15 @@ class CSV extends CSVImporter } // Individuazione impianto e generazione - $impianto = null; - - // Ricerca sulla base della chiave primaria se presente - if (!empty($primary_key)) { - $impianto = Impianto::where($primary_key, $record[$primary_key])->first(); - $impianto->nome = $record['nome']; - } if (empty($impianto)) { - $impianto = Impianto::build($record['matricola'], $record['nome'], $categoria, $record['cliente']); + $impianto = Impianto::build($record['matricola'], $record['nome'], $categoria, $anagrafica->id); } if (!empty($record['data'])) { $impianto->data = $record['data']; } + $impianto->nome = $record['nome']; $impianto->idanagrafica = $anagrafica->idanagrafica; $impianto->id_marca = $id_marca; $impianto->id_modello = $record['modello']; diff --git a/modules/import/actions.php b/modules/import/actions.php index 523ed75e5..f52915567 100755 --- a/modules/import/actions.php +++ b/modules/import/actions.php @@ -97,7 +97,7 @@ switch (filter('op')) { $csv->init(); } - $count = $csv->importRows($offset, $limit); + $count = $csv->importRows($offset, $limit, post('update_record'), post('add_record')); $more = $count == $limit; // Operazioni di finalizzazione per l'importazione diff --git a/modules/import/edit.php b/modules/import/edit.php index da31b30ce..b358efb50 100755 --- a/modules/import/edit.php +++ b/modules/import/edit.php @@ -56,11 +56,19 @@ if (empty($id_record)) {