package CVar;
+use strict;
+
use List::Util qw(first);
+use Scalar::Util qw(blessed);
use Data::Dumper;
use SL::DBUtils;
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}) ];
+ 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);
+ } elsif ($config->{type} eq 'number') {
+ $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
- }
+ }
- $self->_unpack_flags($config);
+ $self->_unpack_flags($config);
+ }
+ $::form->{CVAR_CONFIGS}->{$params{module}} = $configs;
}
$main::lxdebug->leave_sub();
- return $configs;
+ return $::form->{CVAR_CONFIGS}->{$params{module}};
}
sub get_config {
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}));
+ do_query($form, $dbh, qq|DELETE FROM custom_variables WHERE config_id = ?|, conv_i($params{id}));
+ do_query($form, $dbh, qq|DELETE FROM custom_variables_validity 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();
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 $q_var =
qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value
FROM custom_variables
$q_var .= qq| AND (sub_module = ?)| if $params{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') {
$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};
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});
}
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 'bool' ? $act_var->{bool_value}
: $act_var->{text_value};
- $cvar->{valid} = $act_var->{valid};
+ $cvar->{valid} = $valid;
} else {
+ $cvar->{valid} = 1;
+
if ($cvar->{type} eq 'date') {
if ($cvar->{default_value} eq 'NOW') {
$cvar->{value} = $cvar->{current_date};
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 ($config->{type} eq 'bool') {
push @values, $value ? 't' : 'f', undef, undef, undef;
+ } elsif ($config->{type} eq 'customer') {
+ push @values, undef, undef, undef, $value * 1;
}
do_statement($form, $sth, $query, @values);
- $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)
- );
+ 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();
+ $dbh->commit() unless $params{dbh};
$main::lxdebug->leave_sub();
}
sub render_inputs {
- $main::lxdebug->enter_sub();
+ $main::lxdebug->enter_sub(2);
my $self = shift;
my %params = @_;
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->{VALID_BOX} = "<input type=checkbox name='$options{name_prefix}cvar_$var->{name}$options{name_postfix}_valid'@{[$var->{valid} ? ' checked' : '']}>";
+ $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 {
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';
$not = 'NOT' if ($params{filter}->{$name} eq 'no');
push @sub_where, qq|COALESCE(cvar.bool_value, false) = TRUE|;
+ } elsif ($config->{type} eq 'customer') {
+ next unless $params{filter}->{$name};
+
+ push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM customer WHERE name ILIKE ?)|;
+ push @sub_values, "%$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(
# 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 };
$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 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
: $ref->{text_value};
}
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();
$sth->finish();
- $dbh->commit();
+ $dbh->commit() unless $params{dbh};
$main::lxdebug->leave_sub();
}
sub get_custom_variables_validity {
- $main::lxdebug->enter_sub();
+ $main::lxdebug->enter_sub(2);
my $self = shift;
my %params = @_;
my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
- $main::lxdebug->leave_sub();
+ $main::lxdebug->leave_sub(2);
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 config_id, COUNT(*) FROM custom_variables_validity WHERE trans_id = ? GROUP BY config_id|;
+
+ my %invalids = selectall_as_map($form, $dbh, $query, 'config_id', 'count', $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 $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;
+}
+
1;
+
+__END__
+
+=head1 NAME
+
+SL::CVar.pm - Custom Variables module
+
+=head1 SYNOPSIS
+
+ # dealing with configs
+
+ my $all_configs = CVar->get_configs()
+ my $config = CVar->get_config(id => '1234')
+
+ CVar->save_config($config);
+ CVar->delete->config($config)
+
+ # 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 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