X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FCVar.pm;h=37d865ae9b4faacb757401841604bc3b454a021b;hb=2ef19c818a05b2baa2552ae5b4edbcc16bcfc986;hp=0ace069027da47f263262c49e4fa1f9e40a73fc9;hpb=be42a45043cab936e86e91aa895c6fc25bab079f;p=kivitendo-erp.git diff --git a/SL/CVar.pm b/SL/CVar.pm index 0ace06902..37d865ae9 100644 --- a/SL/CVar.pm +++ b/SL/CVar.pm @@ -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,8 @@ use Data::Dumper; use SL::DBUtils; use SL::MoreCommon qw(listify); +use SL::Util qw(trim); +use SL::DB; sub get_configs { $main::lxdebug->enter_sub(); @@ -133,7 +136,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}; } @@ -198,8 +201,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 = @_; @@ -208,7 +219,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() }; @@ -265,9 +276,7 @@ sub save_custom_variables { $sth->finish(); - $dbh->commit() unless $params{dbh}; - - $main::lxdebug->leave_sub(); + return 1; } sub render_inputs { @@ -355,7 +364,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 +415,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 +427,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) { @@ -546,8 +555,16 @@ sub get_field_format_list { } 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 = @_; @@ -556,7 +573,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); @@ -582,14 +599,16 @@ sub save_custom_variables_validity { $sth->finish(); - $dbh->commit() unless $params{dbh}; - - $main::lxdebug->leave_sub(); + return 1; } -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 +619,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 = <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 +761,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 is saved as a function of config_id, and the trans_id