Effettuato modifica in importazione EC generic CSV per controllo inserimento senza duplicati

This commit is contained in:
Amministratore 2025-09-25 16:38:00 +02:00
parent 93de5c36c5
commit 0a968bdc61
3 changed files with 18 additions and 6 deletions

View File

@ -385,7 +385,8 @@ class MovimentiController extends Controller
$data = $this->mapCsvLineToDb($line); $data = $this->mapCsvLineToDb($line);
// dd($data); // dd($data);
if (isset($data['mov_data'])) { if (isset($data['mov_data'])) {
Movimenti::create($data); Movimenti::importNoDuplicate($data);
//Movimenti::create($data);
} }
}); });

View File

@ -28,7 +28,8 @@ class Movimenti extends Model
'mov_inserito_da', 'mov_inserito_da',
'mov_fk_tags', 'mov_fk_tags',
'conto_id_da', 'conto_id_da',
'conto_id_a' 'conto_id_a',
'import_hash',
]; ];
public static $query= 'SELECT public static $query= 'SELECT
@ -241,24 +242,33 @@ public function ContoA()
} }
private function makeHash($data,$movimento_dare, $movimento_avere, $descrizione) public static function makeHash($data,$movimento_dare, $movimento_avere, $descrizione)
{ {
// crea l'hash (serve per evitare duplicati nei movimenti) // crea l'hash (serve per evitare duplicati nei movimenti)
return md5($data.$movimento_dare.$movimento_avere.$descrizione); return md5($data.$movimento_dare.$movimento_avere.$descrizione);
} }
public function importHashExists($hash) public static function importHashExists($hash)
{ {
return self::where('import_hash', $hash)->exists(); return self::where('import_hash', $hash)->exists();
} }
public function importNoDuplicate($data, $movimento_dare, $movimento_avere, $descrizione) public static function importNoDuplicate($data)
{ {
$hash = $this->makeHash($data, $movimento_dare, $movimento_avere, $descrizione); $hash = Movimenti::makeHash($data['mov_data'], $data['mov_importo_dare'], $data['mov_importo_avere'], $data['mov_descrizione']);
$exists = self::where('import_hash', $hash)->exists(); $exists = self::where('import_hash', $hash)->exists();
if (!$exists) { if (!$exists) {
self::create([ self::create([
// TODO: ...altri campi... // TODO: ...altri campi...
'mov_data' => $data['mov_data'],
'mov_fk_categoria' => $data['mov_fk_categoria'],
'mov_descrizione' => $data['mov_descrizione'],
'mov_importo_dare' => $data['mov_importo_dare'],
'mov_importo_avere' => $data['mov_importo_avere'],
'mov_inserito_da' => 1,
'conto_id_da' => 1,
'conto_id_a' => 1,
'mov_fk_tags' => 1,
'import_hash' => $hash, 'import_hash' => $hash,
]); ]);
} }

View File

@ -28,6 +28,7 @@ class CreateMovimentisTable extends Migration
$table->foreign('mov_fk_tags')->references('id')->on('tags'); $table->foreign('mov_fk_tags')->references('id')->on('tags');
$table->foreignId('conto_id_da')->constrained('contos')->nullable(); $table->foreignId('conto_id_da')->constrained('contos')->nullable();
$table->foreignId('conto_id_a')->constrained('contos')->nullable(); $table->foreignId('conto_id_a')->constrained('contos')->nullable();
$table->string('import_hash')->nullable()->index();
}); });
} }