Projekt

Allgemein

Profil

Herunterladen (23,2 KB) Statistiken
| Zweig: | Markierung: | Revision:
8688e71e Moritz Bunkus
package CVar;

3da055b7 Sven Schöling
use strict;

b2b1edc8 Moritz Bunkus
use List::MoreUtils qw(any);
8688e71e Moritz Bunkus
use List::Util qw(first);
db7c3a52 Sven Schöling
use Scalar::Util qw(blessed);
3e2892b1 Sven Schöling
use Data::Dumper;
8688e71e Moritz Bunkus
use SL::DBUtils;
b2b1edc8 Moritz Bunkus
use SL::MoreCommon qw(listify);
88dea78e Moritz Bunkus
use SL::Util qw(trim);
8688e71e Moritz Bunkus
sub get_configs {
$main::lxdebug->enter_sub();

my $self = shift;
my %params = @_;

my $myconfig = \%main::myconfig;
my $form = $main::form;

my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);

my ($where, @values);
if ($params{module}) {
$where = 'WHERE module = ?';
push @values, $params{module};
}

4b7433f4 Sven Schöling
my $query = <<SQL;
SELECT *, date_trunc('seconds', localtimestamp) AS current_timestamp
FROM custom_variable_configs $where ORDER BY sortkey
SQL
8688e71e Moritz Bunkus
0d816a7b Moritz Bunkus
$::form->{CVAR_CONFIGS} = {} unless 'HASH' eq ref $::form->{CVAR_CONFIGS};
4b7433f4 Sven Schöling
if (!$::form->{CVAR_CONFIGS}->{$params{module}}) {
my $configs = selectall_hashref_query($form, $dbh, $query, @values);
8688e71e Moritz Bunkus
4b7433f4 Sven Schöling
foreach my $config (@{ $configs }) {
if ($config->{type} eq 'select') {
$config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
8688e71e Moritz Bunkus
4b7433f4 Sven Schöling
} elsif ($config->{type} eq 'number') {
$config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
8688e71e Moritz Bunkus
a7e924f5 Bernd Bleßmann
} elsif ($config->{type} eq 'textfield') {
$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);

} elsif ($config->{type} eq 'text') {
$config->{maxlength} = $1 if ($config->{options} =~ m/maxlength=(\d+)/i);

4b7433f4 Sven Schöling
}
ef220490 Moritz Bunkus
4b7433f4 Sven Schöling
$self->_unpack_flags($config);
36703a86 Bernd Bleßmann
my $cvar_config = SL::DB::CustomVariableConfig->new(id => $config->{id})->load;
@{$config->{'partsgroups'}} = map {$_->id} @{$cvar_config->partsgroups};

4b7433f4 Sven Schöling
}
$::form->{CVAR_CONFIGS}->{$params{module}} = $configs;
8688e71e Moritz Bunkus
}

$main::lxdebug->leave_sub();

4b7433f4 Sven Schöling
return $::form->{CVAR_CONFIGS}->{$params{module}};
8688e71e Moritz Bunkus
}

ef220490 Moritz Bunkus
sub _unpack_flags {
$main::lxdebug->enter_sub();

my $self = shift;
my $config = shift;

foreach my $flag (split m/:/, $config->{flags}) {
if ($flag =~ m/(.*?)=(.*)/) {
$config->{"flag_${1}"} = $2;
} else {
$config->{"flag_${flag}"} = 1;
}
}

$main::lxdebug->leave_sub();
}

8688e71e Moritz Bunkus
sub get_custom_variables {
$main::lxdebug->enter_sub();

my $self = shift;
my %params = @_;

Common::check_params(\%params, qw(module));

my $myconfig = \%main::myconfig;
my $form = $main::form;

my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);

01cbb526 Waldemar Toews
my $sub_module = $params{sub_module} ? $params{sub_module} : '';

8688e71e Moritz Bunkus
my $q_var =
qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value
FROM custom_variables
01cbb526 Waldemar Toews
WHERE (config_id = ?) AND (trans_id = ?) AND (sub_module = ?)|;
8688e71e Moritz Bunkus
my $h_var = prepare_query($form, $dbh, $q_var);

