diff --git a/code/app/Importers/CSV/CSVImporter.php b/code/app/Importers/CSV/CSVImporter.php index 30087ef6..393c680e 100644 --- a/code/app/Importers/CSV/CSVImporter.php +++ b/code/app/Importers/CSV/CSVImporter.php @@ -16,28 +16,14 @@ public function extraInformations() return null; } + /* + Come parametro di questa funzione si usa il nome della sotto-classe di + CSVImporter che si intende usare, scritto tutto minuscolo + */ public static function getImporter($type) { - $ret = null; - - switch($type) { - case 'products': - $ret = new Products(); - break; - case 'users': - $ret = new Users(); - break; - case 'movements': - $ret = new Movements(); - break; - case 'deliveries': - $ret = new Deliveries(); - break; - default: - Log::error('Unexpected type for CSV import: ' . $type); - break; - } - + $classname = 'App\\Importers\\CSV\\' . ucwords($type); + $ret = new $classname; return $ret; } @@ -67,6 +53,48 @@ private function guessCsvFileSeparator($path) return $target_separator; } + /* + Se la sotto-classe specifica un attributo "sorted_fields" tra i + parametri, pre-popolo l'array dei campi selezionati in fase di revisione + del CSV. Questo viene usato, ad esempio, dall'importer "Products" per + inizializzare l'importazione dei prodotti di un certo fornitore usando + sempre le stesse colonne (anziché doverle riassegnare a mano ogni volta) + */ + private function retrievePreSelectedFields($parameters) + { + $selected = []; + + if (isset($parameters['sorted_fields']) && empty($parameters['sorted_fields']) == false) { + $sorted = $parameters['sorted_fields']; + $fields = $this->fields(); + + foreach($parameters['columns'] as $index => $c) { + if (isset($sorted[$index])) { + $selected[] = (object) [ + 'label' => $fields[$sorted[$index]]->label, + 'name' => $sorted[$index], + ]; + } + else { + $selected[] = (object) [ + 'label' => _i('[Ignora]'), + 'name' => 'none', + ]; + } + } + } + else { + foreach($parameters['columns'] as $c) { + $selected[] = (object) [ + 'label' => _i('[Ignora]'), + 'name' => 'none', + ]; + } + } + + return $selected; + } + protected function storeUploadedFile($request, $parameters) { try { @@ -94,7 +122,7 @@ protected function storeUploadedFile($request, $parameters) } $parameters['columns'] = $sample_line; - + $parameters['selected'] = $this->retrievePreSelectedFields($parameters); return $parameters; } catch (\Exception $e) { diff --git a/code/app/Importers/CSV/Products.php b/code/app/Importers/CSV/Products.php index 8b2c8fed..583bd024 100644 --- a/code/app/Importers/CSV/Products.php +++ b/code/app/Importers/CSV/Products.php @@ -83,6 +83,7 @@ public function guess($request) 'extra_fields' => ['supplier_id' => $s->id], 'extra_description' => [_i('Le categorie e le unità di misura il cui nome non sarà trovato tra quelle esistenti saranno create.')], 'sorting_fields' => $this->fields(), + 'sorted_fields' => json_decode($s->import_template), ]); } @@ -204,7 +205,12 @@ public function select($request) } } - return ['products' => $products, 'supplier' => $s, 'errors' => $errors]; + return [ + 'products' => $products, + 'supplier' => $s, + 'errors' => $errors, + 'sorted_fields' => $columns, + ]; } public function formatSelect($parameters) @@ -285,6 +291,9 @@ public function run($request) break; } + $s->import_template = json_encode(explode(',', $request['sorted_fields'])); + $s->save(); + DB::commit(); return [ diff --git a/code/database/migrations/2024_10_13_151304_supplier_import_template.php b/code/database/migrations/2024_10_13_151304_supplier_import_template.php new file mode 100644 index 00000000..1e9e78a3 --- /dev/null +++ b/code/database/migrations/2024_10_13_151304_supplier_import_template.php @@ -0,0 +1,28 @@ +string('import_template', 1000)->default(''); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('suppliers', function (Blueprint $table) { + $table->dropColumn('import_template'); + }); + } +}; diff --git a/code/resources/views/import/csvproductsselect.blade.php b/code/resources/views/import/csvproductsselect.blade.php index b3e5c69f..ac2e74af 100644 --- a/code/resources/views/import/csvproductsselect.blade.php +++ b/code/resources/views/import/csvproductsselect.blade.php @@ -20,6 +20,7 @@ +
diff --git a/code/resources/views/import/csvsortcolumns.blade.php b/code/resources/views/import/csvsortcolumns.blade.php index 95d41d41..80ba64ff 100644 --- a/code/resources/views/import/csvsortcolumns.blade.php +++ b/code/resources/views/import/csvsortcolumns.blade.php @@ -43,14 +43,17 @@
    - @foreach($columns as $index => $column) -
  • {{ _i('Colonna') }} {{ $index + 1 }}: {{ _i('[Ignora]') }}
  • + @foreach($selected as $index => $sel) +
  • + {{ _i('Colonna') }} {{ $index + 1 }}: {{ $sel->label }} +
  • @endforeach
  • {{ _i('[Ignora]') }}
  • + @foreach($sorting_fields as $name => $metadata)
  • {{ $metadata->label }}