]> wagnertech.de Git - mfinanz.git/blobdiff - SL/DB/Unit.pm
restart apache2 in postinst
[mfinanz.git] / SL / DB / Unit.pm
index 55a190b247635c59ce0141e48d4feb5134083001..2c58ec66d95ab9e52f3a590bf912b07f8153afdb 100644 (file)
@@ -1,21 +1,25 @@
-# This file has been auto-generated only because it didn't exist.
-# Feel free to modify it at will; it will not be overwritten automatically.
-
 package SL::DB::Unit;
 
+use List::MoreUtils qw(any);
+use List::Util qw(first);
+
 use strict;
 
 use SL::DB::MetaSetup::Unit;
-
-# Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
-__PACKAGE__->meta->make_manager_class;
+use SL::DB::Manager::Unit;
+use SL::DB::Helper::ActsAsList;
 
 __PACKAGE__->meta->add_relationships(
   base => {
-    type         => 'one to one',
+    type         => 'many to one',
     class        => 'SL::DB::Unit',
     column_map   => { base_unit => 'name' },
   },
+  translations => {
+    type         => 'one to many',
+    class        => 'SL::DB::UnitsLanguage',
+    column_map   => { name => 'unit' },
+  },
 );
 
 __PACKAGE__->meta->initialize;
@@ -31,21 +35,25 @@ sub unit_class {
 
 sub convertible_units {
   my $self = shift;
+  my $all_units = scalar(@_) && (ref($_[0]) eq 'ARRAY') ? $_[0] : [ @_ ];
+  $all_units    = SL::DB::Manager::Unit->all_units if ! @{ $all_units };
   return [
     sort { $a->sortkey <=> $b->sortkey }
     grep { $_->unit_class->name eq $self->unit_class->name }
-    @{ SL::DB::Manager::Unit->get_all }
+    @{ $all_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;
+  my $cache = $::request->cache('base_factor');
+
+  if (!defined $cache->{$self->id}) {
+    $cache->{$self->id} = !$self->base_unit || !$self->factor || ($self->name eq $self->base_unit) ? 1 : $self->factor * $self->base->base_factor;
   }
 
-  return $self->{__base_factor};
+  return $cache->{$self->id};
 }
 
 sub convert_to {
@@ -54,7 +62,32 @@ sub convert_to {
   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;
+  return ($qty // 0) * $my_base_factor / $other_base_factor;
+}
+
+sub is_time_based {
+  my ($self) = @_;
+
+  return any { $_->id == $self->id } @{ SL::DB::Manager::Unit->time_based_units };
+}
+
+sub get_translation_obj {
+  my ($self, $language) = @_;
+
+  my $language_id = (ref($language) eq 'SL::DB::Language' ? $language->id : $language) || undef;
+
+  return first { $_->language_id => $language_id } @{ $self->translations || [] };
+}
+
+sub get_translation {
+  my ($self, $language, $qty) = @_;
+
+  my $translation     = $self->name; # fallback, if no translation found
+  my $translation_obj = $self->get_translation_obj(language => $language);
+  if ($translation_obj) {
+    $translation = (($qty // 0) > 1 || ($qty // 0) == 0) ? $translation_obj->localized_plural : $translation_obj->localized;
+  }
+  return $translation;
 }
 
 1;