4b7433f4 Sven Schöling
my $custom_variables = $self->get_configs(module => $params{module});
8688e71e Moritz Bunkus
foreach my $cvar (@{ $custom_variables }) {
if ($cvar->{type} eq 'textfield') {
$cvar->{width} = 30;
$cvar->{height} = 5;

$cvar->{width} = $1 if ($cvar->{options} =~ m/width=(\d+)/i);
$cvar->{height} = $1 if ($cvar->{options} =~ m/height=(\d+)/i);

} elsif ($cvar->{type} eq 'text') {
$cvar->{maxlength} = $1 if ($cvar->{options} =~ m/maxlength=(\d+)/i);

} elsif ($cvar->{type} eq 'number') {
$cvar->{precision} = $1 if ($cvar->{options} =~ m/precision=(\d+)/i);

} elsif ($cvar->{type} eq 'select') {
$cvar->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $cvar->{options}) ];
}

4cf0100e Moritz Bunkus
my ($act_var, $valid);
8688e71e Moritz Bunkus
if ($params{trans_id}) {
ff3bb5cf Sven Schöling
my @values = (conv_i($cvar->{id}), conv_i($params{trans_id}), $sub_module);
ef220490 Moritz Bunkus
do_statement($form, $h_var, $q_var, @values);
8688e71e Moritz Bunkus
$act_var = $h_var->fetchrow_hashref();
3e2892b1 Sven Schöling
4cf0100e Moritz Bunkus
$valid = $self->get_custom_variables_validity(config_id => $cvar->{id}, trans_id => $params{trans_id});
86b6ff8a Bernd Bleßmann
} else {
$valid = !$cvar->{flag_defaults_to_invalid};
8688e71e Moritz Bunkus
}

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}
ef90159c Sven Schöling
: $cvar->{type} eq 'customer' ? $act_var->{number_value}
eb518737 Bernd Bleßmann
: $cvar->{type} eq 'vendor' ? $act_var->{number_value}
: $cvar->{type} eq 'part' ? $act_var->{number_value}
8688e71e Moritz Bunkus
: $cvar->{type} eq 'bool' ? $act_var->{bool_value}
: $act_var->{text_value};
4cf0100e Moritz Bunkus
$cvar->{valid} = $valid;
8688e71e Moritz Bunkus
} else {
86b6ff8a Bernd Bleßmann
$cvar->{valid} = $valid // 1;
29b7a641 Sven Schöling
8688e71e Moritz Bunkus
if ($cvar->{type} eq 'date') {
if ($cvar->{default_value} eq 'NOW') {
$cvar->{value} = $cvar->{current_date};
} else {
$cvar->{value} = $cvar->{default_value};
}

} elsif ($cvar->{type} eq 'timestamp') {
if ($cvar->{default_value} eq 'NOW') {
$cvar->{value} = $cvar->{current_timestamp};
} else {
$cvar->{value} = $cvar->{default_value};
}

} elsif ($cvar->{type} eq 'bool') {
$cvar->{value} = $cvar->{default_value} * 1;

} elsif ($cvar->{type} eq 'number') {
$cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');

} else {
$cvar->{value} = $cvar->{default_value};
}
}

if ($cvar->{type} eq 'number') {
$cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
ef90159c Sven Schöling
} elsif ($cvar->{type} eq 'customer') {
require SL::DB::Customer;
$cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1);
eb518737 Bernd Bleßmann
} 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);
8688e71e Moritz Bunkus
}
}

$h_var->finish();

$main::lxdebug->leave_sub();

return $custom_variables;
}

sub save_custom_variables {
$main::lxdebug->enter_sub();

my $self = shift;
my %params = @_;

Common::check_params(\%params, qw(module trans_id variables));

my $myconfig = \%main::myconfig;
my $form = $main::form;

my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);

ef220490 Moritz Bunkus
my @configs = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
8688e71e Moritz Bunkus
my $query =
qq|DELETE FROM custom_variables
WHERE (trans_id = ?)
AND (config_id IN (SELECT DISTINCT id
FROM custom_variable_configs
WHERE module = ?))|;
ef220490 Moritz Bunkus
my @values = (conv_i($params{trans_id}), $params{module});

