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 'customer' ? $act_var->{number_value}
244 : $cvar->{type} eq 'bool' ? $act_var->{bool_value}
245 : $act_var->{text_value};
246 $cvar->{valid} = $valid;
250 if ($cvar->{type} eq 'date') {
251 if ($cvar->{default_value} eq 'NOW') {
252 $cvar->{value} = $cvar->{current_date};
254 $cvar->{value} = $cvar->{default_value};
257 } elsif ($cvar->{type} eq 'timestamp') {
258 if ($cvar->{default_value} eq 'NOW') {
259 $cvar->{value} = $cvar->{current_timestamp};
261 $cvar->{value} = $cvar->{default_value};
264 } elsif ($cvar->{type} eq 'bool') {
265 $cvar->{value} = $cvar->{default_value} * 1;
267 } elsif ($cvar->{type} eq 'number') {
268 $cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');
271 $cvar->{value} = $cvar->{default_value};
275 if ($cvar->{type} eq 'number') {
276 $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
277 } elsif ($cvar->{type} eq 'customer') {
278 require SL::DB::Customer;
279 $cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1);
285 $main::lxdebug->leave_sub();
287 return $custom_variables;
290 sub save_custom_variables {
291 $main::lxdebug->enter_sub();
296 Common::check_params(\%params, qw(module trans_id variables));
298 my $myconfig = \%main::myconfig;
299 my $form = $main::form;
301 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
303 my @configs = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
306 qq|DELETE FROM custom_variables
308 AND (config_id IN (SELECT DISTINCT id
309 FROM custom_variable_configs
311 my @values = (conv_i($params{trans_id}), $params{module});
313 if ($params{sub_module}) {
314 $query .= qq| AND (sub_module = ?)|;
315 push @values, $params{sub_module};
318 do_query($form, $dbh, $query, @values);
321 qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
322 VALUES (?, ?, ?, ?, ?, ?, ?)|;
323 my $sth = prepare_query($form, $dbh, $query);
325 foreach my $config (@configs) {
326 my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
328 my $value = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
330 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
331 push @values, undef, undef, $value, undef;
333 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
334 push @values, undef, conv_date($value), undef, undef;
336 } elsif ($config->{type} eq 'number') {
337 push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));
339 } elsif ($config->{type} eq 'bool') {
340 push @values, $value ? 't' : 'f', undef, undef, undef;
341 } elsif ($config->{type} eq 'customer') {
342 push @values, undef, undef, undef, $value * 1;
345 do_statement($form, $sth, $query, @values);
347 if ($params{save_validity}) {
348 my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
349 $self->save_custom_variables_validity(trans_id => $params{trans_id},
350 config_id => $config->{id},
351 validity => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
360 $main::lxdebug->leave_sub();
364 $main::lxdebug->enter_sub(2);
369 Common::check_params(\%params, qw(variables));
371 my $myconfig = \%main::myconfig;
372 my $form = $main::form;
374 my %options = ( name_prefix => "$params{name_prefix}",
375 name_postfix => "$params{name_postfix}",
376 hide_non_editable => $params{hide_non_editable},
377 show_disabled_message => $params{show_disabled_message},
380 foreach my $var (@{ $params{variables} }) {
381 $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { var => $var, %options });
382 $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
385 $main::lxdebug->leave_sub(2);
388 sub render_search_options {
389 $main::lxdebug->enter_sub();
394 Common::check_params(\%params, qw(variables));
396 my $myconfig = \%main::myconfig;
397 my $form = $main::form;
399 $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
401 $params{include_prefix} = 'l_' unless defined($params{include_prefix});
402 $params{include_value} ||= '1';
404 my $filter = $form->parse_html_template('amcvar/search_filter', \%params);
405 my $include = $form->parse_html_template('amcvar/search_include', \%params);
407 $main::lxdebug->leave_sub();
409 return ($filter, $include);
412 sub build_filter_query {
413 $main::lxdebug->enter_sub();
418 Common::check_params(\%params, qw(module trans_id_field filter));
420 my $myconfig = \%main::myconfig;
421 my $form = $main::form;
423 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
425 my $configs = $self->get_configs(%params);
427 my (@where, @values);
429 foreach my $config (@{ $configs }) {
430 next unless ($config->{searchable});
432 my $name = "cvar_$config->{name}";
434 my (@sub_values, @sub_where, $not);
436 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
437 next unless ($params{filter}->{$name});
439 push @sub_where, qq|cvar.text_value ILIKE ?|;
440 push @sub_values, '%' . $params{filter}->{$name} . '%'
442 } elsif ($config->{type} eq 'select') {
443 next unless ($params{filter}->{$name});
445 push @sub_where, qq|cvar.text_value = ?|;
446 push @sub_values, $params{filter}->{$name};
448 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
449 my $name_from = "${name}_from";
450 my $name_to = "${name}_to";
452 if ($params{filter}->{$name_from}) {
453 push @sub_where, qq|cvar.timestamp_value >= ?|;
454 push @sub_values, conv_date($params{filter}->{$name_from});
457 if ($params{filter}->{$name_to}) {
458 push @sub_where, qq|cvar.timestamp_value <= ?|;
459 push @sub_values, conv_date($params{filter}->{$name_to});
462 } elsif ($config->{type} eq 'number') {
463 next if ($params{filter}->{$name} eq '');
465 my $f_op = $params{filter}->{"${name}_qtyop"};
471 } elsif ($f_op eq '=/=') {
475 } elsif ($f_op eq '<') {
479 } elsif ($f_op eq '<=') {
483 } elsif (($f_op eq '>') || ($f_op eq '>=')) {
490 push @sub_where, qq|cvar.number_value $op ?|;
491 push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
493 } elsif ($config->{type} eq 'bool') {
494 next unless ($params{filter}->{$name});
496 $not = 'NOT' if ($params{filter}->{$name} eq 'no');
497 push @sub_where, qq|COALESCE(cvar.bool_value, false) = TRUE|;
498 } elsif ($config->{type} eq 'customer') {
499 next unless $params{filter}->{$name};
501 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM customer WHERE name ILIKE ?)|;
502 push @sub_values, "%$params{filter}->{$name}%";
506 add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
511 FROM custom_variables cvar
512 LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
513 WHERE (cvarcfg.module = ?)
515 AND (cvar.trans_id = $params{trans_id_field})
516 AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
517 push @values, $params{module}, conv_i($config->{id}), @sub_values;
521 my $query = join ' AND ', @where;
523 $main::lxdebug->leave_sub();
525 return ($query, @values);
528 sub add_custom_variables_to_report {
529 $main::lxdebug->enter_sub();
534 Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
536 my $myconfig = \%main::myconfig;
537 my $form = $main::form;
538 my $locale = $main::locale;
540 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
542 my $configs = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
544 if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
545 $main::lxdebug->leave_sub();
549 # allow sub_module to be a coderef or a fixed value
550 if (ref $params{sub_module} ne 'CODE') {
551 my $sub_module = "$params{sub_module}";
552 $params{sub_module} = sub { $sub_module };
555 my %cfg_map = map { $_->{id} => $_ } @{ $configs };
556 my @cfg_ids = keys %cfg_map;
559 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
560 FROM custom_variables
561 WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
563 AND (sub_module = ?)|;
564 my $sth = prepare_query($form, $dbh, $query);
566 foreach my $row (@{ $params{data} }) {
567 do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
569 while (my $ref = $sth->fetchrow_hashref()) {
570 my $cfg = $cfg_map{$ref->{config_id}};
572 $row->{"cvar_$cfg->{name}"} =
573 $cfg->{type} eq 'date' ? $ref->{date_value}
574 : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
575 : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
576 : $cfg->{type} eq 'customer' ? SL::DB::Manager::Customer->find_by(id => 1* $ref->{number_value})->name
577 : $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
578 : $ref->{text_value};
584 $main::lxdebug->leave_sub();
587 sub get_field_format_list {
588 $main::lxdebug->enter_sub();
593 Common::check_params(\%params, qw(module));
595 my $myconfig = \%main::myconfig;
596 my $form = $main::form;
598 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
600 my $configs = $self->get_configs(%params);
602 my $date_fields = [];
603 my $number_fields = {};
605 foreach my $config (@{ $configs }) {
606 my $name = "$params{prefix}cvar_$config->{name}";
608 if ($config->{type} eq 'date') {
609 push @{ $date_fields }, $name;
611 } elsif ($config->{type} eq 'number') {
612 $number_fields->{$config->{precision}} ||= [];
613 push @{ $number_fields->{$config->{precision}} }, $name;
617 $main::lxdebug->leave_sub();
619 return ($date_fields, $number_fields);
622 sub save_custom_variables_validity {
623 $main::lxdebug->enter_sub();
628 Common::check_params(\%params, qw(config_id trans_id validity));
630 my $myconfig = \%main::myconfig;
631 my $form = $main::form;
633 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
635 my (@where, @values);
636 add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
637 add_token(\@where, \@values, col => "trans_id", val => $params{trans_id}, esc => \&conv_i);
639 my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
640 my $query = qq|DELETE FROM custom_variables_validity $where|;
642 do_query($form, $dbh, $query, @values);
645 qq|INSERT INTO custom_variables_validity (config_id, trans_id)
647 my $sth = prepare_query($form, $dbh, $query);
649 unless ($params{validity}) {
650 foreach my $config_id (listify $params{config_id}) {
651 foreach my $trans_id (listify $params{trans_id}) {
652 do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
661 $main::lxdebug->leave_sub();
664 sub get_custom_variables_validity {
665 $main::lxdebug->enter_sub(2);
670 Common::check_params(\%params, qw(config_id trans_id));
672 my $myconfig = \%main::myconfig;
673 my $form = $main::form;
675 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
677 my $query = qq|SELECT COUNT(*) FROM custom_variables_validity WHERE config_id = ? AND trans_id = ?|;
679 my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
681 $main::lxdebug->leave_sub(2);
686 sub custom_variables_validity_by_trans_id {
687 $main::lxdebug->enter_sub(2);
692 return sub { 0 } unless $params{trans_id};
694 my $myconfig = \%main::myconfig;
695 my $form = $main::form;
697 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
699 my $query = qq|SELECT config_id, COUNT(*) FROM custom_variables_validity WHERE trans_id = ? GROUP BY config_id|;
701 my %invalids = selectall_as_map($form, $dbh, $query, 'config_id', 'count', $params{trans_id});
703 $main::lxdebug->leave_sub(2);
705 return sub { !$invalids{+shift} };
714 SL::CVar.pm - Custom Variables module
718 # dealing with configs
720 my $all_configs = CVar->get_configs()
721 my $config = CVar->get_config(id => '1234')
723 CVar->save_config($config);
724 CVar->delete->config($config)
726 # dealing with custom vars
728 CVar->get_custom_variables(module => 'ic')
732 Suppose the following scenario:
734 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.
736 Validity is assumed. If you modify validity, you actually save B<invalidity>.
737 Invalidity is saved as a function of config_id, and the trans_id
739 In the naive way, disable an attribute for a specific id (simple)