From 65bc835d4495503a806c9cbe322b3d6725af0abd Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 1 Nov 2010 18:37:48 +0100 Subject: [PATCH] Hilfsroutinen zum Umrechnen zwischen Einheiten --- SL/DB/Unit.pm | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/SL/DB/Unit.pm b/SL/DB/Unit.pm index 6a197337c..55a190b24 100644 --- a/SL/DB/Unit.pm +++ b/SL/DB/Unit.pm @@ -38,4 +38,23 @@ sub convertible_units { ]; } +sub base_factor { + my ($self) = @_; + + if (!defined $self->{__base_factor}) { + $self->{__base_factor} = !$self->base_unit || !$self->factor || ($self->name eq $self->base_unit) ? 1 : $self->factor * $self->base->base_factor; + } + + return $self->{__base_factor}; +} + +sub convert_to { + my ($self, $qty, $other_unit) = @_; + + my $my_base_factor = $self->base_factor || 1; + my $other_base_factor = $other_unit->base_factor || 1; + + return $qty * $my_base_factor / $other_base_factor; +} + 1; -- 2.20.1