5 use SL::DB::MetaSetup::Unit;
6 use SL::DB::Manager::Unit;
7 use SL::DB::Helper::ActsAsList;
9 __PACKAGE__->meta->add_relationships(
11 type => 'many to one',
12 class => 'SL::DB::Unit',
13 column_map => { base_unit => 'name' },
17 __PACKAGE__->meta->initialize;
24 return $self if !$self->base_unit || $self->name eq $self->base_unit;
25 return $self->base->unit_class;
28 sub convertible_units {
31 sort { $a->sortkey <=> $b->sortkey }
32 grep { $_->unit_class->name eq $self->unit_class->name }
33 @{ SL::DB::Manager::Unit->get_all }
40 if (!defined $self->{__base_factor}) {
41 $self->{__base_factor} = !$self->base_unit || !$self->factor || ($self->name eq $self->base_unit) ? 1 : $self->factor * $self->base->base_factor;
44 return $self->{__base_factor};
48 my ($self, $qty, $other_unit) = @_;
50 my $my_base_factor = $self->base_factor || 1;
51 my $other_base_factor = $other_unit->base_factor || 1;
53 return $qty * $my_base_factor / $other_base_factor;