Skip to content

Commit

Permalink
perfezionata importazione prodotti per gestire listini lunghi
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Oct 29, 2023
1 parent cc8a898 commit 89efa2d
Show file tree
Hide file tree
Showing 21 changed files with 162 additions and 55 deletions.
2 changes: 1 addition & 1 deletion code/app/Helpers/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function formatObjectsToComponentRec($options)
$ret = [];

foreach($options as $option) {
if (is_a($option, \App\Models\Concerns\HasChildren::class) && $option->children()->count() != 0) {
if (is_a($option, \App\Models\Concerns\HasChildren::class) && $option->children->count() != 0) {
$ret[$option->id] = (object) [
// @phpstan-ignore-next-line
'label' => $option->printableName(),
Expand Down
10 changes: 10 additions & 0 deletions code/app/Http/Controllers/ProductsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,14 @@ public function updatePrices(Request $request, $id)

return $this->successResponse();
}

public function search(Request $request)
{
return $this->easyExecute(function() use ($request) {
$supplier = $request->input('supplier');
$term = $request->input('term');
$products = $this->service->search($supplier, $term);
return response()->json($products);
});
}
}
23 changes: 14 additions & 9 deletions code/app/Importers/CSV/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,18 @@ public function select($request)
$test = null;

if ($supplier_code_index != -1 && filled($line[$supplier_code_index])) {
$test = $all_products->firstWhere('supplier_code', $line[$supplier_code_index]);
$test = $all_products->firstWhereAbout('supplier_code', $line[$supplier_code_index]);
}

if (is_null($test)) {
if ($name_index != -1 && filled($line[$name_index])) {
$test = $all_products->firstWhere('name', $line[$name_index]);
$test = $all_products->firstWhereAbout('name', $line[$name_index]);
}
}

$want_replace = is_null($test) ? 0 : $test->id;

if ($want_replace) {
if (is_null($test) == false) {
$p = $test;
$p->want_replace = $test;
}
else {
$p = new Product();
Expand All @@ -143,13 +142,12 @@ public function select($request)
$p->multiple = 0;
$p->package_size = 0;
$p->portion_quantity = 0;
$p->want_replace = null;
$price_without_vat = null;
$vat_rate = null;
$package_price = null;
}

$p->want_replace = $want_replace;

foreach ($columns as $index => $field) {
$value = trim($line[$index]);

Expand Down Expand Up @@ -268,8 +266,15 @@ public function run($request)
}
}

if ($request->has('reset_list')) {
$s->products()->whereNotIn('id', $products_ids)->update(['active' => false]);
$reset_mode = $request->input('reset_list');
switch($reset_mode) {
case 'disable':
$s->products()->whereNotIn('id', $products_ids)->update(['active' => false]);
break;

case 'remove':
$s->products()->whereNotIn('id', $products_ids)->delete();
break;
}

DB::commit();
Expand Down
14 changes: 14 additions & 0 deletions code/app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ public function boot()
/** @var Collection $this */
return $this->sortBy(fn($b) => $b->user->printableName());
});

/*
Estrapola da una Collection il primo elemento il cui attributo $attr
ha un valore "simile" a $value
*/
Collection::macro('firstWhereAbout', function ($attr, $value) {
$value = preg_replace('/[^a-zA-Z0-9]*/', '', mb_strtolower(trim($value)));

/** @var Collection $this */
return $this->first(function($o, $k) use ($attr, $value) {
$test = preg_replace('/[^a-zA-Z0-9]*/', '', mb_strtolower(trim($o->$attr)));
return $test == $value;
});
});
}

public function register()
Expand Down
24 changes: 22 additions & 2 deletions code/app/Services/ProductsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,29 @@