if ($params{sub_module}) {
$query .= qq| AND (sub_module = ?)|;
push @values, $params{sub_module};
}

do_query($form, $dbh, $query, @values);
8688e71e Moritz Bunkus
$query =
ef220490 Moritz Bunkus
qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
VALUES (?, ?, ?, ?, ?, ?, ?)|;
8688e71e Moritz Bunkus
my $sth = prepare_query($form, $dbh, $query);

foreach my $config (@configs) {
ef220490 Moritz Bunkus
my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
8688e71e Moritz Bunkus
ef220490 Moritz Bunkus
my $value = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
8688e71e Moritz Bunkus
if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
push @values, undef, undef, $value, undef;

} elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
push @values, undef, conv_date($value), undef, undef;

} elsif ($config->{type} eq 'number') {
push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));

} elsif ($config->{type} eq 'bool') {
push @values, $value ? 't' : 'f', undef, undef, undef;
eb518737 Bernd Bleßmann
} elsif (any { $config->{type} eq $_ } qw(customer vendor part)) {
ef90159c Sven Schöling
push @values, undef, undef, undef, $value * 1;
8688e71e Moritz Bunkus
}

do_statement($form, $sth, $query, @values);
3e2892b1 Sven Schöling
2bfb8a50 Moritz Bunkus
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)
);
}
8688e71e Moritz Bunkus
}

$sth->finish();

7ecc9c39 Geoffrey Richardson
$dbh->commit() unless $params{dbh};
8688e71e Moritz Bunkus
$main::lxdebug->leave_sub();
}

sub render_inputs {
77a4906b Sven Schöling
$main::lxdebug->enter_sub(2);
8688e71e Moritz Bunkus
my $self = shift;
my %params = @_;

Common::check_params(\%params, qw(variables));

my $myconfig = \%main::myconfig;
my $form = $main::form;

36703a86 Bernd Bleßmann
my %options = ( name_prefix => "$params{name_prefix}",
name_postfix => "$params{name_postfix}",
hide_non_editable => $params{hide_non_editable},
6988b41a Sven Schöling
show_disabled_message => $params{show_disabled_message},
ef220490 Moritz Bunkus
);

36703a86 Bernd Bleßmann
# should this cvar be filtered by partsgroups?
8688e71e Moritz Bunkus
foreach my $var (@{ $params{variables} }) {
36703a86 Bernd Bleßmann
if ($var->{flag_partsgroup_filter}) {
if (!$params{partsgroup_id} || (!grep {$params{partsgroup_id} == $_} @{ $var->{partsgroups} })) {
$var->{partsgroup_filtered} = 1;
}
}

6988b41a Sven Schöling
$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 });
8688e71e Moritz Bunkus
}

77a4906b Sven Schöling
$main::lxdebug->leave_sub(2);
8688e71e Moritz Bunkus
}

sub render_search_options {
$main::lxdebug->enter_sub();

my $self = shift;
my %params = @_;

Common::check_params(\%params, qw(variables));

my $myconfig = \%main::myconfig;
my $form = $main::form;

c1e220bf Sven Schöling
$params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};

8688e71e Moritz Bunkus
$params{include_prefix} = 'l_' unless defined($params{include_prefix});
$params{include_value} ||= '1';
c4713436 Sven Schöling
$params{filter_prefix} ||= '';
8688e71e Moritz Bunkus
my $filter = $form->parse_html_template('amcvar/search_filter', \%params);
my $include = $form->parse_html_template('amcvar/search_include', \%params);

$main::lxdebug->leave_sub();

return ($filter, $include);
}

sub build_filter_query {
$main::lxdebug->enter_sub();

my $self = shift;
my %params = @_;

Common::check_params(\%params, qw(module trans_id_field filter));

my $myconfig = \%main::myconfig;
my $form = $main::form;

my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);

my $configs = $self->get_configs(%params);

my (@where, @values);

