From 1b6b9bff3e7f24699d9c87c7c95f60aaae7f697a Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Sun, 29 Oct 2023 15:49:43 +0100 Subject: [PATCH] =?UTF-8?q?piccole=20correzioni=20in=20importazione=20csv;?= =?UTF-8?q?=20rimosso=20elenco=20prodotti=20in=20pannello=20delle=20unit?= =?UTF-8?q?=C3=A0=20di=20misura?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Http/Controllers/MeasuresController.php | 6 ------ code/app/Importers/CSV/Products.php | 20 +++++++++++-------- code/resources/views/measures/edit.blade.php | 14 +++++++++++-- .../views/measures/products-list.blade.php | 11 ---------- code/routes/web.php | 1 - 5 files changed, 24 insertions(+), 28 deletions(-) delete mode 100644 code/resources/views/measures/products-list.blade.php diff --git a/code/app/Http/Controllers/MeasuresController.php b/code/app/Http/Controllers/MeasuresController.php index 337596575..321a3ca1a 100644 --- a/code/app/Http/Controllers/MeasuresController.php +++ b/code/app/Http/Controllers/MeasuresController.php @@ -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(); diff --git a/code/app/Importers/CSV/Products.php b/code/app/Importers/CSV/Products.php index 55b4f1d0c..3ba431b55 100644 --- a/code/app/Importers/CSV/Products.php +++ b/code/app/Importers/CSV/Products.php @@ -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'; } @@ -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]))) { @@ -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]); } } @@ -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); @@ -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; } } @@ -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 { diff --git a/code/resources/views/measures/edit.blade.php b/code/resources/views/measures/edit.blade.php index edb0c5c94..afc28b89c 100644 --- a/code/resources/views/measures/edit.blade.php +++ b/code/resources/views/measures/edit.blade.php @@ -40,8 +40,18 @@ [ 'label' => _i('Prodotti'), 'field' => 'id', - 'type' => 'custom', - 'contents' => '' + 'type' => 't', + 'extra_callback' => function($content, $attributes) { + $count = $content->products()->count(); + if ($count) { + $attributes['value'] = $count; + } + else { + $attributes['value'] = '-'; + } + + return $attributes; + } ] ] ]) diff --git a/code/resources/views/measures/products-list.blade.php b/code/resources/views/measures/products-list.blade.php deleted file mode 100644 index 84de4c68f..000000000 --- a/code/resources/views/measures/products-list.blade.php +++ /dev/null @@ -1,11 +0,0 @@ -@if($products->isEmpty()) -
- {{ _i('Non ci sono prodotti') }} -
-@else - -@endif diff --git a/code/routes/web.php b/code/routes/web.php index 5f60a9d38..552987a8c 100644 --- a/code/routes/web.php +++ b/code/routes/web.php @@ -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');