From: Moritz Bunkus Date: Mon, 1 Nov 2010 17:37:48 +0000 (+0100) Subject: Hilfsroutinen zum Umrechnen zwischen Einheiten X-Git-Tag: release-2.6.3~61^2~7^2~1^2~2^2~34 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=65bc835d4495503a806c9cbe322b3d6725af0abd;p=kivitendo-erp.git Hilfsroutinen zum Umrechnen zwischen Einheiten --- 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;