foreach my $config (@{ $configs }) {
next unless ($config->{searchable});

my $name = "cvar_$config->{name}";

my (@sub_values, @sub_where, $not);

if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
next unless ($params{filter}->{$name});

push @sub_where, qq|cvar.text_value ILIKE ?|;
bc40bcab Moritz Bunkus
push @sub_values, like($params{filter}->{$name});
8688e71e Moritz Bunkus
} elsif ($config->{type} eq 'select') {
next unless ($params{filter}->{$name});

push @sub_where, qq|cvar.text_value = ?|;
push @sub_values, $params{filter}->{$name};

} elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
my $name_from = "${name}_from";
my $name_to = "${name}_to";

if ($params{filter}->{$name_from}) {
push @sub_where, qq|cvar.timestamp_value >= ?|;
push @sub_values, conv_date($params{filter}->{$name_from});
}

if ($params{filter}->{$name_to}) {
push @sub_where, qq|cvar.timestamp_value <= ?|;
push @sub_values, conv_date($params{filter}->{$name_to});
}

} elsif ($config->{type} eq 'number') {
next if ($params{filter}->{$name} eq '');

my $f_op = $params{filter}->{"${name}_qtyop"};

1163cee7 Sven Schöling
my $op;
8688e71e Moritz Bunkus
if ($f_op eq '==') {
$op = '=';

} elsif ($f_op eq '=/=') {
$not = 'NOT';
$op = '<>';

} elsif ($f_op eq '<') {
$not = 'NOT';
$op = '>=';

} elsif ($f_op eq '<=') {
$not = 'NOT';
$op = '>';

} elsif (($f_op eq '>') || ($f_op eq '>=')) {
$op = $f_op;

} else {
$op = '=';
}

push @sub_where, qq|cvar.number_value $op ?|;
88dea78e Moritz Bunkus
push @sub_values, $form->parse_amount($myconfig, trim($params{filter}->{$name}));
8688e71e Moritz Bunkus
} 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|;
121626b9 Bernd Bleßmann
} elsif (any { $config->{type} eq $_ } qw(customer vendor)) {
ef90159c Sven Schöling
next unless $params{filter}->{$name};

eb518737 Bernd Bleßmann
my $table = $config->{type};
push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM $table WHERE name ILIKE ?)|;
bc40bcab Moritz Bunkus
push @sub_values, like($params{filter}->{$name});
eb518737 Bernd Bleßmann
} 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 ?)|;
bc40bcab Moritz Bunkus
push @sub_values, like($params{filter}->{$name});
8688e71e Moritz Bunkus
}

if (@sub_where) {
60ebd03b Sven Schöling
add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
ef220490 Moritz Bunkus
8688e71e Moritz Bunkus
push @where,
qq|$not EXISTS(
SELECT cvar.id
FROM custom_variables cvar
LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
WHERE (cvarcfg.module = ?)
AND (cvarcfg.id = ?)
AND (cvar.trans_id = $params{trans_id_field})
AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
push @values, $params{module}, conv_i($config->{id}), @sub_values;
}
}

my $query = join ' AND ', @where;

$main::lxdebug->leave_sub();

return ($query, @values);
}

sub add_custom_variables_to_report {
$main::lxdebug->enter_sub();

my $self = shift;
my %params = @_;

Common::check_params(\%params, qw(module trans_id_field column_defs data configs));

my $myconfig = \%main::myconfig;
my $form = $main::form;
my $locale = $main::locale;

my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);

my $configs = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];

if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
$main::lxdebug->leave_sub();
return;
}

1163cee7 Sven Schöling
# allow sub_module to be a coderef or a fixed value
if (ref $params{sub_module} ne 'CODE') {
a35a796b Sven Schöling
my $sub_module = "$params{sub_module}";
$params{sub_module} = sub { $sub_module };
1163cee7 Sven Schöling
}

8688e71e Moritz Bunkus
my %cfg_map = map { $_->{id} => $_ } @{ $configs };
my @cfg_ids = keys %cfg_map;

my $query =
qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
FROM custom_variables
ef220490 Moritz Bunkus
WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
AND (trans_id = ?)
AND (sub_module = ?)|;
8688e71e Moritz Bunkus
my $sth = prepare_query($form, $dbh, $query);

