Leistungsdatum: wiederkehrende Rechnungen
[kivitendo-erp.git] / SL / DB / Helper / CustomVariables.pm
index d23e46e..a6ab5ae 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use Carp;
 use Data::Dumper;
 use List::Util qw(first);
 use Carp;
 use Data::Dumper;
 use List::Util qw(first);
-use List::UtilsBy qw(sort_by);
+use List::UtilsBy qw(partition_by);
 
 use constant META_CVARS => 'cvars_config';
 
 
 use constant META_CVARS => 'cvars_config';
 
@@ -84,23 +84,26 @@ sub make_cvar_by_configs {
     my $configs     = _all_configs(%params);
     my $cvars       = $self->custom_variables;
     my %cvars_by_config = map { $_->config_id => $_ } @$cvars;
     my $configs     = _all_configs(%params);
     my $cvars       = $self->custom_variables;
     my %cvars_by_config = map { $_->config_id => $_ } @$cvars;
+    my $invalids    = _all_invalids($self->${\ $self->meta->primary_key_columns->[0]->name }, $configs, %params);
+    my %invalids_by_config = map { $_->config_id => 1 } @$invalids;
 
     my @return = map(
       {
 
     my @return = map(
       {
+        my $cvar;
         if ( $cvars_by_config{$_->id} ) {
         if ( $cvars_by_config{$_->id} ) {
-          $cvars_by_config{$_->id};
+          $cvar = $cvars_by_config{$_->id};
         }
         else {
         }
         else {
-          my $cvar = _new_cvar($self, %params, config => $_);
+          $cvar = _new_cvar($self, %params, config => $_);
           $self->add_custom_variables($cvar);
           $self->add_custom_variables($cvar);
-          $cvar;
         }
         }
+        $cvar->{is_valid} = !$invalids_by_config{$_->id};
+        $cvar->{config}   = $_;
+        $cvar;
       }
       @$configs
     );
 
       }
       @$configs
     );
 
-    @return = sort_by { $_->config->sortkey } @return;
-
     return \@return;
   }
 }
     return \@return;
   }
 }
@@ -168,7 +171,17 @@ sub _all_configs {
 
   require SL::DB::CustomVariableConfig;
 
 
   require SL::DB::CustomVariableConfig;
 
-  SL::DB::Manager::CustomVariableConfig->get_all_sorted($params{module} ? (query => [ module => $params{module} ]) : ());
+  my $cache  = $::request->cache("::SL::DB::Helper::CustomVariables::object_cache");
+
+  if (!$cache->{all}) {
+    my $configs = SL::DB::Manager::CustomVariableConfig->get_all_sorted;
+    $cache->{all}    =  $configs;
+    $cache->{module} = { partition_by { $_->module } @$configs };
+  }
+
+  return $params{module} && !ref $params{module} ? $cache->{module}{$params{module}}
+       : $params{module} &&  ref $params{module} ? [ map { @{ $cache->{module}{$_} // [] } } @{ $params{module} } ]
+       : $cache->{all};
 }
 
 sub _overload_by_module {
 }
 
 sub _overload_by_module {
@@ -353,6 +366,32 @@ sub make_cvar_custom_filter {
   );
 }
 
   );
 }
 
+
+sub _all_invalids {
+  my ($trans_id, $configs, %params) = @_;
+
+  require SL::DB::CustomVariableValidity;
+
+  # easy 1: no trans_id, all valid by default.
+  return [] unless $trans_id;
+
+  # easy 2: no module in params? no validity
+  return [] unless $params{module};
+
+  my %wanted_modules = ref $params{module} ? map { $_ => 1 } @{ $params{module} } : ($params{module} => 1);
+  my @module_configs = grep { $wanted_modules{$_->module} } @$configs;
+
+  return [] unless @module_configs;
+
+  # nor find all entries for that and return
+  SL::DB::Manager::CustomVariableValidity->get_all(
+    query => [
+      config_id => [ map { $_->id } @module_configs ],
+      trans_id => $trans_id,
+    ]
+  );
+}
+
 1;
 
 __END__
 1;
 
 __END__
@@ -422,7 +461,7 @@ passed to import.
 
 =item C<cvars_by_config>
 
 
 =item C<cvars_by_config>
 
-Thi will return a list of CVars with the following changes over the standard accessor:
+This will return a list of CVars with the following changes over the standard accessor:
 
 =over 4
 
 
 =over 4
 
@@ -522,11 +561,11 @@ If the Manager for the calling C<SL::DB::Object> has included the helper L<SL::D
 Prolonged use has shown that users expect all methods to be present or none.
 Future versions of this will likely remove the optional aliasing.
 
 Prolonged use has shown that users expect all methods to be present or none.
 Future versions of this will likely remove the optional aliasing.
 
-=item * Sematics need to be updated
+=item * Semantics need to be updated
 
 There are a few transitions that are currently neither supported nor well
 
 There are a few transitions that are currently neither supported nor well
-defined, most of the happening when the config of a cvar gets changed which
-instances are already saved. This needs to be cleaned up.
+defined, most of them happening when the config of a cvar gets changed, but
+whose instances have already been saved. This needs to be cleaned up.
 
 =back
 
 
 =back