class ProductsService extends BaseService
{
public function list($term = '', $all = false)
public function search($supplier_id, $term)
{
/* TODO */
$ret = (object) [
'results' => [
(object) [
'id' => 0,
'text' => _i('Nessuno'),
]
]
];

$supplier = Supplier::findOrFail($supplier_id);
$term = sprintf('%%%s%%', $term);
$products = $supplier->products()->where('name', 'like', $term)->orderBy('name', 'asc')->get();

foreach($products as $prod) {
$ret->results[] = (object) [
'id' => $prod->id,
'text' => $prod->printableName(),
];
}

return $ret;
}

public function show($id)
Expand Down
13 changes: 13 additions & 0 deletions code/config/larastrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@
'classes' => ['inner-form'],
],
],
'wizardform' => [
'extends' => 'form',
'params' => [
'method' => 'POST',
'buttons' => [
[
'color' => 'success',
'label' => 'Avanti',
'attributes' => ['type' => 'submit'],
]
]
],
],
'scheck' => [
'extends' => 'check',
'params' => [
Expand Down
2 changes: 1 addition & 1 deletion code/public/css/gasdotto.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion code/public/css/gasdotto.css.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion code/public/js/gasdotto.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion code/public/js/gasdotto.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions code/public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"/js/gasdotto.js": "/js/gasdotto.js?id=3fea077514cf73a198a676d9df2e8e8c",
"/css/gasdotto.css": "/css/gasdotto.css?id=7cfc917f5221cc270b4ee4c7b1d57124"
"/js/gasdotto.js": "/js/gasdotto.js?id=ee396f1ace20cb49de71b8f9f8dda840",
"/css/gasdotto.css": "/css/gasdotto.css?id=fb5adf37fb0db9ce1e72b40389fc1081"
}
24 changes: 10 additions & 14 deletions code/resources/assets/js/gasdotto.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,7 @@ $(document).ready(function() {
return;
}

var submit_button = utils.j().submitButton(form);

submit_button.each(function() {
var idle_text = $(this).text();
$(this).attr('data-idle-text', idle_text).empty().append('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>').prop('disabled', true);
});
utils.spinSubmitButton(form);

var params = {
method: form.attr('method'),
Expand All @@ -570,21 +565,17 @@ $(document).ready(function() {

success: function(data) {
if (miscInnerCallbacks(form, data) == true) {
submit_button.each(function() {
utils.j().submitButton(form).each(function() {
utils.inlineFeedback($(this), _('Salvato!'));
});
}
else {
submit_button.each(function() {
utils.inlineFeedback($(this), _('ERRORE!'));
});
utils.formErrorFeedback(form);
}
},

error: function(data) {
submit_button.each(function() {
utils.inlineFeedback($(this), _('ERRORE!'));
});
utils.formErrorFeedback(form);
}
};

Expand Down Expand Up @@ -799,7 +790,8 @@ $(document).ready(function() {

var form = $(this);
var data = form.serializeArray();
form.find('button[type=submit]').prop('disabled', true);

utils.spinSubmitButton(form);

$.ajax({
method: form.attr('method'),
Expand All @@ -809,6 +801,10 @@ $(document).ready(function() {

success: function(data) {
wizardLoadPage(form, data);
},

error: function(data) {
utils.formErrorFeedback(form);
}
});

Expand Down
21 changes: 21 additions & 0 deletions code/resources/assets/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ class Utils {
return url;
}

static spinSubmitButton(form)
{
let submit_button = this.j().submitButton(form);

submit_button.each(function() {
var idle_text = $(this).text();
$(this).attr('data-idle-text', idle_text).empty().append('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>').prop('disabled', true);
});
}

static formErrorFeedback(form)
{
let submit_button = this.j().submitButton(form);

submit_button.each((index, button) => {
let btn = $(button);
this.inlineFeedback(btn, _('ERRORE!'));
btn.prop('disabled', true);
});
}

static inlineFeedback(button, feedback_text)
{
var idle_text = button.attr('data-idle-text');
Expand Down
14 changes: 14 additions & 0 deletions code/resources/assets/js/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ class Widgets {
dropdownParent: container,
});

$('.remote-select', container).each((index, node) => {
node = $(node);
let url = node.attr('data-remote-url');

node.select2({
theme: "bootstrap-5",
dropdownParent: container,
ajax: {
url: url,
dataType: 'JSON',
},
});
});

/*
https://stackoverflow.com/questions/15989591/how-can-i-keep-bootstrap-popover-alive-while-the-popover-is-being-hovered
*/
Expand Down
4 changes: 4 additions & 0 deletions code/resources/assets/sass/gasdotto.scss
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ table {
margin-bottom: 0;
font-weight: normal;
}

&.fixed-table {
table-layout: fixed;
}
}

.dynamic-table {
Expand Down
4 changes: 2 additions & 2 deletions code/resources/views/import/csvbookingsselect.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<x-larastrap::modal :title="_i('Importa consegne')" size="fullscreen">
<div class="wizard_page">
<x-larastrap::form method="POST" :action="url('import/csv?type=deliveries&step=run')" :buttons="[['color' => 'success', 'type' => 'submit', 'label' => _i('Avanti')]]">
<x-larastrap::wizardform :action="url('import/csv?type=deliveries&step=run')">
<input type="hidden" name="aggregate_id" value="{{ $aggregate_id }}">
<input type="hidden" name="order_id" value="{{ $order_id }}">
<input type="hidden" name="data" value="{{ json_encode($data) }}">
Expand Down Expand Up @@ -32,6 +32,6 @@
@endforeach
</tbody>
</table>
</x-larastrap::form>
</x-larastrap::wizardform>
</div>
</x-larastrap::modal>
4 changes: 2 additions & 2 deletions code/resources/views/import/csvmovementsselect.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<input type="hidden" name="matching_methods_for_movement_types" value='{!! json_encode($methods) !!}'>

<div class="wizard_page">
<x-larastrap::form method="POST" :action="url('import/csv?type=movements&step=run')" :buttons="[['color' => 'success', 'type' => 'submit', 'label' => _i('Avanti')]]">
<x-larastrap::wizardform :action="url('import/csv?type=movements&step=run')">
@if(!empty($errors))
<p>
{{ _i('Errori') }}:
Expand Down Expand Up @@ -100,6 +100,6 @@
@endforeach
</tbody>
</table>
</x-larastrap::form>
</x-larastrap::wizardform>
</div>
</x-larastrap::modal>
39 changes: 24 additions & 15 deletions code/resources/views/import/csvproductsselect.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
$original_products = $supplier->products;
$categories = App\Category::orderBy('name', 'asc')->where('parent_id', '=', null)->get();
$categories = App\Category::orderBy('name', 'asc')->whereNull('parent_id')->with('children')->get();
$measures = App\Measure::orderBy('name', 'asc')->get();
$vat_rates = App\VatRate::orderBy('percentage', 'asc')->get();
Expand All @@ -19,26 +18,28 @@
</div>
@endif

<x-larastrap::form method="POST" :action="url('import/csv?type=products&step=run')" :buttons="[['color' => 'success', 'type' => 'submit', 'label' => _i('Avanti')]]">
<x-larastrap::wizardform :action="url('import/csv?type=products&step=run')">
<input type="hidden" name="supplier_id" value="{{ $supplier->id }}">

<div class="row">
<div class="col-md-6">
<x-larastrap::check name="reset_list" :label="_i('Disattiva prodotti di questo fornitore non inclusi nell\'elenco')" />
<x-larastrap::radios name="reset_list" :label="_i('Prodotti Esistenti')" :options="['no' => _i('Ignora'), 'disable' => _i('Disabilita'), 'remove' => _i('Elimina')]" value="no" />
</div>
</div>

<table class="table">
<hr>

<table class="table fixed-table">
<thead>
<tr>
<th width="3%">{{ _i('Importa') }}</th>
<th width="5%">{{ _i('Importa') }}</th>
<th width="15%">{{ _i('Nome') }}</th>
<th width="15%">{{ _i('Descrizione') }}</th>
<th width="8%">{{ _i('Prezzo Unitario') }}</th>
<th width="11%">{{ _i('Categoria') }}</th>
<th width="11%">{{ _i('Unità di Misura') }}</th>
<th width="11%">{{ _i('Aliquota IVA') }}</th>
<th width="9%">{{ _i('Codice Fornitore') }}</th>
<th width="10%">{{ _i('Prezzo Unitario') }}</th>
<th width="10%">{{ _i('Categoria') }}</th>
<th width="10%">{{ _i('Unità di Misura') }}</th>
<th width="10%">{{ _i('Aliquota IVA') }}</th>
<th width="10%">{{ _i('Codice Fornitore') }}</th>
<th width="15%">{{ _i('Aggiorna') }}</th>
</tr>
</thead>
Expand Down Expand Up @@ -88,9 +89,17 @@
<td>
<x-larastrap::text name="supplier_code" squeeze npostfix="[]" />
</td>
<td>
@if($original_products->isEmpty() == false)
<x-larastrap::selectobj name="want_replace" squeeze npostfix="[]" :options="$original_products" :extraitem="_i('Nessuno')" :value="$product->want_replace" />
<td width="15%">
@if($supplier->products->isEmpty() == false)
@php
$original_products = [0 => _i('Nessuno')];
if ($product->want_replace) {
$original_products[$product->want_replace->id] = $product->want_replace->printableName();
}
@endphp
<x-larastrap::select name="want_replace" squeeze npostfix="[]" :options="$original_products" :value="$product->want_replace->id ?? 0" classes="remote-select" :data-remote-url="route('products.search', ['supplier' => $supplier->id])" />
@else
{{ _i('Nessun Prodotto Aggiornabile') }}
<input type="hidden" name="want_replace[]" value="0">
Expand All @@ -101,6 +110,6 @@
@endforeach
</tbody>
</table>
</x-larastrap::form>
</x-larastrap::wizardform>
</div>
</x-larastrap::modal>
Loading

0 comments on commit 89efa2d

Please sign in to comment.