X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FCVar.pm;h=637f129c880481d0b281461b6679255d8ad4d605;hb=3782a90c336bc6c506f572e607c8526cb5e79ea3;hp=160a23e5a5c1e8a57c8792458bf9a98cbf492d1f;hpb=65b7c724e114cd698d3574524a96c282ee7b992c;p=kivitendo-erp.git diff --git a/SL/CVar.pm b/SL/CVar.pm index 160a23e5a..637f129c8 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,9 @@ 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(); @@ -43,7 +47,7 @@ SQL } elsif ($config->{type} eq 'number') { $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i); - } elsif ($config->{type} eq 'textfield') { + } elsif ($config->{type} =~ m{^(?:html|text)field$}) { $config->{width} = 30; $config->{height} = 5; $config->{width} = $1 if ($config->{options} =~ m/width=(\d+)/i); @@ -98,8 +102,6 @@ sub get_custom_variables { my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); - my $trans_id = $params{trans_id} ? 'OR (v.trans_id = ?) ' : ''; - my $sub_module = $params{sub_module} ? $params{sub_module} : ''; my $q_var = @@ -111,7 +113,7 @@ sub get_custom_variables { 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; @@ -135,7 +137,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}; } @@ -200,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 = @_; @@ -210,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() }; @@ -235,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')) { @@ -255,21 +288,11 @@ sub save_custom_variables { } do_statement($form, $sth, $query, @values); - - if ($params{save_validity}) { - my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid"; - $self->save_custom_variables_validity(trans_id => $params{trans_id}, - config_id => $config->{id}, - validity => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0) - ); - } } $sth->finish(); - $dbh->commit() unless $params{dbh}; - - $main::lxdebug->leave_sub(); + return 1; } sub render_inputs { @@ -301,9 +324,6 @@ sub render_inputs { $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options }); } - # needed for customer_vendor_picker - $::request->{layout}->add_javascripts('autocomplete_customer.js'); - $main::lxdebug->leave_sub(2); } @@ -356,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}); @@ -411,7 +431,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}); @@ -423,12 +443,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) { @@ -506,6 +526,7 @@ sub add_custom_variables_to_report { : $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}; } } @@ -551,8 +572,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 = @_; @@ -561,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); @@ -587,14 +616,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 = @_; @@ -605,11 +636,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; } @@ -657,6 +706,56 @@ sub format_to_template { 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__ @@ -679,7 +778,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