Skip to content

Commit

Permalink
piccole correzioni in importazione csv; rimosso elenco prodotti in pa…
Browse files Browse the repository at this point in the history
…nnello delle unità di misura
  • Loading branch information
madbob committed Oct 29, 2023
1 parent 43b8a0f commit 1b6b9bf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
6 changes: 0 additions & 6 deletions code/app/Http/Controllers/MeasuresController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ public function update(Request $request, $id)
return $this->successResponse();
}

public function listProducts(Request $request, $id)
{
$measure = Measure::findOrFail($id);
return view('measures.products-list', ['products' => $measure->products]);
}

public function discretes()
{
$measures = Measure::all();
Expand Down
20 changes: 12 additions & 8 deletions code/app/Importers/CSV/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public function guess($request)
]);
}

private function mapSelection($class, $param, $value, $field, &$product)
private function mapSelection($objects, $param, $value, $field, &$product)
{
$test = $class::where($param, $value)->first();
$test = $objects->firstWhere($param, $value);
if (is_null($test)) {
return 'temp_' . $field . '_name';
}
Expand All @@ -106,6 +106,10 @@ public function select($request)
$s = $this->getSupplier($request);

$products = $errors = [];
$all_products = $s->products;
$all_categories = Category::all();
$all_measures = Measure::all();
$all_vatrates = VatRate::all();

foreach($reader->getRecords() as $line) {
if (empty($line) || (count($line) == 1 && empty($line[0]))) {
Expand All @@ -116,12 +120,12 @@ public function select($request)
$test = null;

if ($supplier_code_index != -1 && filled($line[$supplier_code_index])) {
$test = $s->products()->withTrashed()->where('supplier_code', $line[$supplier_code_index])->orderBy('id', 'desc')->first();
$test = $all_products->firstWhere('supplier_code', $line[$supplier_code_index]);
}

if (is_null($test)) {
if ($name_index != -1 && filled($line[$name_index])) {
$test = $s->products()->withTrashed()->where('name', $line[$name_index])->orderBy('id', 'desc')->first();
$test = $all_products->firstWhere('name', $line[$name_index]);
}
}

Expand Down Expand Up @@ -150,10 +154,10 @@ public function select($request)
$value = trim($line[$index]);

if ($field == 'category') {
$field = $this->mapSelection(Category::class, 'name', $value, 'category', $p);
$field = $this->mapSelection($all_categories, 'name', $value, 'category', $p);
}
elseif ($field == 'measure') {
$field = $this->mapSelection(Measure::class, 'name', $value, 'measure', $p);
$field = $this->mapSelection($all_measures, 'name', $value, 'measure', $p);
}
elseif ($field == 'price') {
$value = guessDecimal($value);
Expand All @@ -165,7 +169,7 @@ public function select($request)
continue;
}
else {
$field = $this->mapSelection(VatRate::class, 'percentage', $value, 'vat_rate', $p);
$field = $this->mapSelection($all_vatrates, 'percentage', $value, 'vat_rate', $p);
$vat_rate = $value;
}
}
Expand Down Expand Up @@ -222,7 +226,7 @@ public function run($request)

foreach($data['import'] as $index) {
try {
if ($data['want_replace'][$index] != '0') {
if (isset($data['want_replace'][$index]) && $data['want_replace'][$index] != '0') {
$p = Product::find($data['want_replace'][$index]);
}
else {
Expand Down
14 changes: 12 additions & 2 deletions code/resources/views/measures/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,18 @@
[
'label' => _i('Prodotti'),
'field' => 'id',
'type' => 'custom',
'contents' => '<button type="button" class="btn btn-light async-popover" data-contents-url="' . url('measures/list/%s') . '" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="right" data-bs-content="placeholder" data-bs-html="true" data-bs-trigger="hover"><i class="bi-list"></i></button>'
'type' => 't',
'extra_callback' => function($content, $attributes) {
$count = $content->products()->count();
if ($count) {
$attributes['value'] = $count;
}
else {
$attributes['value'] = '-';
}
return $attributes;
}
]
]
])
Expand Down
11 changes: 0 additions & 11 deletions code/resources/views/measures/products-list.blade.php

This file was deleted.

1 change: 0 additions & 1 deletion code/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
Route::get('categories/{id}/header', 'CategoriesController@objhead')->name('categories.objhead');

Route::get('measures/{id}/header', 'MeasuresController@objhead')->name('measures.objhead');
Route::get('measures/list/{id}', 'MeasuresController@listProducts');
Route::get('measures/discretes', 'MeasuresController@discretes');

Route::get('variants/{id}/matrix', 'VariantsController@matrix')->name('variants.matrix');
Expand Down

0 comments on commit 1b6b9bf

Please sign in to comment.