From f01ed5d1c0d1bed1d04005e004a785c6cf047233 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bernd=20Ble=C3=9Fmann?= Date: Thu, 10 Jun 2021 16:32:10 +0200 Subject: [PATCH] Erzeugnisse: Gewicht aus einzelnen Bestandteilen ermitteln und speichern. --- SL/Controller/Part.pm | 12 +++++++++--- SL/DB/Assembly.pm | 7 +++++++ SL/DB/Part.pm | 23 +++++++++++++++++++++-- templates/webpages/part/_basic_data.html | 2 +- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/SL/Controller/Part.pm b/SL/Controller/Part.pm index 0b476ea81..5a2d01233 100644 --- a/SL/Controller/Part.pm +++ b/SL/Controller/Part.pm @@ -284,8 +284,9 @@ sub action_update_item_totals { my $part_type = $::form->{part_type}; die unless $part_type =~ /^(assortment|assembly)$/; - my $sellprice_sum = $self->recalc_item_totals(part_type => $part_type, price_type => 'sellcost'); - my $lastcost_sum = $self->recalc_item_totals(part_type => $part_type, price_type => 'lastcost'); + my $sellprice_sum = $self->recalc_item_totals(part_type => $part_type, price_type => 'sellcost'); + my $lastcost_sum = $self->recalc_item_totals(part_type => $part_type, price_type => 'lastcost'); + my $items_weight_sum = $self->recalc_item_totals(part_type => $part_type, price_type => 'weight'); my $sum_diff = $sellprice_sum-$lastcost_sum; @@ -295,6 +296,7 @@ sub action_update_item_totals { ->html('#items_sum_diff', $::form->format_amount(\%::myconfig, $sum_diff, 2, 0)) ->html('#items_sellprice_sum_basic', $::form->format_amount(\%::myconfig, $sellprice_sum, 2, 0)) ->html('#items_lastcost_sum_basic', $::form->format_amount(\%::myconfig, $lastcost_sum, 2, 0)) + ->html('#items_weight_sum_basic' , $::form->format_amount(\%::myconfig, $items_weight_sum)) ->no_flash_clear->render(); } @@ -400,6 +402,7 @@ sub action_add_assembly_item { my $items_sellprice_sum = $part->items_sellprice_sum; my $items_lastcost_sum = $part->items_lastcost_sum; my $items_sum_diff = $items_sellprice_sum - $items_lastcost_sum; + my $items_weight_sum = $part->items_weight_sum; $self->js ->append('#assembly_rows', $html) # append in tbody @@ -410,6 +413,7 @@ sub action_add_assembly_item { ->html('#items_sum_diff', $::form->format_amount(\%::myconfig, $items_sum_diff , 2, 0)) ->html('#items_sellprice_sum_basic', $::form->format_amount(\%::myconfig, $items_sellprice_sum, 2, 0)) ->html('#items_lastcost_sum_basic' , $::form->format_amount(\%::myconfig, $items_lastcost_sum , 2, 0)) + ->html('#items_weight_sum_basic' , $::form->format_amount(\%::myconfig, $items_weight_sum)) ->render; } @@ -734,7 +738,9 @@ sub recalc_item_totals { } } elsif ( $part->is_assembly ) { $part->assemblies( @{$self->assembly_items} ); - if ( $params{price_type} eq 'lastcost' ) { + if ( $params{price_type} eq 'weight' ) { + return $part->items_weight_sum; + } elsif ( $params{price_type} eq 'lastcost' ) { return $part->items_lastcost_sum; } else { return $part->items_sellprice_sum; diff --git a/SL/DB/Assembly.pm b/SL/DB/Assembly.pm index 42d155f4b..fd645c2c9 100644 --- a/SL/DB/Assembly.pm +++ b/SL/DB/Assembly.pm @@ -23,4 +23,11 @@ sub linetotal_lastcost { return $self->qty * $self->part->lastcost / ( $self->part->price_factor_id ? $self->part->price_factor->factor : 1 ); } +sub linetotal_weight { + my ($self) = @_; + + return 0 unless $self->qty > 0 and ($self->part->weight||0) > 0; + return $self->qty * $self->part->weight; +} + 1; diff --git a/SL/DB/Part.pm b/SL/DB/Part.pm index 0f855231e..1ee4f26ee 100644 --- a/SL/DB/Part.pm +++ b/SL/DB/Part.pm @@ -4,6 +4,7 @@ use strict; use Carp; use List::MoreUtils qw(any uniq); +use List::Util qw(sum); use Rose::DB::Object::Helpers qw(as_tree); use SL::Locale::String qw(t8); @@ -26,7 +27,6 @@ use SL::DB::Helper::DisplayableNamePreferences ( {name => 'ean', title => t8('EAN') }, ], ); -use List::Util qw(sum); __PACKAGE__->meta->add_relationships( assemblies => { @@ -91,6 +91,7 @@ __PACKAGE__->attr_sorted({ unsorted => 'makemodels', position => 'sortorder' __PACKAGE__->attr_sorted({ unsorted => 'customerprices', position => 'sortorder' }); __PACKAGE__->before_save('_before_save_set_partnumber'); +__PACKAGE__->before_save('_before_save_set_assembly_weight'); sub _before_save_set_partnumber { my ($self) = @_; @@ -99,6 +100,16 @@ sub _before_save_set_partnumber { return 1; } +sub _before_save_set_assembly_weight { + my ($self) = @_; + + if ( $self->part_type eq 'assembly' ) { + my $weight_sum = $self->items_weight_sum; + $self->weight($self->items_weight_sum) if $weight_sum; + } + return 1; +} + sub items { my ($self) = @_; @@ -425,7 +436,7 @@ select unnest(ids) SQL my $objs = SL::DB::Manager::Inventory->get_all( - query => [ id => [ \"$query" ] ], + query => [ id => [ \"$query" ] ], # make emacs happy " with_objects => [ 'parts', 'trans_type', 'bin', 'bin.warehouse' ], # prevent lazy loading in template sort_by => 'itime DESC', ); @@ -519,6 +530,14 @@ sub items_lastcost_sum { sum map { $_->linetotal_lastcost } @{$self->items}; }; +sub items_weight_sum { + my ($self) = @_; + + return unless $self->is_assembly; + return unless $self->items; + sum map { $_->linetotal_weight} @{$self->items}; +}; + 1; __END__ diff --git a/templates/webpages/part/_basic_data.html b/templates/webpages/part/_basic_data.html index 47cd8f3e8..ba18da266 100644 --- a/templates/webpages/part/_basic_data.html +++ b/templates/webpages/part/_basic_data.html @@ -145,7 +145,7 @@ [% 'Weight' | $T8 %] [%- IF SELF.part.is_assembly %] - [% LxERP.format_amount(SELF.part.weight) %] + [% LxERP.format_amount(SELF.part.weight) %] [% ELSE %] [% L.input_tag('part.weight_as_number', SELF.part.weight_as_number, size=10, class='reformat_number numeric') %] [% END %] -- 2.20.1