epic-s6ts
[kivitendo-erp.git] / SL / CVar.pm
index f4ca649..637f129 100644 (file)
@@ -2,11 +2,17 @@ package CVar;
 
 use strict;
 
+use Carp;
+use List::MoreUtils qw(any);
 use List::Util qw(first);
+use Scalar::Util qw(blessed);
 use Data::Dumper;
 
 use SL::DBUtils;
 use SL::MoreCommon qw(listify);
+use SL::Presenter::Text;
+use SL::Util qw(trim);
+use SL::DB;
 
 sub get_configs {
   $main::lxdebug->enter_sub();
@@ -25,49 +31,45 @@ sub get_configs {
     push @values, $params{module};
   }
 
-  my $query    = qq|SELECT * FROM custom_variable_configs $where ORDER BY sortkey|;
+  my $query    = <<SQL;
+    SELECT *, date_trunc('seconds', localtimestamp) AS current_timestamp
+    FROM custom_variable_configs $where ORDER BY sortkey
+SQL
 
-  my $configs  = selectall_hashref_query($form, $dbh, $query, @values);
+  $::form->{CVAR_CONFIGS} = {} unless 'HASH' eq ref $::form->{CVAR_CONFIGS};
+  if (!$::form->{CVAR_CONFIGS}->{$params{module}}) {
+    my $configs  = selectall_hashref_query($form, $dbh, $query, @values);
 
-  foreach my $config (@{ $configs }) {
-    if ($config->{type} eq 'select') {
-      $config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
-
-    } elsif ($config->{type} eq 'number') {
-      $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
+    foreach my $config (@{ $configs }) {
+      if ($config->{type} eq 'select') {
+        $config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
 
-    }
+      } elsif ($config->{type} eq 'number') {
+        $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
 
-    $self->_unpack_flags($config);
-  }
+      } elsif ($config->{type} =~ m{^(?:html|text)field$}) {
+        $config->{width}  = 30;
+        $config->{height} =  5;
+        $config->{width}  = $1 if ($config->{options} =~ m/width=(\d+)/i);
+        $config->{height} = $1 if ($config->{options} =~ m/height=(\d+)/i);
 
-  $main::lxdebug->leave_sub();
+      } elsif ($config->{type} eq 'text') {
+        $config->{maxlength} = $1 if ($config->{options} =~ m/maxlength=(\d+)/i);
 
-  return $configs;
-}
-
-sub get_config {
-  $main::lxdebug->enter_sub();
-
-  my $self     = shift;
-  my %params   = @_;
-
-  Common::check_params(\%params, qw(id));
-
-  my $myconfig = \%main::myconfig;
-  my $form     = $main::form;
-
-  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
+      }
 
-  my $query    = qq|SELECT * FROM custom_variable_configs WHERE id = ?|;
+      $self->_unpack_flags($config);
 
-  my $config   = selectfirst_hashref_query($form, $dbh, $query, conv_i($params{id})) || { };
+      my $cvar_config = SL::DB::CustomVariableConfig->new(id => $config->{id})->load;
+      @{$config->{'partsgroups'}} = map {$_->id} @{$cvar_config->partsgroups};
 
-  $self->_unpack_flags($config);
+    }
+    $::form->{CVAR_CONFIGS}->{$params{module}} = $configs;
+  }
 
   $main::lxdebug->leave_sub();
 
-  return $config;
+  return $::form->{CVAR_CONFIGS}->{$params{module}};
 }
 
 sub _unpack_flags {
@@ -87,95 +89,6 @@ sub _unpack_flags {
   $main::lxdebug->leave_sub();
 }
 
-sub save_config {
-  $main::lxdebug->enter_sub();
-
-  my $self     = shift;
-  my %params   = @_;
-
-  Common::check_params(\%params, qw(module config));
-
-  my $myconfig = \%main::myconfig;
-  my $form     = $main::form;
-
-  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
-
-  my $q_id     = qq|SELECT nextval('custom_variable_configs_id')|;
-  my $h_id     = prepare_query($form, $dbh, $q_id);
-
-  my $q_new    =
-    qq|INSERT INTO custom_variable_configs (name, description, type, default_value, options, searchable, includeable, included_by_default, module, flags, id, sortkey)
-       VALUES                              (?,    ?,           ?,    ?,             ?,       ?,          ?,           ?,                   ?,      ?,     ?,
-         (SELECT COALESCE(MAX(sortkey) + 1, 1) FROM custom_variable_configs))|;
-  my $h_new    = prepare_query($form, $dbh, $q_new);
-
-  my $q_update =
-    qq|UPDATE custom_variable_configs SET
-         name        = ?, description         = ?,
-         type        = ?, default_value       = ?,
-         options     = ?, searchable          = ?,
-         includeable = ?, included_by_default = ?,
-         module      = ?, flags               = ?
-       WHERE id  = ?|;
-  my $h_update = prepare_query($form, $dbh, $q_update);
-
-  my @configs;
-  if ('ARRAY' eq ref $params{config}) {
-    @configs = @{ $params{config} };
-  } else {
-    @configs = ($params{config});
-  }
-
-  foreach my $config (@configs) {
-    my ($h_actual, $q_actual);
-
-    if (!$config->{id}) {
-      do_statement($form, $h_id, $q_id);
-      ($config->{id}) = $h_id->fetchrow_array();
-
-      $h_actual       = $h_new;
-      $q_actual       = $q_new;
-
-    } else {
-      $h_actual       = $h_update;
-      $q_actual       = $q_update;
-    }
-
-    do_statement($form, $h_actual, $q_actual, @{$config}{qw(name description type default_value options)},
-                 $config->{searchable} ? 't' : 'f', $config->{includeable} ? 't' : 'f', $config->{included_by_default} ? 't' : 'f',
-                 $params{module}, $config->{flags}, conv_i($config->{id}));
-  }
-
-  $h_id->finish();
-  $h_new->finish();
-  $h_update->finish();
-
-  $dbh->commit();
-
-  $main::lxdebug->leave_sub();
-}
-
-sub delete_config {
-  $main::lxdebug->enter_sub();
-
-  my $self     = shift;
-  my %params   = @_;
-
-  Common::check_params(\%params, qw(id));
-
-  my $myconfig = \%main::myconfig;
-  my $form     = $main::form;
-
-  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
-
-  do_query($form, $dbh, qq|DELETE FROM custom_variables        WHERE config_id = ?|, conv_i($params{id}));
-  do_query($form, $dbh, qq|DELETE FROM custom_variable_configs WHERE id        = ?|, conv_i($params{id}));
-
-  $dbh->commit();
-
-  $main::lxdebug->leave_sub();
-}
-
 sub get_custom_variables {
   $main::lxdebug->enter_sub();
 
@@ -189,26 +102,18 @@ sub get_custom_variables {
 
   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
-  my $trans_id = $params{trans_id} ? 'OR (v.trans_id = ?) ' : '';
-
-  my $q_cfg    =
-    qq|SELECT id, name, description, type, default_value, options,
-         date_trunc('seconds', localtimestamp) AS current_timestamp, current_date AS current_date
-       FROM custom_variable_configs
-       WHERE module = ?
-       ORDER BY sortkey|;
+  my $sub_module = $params{sub_module} ? $params{sub_module} : '';
 
   my $q_var    =
     qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value
        FROM custom_variables
-       WHERE (config_id = ?) AND (trans_id = ?)|;
-  $q_var      .= qq| AND (sub_module = ?)| if $params{sub_module};
+       WHERE (config_id = ?) AND (trans_id = ?) AND (sub_module = ?)|;
   my $h_var    = prepare_query($form, $dbh, $q_var);
 
-  my $custom_variables = selectall_hashref_query($form, $dbh, $q_cfg, $params{module});
+  my $custom_variables = $self->get_configs(module => $params{module});
 
   foreach my $cvar (@{ $custom_variables }) {
-    if ($cvar->{type} eq 'textfield') {
+    if ($cvar->{type} =~ m{^(?:html|text)field}) {
       $cvar->{width}  = 30;
       $cvar->{height} =  5;
 
@@ -225,25 +130,31 @@ sub get_custom_variables {
       $cvar->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $cvar->{options}) ];
     }
 
-    my $act_var;
+    my ($act_var, $valid);
     if ($params{trans_id}) {
-      my @values = (conv_i($cvar->{id}), conv_i($params{trans_id}));
-      push @values, $params{sub_module} if $params{sub_module};
+      my @values = (conv_i($cvar->{id}), conv_i($params{trans_id}), $sub_module);
 
       do_statement($form, $h_var, $q_var, @values);
       $act_var = $h_var->fetchrow_hashref();
 
-      $act_var->{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};
     }
 
     if ($act_var) {
       $cvar->{value} = $cvar->{type} eq 'date'      ? $act_var->{date_value}
                      : $cvar->{type} eq 'timestamp' ? $act_var->{timestamp_value}
                      : $cvar->{type} eq 'number'    ? $act_var->{number_value}
+                     : $cvar->{type} eq 'customer'  ? $act_var->{number_value}
+                     : $cvar->{type} eq 'vendor'    ? $act_var->{number_value}
+                     : $cvar->{type} eq 'part'      ? $act_var->{number_value}
                      : $cvar->{type} eq 'bool'      ? $act_var->{bool_value}
                      :                                $act_var->{text_value};
-      $cvar->{valid} = $act_var->{valid};
+      $cvar->{valid} = $valid;
     } else {
+      $cvar->{valid} = $valid // 1;
+
       if ($cvar->{type} eq 'date') {
         if ($cvar->{default_value} eq 'NOW') {
           $cvar->{value} = $cvar->{current_date};
@@ -271,6 +182,15 @@ sub get_custom_variables {
 
     if ($cvar->{type} eq 'number') {
       $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
+    } elsif ($cvar->{type} eq 'customer') {
+      require SL::DB::Customer;
+      $cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1);
+    } elsif ($cvar->{type} eq 'vendor') {
+      require SL::DB::Vendor;
+      $cvar->{value} = SL::DB::Manager::Vendor->find_by(id => $cvar->{value} * 1);
+    } elsif ($cvar->{type} eq 'part') {
+      require SL::DB::Part;
+      $cvar->{value} = SL::DB::Manager::Part->find_by(id => $cvar->{value} * 1);
     }
   }
 
@@ -282,8 +202,16 @@ sub get_custom_variables {
 }
 
 sub save_custom_variables {
+  my ($self, %params) = @_;
   $main::lxdebug->enter_sub();
 
+  my $rc = SL::DB->client->with_transaction(\&_save_custom_variables, $self, %params);
+
+  $::lxdebug->leave_sub;
+  return $rc;
+}
+
+sub _save_custom_variables {
   my $self     = shift;
   my %params   = @_;
 
@@ -292,7 +220,7 @@ sub save_custom_variables {
   my $myconfig = \%main::myconfig;
   my $form     = $main::form;
 
-  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
+  my $dbh      = $params{dbh} || SL::DB->client->dbh;
 
   my @configs  = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
 
@@ -317,11 +245,34 @@ sub save_custom_variables {
   my $sth = prepare_query($form, $dbh, $query);
 
   foreach my $config (@configs) {
+    if ($params{save_validity}) {
+      my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
+      my $new_valid   = $params{variables}{$valid_index} || $params{always_valid} ? 1 : 0;
+      my $old_valid   = $self->get_custom_variables_validity(trans_id => $params{trans_id}, config_id => $config->{id});
+
+      $self->save_custom_variables_validity(trans_id  => $params{trans_id},
+                                            config_id => $config->{id},
+                                            validity  => $new_valid,
+                                           );
+
+      if (!$new_valid || !$old_valid) {
+        # When activating a cvar (old_valid == 0 && new_valid == 1)
+        # the input to hold the variable's value wasn't actually
+        # rendered, meaning saving the value now would only save an
+        # empty value/the value 0. This means that the next time the
+        # form is rendered, an existing value is found and used
+        # instead of the variable's default value from the
+        # configuration. Therefore don't save the values in such
+        # cases.
+        next;
+      }
+    }
+
     my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
 
     my $value  = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
 
-    if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
+    if (any { $config->{type} eq $_ } qw(text textfield htmlfield select)) {
       push @values, undef, undef, $value, undef;
 
     } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
@@ -332,26 +283,20 @@ sub save_custom_variables {
 
     } elsif ($config->{type} eq 'bool') {
       push @values, $value ? 't' : 'f', undef, undef, undef;
+    } elsif (any { $config->{type} eq $_ } qw(customer vendor part)) {
+      push @values, undef, undef, undef, $value * 1;
     }
 
     do_statement($form, $sth, $query, @values);
-
-    unless ($params{always_valid}) {
-      $self->save_custom_variables_validity(trans_id => $params{trans_id}, config_id => $config->{id},
-        validity => ($params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid"} ? 1 : 0)
-      );
-    };
   }
 
   $sth->finish();
 
-  $dbh->commit();
-
-  $main::lxdebug->leave_sub();
+  return 1;
 }
 
 sub render_inputs {
-  $main::lxdebug->enter_sub();
+  $main::lxdebug->enter_sub(2);
 
   my $self     = shift;
   my %params   = @_;
@@ -361,18 +306,25 @@ sub render_inputs {
   my $myconfig = \%main::myconfig;
   my $form     = $main::form;
 
-  my %options  = ( name_prefix       => "$params{name_prefix}",
-                   name_postfix      => "$params{name_postfix}",
-                   hide_non_editable => $params{hide_non_editable},
+  my %options  = ( name_prefix           => "$params{name_prefix}",
+                   name_postfix          => "$params{name_postfix}",
+                   hide_non_editable     => $params{hide_non_editable},
                    show_disabled_message => $params{show_disabled_message},
                  );
 
+  # should this cvar be filtered by partsgroups?
   foreach my $var (@{ $params{variables} }) {
+    if ($var->{flag_partsgroup_filter}) {
+      if (!$params{partsgroup_id} || (!grep {$params{partsgroup_id} == $_} @{ $var->{partsgroups} })) {
+        $var->{partsgroup_filtered} = 1;
+      }
+    }
+
     $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs',     { var => $var, %options });
     $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
   }
 
-  $main::lxdebug->leave_sub();
+  $main::lxdebug->leave_sub(2);
 }
 
 sub render_search_options {
@@ -386,8 +338,11 @@ sub render_search_options {
   my $myconfig = \%main::myconfig;
   my $form     = $main::form;
 
+  $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
+
   $params{include_prefix}   = 'l_' unless defined($params{include_prefix});
   $params{include_value}  ||= '1';
+  $params{filter_prefix}  ||= '';
 
   my $filter  = $form->parse_html_template('amcvar/search_filter',  \%params);
   my $include = $form->parse_html_template('amcvar/search_include', \%params);
@@ -421,11 +376,11 @@ sub build_filter_query {
 
     my (@sub_values, @sub_where, $not);
 
-    if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
+    if (any { $config->{type} eq $_ } qw(text textfield htmlfield)) {
       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});
@@ -476,18 +431,28 @@ 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});
 
       $not = 'NOT' if ($params{filter}->{$name} eq 'no');
       push @sub_where,  qq|COALESCE(cvar.bool_value, false) = TRUE|;
+    } elsif (any { $config->{type} eq $_ } qw(customer vendor)) {
+      next unless $params{filter}->{$name};
+
+      my $table = $config->{type};
+      push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM $table WHERE name ILIKE ?)|;
+      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, like($params{filter}->{$name});
     }
 
     if (@sub_where) {
-      push @sub_where,  qq|cvar.sub_module = ?|;
-      push @sub_values, "$params{sub_module}";
+      add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
 
       push @where,
         qq|$not EXISTS(
@@ -557,7 +522,11 @@ sub add_custom_variables_to_report {
           $cfg->{type} eq 'date'      ? $ref->{date_value}
         : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
         : $cfg->{type} eq 'number'    ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
+        : $cfg->{type} eq 'customer'  ? (SL::DB::Manager::Customer->find_by(id => 1*$ref->{number_value}) || SL::DB::Customer->new)->name
+        : $cfg->{type} eq 'vendor'    ? (SL::DB::Manager::Vendor->find_by(id => 1*$ref->{number_value})   || SL::DB::Vendor->new)->name
+        : $cfg->{type} eq 'part'      ? (SL::DB::Manager::Part->find_by(id => 1*$ref->{number_value})     || SL::DB::Part->new)->partnumber
         : $cfg->{type} eq 'bool'      ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
+        : $cfg->{type} eq 'htmlfield' ? SL::Presenter::Text::stripped_html($ref->{text_value})
         :                               $ref->{text_value};
     }
   }
@@ -602,21 +571,17 @@ sub get_field_format_list {
   return ($date_fields, $number_fields);
 }
 
-=head2 VALIDITY
-
-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.
-
-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
-
-In the naive way, disable an attribute for a specific id (simple)
-
-=cut
 sub save_custom_variables_validity {
+  my ($self, %params) = @_;
   $main::lxdebug->enter_sub();
 
+  my $rc = SL::DB->client->with_transaction(\&_save_custom_variables_validity, $self, %params);
+
+  $::lxdebug->leave_sub;
+  return $rc;
+}
+
+sub _save_custom_variables_validity {
   my $self     = shift;
   my %params   = @_;
 
@@ -625,7 +590,7 @@ sub save_custom_variables_validity {
   my $myconfig = \%main::myconfig;
   my $form     = $main::form;
 
-  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
+  my $dbh      = $params{dbh} || SL::DB->client->dbh;
 
   my (@where, @values);
   add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
@@ -642,8 +607,8 @@ sub save_custom_variables_validity {
   my $sth = prepare_query($form, $dbh, $query);
 
   unless ($params{validity}) {
-    foreach my $config_id (listify $params{config_id}) {
-      foreach my $trans_id (listify $params{trans_id}) {
+    foreach my $config_id (listify($params{config_id})) {
+      foreach my $trans_id (listify($params{trans_id})) {
         do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
       }
     }
@@ -651,14 +616,16 @@ sub save_custom_variables_validity {
 
   $sth->finish();
 
-  $dbh->commit();
-
-  $main::lxdebug->leave_sub();
+  return 1;
 }
 
-sub get_custom_variables_validity {
-  $main::lxdebug->enter_sub();
+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   = @_;
 
@@ -669,13 +636,153 @@ sub get_custom_variables_validity {
 
   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
-  my $query    = qq|SELECT COUNT(*) FROM custom_variables_validity WHERE config_id = ? AND trans_id = ?|;
+  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();
+  my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
 
   return !$invalid;
 }
 
+sub custom_variables_validity_by_trans_id {
+  $main::lxdebug->enter_sub(2);
+
+  my $self     = shift;
+  my %params   = @_;
+
+  return sub { 0 } unless $params{trans_id};
+
+  my $myconfig = \%main::myconfig;
+  my $form     = $main::form;
+
+  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
+
+  my $query    = qq|SELECT DISTINCT config_id FROM custom_variables_validity WHERE trans_id = ?|;
+
+  my %invalids = map { +($_->{config_id} => 1) } selectall_hashref_query($form, $dbh, $query, $params{trans_id});
+
+  $main::lxdebug->leave_sub(2);
+
+  return sub { !$invalids{+shift} };
+}
+
+sub parse {
+  my ($self, $value, $config) = @_;
+
+  return $::form->parse_amount(\%::myconfig, $value)          if $config->{type} eq 'number';
+  return DateTime->from_lxoffice($value)                      if $config->{type} eq 'date';
+  return !ref $value ? SL::DB::Manager::Customer->find_by(id => $value * 1) : $value  if $config->{type} eq 'customer';
+  return !ref $value ? SL::DB::Manager::Vendor->find_by(id => $value * 1)   : $value  if $config->{type} eq 'vendor';
+  return !ref $value ? SL::DB::Manager::Part->find_by(id => $value * 1)     : $value  if $config->{type} eq 'part';
+  return $value;
+}
+
+sub format_to_template {
+  my ($self, $value, $config) = @_;
+  # stupid template expects everything formated. except objects
+  # do not use outside of print routines for legacy templates
+
+  return $::form->format_amount(\%::myconfig, $value) if $config->{type} eq 'number';
+  return $value->to_lxoffice if $config->{type} eq 'date' && blessed $value && $value->can('to_lxoffice');
+  return $value;
+}
+
+sub get_non_editable_ic_cvars {
+  $main::lxdebug->enter_sub(2);
+  my $self     = shift;
+  my %params   = @_;
+
+  Common::check_params(\%params, qw(form dbh row sub_module may_converted_from));
+  my $form               = $params{form};
+  my $dbh                = $params{dbh};
+  my $row                = $params{row};
+  my $sub_module         = $params{sub_module};
+  my $may_converted_from = $params{may_converted_from};
+
+  my $cvars;
+  if (! $form->{"${sub_module}_id_${row}"}) {
+    my $conv_from = 0;
+    foreach (@{ $may_converted_from }) {
+      if ($form->{"converted_from_${_}_id_$row"}) {
+        $cvars = CVar->get_custom_variables(dbh        => $dbh,
+                                            module     => 'IC',
+                                            sub_module => $_,
+                                            trans_id   => $form->{"converted_from_${_}_id_$row"},
+                                           );
+        $conv_from = 1;
+        last;
+      }
+    }
+    # get values for CVars from master data for new items
+    if (!$conv_from) {
+      $cvars = CVar->get_custom_variables(dbh      => $dbh,
+                                          module   => 'IC',
+                                          trans_id => $form->{"id_$row"},
+                                         );
+    }
+  } else {
+    # get values for CVars from custom_variables for existing items
+    $cvars = CVar->get_custom_variables(dbh        => $dbh,
+                                        module     => 'IC',
+                                        sub_module => $sub_module,
+                                        trans_id   => $form->{"${sub_module}_id_${row}"},
+                                       );
+  }
+  # map only non-editable CVars to form
+  foreach (@{ $cvars }) {
+    next if $_->{flag_editable};
+    $form->{"ic_cvar_$_->{name}_$row"} = $_->{value}
+  }
+
+  $main::lxdebug->leave_sub(2);
+}
+
 1;
+
+__END__
+
+=head1 NAME
+
+SL::CVar.pm - Custom Variables module
+
+=head1 SYNOPSIS
+
+  # dealing with configs
+
+  my $all_configs = CVar->get_configs()
+
+  # dealing with custom vars
+
+  CVar->get_custom_variables(module => 'ic')
+
+=head2 VALIDITY
+
+Suppose the following scenario:
+
+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
+
+In the naive way, disable an attribute for a specific id (simple)
+
+=cut