Skip to content

Commit

Permalink
gestione dei campi csv importati per ciascun fornitore. closes #291
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Oct 13, 2024
1 parent c22e4af commit e4a0cc6
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 24 deletions.
70 changes: 49 additions & 21 deletions code/app/Importers/CSV/CSVImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -94,7 +122,7 @@ protected function storeUploadedFile($request, $parameters)
}

$parameters['columns'] = $sample_line;

$parameters['selected'] = $this->retrievePreSelectedFields($parameters);
return $parameters;
}
catch (\Exception $e) {
Expand Down
11 changes: 10 additions & 1 deletion code/app/Importers/CSV/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -285,6 +291,9 @@ public function run($request)
break;
}

$s->import_template = json_encode(explode(',', $request['sorted_fields']));
$s->save();

DB::commit();

return [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('suppliers', function (Blueprint $table) {
$table->string('import_template', 1000)->default('');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('suppliers', function (Blueprint $table) {
$table->dropColumn('import_template');
});
}
};
1 change: 1 addition & 0 deletions code/resources/views/import/csvproductsselect.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<x-larastrap::wizardform :action="url('import/csv?type=products&step=run')">
<input type="hidden" name="supplier_id" value="{{ $supplier->id }}">
<input type="hidden" name="sorted_fields" value="{{ join(',', $sorted_fields) }}">

<div class="row">
<div class="col-md-6">
Expand Down
7 changes: 5 additions & 2 deletions code/resources/views/import/csvsortcolumns.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@
</div>
<div class="col-4">
<ul class="list-group">
@foreach($columns as $index => $column)
<li class="list-group-item im_droppable">{{ _i('Colonna') }} <span class="columns_index">{{ $index + 1 }}</span>: <span class="column_content"><input type="hidden" name="column[]" value="none" />{{ _i('[Ignora]') }}</span></li>
@foreach($selected as $index => $sel)
<li class="list-group-item im_droppable">
{{ _i('Colonna') }} <span class="columns_index">{{ $index + 1 }}</span>: <span class="column_content"><input type="hidden" name="column[]" value="{{ $sel->name }}" />{{ $sel->label }}</span>
</li>
@endforeach
</ul>
</div>
<div class="col-4">
<ul class="list-group">
<li class="list-group-item im_draggable"><input type="hidden" name="wannabe_column[]" value="none" />{{ _i('[Ignora]') }}</li>

@foreach($sorting_fields as $name => $metadata)
<li class="list-group-item im_draggable"><input type="hidden" name="wannabe_column[]" value="{{ $name }}" />
{{ $metadata->label }}
Expand Down

0 comments on commit e4a0cc6

Please sign in to comment.