5 use List::Util qw(first);
9 use SL::MoreCommon qw(listify);
12 $main::lxdebug->enter_sub();
17 my $myconfig = \%main::myconfig;
18 my $form = $main::form;
20 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
23 if ($params{module}) {
24 $where = 'WHERE module = ?';
25 push @values, $params{module};
28 my $query = qq|SELECT * FROM custom_variable_configs $where ORDER BY sortkey|;
30 my $configs = selectall_hashref_query($form, $dbh, $query, @values);
32 foreach my $config (@{ $configs }) {
33 if ($config->{type} eq 'select') {
34 $config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
36 } elsif ($config->{type} eq 'number') {
37 $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
41 $self->_unpack_flags($config);
44 $main::lxdebug->leave_sub();
50 $main::lxdebug->enter_sub();
55 Common::check_params(\%params, qw(id));
57 my $myconfig = \%main::myconfig;
58 my $form = $main::form;
60 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
62 my $query = qq|SELECT * FROM custom_variable_configs WHERE id = ?|;
64 my $config = selectfirst_hashref_query($form, $dbh, $query, conv_i($params{id})) || { };
66 $self->_unpack_flags($config);
68 $main::lxdebug->leave_sub();
74 $main::lxdebug->enter_sub();
79 foreach my $flag (split m/:/, $config->{flags}) {
80 if ($flag =~ m/(.*?)=(.*)/) {
81 $config->{"flag_${1}"} = $2;
83 $config->{"flag_${flag}"} = 1;
87 $main::lxdebug->leave_sub();
91 $main::lxdebug->enter_sub();
96 Common::check_params(\%params, qw(module config));
98 my $myconfig = \%main::myconfig;
99 my $form = $main::form;
101 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
103 my $q_id = qq|SELECT nextval('custom_variable_configs_id')|;
104 my $h_id = prepare_query($form, $dbh, $q_id);
107 qq|INSERT INTO custom_variable_configs (name, description, type, default_value, options, searchable, includeable, included_by_default, module, flags, id, sortkey)
108 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
109 (SELECT COALESCE(MAX(sortkey) + 1, 1) FROM custom_variable_configs))|;
110 my $h_new = prepare_query($form, $dbh, $q_new);
113 qq|UPDATE custom_variable_configs SET
114 name = ?, description = ?,
115 type = ?, default_value = ?,
116 options = ?, searchable = ?,
117 includeable = ?, included_by_default = ?,
118 module = ?, flags = ?
120 my $h_update = prepare_query($form, $dbh, $q_update);
123 if ('ARRAY' eq ref $params{config}) {
124 @configs = @{ $params{config} };
126 @configs = ($params{config});
129 foreach my $config (@configs) {
130 my ($h_actual, $q_actual);
132 if (!$config->{id}) {
133 do_statement($form, $h_id, $q_id);
134 ($config->{id}) = $h_id->fetchrow_array();
140 $h_actual = $h_update;
141 $q_actual = $q_update;
144 do_statement($form, $h_actual, $q_actual, @{$config}{qw(name description type default_value options)},
145 $config->{searchable} ? 't' : 'f', $config->{includeable} ? 't' : 'f', $config->{included_by_default} ? 't' : 'f',
146 $params{module}, $config->{flags}, conv_i($config->{id}));
155 $main::lxdebug->leave_sub();
159 $main::lxdebug->enter_sub();
164 Common::check_params(\%params, qw(id));
166 my $myconfig = \%main::myconfig;
167 my $form = $main::form;
169 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
171 do_query($form, $dbh, qq|DELETE FROM custom_variables WHERE config_id = ?|, conv_i($params{id}));
172 do_query($form, $dbh, qq|DELETE FROM custom_variable_configs WHERE id = ?|, conv_i($params{id}));
176 $main::lxdebug->leave_sub();
179 sub get_custom_variables {
180 $main::lxdebug->enter_sub();
185 Common::check_params(\%params, qw(module));
187 my $myconfig = \%main::myconfig;
188 my $form = $main::form;
190 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
192 my $trans_id = $params{trans_id} ? 'OR (v.trans_id = ?) ' : '';
195 qq|SELECT id, name, description, type, default_value, options,
196 date_trunc('seconds', localtimestamp) AS current_timestamp, current_date AS current_date
197 FROM custom_variable_configs
202 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value
203 FROM custom_variables
204 WHERE (config_id = ?) AND (trans_id = ?)|;
205 $q_var .= qq| AND (sub_module = ?)| if $params{sub_module};
206 my $h_var = prepare_query($form, $dbh, $q_var);
208 my $custom_variables = selectall_hashref_query($form, $dbh, $q_cfg, $params{module});
210 foreach my $cvar (@{ $custom_variables }) {
211 if ($cvar->{type} eq 'textfield') {
215 $cvar->{width} = $1 if ($cvar->{options} =~ m/width=(\d+)/i);
216 $cvar->{height} = $1 if ($cvar->{options} =~ m/height=(\d+)/i);
218 } elsif ($cvar->{type} eq 'text') {
219 $cvar->{maxlength} = $1 if ($cvar->{options} =~ m/maxlength=(\d+)/i);
221 } elsif ($cvar->{type} eq 'number') {
222 $cvar->{precision} = $1 if ($cvar->{options} =~ m/precision=(\d+)/i);
224 } elsif ($cvar->{type} eq 'select') {
225 $cvar->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $cvar->{options}) ];
229 if ($params{trans_id}) {
230 my @values = (conv_i($cvar->{id}), conv_i($params{trans_id}));
231 push @values, $params{sub_module} if $params{sub_module};
233 do_statement($form, $h_var, $q_var, @values);
234 $act_var = $h_var->fetchrow_hashref();
236 $act_var->{valid} = $self->get_custom_variables_validity(config_id => $cvar->{id}, trans_id => $params{trans_id});
240 $cvar->{value} = $cvar->{type} eq 'date' ? $act_var->{date_value}
241 : $cvar->{type} eq 'timestamp' ? $act_var->{timestamp_value}
242 : $cvar->{type} eq 'number' ? $act_var->{number_value}
243 : $cvar->{type} eq 'bool' ? $act_var->{bool_value}
244 : $act_var->{text_value};
245 $cvar->{valid} = $act_var->{valid};
249 if ($cvar->{type} eq 'date') {
250 if ($cvar->{default_value} eq 'NOW') {
251 $cvar->{value} = $cvar->{current_date};
253 $cvar->{value} = $cvar->{default_value};
256 } elsif ($cvar->{type} eq 'timestamp') {
257 if ($cvar->{default_value} eq 'NOW') {
258 $cvar->{value} = $cvar->{current_timestamp};
260 $cvar->{value} = $cvar->{default_value};
263 } elsif ($cvar->{type} eq 'bool') {
264 $cvar->{value} = $cvar->{default_value} * 1;
266 } elsif ($cvar->{type} eq 'number') {
267 $cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');
270 $cvar->{value} = $cvar->{default_value};
274 if ($cvar->{type} eq 'number') {
275 $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
281 $main::lxdebug->leave_sub();
283 return $custom_variables;
286 sub save_custom_variables {
287 $main::lxdebug->enter_sub();
292 Common::check_params(\%params, qw(module trans_id variables));
294 my $myconfig = \%main::myconfig;
295 my $form = $main::form;
297 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
299 my @configs = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
302 qq|DELETE FROM custom_variables
304 AND (config_id IN (SELECT DISTINCT id
305 FROM custom_variable_configs
307 my @values = (conv_i($params{trans_id}), $params{module});
309 if ($params{sub_module}) {
310 $query .= qq| AND (sub_module = ?)|;
311 push @values, $params{sub_module};
314 do_query($form, $dbh, $query, @values);
317 qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
318 VALUES (?, ?, ?, ?, ?, ?, ?)|;
319 my $sth = prepare_query($form, $dbh, $query);
321 foreach my $config (@configs) {
322 my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
324 my $value = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
326 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
327 push @values, undef, undef, $value, undef;
329 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
330 push @values, undef, conv_date($value), undef, undef;
332 } elsif ($config->{type} eq 'number') {
333 push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));
335 } elsif ($config->{type} eq 'bool') {
336 push @values, $value ? 't' : 'f', undef, undef, undef;
339 do_statement($form, $sth, $query, @values);
341 unless ($params{always_valid}) {
342 $self->save_custom_variables_validity(trans_id => $params{trans_id}, config_id => $config->{id},
343 validity => ($params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid"} ? 1 : 0)
352 $main::lxdebug->leave_sub();
356 $main::lxdebug->enter_sub(2);
361 Common::check_params(\%params, qw(variables));
363 my $myconfig = \%main::myconfig;
364 my $form = $main::form;
366 my %options = ( name_prefix => "$params{name_prefix}",
367 name_postfix => "$params{name_postfix}",
368 hide_non_editable => $params{hide_non_editable},
369 show_disabled_message => $params{show_disabled_message},
372 foreach my $var (@{ $params{variables} }) {
373 $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { var => $var, %options });
374 $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
377 $main::lxdebug->leave_sub(2);
380 sub render_search_options {
381 $main::lxdebug->enter_sub();
386 Common::check_params(\%params, qw(variables));
388 my $myconfig = \%main::myconfig;
389 my $form = $main::form;
391 $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
393 $params{include_prefix} = 'l_' unless defined($params{include_prefix});
394 $params{include_value} ||= '1';
396 my $filter = $form->parse_html_template('amcvar/search_filter', \%params);
397 my $include = $form->parse_html_template('amcvar/search_include', \%params);
399 $main::lxdebug->leave_sub();
401 return ($filter, $include);
404 sub build_filter_query {
405 $main::lxdebug->enter_sub();
410 Common::check_params(\%params, qw(module trans_id_field filter));
412 my $myconfig = \%main::myconfig;
413 my $form = $main::form;
415 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
417 my $configs = $self->get_configs(%params);
419 my (@where, @values);
421 foreach my $config (@{ $configs }) {
422 next unless ($config->{searchable});
424 my $name = "cvar_$config->{name}";
426 my (@sub_values, @sub_where, $not);
428 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
429 next unless ($params{filter}->{$name});
431 push @sub_where, qq|cvar.text_value ILIKE ?|;
432 push @sub_values, '%' . $params{filter}->{$name} . '%'
434 } elsif ($config->{type} eq 'select') {
435 next unless ($params{filter}->{$name});
437 push @sub_where, qq|cvar.text_value = ?|;
438 push @sub_values, $params{filter}->{$name};
440 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
441 my $name_from = "${name}_from";
442 my $name_to = "${name}_to";
444 if ($params{filter}->{$name_from}) {
445 push @sub_where, qq|cvar.timestamp_value >= ?|;
446 push @sub_values, conv_date($params{filter}->{$name_from});
449 if ($params{filter}->{$name_to}) {
450 push @sub_where, qq|cvar.timestamp_value <= ?|;
451 push @sub_values, conv_date($params{filter}->{$name_to});
454 } elsif ($config->{type} eq 'number') {
455 next if ($params{filter}->{$name} eq '');
457 my $f_op = $params{filter}->{"${name}_qtyop"};
463 } elsif ($f_op eq '=/=') {
467 } elsif ($f_op eq '<') {
471 } elsif ($f_op eq '<=') {
475 } elsif (($f_op eq '>') || ($f_op eq '>=')) {
482 push @sub_where, qq|cvar.number_value $op ?|;
483 push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
485 } elsif ($config->{type} eq 'bool') {
486 next unless ($params{filter}->{$name});
488 $not = 'NOT' if ($params{filter}->{$name} eq 'no');
489 push @sub_where, qq|COALESCE(cvar.bool_value, false) = TRUE|;
493 push @sub_where, qq|cvar.sub_module = ?|;
494 push @sub_values, "$params{sub_module}";
499 FROM custom_variables cvar
500 LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
501 WHERE (cvarcfg.module = ?)
503 AND (cvar.trans_id = $params{trans_id_field})
504 AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
505 push @values, $params{module}, conv_i($config->{id}), @sub_values;
509 my $query = join ' AND ', @where;
511 $main::lxdebug->leave_sub();
513 return ($query, @values);
516 sub add_custom_variables_to_report {
517 $main::lxdebug->enter_sub();
522 Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
524 my $myconfig = \%main::myconfig;
525 my $form = $main::form;
526 my $locale = $main::locale;
528 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
530 my $configs = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
532 if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
533 $main::lxdebug->leave_sub();
537 # allow sub_module to be a coderef or a fixed value
538 if (ref $params{sub_module} ne 'CODE') {
539 my $sub_module = "$params{sub_module}";
540 $params{sub_module} = sub { $sub_module };
543 my %cfg_map = map { $_->{id} => $_ } @{ $configs };
544 my @cfg_ids = keys %cfg_map;
547 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
548 FROM custom_variables
549 WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
551 AND (sub_module = ?)|;
552 my $sth = prepare_query($form, $dbh, $query);
554 foreach my $row (@{ $params{data} }) {
555 do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
557 while (my $ref = $sth->fetchrow_hashref()) {
558 my $cfg = $cfg_map{$ref->{config_id}};
560 $row->{"cvar_$cfg->{name}"} =
561 $cfg->{type} eq 'date' ? $ref->{date_value}
562 : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
563 : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
564 : $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
565 : $ref->{text_value};
571 $main::lxdebug->leave_sub();
574 sub get_field_format_list {
575 $main::lxdebug->enter_sub();
580 Common::check_params(\%params, qw(module));
582 my $myconfig = \%main::myconfig;
583 my $form = $main::form;
585 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
587 my $configs = $self->get_configs(%params);
589 my $date_fields = [];
590 my $number_fields = {};
592 foreach my $config (@{ $configs }) {
593 my $name = "$params{prefix}cvar_$config->{name}";
595 if ($config->{type} eq 'date') {
596 push @{ $date_fields }, $name;
598 } elsif ($config->{type} eq 'number') {
599 $number_fields->{$config->{precision}} ||= [];
600 push @{ $number_fields->{$config->{precision}} }, $name;
604 $main::lxdebug->leave_sub();
606 return ($date_fields, $number_fields);
611 Suppose the following scenario:
613 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.
615 Validity is assumed. If you modify validity, you actually save B<invalidity>.
616 iNvalidity is saved as a function of config_id, and the trans_id
618 In the naive way, disable an attribute for a specific id (simple)
621 sub save_custom_variables_validity {
622 $main::lxdebug->enter_sub();
627 Common::check_params(\%params, qw(config_id trans_id validity));
629 my $myconfig = \%main::myconfig;
630 my $form = $main::form;
632 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
634 my (@where, @values);
635 add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
636 add_token(\@where, \@values, col => "trans_id", val => $params{trans_id}, esc => \&conv_i);
638 my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
639 my $query = qq|DELETE FROM custom_variables_validity $where|;
641 do_query($form, $dbh, $query, @values);
644 qq|INSERT INTO custom_variables_validity (config_id, trans_id)
646 my $sth = prepare_query($form, $dbh, $query);
648 unless ($params{validity}) {
649 foreach my $config_id (listify $params{config_id}) {
650 foreach my $trans_id (listify $params{trans_id}) {
651 do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
660 $main::lxdebug->leave_sub();
663 sub get_custom_variables_validity {
664 $main::lxdebug->enter_sub(2);
669 Common::check_params(\%params, qw(config_id trans_id));
671 my $myconfig = \%main::myconfig;
672 my $form = $main::form;
674 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
676 my $query = qq|SELECT COUNT(*) FROM custom_variables_validity WHERE config_id = ? AND trans_id = ?|;
678 my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
680 $main::lxdebug->leave_sub(2);