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 {
30 my $all_units = scalar(@_) && (ref($_[0]) eq 'ARRAY') ? $_[0] : \@_;
31 $all_units = SL::DB::Manager::Unit->get_all if !@{ $all_units };
33 sort { $a->sortkey <=> $b->sortkey }
34 grep { $_->unit_class->name eq $self->unit_class->name }
42 if (!defined $self->{__base_factor}) {
43 $self->{__base_factor} = !$self->base_unit || !$self->factor || ($self->name eq $self->base_unit) ? 1 : $self->factor * $self->base->base_factor;
46 return $self->{__base_factor};
50 my ($self, $qty, $other_unit) = @_;
52 my $my_base_factor = $self->base_factor || 1;
53 my $other_base_factor = $other_unit->base_factor || 1;
55 return $qty * $my_base_factor / $other_base_factor;