Lieferbedingungen haben kein Attribut description_long_invoice
[kivitendo-erp.git] / SL / CVar.pm
index 0ace069..a2668c2 100644 (file)
@@ -2,6 +2,7 @@ package CVar;
 
 use strict;
 
+use Carp;
 use List::MoreUtils qw(any);
 use List::Util qw(first);
 use Scalar::Util qw(blessed);
@@ -9,6 +10,7 @@ use Data::Dumper;
 
 use SL::DBUtils;
 use SL::MoreCommon qw(listify);
+use SL::Util qw(trim);
 
 sub get_configs {
   $main::lxdebug->enter_sub();
@@ -133,7 +135,7 @@ sub get_custom_variables {
       do_statement($form, $h_var, $q_var, @values);
       $act_var = $h_var->fetchrow_hashref();
 
-      $valid = $self->get_custom_variables_validity(config_id => $cvar->{id}, trans_id => $params{trans_id});
+      $valid = $self->get_custom_variables_validity(config_id => $cvar->{id}, trans_id => $params{trans_id}, sub_module => $params{sub_module});
     } else {
       $valid = !$cvar->{flag_defaults_to_invalid};
     }
@@ -355,7 +357,7 @@ sub build_filter_query {
       next unless ($params{filter}->{$name});
 
       push @sub_where,  qq|cvar.text_value ILIKE ?|;
-      push @sub_values, '%' . $params{filter}->{$name} . '%'
+      push @sub_values, like($params{filter}->{$name});
 
     } elsif ($config->{type} eq 'select') {
       next unless ($params{filter}->{$name});
@@ -406,7 +408,7 @@ sub build_filter_query {
       }
 
       push @sub_where,  qq|cvar.number_value $op ?|;
-      push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
+      push @sub_values, $form->parse_amount($myconfig, trim($params{filter}->{$name}));
 
     } elsif ($config->{type} eq 'bool') {
       next unless ($params{filter}->{$name});
@@ -418,12 +420,12 @@ sub build_filter_query {
 
       my $table = $config->{type};
       push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM $table WHERE name ILIKE ?)|;
-      push @sub_values, "%$params{filter}->{$name}%";
+      push @sub_values, like($params{filter}->{$name});
     } elsif ($config->{type} eq 'part') {
       next unless $params{filter}->{$name};
 
       push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM parts WHERE partnumber ILIKE ?)|;
-      push @sub_values, "%$params{filter}->{$name}%";
+      push @sub_values, like($params{filter}->{$name});
     }
 
     if (@sub_where) {
@@ -587,9 +589,13 @@ sub save_custom_variables_validity {
   $main::lxdebug->leave_sub();
 }
 
-sub get_custom_variables_validity {
-  $main::lxdebug->enter_sub(2);
+my %_validity_sub_module_mapping = (
+  orderitems           => { table => 'orderitems',           result_column => 'parts_id', trans_id_column => 'id', },
+  delivery_order_items => { table => 'delivery_order_items', result_column => 'parts_id', trans_id_column => 'id', },
+  invoice              => { table => 'invoice',              result_column => 'parts_id', trans_id_column => 'id', },
+);
 
+sub get_custom_variables_validity {
   my $self     = shift;
   my %params   = @_;
 
@@ -600,11 +606,29 @@ sub get_custom_variables_validity {
 
   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
-  my $query    = qq|SELECT id FROM custom_variables_validity WHERE config_id = ? AND trans_id = ? LIMIT 1|;
+  my $query;
 
-  my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
+  if ($params{sub_module}) {
+    my %mapping = %{ $_validity_sub_module_mapping{ $params{sub_module} } || croak("Invalid sub_module '" . $params{sub_module} . "'") };
+    $query = <<SQL;
+      SELECT cvv.id
+      FROM $mapping{table} mt
+      LEFT JOIN custom_variables_validity cvv ON (cvv.trans_id = mt.$mapping{result_column})
+      WHERE (cvv.config_id                = ?)
+        AND (mt.$mapping{trans_id_column} = ?)
+      LIMIT 1
+SQL
+  } else {
+    $query = <<SQL;
+      SELECT id
+      FROM custom_variables_validity
+      WHERE (config_id = ?)
+        AND (trans_id  = ?)
+      LIMIT 1
+SQL
+  }
 
-  $main::lxdebug->leave_sub(2);
+  my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
 
   return !$invalid;
 }
@@ -724,7 +748,7 @@ SL::CVar.pm - Custom Variables module
 
 Suppose the following scenario:
 
-You have a lot of parts in your database, and a set of properties cofigured. Now not every part has every of these properties, some combinations will just make no sense. In order to clean up your inputs a bit, you want to mark certain combinations as invalid, blocking them from modification and possibly display.
+You have a lot of parts in your database, and a set of properties configured. Now not every part has every of these properties, some combinations will just make no sense. In order to clean up your inputs a bit, you want to mark certain combinations as invalid, blocking them from modification and possibly display.
 
 Validity is assumed. If you modify validity, you actually save B<invalidity>.
 Invalidity is saved as a function of config_id, and the trans_id