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};
29 SELECT *, date_trunc('seconds', localtimestamp) AS current_timestamp
30 FROM custom_variable_configs $where ORDER BY sortkey
33 $::form->{CVAR_CONFIGS} = {} unless 'HASH' eq ref $::form->{CVAR_CONFIGS};
34 if (!$::form->{CVAR_CONFIGS}->{$params{module}}) {
35 my $configs = selectall_hashref_query($form, $dbh, $query, @values);
37 foreach my $config (@{ $configs }) {
38 if ($config->{type} eq 'select') {
39 $config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
41 } elsif ($config->{type} eq 'number') {
42 $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
46 $self->_unpack_flags($config);
48 $::form->{CVAR_CONFIGS}->{$params{module}} = $configs;
51 $main::lxdebug->leave_sub();
53 return $::form->{CVAR_CONFIGS}->{$params{module}};
57 $main::lxdebug->enter_sub();
62 Common::check_params(\%params, qw(id));
64 my $myconfig = \%main::myconfig;
65 my $form = $main::form;
67 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
69 my $query = qq|SELECT * FROM custom_variable_configs WHERE id = ?|;
71 my $config = selectfirst_hashref_query($form, $dbh, $query, conv_i($params{id})) || { };
73 $self->_unpack_flags($config);
75 $main::lxdebug->leave_sub();
81 $main::lxdebug->enter_sub();
86 foreach my $flag (split m/:/, $config->{flags}) {
87 if ($flag =~ m/(.*?)=(.*)/) {
88 $config->{"flag_${1}"} = $2;
90 $config->{"flag_${flag}"} = 1;
94 $main::lxdebug->leave_sub();
98 $main::lxdebug->enter_sub();
103 Common::check_params(\%params, qw(module config));
105 my $myconfig = \%main::myconfig;
106 my $form = $main::form;
108 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
110 my $q_id = qq|SELECT nextval('custom_variable_configs_id')|;
111 my $h_id = prepare_query($form, $dbh, $q_id);
114 qq|INSERT INTO custom_variable_configs (name, description, type, default_value, options, searchable, includeable, included_by_default, module, flags, id, sortkey)
115 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
116 (SELECT COALESCE(MAX(sortkey) + 1, 1) FROM custom_variable_configs))|;
117 my $h_new = prepare_query($form, $dbh, $q_new);
120 qq|UPDATE custom_variable_configs SET
121 name = ?, description = ?,
122 type = ?, default_value = ?,
123 options = ?, searchable = ?,
124 includeable = ?, included_by_default = ?,
125 module = ?, flags = ?
127 my $h_update = prepare_query($form, $dbh, $q_update);
130 if ('ARRAY' eq ref $params{config}) {
131 @configs = @{ $params{config} };
133 @configs = ($params{config});
136 foreach my $config (@configs) {
137 my ($h_actual, $q_actual);
139 if (!$config->{id}) {
140 do_statement($form, $h_id, $q_id);
141 ($config->{id}) = $h_id->fetchrow_array();
147 $h_actual = $h_update;
148 $q_actual = $q_update;
151 do_statement($form, $h_actual, $q_actual, @{$config}{qw(name description type default_value options)},
152 $config->{searchable} ? 't' : 'f', $config->{includeable} ? 't' : 'f', $config->{included_by_default} ? 't' : 'f',
153 $params{module}, $config->{flags}, conv_i($config->{id}));
162 $main::lxdebug->leave_sub();
166 $main::lxdebug->enter_sub();
171 Common::check_params(\%params, qw(id));
173 my $myconfig = \%main::myconfig;
174 my $form = $main::form;
176 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
178 do_query($form, $dbh, qq|DELETE FROM custom_variables WHERE config_id = ?|, conv_i($params{id}));
179 do_query($form, $dbh, qq|DELETE FROM custom_variable_configs WHERE id = ?|, conv_i($params{id}));
183 $main::lxdebug->leave_sub();
186 sub get_custom_variables {
187 $main::lxdebug->enter_sub();
192 Common::check_params(\%params, qw(module));
194 my $myconfig = \%main::myconfig;
195 my $form = $main::form;
197 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
199 my $trans_id = $params{trans_id} ? 'OR (v.trans_id = ?) ' : '';
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 = $self->get_configs(module => $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}) ];
228 my ($act_var, $valid);
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 $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} = $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 if ($params{save_validity}) {
342 my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
343 $self->save_custom_variables_validity(trans_id => $params{trans_id},
344 config_id => $config->{id},
345 validity => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
354 $main::lxdebug->leave_sub();
358 $main::lxdebug->enter_sub(2);
363 Common::check_params(\%params, qw(variables));
365 my $myconfig = \%main::myconfig;
366 my $form = $main::form;
368 my %options = ( name_prefix => "$params{name_prefix}",
369 name_postfix => "$params{name_postfix}",
370 hide_non_editable => $params{hide_non_editable},
371 show_disabled_message => $params{show_disabled_message},
374 foreach my $var (@{ $params{variables} }) {
375 $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { var => $var, %options });
376 $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
379 $main::lxdebug->leave_sub(2);
382 sub render_search_options {
383 $main::lxdebug->enter_sub();
388 Common::check_params(\%params, qw(variables));
390 my $myconfig = \%main::myconfig;
391 my $form = $main::form;
393 $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
395 $params{include_prefix} = 'l_' unless defined($params{include_prefix});
396 $params{include_value} ||= '1';
398 my $filter = $form->parse_html_template('amcvar/search_filter', \%params);
399 my $include = $form->parse_html_template('amcvar/search_include', \%params);
401 $main::lxdebug->leave_sub();
403 return ($filter, $include);
406 sub build_filter_query {
407 $main::lxdebug->enter_sub();
412 Common::check_params(\%params, qw(module trans_id_field filter));
414 my $myconfig = \%main::myconfig;
415 my $form = $main::form;
417 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
419 my $configs = $self->get_configs(%params);
421 my (@where, @values);
423 foreach my $config (@{ $configs }) {
424 next unless ($config->{searchable});
426 my $name = "cvar_$config->{name}";
428 my (@sub_values, @sub_where, $not);
430 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
431 next unless ($params{filter}->{$name});
433 push @sub_where, qq|cvar.text_value ILIKE ?|;
434 push @sub_values, '%' . $params{filter}->{$name} . '%'
436 } elsif ($config->{type} eq 'select') {
437 next unless ($params{filter}->{$name});
439 push @sub_where, qq|cvar.text_value = ?|;
440 push @sub_values, $params{filter}->{$name};
442 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
443 my $name_from = "${name}_from";
444 my $name_to = "${name}_to";
446 if ($params{filter}->{$name_from}) {
447 push @sub_where, qq|cvar.timestamp_value >= ?|;
448 push @sub_values, conv_date($params{filter}->{$name_from});
451 if ($params{filter}->{$name_to}) {
452 push @sub_where, qq|cvar.timestamp_value <= ?|;
453 push @sub_values, conv_date($params{filter}->{$name_to});
456 } elsif ($config->{type} eq 'number') {
457 next if ($params{filter}->{$name} eq '');
459 my $f_op = $params{filter}->{"${name}_qtyop"};
465 } elsif ($f_op eq '=/=') {
469 } elsif ($f_op eq '<') {
473 } elsif ($f_op eq '<=') {
477 } elsif (($f_op eq '>') || ($f_op eq '>=')) {
484 push @sub_where, qq|cvar.number_value $op ?|;
485 push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
487 } elsif ($config->{type} eq 'bool') {
488 next unless ($params{filter}->{$name});
490 $not = 'NOT' if ($params{filter}->{$name} eq 'no');
491 push @sub_where, qq|COALESCE(cvar.bool_value, false) = TRUE|;
495 add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
500 FROM custom_variables cvar
501 LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
502 WHERE (cvarcfg.module = ?)
504 AND (cvar.trans_id = $params{trans_id_field})
505 AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
506 push @values, $params{module}, conv_i($config->{id}), @sub_values;
510 my $query = join ' AND ', @where;
512 $main::lxdebug->leave_sub();
514 return ($query, @values);
517 sub add_custom_variables_to_report {
518 $main::lxdebug->enter_sub();
523 Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
525 my $myconfig = \%main::myconfig;
526 my $form = $main::form;
527 my $locale = $main::locale;
529 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
531 my $configs = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
533 if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
534 $main::lxdebug->leave_sub();
538 # allow sub_module to be a coderef or a fixed value
539 if (ref $params{sub_module} ne 'CODE') {
540 my $sub_module = "$params{sub_module}";
541 $params{sub_module} = sub { $sub_module };
544 my %cfg_map = map { $_->{id} => $_ } @{ $configs };
545 my @cfg_ids = keys %cfg_map;
548 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
549 FROM custom_variables
550 WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
552 AND (sub_module = ?)|;
553 my $sth = prepare_query($form, $dbh, $query);
555 foreach my $row (@{ $params{data} }) {
556 do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
558 while (my $ref = $sth->fetchrow_hashref()) {
559 my $cfg = $cfg_map{$ref->{config_id}};
561 $row->{"cvar_$cfg->{name}"} =
562 $cfg->{type} eq 'date' ? $ref->{date_value}
563 : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
564 : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
565 : $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
566 : $ref->{text_value};
572 $main::lxdebug->leave_sub();
575 sub get_field_format_list {
576 $main::lxdebug->enter_sub();
581 Common::check_params(\%params, qw(module));
583 my $myconfig = \%main::myconfig;
584 my $form = $main::form;
586 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
588 my $configs = $self->get_configs(%params);
590 my $date_fields = [];
591 my $number_fields = {};
593 foreach my $config (@{ $configs }) {
594 my $name = "$params{prefix}cvar_$config->{name}";
596 if ($config->{type} eq 'date') {
597 push @{ $date_fields }, $name;
599 } elsif ($config->{type} eq 'number') {
600 $number_fields->{$config->{precision}} ||= [];
601 push @{ $number_fields->{$config->{precision}} }, $name;
605 $main::lxdebug->leave_sub();
607 return ($date_fields, $number_fields);
610 sub save_custom_variables_validity {
611 $main::lxdebug->enter_sub();
616 Common::check_params(\%params, qw(config_id trans_id validity));
618 my $myconfig = \%main::myconfig;
619 my $form = $main::form;
621 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
623 my (@where, @values);
624 add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
625 add_token(\@where, \@values, col => "trans_id", val => $params{trans_id}, esc => \&conv_i);
627 my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
628 my $query = qq|DELETE FROM custom_variables_validity $where|;
630 do_query($form, $dbh, $query, @values);
633 qq|INSERT INTO custom_variables_validity (config_id, trans_id)
635 my $sth = prepare_query($form, $dbh, $query);
637 unless ($params{validity}) {
638 foreach my $config_id (listify $params{config_id}) {
639 foreach my $trans_id (listify $params{trans_id}) {
640 do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
649 $main::lxdebug->leave_sub();
652 sub get_custom_variables_validity {
653 $main::lxdebug->enter_sub(2);
658 Common::check_params(\%params, qw(config_id trans_id));
660 my $myconfig = \%main::myconfig;
661 my $form = $main::form;
663 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
665 my $query = qq|SELECT COUNT(*) FROM custom_variables_validity WHERE config_id = ? AND trans_id = ?|;
667 my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
669 $main::lxdebug->leave_sub(2);
674 sub custom_variables_validity_by_trans_id {
675 $main::lxdebug->enter_sub(2);
680 return sub { 0 } unless $params{trans_id};
682 my $myconfig = \%main::myconfig;
683 my $form = $main::form;
685 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
687 my $query = qq|SELECT config_id, COUNT(*) FROM custom_variables_validity WHERE trans_id = ? GROUP BY config_id|;
689 my %invalids = selectall_as_map($form, $dbh, $query, 'config_id', 'count', $params{trans_id});
691 $main::lxdebug->leave_sub(2);
693 return sub { !$invalids{+shift} };
702 SL::CVar.pm - Custom Variables module
706 # dealing with configs
708 my $all_configs = CVar->get_configs()
709 my $config = CVar->get_config(id => '1234')
711 CVar->save_config($config);
712 CVar->delete->config($config)
714 # dealing with custom vars
716 CVar->get_custom_variables(module => 'ic')
720 Suppose the following scenario:
722 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.
724 Validity is assumed. If you modify validity, you actually save B<invalidity>.
725 Invalidity is saved as a function of config_id, and the trans_id
727 In the naive way, disable an attribute for a specific id (simple)