Fix für Bug 1081. Bei Buchung einer Einkaufsrechnung geht das Rabatt verloren. 1...
[kivitendo-erp.git] / SL / CVar.pm
index 6976a99..6b603d8 100644 (file)
@@ -1,8 +1,12 @@
 package CVar;
 
+use strict;
+
 use List::Util qw(first);
+use Data::Dumper;
 
 use SL::DBUtils;
+use SL::MoreCommon qw(listify);
 
 sub get_configs {
   $main::lxdebug->enter_sub();
@@ -228,6 +232,8 @@ sub get_custom_variables {
 
       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});
     }
 
     if ($act_var) {
@@ -236,8 +242,10 @@ sub get_custom_variables {
                      : $cvar->{type} eq 'number'    ? $act_var->{number_value}
                      : $cvar->{type} eq 'bool'      ? $act_var->{bool_value}
                      :                                $act_var->{text_value};
-
+      $cvar->{valid} = $act_var->{valid};
     } else {
+      $cvar->{valid}  =  1;
+
       if ($cvar->{type} eq 'date') {
         if ($cvar->{default_value} eq 'NOW') {
           $cvar->{value} = $cvar->{current_date};
@@ -329,6 +337,12 @@ sub save_custom_variables {
     }
 
     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();
@@ -352,10 +366,12 @@ sub render_inputs {
   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},
                  );
 
   foreach my $var (@{ $params{variables} }) {
-    $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { 'var' => $var, %options });
+    $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();
@@ -518,7 +534,8 @@ sub add_custom_variables_to_report {
 
   # allow sub_module to be a coderef or a fixed value
   if (ref $params{sub_module} ne 'CODE') {
-    $params{sub_module} = sub { "$params{sub_module}" };
+    my $sub_module = "$params{sub_module}";
+    $params{sub_module} = sub { $sub_module };
   }
 
   my %cfg_map   = map { $_->{id} => $_ } @{ $configs };
@@ -587,5 +604,80 @@ 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 {
+  $main::lxdebug->enter_sub();
+
+  my $self     = shift;
+  my %params   = @_;
+
+  Common::check_params(\%params, qw(config_id trans_id validity));
+
+  my $myconfig = \%main::myconfig;
+  my $form     = $main::form;
+
+  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
+
+  my (@where, @values);
+  add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
+  add_token(\@where, \@values, col => "trans_id",  val => $params{trans_id},  esc => \&conv_i);
+
+  my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
+  my $query = qq|DELETE FROM custom_variables_validity $where|;
+
+  do_query($form, $dbh, $query, @values);
+
+  $query  =
+    qq|INSERT INTO custom_variables_validity (config_id, trans_id)
+       VALUES                                (?,         ?       )|;
+  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}) {
+        do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
+      }
+    }
+  }
+
+  $sth->finish();
+
+  $dbh->commit();
+
+  $main::lxdebug->leave_sub();
+}
+
+sub get_custom_variables_validity {
+  $main::lxdebug->enter_sub();
+
+  my $self     = shift;
+  my %params   = @_;
+
+  Common::check_params(\%params, qw(config_id trans_id));
+
+  my $myconfig = \%main::myconfig;
+  my $form     = $main::form;
+
+  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 ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
+
+  $main::lxdebug->leave_sub();
+
+  return !$invalid;
+}
 
 1;