foreach my $row (@{ $params{data} }) {
1163cee7 Sven Schöling
do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
8688e71e Moritz Bunkus
while (my $ref = $sth->fetchrow_hashref()) {
my $cfg = $cfg_map{$ref->{config_id}};

$row->{"cvar_$cfg->{name}"} =
$cfg->{type} eq 'date' ? $ref->{date_value}
: $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
1163cee7 Sven Schöling
: $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
6dc16f23 Jan Büren
: $cfg->{type} eq 'customer' ? (SL::DB::Manager::Customer->find_by(id => 1*$ref->{number_value}) || SL::DB::Customer->new)->name
eb518737 Bernd Bleßmann
: $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
8688e71e Moritz Bunkus
: $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
: $ref->{text_value};
}
}

$sth->finish();

$main::lxdebug->leave_sub();
}

sub get_field_format_list {
$main::lxdebug->enter_sub();

my $self = shift;
my %params = @_;

Common::check_params(\%params, qw(module));

my $myconfig = \%main::myconfig;
my $form = $main::form;

my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);

my $configs = $self->get_configs(%params);

my $date_fields = [];
my $number_fields = {};

foreach my $config (@{ $configs }) {
my $name = "$params{prefix}cvar_$config->{name}";
b15e4435 Moritz Bunkus
8688e71e Moritz Bunkus
if ($config->{type} eq 'date') {
push @{ $date_fields }, $name;

} elsif ($config->{type} eq 'number') {
$number_fields->{$config->{precision}} ||= [];
push @{ $number_fields->{$config->{precision}} }, $name;
}
}

$main::lxdebug->leave_sub();

return ($date_fields, $number_fields);
}

3e2892b1 Sven Schöling
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}) {
d6adcf01 Sven Schöling
foreach my $config_id (listify($params{config_id})) {
foreach my $trans_id (listify($params{trans_id})) {
3e2892b1 Sven Schöling
do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
}
}
}

$sth->finish();

7ecc9c39 Geoffrey Richardson
$dbh->commit() unless $params{dbh};
3e2892b1 Sven Schöling
$main::lxdebug->leave_sub();
}

sub get_custom_variables_validity {
77a4906b Sven Schöling
$main::lxdebug->enter_sub(2);
3e2892b1 Sven Schöling
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);

873a9a83 Moritz Bunkus
my $query = qq|SELECT id FROM custom_variables_validity WHERE config_id = ? AND trans_id = ? LIMIT 1|;
3e2892b1 Sven Schöling
561744fd Sven Schöling
my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
3e2892b1 Sven Schöling
77a4906b Sven Schöling
$main::lxdebug->leave_sub(2);
3e2892b1 Sven Schöling
561744fd Sven Schöling
return !$invalid;
3e2892b1 Sven Schöling
}
8688e71e Moritz Bunkus
ad2e3220 Sven Schöling
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);

873a9a83 Moritz Bunkus
my $query = qq|SELECT DISTINCT config_id FROM custom_variables_validity WHERE trans_id = ?|;
ad2e3220 Sven Schöling
873a9a83 Moritz Bunkus
my %invalids = map { +($_->{config_id} => 1) } selectall_hashref_query($form, $dbh, $query, $params{trans_id});
ad2e3220 Sven Schöling
$main::lxdebug->leave_sub(2);

return sub { !$invalids{+shift} };
}

db7c3a52 Sven Schöling
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';
ed4b292c Bernd Bleßmann
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';
db7c3a52 Sven Schöling
return $value;
}

d729e328 Sven Schöling
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

2193f03f Bernd Bleßmann
return $::form->format_amount(\%::myconfig, $value) if $config->{type} eq 'number';
d729e328 Sven Schöling
return $value->to_lxoffice if $config->{type} eq 'date' && blessed $value && $value->can('to_lxoffice');
return $value;
}

be42a450 Bernd Bleßmann
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);
}

8688e71e Moritz Bunkus
1;
66022cbd Sven Schöling
__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:

d88c278c Geoffrey Richardson
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.
66022cbd Sven Schöling
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