diff --git a/modules/anagrafiche/import.php b/modules/anagrafiche/import.php index 189adf5ab..87d314078 100644 --- a/modules/anagrafiche/import.php +++ b/modules/anagrafiche/import.php @@ -8,22 +8,45 @@ switch (post('op')) { foreach ($data as $key => $value) { if (!empty($value)){ - $i++; + (array) $idtipoanagrafica = $data[$key]['tipologia']; unset($data[$key]['tipologia']); - $dbo->insert('an_anagrafiche', $data[$key]); + + + //update + if (post('primary_key')!=''){ + + $primary_key = post('primary_key'); + + $rs = $dbo->select('an_anagrafiche', $primary_key, [$primary_key => $data[$key][$primary_key]]); + + if (!in_array($data[$key][$primary_key], $rs[0])) { + + //insert + $dbo->insert('an_anagrafiche', $data[$key]); + + //campi extra + if (count($idtipoanagrafica)>0){ + // Aggiornamento della tipologia di anagrafiche + $dbo->sync('an_tipianagrafiche_anagrafiche', [ + 'idanagrafica' => $dbo->lastInsertedID(), + ], [ + 'idtipoanagrafica' => (array) $idtipoanagrafica, + ]); + } + + }else{ + //update + $dbo->update('an_anagrafiche', $data[$key], [$primary_key => $data[$key][$primary_key]]); + } + } + + + unset($data[$key]); - //campi extra - if (count($idtipoanagrafica)>0){ - // Aggiornamento della tipologia di anagrafiche - $dbo->sync('an_tipianagrafiche_anagrafiche', [ - 'idanagrafica' => $dbo->lastInsertedID(), - ], [ - 'idtipoanagrafica' => (array) $idtipoanagrafica, - ]); - } + } @@ -39,6 +62,7 @@ return [ [ 'field' => 'codice', 'label' => 'Codice', + 'primary_key' => '1', ], [ 'field' => 'ragione_sociale', diff --git a/modules/import/edit.php b/modules/import/edit.php index 7df1b5dac..a82631b0f 100644 --- a/modules/import/edit.php +++ b/modules/import/edit.php @@ -5,16 +5,6 @@ include_once __DIR__.'/../../core.php'; if (empty($id_record)) { require $docroot.'/add.php'; } else { - echo ' -
- - - -
-
- {[ "type": "checkbox", "label": "'.tr('Importa prima riga').'", "name": "first_row", "extra":"", "value": "1" ]} -
-
'; // Inclusione del file del modulo per eventuale HTML personalizzato include $imports[$id_record]['import']; @@ -22,13 +12,37 @@ if (empty($id_record)) { $fields = Import::getFields($id_record); $select = []; + $select2 = []; foreach ($fields as $key => $value) { $select[] = [ 'id' => $key, 'text' => $value['label'], ]; + + $select2[] = [ + 'id' => $value['field'], + 'text' => $value['label'], + ]; + + if ($value['primary_key']){ + $primary_key = $value['field']; + } } + echo ' + + + + +
+
+ {[ "type": "checkbox", "label": "'.tr('Importa prima riga').'", "name": "first_row", "extra":"", "value": "1" ]} +
+
+ {[ "type": "select", "label": "'.tr('Chiave primaria').'", "name": "primary_key", "values": '.json_encode($select2).', "value": "'.$primary_key.'" ]} +
+
'; + $rows = Import::getFile($id_record, $records[0]['id'], [ 'limit' => 10, ]);