Skip to content

Commit

Permalink
qualche fix in importazione da archivio centrale
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Nov 25, 2023
1 parent fcb01c1 commit 7ab9d24
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 18 deletions.
5 changes: 5 additions & 0 deletions code/app/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public function children(): HasMany
{
return $this->hasMany('App\Category', 'parent_id');
}

public static function defaultValue()
{
return 'non-specificato';
}
}
8 changes: 5 additions & 3 deletions code/app/Http/Controllers/CategoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down Expand Up @@ -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();
}

Expand Down
9 changes: 6 additions & 3 deletions code/app/Http/Controllers/MeasuresController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down Expand Up @@ -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]);
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions code/app/Importers/CSV/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion code/app/Importers/GDXP/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions code/app/Importers/GDXP/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
13 changes: 12 additions & 1 deletion code/app/Importers/GDXP/Suppliers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions code/app/Measure.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public function products(): HasMany
{
return $this->hasMany('App\Product')->orderBy('name', 'asc');
}

public static function defaultValue()
{
return 'non-specificato';
}
}
6 changes: 4 additions & 2 deletions code/app/Services/ProductsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 4 additions & 6 deletions code/resources/views/import/esmodal.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@
<table class="table" id="remoteSuppliers">
<thead>
<tr>
<th width="20%">{{ _i('Nome') }}</th>
<th width="15%">{{ _i('Città') }}</th>
<th width="25%">{{ _i('Nome') }}</th>
<th width="20%">{{ _i('Partita IVA') }}</th>
<th width="20%">{{ _i('Aggiornato') }}</th>
<th width="20%">{{ _i('Ultima Lettura') }}</th>
<th width="25%">{{ _i('Aggiornato') }}</th>
<th width="25%">{{ _i('Ultima Lettura') }}</th>
<th width="5%">{{ _i('Importa') }}</th>
</tr>
</thead>
<tbody>
@foreach($entries as $entry)
<?php $mine = App\Supplier::where('vat', $entry->vat)->first() ?>
<tr>
<td><span class="text-filterable-cell">{{ $entry->name }}</span></td>
<td><span class="text-filterable-cell">{{ $entry->locality }}</span></td>
<td><span class="text-filterable-cell">{{ $entry->name }} ({{ $entry->locality }})</span></td>
<td><span class="text-filterable-cell">{{ $entry->vat }}</span></td>
<td>{{ printableDate($entry->lastchange) }}</td>
<td>{{ $mine ? printableDate($mine->remote_lastimport) : _i('Mai') }}</td>
Expand Down
6 changes: 6 additions & 0 deletions code/resources/views/supplier/products.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
@can('supplier.modify', $supplier)
@if($supplier->remote_lastimport)
<div class="alert alert-info mb-2">
{{ _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.") }}
</div>
@endif

<div class="row">
<div class="col">
@include('commons.addingbutton', [
Expand Down

0 comments on commit 7ab9d24

Please sign in to comment.