3 use List::MoreUtils qw(any);
8 use SL::DB::MetaSetup::Unit;
9 use SL::DB::Manager::Unit;
10 use SL::DB::Helper::ActsAsList;
12 __PACKAGE__->meta->add_relationships(
14 type => 'many to one',
15 class => 'SL::DB::Unit',
16 column_map => { base_unit => 'name' },
20 __PACKAGE__->meta->initialize;
27 return $self if !$self->base_unit || $self->name eq $self->base_unit;
28 return $self->base->unit_class;
31 sub convertible_units {
33 my $all_units = scalar(@_) && (ref($_[0]) eq 'ARRAY') ? $_[0] : [ @_ ];
34 $all_units = SL::DB::Manager::Unit->all_units if ! @{ $all_units };
36 sort { $a->sortkey <=> $b->sortkey }
37 grep { $_->unit_class->name eq $self->unit_class->name }
45 my $cache = $::request->cache('base_factor');
47 if (!defined $cache->{$self->id}) {
48 $cache->{$self->id} = !$self->base_unit || !$self->factor || ($self->name eq $self->base_unit) ? 1 : $self->factor * $self->base->base_factor;
51 return $cache->{$self->id};
55 my ($self, $qty, $other_unit) = @_;
57 my $my_base_factor = $self->base_factor || 1;
58 my $other_base_factor = $other_unit->base_factor || 1;
60 return ($qty // 0) * $my_base_factor / $other_base_factor;
66 return any { $_->id == $self->id } @{ SL::DB::Manager::Unit->time_based_units };