Skip to content

Commit

Permalink
Varianten: Sortieren der Variantentabelle ermöglicht
Browse files Browse the repository at this point in the history
  • Loading branch information
z4m1n0 committed Jan 12, 2024
1 parent e4982f5 commit 4d45be8
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 28 deletions.
45 changes: 45 additions & 0 deletions SL/Controller/Part.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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) = @_;

Expand Down
12 changes: 12 additions & 0 deletions SL/DB/Part.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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) = @_;

Expand Down
74 changes: 54 additions & 20 deletions js/kivi.Part.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + 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(
Expand Down Expand Up @@ -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();
};
Expand Down
37 changes: 29 additions & 8 deletions templates/design40_webpages/part/_parent_variant.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,36 @@
[% USE L %]
[% USE P %]

<div class="wrapper">
<div id="parent_variant" class="wrapper">
<div class="wrapper input-panel">
<h3> [% LxERP.t8("Variant Properties") %] </h3>
<table class="tbl-list">
<table id="parent_variant_table" class="tbl-list">
<caption>
[% LxERP.t8("Variants") %]
</caption>
<thead>
<th>[% "Partnumber" | $T8 %]</th>
<th>[% "Description" | $T8 %]</th>
<th>[% "Property Values (Abbreviation)" | $T8 %]</th>
<th></th>
<th id="variant_partnumber_header_id">
<a href='#' onClick='javascript:kivi.Part.reorder_variants("partnumber")'>
[% 'Partnumber' | $T8 %]
</a>
</th>
<th id="variant_description_header_id">
<a href='#' onClick='javascript:kivi.Part.reorder_variants("description")'>
[% "Description" | $T8 %]
</a>
</th>
<th id="variant_variant_values_header_id">
<a href='#' onClick='javascript:kivi.Part.reorder_variants("variant_values")'>
[% "Property Values (Abbreviation)" | $T8 %]
</a>
</th>
[% FOREACH variant_property = SELF.part.variant_properties %]
<th> [% variant_property.displayable_name %] </th>
<th id="variant_variant_property_[% variant_property.unique_name %]_header_id">
<a href='#' onClick='javascript:kivi.Part.reorder_variants("variant_property_[% variant_property.unique_name %]")'>
[% variant_property.displayable_name %]
</a>
</th>
[% END %]
<th>
[% L.select_tag("add_variant_property", AVAILABLE_VARIANT_PROPERIES
Expand All @@ -27,9 +44,13 @@ <h3> [% LxERP.t8("Variant Properties") %] </h3>
[% L.button_tag('kivi.Part.add_variant_property();', LxERP.t8("Insert new")) %]
</th>
</thead>
<tbody class="row_entry listrow">
<tbody class="listrow">
[% FOREACH variant = SELF.part.variants %]
<tr>
<tr class="variant_row_entry">
<td>
[% L.hidden_tag("variants[+].id", variant.id) %]
[% L.hidden_tag("variants[].position", loop.count) %]
</td>
<td>[% variant.presenter.part %]</td>
<td>[% variant.description | html %]</td>
<td>[% variant.variant_values | html %]</td>
Expand Down

0 comments on commit 4d45be8

Please sign in to comment.