From 4d45be848f4418fa2518f62c111d1f0b164c5eb6 Mon Sep 17 00:00:00 2001 From: Tamino Steinert Date: Fri, 12 Jan 2024 10:42:46 +0100 Subject: [PATCH] =?UTF-8?q?Varianten:=20Sortieren=20der=20Variantentabelle?= =?UTF-8?q?=20erm=C3=B6glicht?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SL/Controller/Part.pm | 45 +++++++++++ SL/DB/Part.pm | 12 +++ js/kivi.Part.js | 74 ++++++++++++++----- .../part/_parent_variant.html | 37 ++++++++-- 4 files changed, 140 insertions(+), 28 deletions(-) diff --git a/SL/Controller/Part.pm b/SL/Controller/Part.pm index 588b03d5e1..0a37ab2952 100644 --- a/SL/Controller/Part.pm +++ b/SL/Controller/Part.pm @@ -857,6 +857,51 @@ sub action_reorder_items { $self->js->run('kivi.Part.redisplay_items', \@to_sort)->render; } +sub action_reorder_variants { + my ($self) = @_; + + my $part= $self->part; + + my %sort_keys = ( + partnumber => sub { $_[0]->partnumber }, + description => sub { $_[0]->description }, + sellprice => sub { $_[0]->sellprice }, + lastcost => sub { $_[0]->lastcost }, + variant_values => sub { $_[0]->variant_values }, + ); + foreach my $variant_property (@{$part->variant_properties}) { + my $key = 'variant_property_' . $variant_property->unique_name; + $sort_keys{$key} = sub { + $_[0]->get_variant_property_value_by_unique_name($variant_property->unique_name)->value; + } + } + + my $method = $sort_keys{$::form->{order_by}}; + + my @items = $part->variants; + + my %variant_id_to_position = + map {$_->{id} => $_->{position}} + @{$::form->{variants}}; + + my @to_sort = map { { old_pos => $variant_id_to_position{$_->id}, order_by => $method->($_) } } @items; + if ($::form->{order_by} =~ /^(sellprice|lastcost)$/) { + if ($::form->{sort_dir}) { + @to_sort = sort { $a->{order_by} <=> $b->{order_by} } @to_sort; + } else { + @to_sort = sort { $b->{order_by} <=> $a->{order_by} } @to_sort; + } + } else { + if ($::form->{sort_dir}) { + @to_sort = sort { $a->{order_by} cmp $b->{order_by} } @to_sort; + } else { + @to_sort = sort { $b->{order_by} cmp $a->{order_by} } @to_sort; + } + }; + + $self->js->run('kivi.Part.redisplay_variants', \@to_sort)->render; +} + sub action_warehouse_changed { my ($self) = @_; diff --git a/SL/DB/Part.pm b/SL/DB/Part.pm index bbed1a5536..75f38aa8de 100644 --- a/SL/DB/Part.pm +++ b/SL/DB/Part.pm @@ -339,6 +339,18 @@ sub buchungsgruppe { shift->buchungsgruppen(@_); } +sub get_variant_property_value_by_unique_name { + my ($self, $variant_property_unique_name) = @_; + + my %unique_name_to_variant_property_value = + map { $_->variant_property->unique_name => $_ } + $self->variant_property_values; + + my $variant_property_value = $unique_name_to_variant_property_value{$variant_property_unique_name} + or confess "Part is not associated with a matching SL::DB::VariantPropertyValue"; + return $variant_property_value; +} + sub get_taxkey { my ($self, %params) = @_; diff --git a/js/kivi.Part.js b/js/kivi.Part.js index f1841e9b5a..12f93acd7e 100644 --- a/js/kivi.Part.js +++ b/js/kivi.Part.js @@ -83,6 +83,60 @@ namespace('kivi.Part', function(ns) { $.post("controller.pl", data, kivi.eval_json_result); }; + ns.redisplay_items = function(data) { + var old_rows; + var part_type = $("#part_part_type").val(); + if (part_type === 'assortment') { + old_rows = $('.assortment_item_row').detach(); + } else if ( part_type === 'assembly') { + old_rows = $('.assembly_item_row').detach(); + } + var new_rows = []; + $(data).each(function(_idx, elt) { + new_rows.push(old_rows[elt.old_pos - 1]); + }); + if (part_type === 'assortment') { + $(new_rows).appendTo($('#assortment_items')); + } else if ( part_type === 'assembly') { + $(new_rows).appendTo($('#assembly_items')); + } + ns.renumber_positions(); + }; + + ns.reorder_variants = function(order_by) { + var dir = $('#variant_' + order_by + '_header_id a img').attr("data-sort-dir"); + $('#parent_variant_table thead a img').remove(); + + var src; + if (dir == "1") { + dir = "0"; + src = "image/up.png"; + } else { + dir = "1"; + src = "image/down.png"; + } + + $('#variant_' + order_by + '_header_id a').append('' + kivi.t8('sort items') + ''); + + var data = $('#ic').serializeArray(); + data.push({ name: 'action', value: 'Part/reorder_variants' }, + { name: 'order_by', value: order_by }, + { name: 'sort_dir', value: dir }); + + $.post("controller.pl", data, kivi.eval_json_result); + }; + + ns.redisplay_variants = function(data) { + var old_rows = $('.variant_row_entry').detach(); + var new_rows = []; + $(data).each(function(idx, elt) { + let new_row = old_rows[elt.old_pos - 1]; + $(new_row).find('[name="variants[].position"]').val( idx+1); + new_rows.push(new_row); + }); + $(new_rows).appendTo($('#parent_variant_table')); + }; + ns.assortment_recalc = function() { var data = $('#assortment :input').serializeArray(); data.push( @@ -215,26 +269,6 @@ namespace('kivi.Part', function(ns) { }); }; - ns.redisplay_items = function(data) { - var old_rows; - var part_type = $("#part_part_type").val(); - if (part_type === 'assortment') { - old_rows = $('.assortment_item_row').detach(); - } else if ( part_type === 'assembly') { - old_rows = $('.assembly_item_row').detach(); - } - var new_rows = []; - $(data).each(function(idx, elt) { - new_rows.push(old_rows[elt.old_pos - 1]); - }); - if (part_type === 'assortment') { - $(new_rows).appendTo($('#assortment_items')); - } else if ( part_type === 'assembly') { - $(new_rows).appendTo($('#assembly_items')); - } - ns.renumber_positions(); - }; - ns.focus_last_assortment_input = function () { $("#assortment_items tr:last").find('input[type=text]').filter(':visible:first').focus(); }; diff --git a/templates/design40_webpages/part/_parent_variant.html b/templates/design40_webpages/part/_parent_variant.html index fbfd9491b1..d30e5f8908 100644 --- a/templates/design40_webpages/part/_parent_variant.html +++ b/templates/design40_webpages/part/_parent_variant.html @@ -4,19 +4,36 @@ [% USE L %] [% USE P %] -
+

[% LxERP.t8("Variant Properties") %]

- +
- - - + + + + [% FOREACH variant_property = SELF.part.variant_properties %] - + [% END %] - + [% FOREACH variant = SELF.part.variants %] - + +
[% LxERP.t8("Variants") %]
[% "Partnumber" | $T8 %][% "Description" | $T8 %][% "Property Values (Abbreviation)" | $T8 %] + + [% 'Partnumber' | $T8 %] + + + + [% "Description" | $T8 %] + + + + [% "Property Values (Abbreviation)" | $T8 %] + + [% variant_property.displayable_name %] + + [% variant_property.displayable_name %] + + [% L.select_tag("add_variant_property", AVAILABLE_VARIANT_PROPERIES @@ -27,9 +44,13 @@

[% LxERP.t8("Variant Properties") %]

[% L.button_tag('kivi.Part.add_variant_property();', LxERP.t8("Insert new")) %]
+ [% L.hidden_tag("variants[+].id", variant.id) %] + [% L.hidden_tag("variants[].position", loop.count) %] + [% variant.presenter.part %] [% variant.description | html %] [% variant.variant_values | html %]