From 7ab9d24010309284fed4ca9f0821df9a68070f37 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Sat, 25 Nov 2023 15:56:05 +0100 Subject: [PATCH] qualche fix in importazione da archivio centrale --- code/app/Category.php | 5 +++++ code/app/Http/Controllers/CategoriesController.php | 8 +++++--- code/app/Http/Controllers/MeasuresController.php | 9 ++++++--- code/app/Importers/CSV/Products.php | 4 ++-- code/app/Importers/GDXP/Contacts.php | 3 ++- code/app/Importers/GDXP/Products.php | 6 ++++++ code/app/Importers/GDXP/Suppliers.php | 13 ++++++++++++- code/app/Measure.php | 5 +++++ code/app/Services/ProductsService.php | 6 ++++-- code/resources/views/import/esmodal.blade.php | 10 ++++------ code/resources/views/supplier/products.blade.php | 6 ++++++ 11 files changed, 57 insertions(+), 18 deletions(-) diff --git a/code/app/Category.php b/code/app/Category.php index 7fff19348..a5e547ae2 100644 --- a/code/app/Category.php +++ b/code/app/Category.php @@ -26,4 +26,9 @@ public function children(): HasMany { return $this->hasMany('App\Category', 'parent_id'); } + + public static function defaultValue() + { + return 'non-specificato'; + } } diff --git a/code/app/Http/Controllers/CategoriesController.php b/code/app/Http/Controllers/CategoriesController.php index f59cf2fb6..8da991cbd 100644 --- a/code/app/Http/Controllers/CategoriesController.php +++ b/code/app/Http/Controllers/CategoriesController.php @@ -28,7 +28,7 @@ public function index() abort(503); } - $categories = Category::where('id', '!=', 'non-specificato')->where('parent_id', '=', null)->get(); + $categories = Category::where('id', '!=', Category::defaultValue())->where('parent_id', '=', null)->get(); return view('categories.edit', ['categories' => $categories]); } @@ -104,13 +104,15 @@ public function update(Request $request, $id) } $data = $request->input('serialized'); - $accumulator = ['non-specificato']; + $accumulator = [ + Category::defaultValue() + ]; $this->updateRecursive($data, null, $accumulator); $orphaned_products = Product::withoutGlobalScopes()->whereNotIn('category_id', $accumulator)->get(); foreach($orphaned_products as $op) { - $op->category_id = 'non-specificato'; + $op->category_id = Category::defaultValue(); $op->save(); } diff --git a/code/app/Http/Controllers/MeasuresController.php b/code/app/Http/Controllers/MeasuresController.php index 321a3ca1a..61fcb8e37 100644 --- a/code/app/Http/Controllers/MeasuresController.php +++ b/code/app/Http/Controllers/MeasuresController.php @@ -27,7 +27,7 @@ public function index() abort(503); } - $measures = Measure::where('id', '!=', 'non-specificato')->orderBy('name', 'asc')->get(); + $measures = Measure::where('id', '!=', Measure::defaultValue())->orderBy('name', 'asc')->get(); return view('measures.edit', ['measures' => $measures]); } @@ -65,7 +65,10 @@ public function update(Request $request, $id) $ids = $request->input('id', []); $new_names = $request->input('name', []); $new_discretes = $request->input('discrete', []); - $saved_ids = ['non-specificato']; + + $saved_ids = [ + Measure::defaultValue() + ]; for ($i = 0; $i < count($ids); ++$i) { $name = trim($new_names[$i]); @@ -85,7 +88,7 @@ public function update(Request $request, $id) $saved_ids[] = $measure->id; } - Product::whereNotIn('measure_id', $saved_ids)->withTrashed()->update(['measure_id' => 'non-specificato']); + Product::whereNotIn('measure_id', $saved_ids)->withTrashed()->update(['measure_id' => Measure::defaultValue()]); Measure::whereNotIn('id', $saved_ids)->delete(); return $this->successResponse(); diff --git a/code/app/Importers/CSV/Products.php b/code/app/Importers/CSV/Products.php index 4461087e6..28b6e430a 100644 --- a/code/app/Importers/CSV/Products.php +++ b/code/app/Importers/CSV/Products.php @@ -135,8 +135,8 @@ public function select($request) } else { $p = new Product(); - $p->category_id = 'non-specificato'; - $p->measure_id = 'non-specificato'; + $p->category_id = Category::defaultValue(); + $p->measure_id = Measure::defaultValue(); $p->weight = 0; $p->min_quantity = 0; $p->multiple = 0; diff --git a/code/app/Importers/GDXP/Contacts.php b/code/app/Importers/GDXP/Contacts.php index 984337106..3aae31053 100644 --- a/code/app/Importers/GDXP/Contacts.php +++ b/code/app/Importers/GDXP/Contacts.php @@ -15,12 +15,13 @@ private static function map($attribute) { $map = [ 'phoneNumber' => 'phone', + 'mobileNumber' => 'mobile', 'faxNumber' => 'fax', 'emailAddress' => 'email', 'webSite' => 'website', ]; - return $map[$attribute]; + return $map[$attribute] ?? null; } public static function importXML($xml, $parent) diff --git a/code/app/Importers/GDXP/Products.php b/code/app/Importers/GDXP/Products.php index f9ae62c9b..5377f92e1 100644 --- a/code/app/Importers/GDXP/Products.php +++ b/code/app/Importers/GDXP/Products.php @@ -159,12 +159,18 @@ public static function importJSON($master, $json, $replace) $category = Category::firstOrCreate(['name' => $name]); $product->category_id = $category->id; } + else { + $product->category_id = Category::defaultValue(); + } $name = $json->um ?? ''; if (!empty($name)) { $measure = Measure::firstOrCreate(['name' => $name]); $product->measure_id = $measure->id; } + else { + $product->measure_id = Measure::defaultValue(); + } $name = $json->orderInfo->vatRate ?? null; if (!is_null($name)) { diff --git a/code/app/Importers/GDXP/Suppliers.php b/code/app/Importers/GDXP/Suppliers.php index 3634b60fe..db9f25369 100644 --- a/code/app/Importers/GDXP/Suppliers.php +++ b/code/app/Importers/GDXP/Suppliers.php @@ -183,7 +183,18 @@ public static function importJSON($master, $json, $replace) } foreach($json->products as $json_product) { - $ex_product = $supplier->products()->where('name', $json_product->name)->first(); + $pname = $json_product->name; + $psku = $json_product->sku ?? ''; + $ex_product = null; + + if (empty($psku) == false) { + $ex_product = $supplier->products()->where('supplier_code', $psku)->first(); + } + + if (is_null($ex_product)) { + $ex_product = $supplier->products()->where('name', $pname)->first(); + } + $product = Products::importJSON($master, $json_product, $ex_product->id ?? null); $product->supplier_id = $supplier->id; $product->save(); diff --git a/code/app/Measure.php b/code/app/Measure.php index 26cd1f2a9..5b3e90843 100644 --- a/code/app/Measure.php +++ b/code/app/Measure.php @@ -25,4 +25,9 @@ public function products(): HasMany { return $this->hasMany('App\Product')->orderBy('name', 'asc'); } + + public static function defaultValue() + { + return 'non-specificato'; + } } diff --git a/code/app/Services/ProductsService.php b/code/app/Services/ProductsService.php index ff0df54b6..2e92375d5 100644 --- a/code/app/Services/ProductsService.php +++ b/code/app/Services/ProductsService.php @@ -9,6 +9,8 @@ use App\User; use App\Supplier; use App\Product; +use App\Category; +use App\Measure; use App\Role; class ProductsService extends BaseService @@ -63,12 +65,12 @@ private function setCommonAttributes(&$product, $request) $this->setIfSet($product, $request, 'category_id'); if (empty($product->category_id)) { - $product->category_id = 'non-specificato'; + $product->category_id = Category::defaultValue(); } $this->setIfSet($product, $request, 'measure_id'); if (empty($product->measure_id)) { - $product->measure_id = 'non-specificato'; + $product->measure_id = Measure::defaultValue(); } $this->transformAndSetIfSet($product, $request, 'vat_rate_id', function($value) { diff --git a/code/resources/views/import/esmodal.blade.php b/code/resources/views/import/esmodal.blade.php index 47ae9bb20..4da268b32 100644 --- a/code/resources/views/import/esmodal.blade.php +++ b/code/resources/views/import/esmodal.blade.php @@ -17,11 +17,10 @@ - - + - - + + @@ -29,8 +28,7 @@ @foreach($entries as $entry) vat)->first() ?> - - + diff --git a/code/resources/views/supplier/products.blade.php b/code/resources/views/supplier/products.blade.php index 9ff51b2b9..12600f23f 100644 --- a/code/resources/views/supplier/products.blade.php +++ b/code/resources/views/supplier/products.blade.php @@ -1,4 +1,10 @@ @can('supplier.modify', $supplier) + @if($supplier->remote_lastimport) +
+ {{ _i("Il listino di questo fornitore è stato importato dall'archivio centralizzato: si raccomanda si modificarlo il meno possibile in modo che sia più semplice poi gestirne gli aggiornamenti futuri.") }} +
+ @endif +
@include('commons.addingbutton', [
{{ _i('Nome') }}{{ _i('Città') }}{{ _i('Nome') }} {{ _i('Partita IVA') }}{{ _i('Aggiornato') }}{{ _i('Ultima Lettura') }}{{ _i('Aggiornato') }}{{ _i('Ultima Lettura') }} {{ _i('Importa') }}
{{ $entry->name }}{{ $entry->locality }}{{ $entry->name }} ({{ $entry->locality }}) {{ $entry->vat }} {{ printableDate($entry->lastchange) }} {{ $mine ? printableDate($mine->remote_lastimport) : _i('Mai') }}