X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FHelper%2FNumber.pm;h=43b22781919d2538a4a4538d7582d25417f6a315;hb=d8b90e8d02961440c0c0838692a0d352a8fac8ec;hp=0a0ef15fee52519a210c1c2a1f33ba2b2b18fa1f;hpb=dccb820ac74462367f37c2d7d4afab8b214a37ad;p=kivitendo-erp.git diff --git a/SL/Helper/Number.pm b/SL/Helper/Number.pm index 0a0ef15fe..43b227819 100644 --- a/SL/Helper/Number.pm +++ b/SL/Helper/Number.pm @@ -3,12 +3,14 @@ package SL::Helper::Number; use strict; use Exporter qw(import); use List::Util qw(max min); +use List::UtilsBy qw(rev_nsort_by); use Config; our @EXPORT_OK = qw( _format_number _round_number _format_total _round_total _parse_number + _format_number_units ); our %EXPORT_TAGS = (ALL => \@EXPORT_OK); @@ -50,6 +52,56 @@ sub _format_number { $amount; } +sub _format_number_units { + my ($amount, $places, $unit_from, $unit_to, %params) = @_; + + my $all_units = $params{all_units} //= SL::DB::Manager::Unit->get_all; + + if (!$unit_from || !$unit_to) { + return _format_number($amount, $places, %params); + } + + $amount *= $unit_from->factor; + + # unline AM::convertible_uits, this one doesn't sort by default + my @conv_units = rev_nsort_by { $_->factor // 0 } @{ $unit_from->convertible_units($all_units) }; + + if (!scalar @conv_units) { + return _format_number($amount, $places, %params) . " " . $unit_to->name; + } + + my @values; + my $num; + + for my $unit (@conv_units) { + my $last = $unit->name eq $unit_to->name; + if (!$last) { + $num = int($amount / $unit->factor); + $amount -= $num * $unit->factor; + } + + if ($last ? $amount : $num) { + push @values, { + unit => $unit->name, + amount => $last ? $amount / $unit->factor : $num, + places => $last ? $places : 0 + }; + } + + last if $last; + } + + if (!@values) { + push @values, { "unit" => $unit_to->name, + "amount" => 0, + "places" => 0 }; + } + + return join " ", map { + _format_number($_->{amount}, $_->{places}, %params), $_->{unit} + } @values; +} + sub _round_number { my ($amount, $places, $adjust) = @_;