5 use List::Util qw(first);
6 use Scalar::Util qw(blessed);
10 use SL::MoreCommon qw(listify);
13 $main::lxdebug->enter_sub();
18 my $myconfig = \%main::myconfig;
19 my $form = $main::form;
21 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
24 if ($params{module}) {
25 $where = 'WHERE module = ?';
26 push @values, $params{module};
30 SELECT *, date_trunc('seconds', localtimestamp) AS current_timestamp
31 FROM custom_variable_configs $where ORDER BY sortkey
34 $::form->{CVAR_CONFIGS} = {} unless 'HASH' eq ref $::form->{CVAR_CONFIGS};
35 if (!$::form->{CVAR_CONFIGS}->{$params{module}}) {
36 my $configs = selectall_hashref_query($form, $dbh, $query, @values);
38 foreach my $config (@{ $configs }) {
39 if ($config->{type} eq 'select') {
40 $config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
42 } elsif ($config->{type} eq 'number') {
43 $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
47 $self->_unpack_flags($config);
49 $::form->{CVAR_CONFIGS}->{$params{module}} = $configs;
52 $main::lxdebug->leave_sub();
54 return $::form->{CVAR_CONFIGS}->{$params{module}};
58 $main::lxdebug->enter_sub();
63 Common::check_params(\%params, qw(id));
65 my $myconfig = \%main::myconfig;
66 my $form = $main::form;
68 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
70 my $query = qq|SELECT * FROM custom_variable_configs WHERE id = ?|;
72 my $config = selectfirst_hashref_query($form, $dbh, $query, conv_i($params{id})) || { };
74 $self->_unpack_flags($config);
76 $main::lxdebug->leave_sub();
82 $main::lxdebug->enter_sub();
87 foreach my $flag (split m/:/, $config->{flags}) {
88 if ($flag =~ m/(.*?)=(.*)/) {
89 $config->{"flag_${1}"} = $2;
91 $config->{"flag_${flag}"} = 1;
95 $main::lxdebug->leave_sub();
99 $main::lxdebug->enter_sub();
104 Common::check_params(\%params, qw(module config));
106 my $myconfig = \%main::myconfig;
107 my $form = $main::form;
109 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
111 my $q_id = qq|SELECT nextval('custom_variable_configs_id')|;
112 my $h_id = prepare_query($form, $dbh, $q_id);
115 qq|INSERT INTO custom_variable_configs (name, description, type, default_value, options, searchable, includeable, included_by_default, module, flags, id, sortkey)
116 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
117 (SELECT COALESCE(MAX(sortkey) + 1, 1) FROM custom_variable_configs))|;
118 my $h_new = prepare_query($form, $dbh, $q_new);
121 qq|UPDATE custom_variable_configs SET
122 name = ?, description = ?,
123 type = ?, default_value = ?,
124 options = ?, searchable = ?,
125 includeable = ?, included_by_default = ?,
126 module = ?, flags = ?
128 my $h_update = prepare_query($form, $dbh, $q_update);
131 if ('ARRAY' eq ref $params{config}) {
132 @configs = @{ $params{config} };
134 @configs = ($params{config});
137 foreach my $config (@configs) {
138 my ($h_actual, $q_actual);
140 if (!$config->{id}) {
141 do_statement($form, $h_id, $q_id);
142 ($config->{id}) = $h_id->fetchrow_array();
148 $h_actual = $h_update;
149 $q_actual = $q_update;
152 do_statement($form, $h_actual, $q_actual, @{$config}{qw(name description type default_value options)},
153 $config->{searchable} ? 't' : 'f', $config->{includeable} ? 't' : 'f', $config->{included_by_default} ? 't' : 'f',
154 $params{module}, $config->{flags}, conv_i($config->{id}));
163 $main::lxdebug->leave_sub();
167 $main::lxdebug->enter_sub();
172 Common::check_params(\%params, qw(id));
174 my $myconfig = \%main::myconfig;
175 my $form = $main::form;
177 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
179 do_query($form, $dbh, qq|DELETE FROM custom_variables WHERE config_id = ?|, conv_i($params{id}));
180 do_query($form, $dbh, qq|DELETE FROM custom_variables_validity WHERE config_id = ?|, conv_i($params{id}));
181 do_query($form, $dbh, qq|DELETE FROM custom_variable_configs WHERE id = ?|, conv_i($params{id}));
185 $main::lxdebug->leave_sub();
188 sub get_custom_variables {
189 $main::lxdebug->enter_sub();
194 Common::check_params(\%params, qw(module));
196 my $myconfig = \%main::myconfig;
197 my $form = $main::form;
199 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
201 my $trans_id = $params{trans_id} ? 'OR (v.trans_id = ?) ' : '';
204 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value
205 FROM custom_variables
206 WHERE (config_id = ?) AND (trans_id = ?)|;
207 $q_var .= qq| AND (sub_module = ?)| if $params{sub_module};
208 my $h_var = prepare_query($form, $dbh, $q_var);
210 my $custom_variables = $self->get_configs(module => $params{module});
212 foreach my $cvar (@{ $custom_variables }) {
213 if ($cvar->{type} eq 'textfield') {
217 $cvar->{width} = $1 if ($cvar->{options} =~ m/width=(\d+)/i);
218 $cvar->{height} = $1 if ($cvar->{options} =~ m/height=(\d+)/i);
220 } elsif ($cvar->{type} eq 'text') {
221 $cvar->{maxlength} = $1 if ($cvar->{options} =~ m/maxlength=(\d+)/i);
223 } elsif ($cvar->{type} eq 'number') {
224 $cvar->{precision} = $1 if ($cvar->{options} =~ m/precision=(\d+)/i);
226 } elsif ($cvar->{type} eq 'select') {
227 $cvar->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $cvar->{options}) ];
230 my ($act_var, $valid);
231 if ($params{trans_id}) {
232 my @values = (conv_i($cvar->{id}), conv_i($params{trans_id}));
233 push @values, $params{sub_module} if $params{sub_module};
235 do_statement($form, $h_var, $q_var, @values);
236 $act_var = $h_var->fetchrow_hashref();
238 $valid = $self->get_custom_variables_validity(config_id => $cvar->{id}, trans_id => $params{trans_id});
242 $cvar->{value} = $cvar->{type} eq 'date' ? $act_var->{date_value}
243 : $cvar->{type} eq 'timestamp' ? $act_var->{timestamp_value}
244 : $cvar->{type} eq 'number' ? $act_var->{number_value}
245 : $cvar->{type} eq 'customer' ? $act_var->{number_value}
246 : $cvar->{type} eq 'bool' ? $act_var->{bool_value}
247 : $act_var->{text_value};
248 $cvar->{valid} = $valid;
252 if ($cvar->{type} eq 'date') {
253 if ($cvar->{default_value} eq 'NOW') {
254 $cvar->{value} = $cvar->{current_date};
256 $cvar->{value} = $cvar->{default_value};
259 } elsif ($cvar->{type} eq 'timestamp') {
260 if ($cvar->{default_value} eq 'NOW') {
261 $cvar->{value} = $cvar->{current_timestamp};
263 $cvar->{value} = $cvar->{default_value};
266 } elsif ($cvar->{type} eq 'bool') {
267 $cvar->{value} = $cvar->{default_value} * 1;
269 } elsif ($cvar->{type} eq 'number') {
270 $cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');
273 $cvar->{value} = $cvar->{default_value};
277 if ($cvar->{type} eq 'number') {
278 $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
279 } elsif ($cvar->{type} eq 'customer') {
280 require SL::DB::Customer;
281 $cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1);
287 $main::lxdebug->leave_sub();
289 return $custom_variables;
292 sub save_custom_variables {
293 $main::lxdebug->enter_sub();
298 Common::check_params(\%params, qw(module trans_id variables));
300 my $myconfig = \%main::myconfig;
301 my $form = $main::form;
303 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
305 my @configs = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
308 qq|DELETE FROM custom_variables
310 AND (config_id IN (SELECT DISTINCT id
311 FROM custom_variable_configs
313 my @values = (conv_i($params{trans_id}), $params{module});
315 if ($params{sub_module}) {
316 $query .= qq| AND (sub_module = ?)|;
317 push @values, $params{sub_module};
320 do_query($form, $dbh, $query, @values);
323 qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
324 VALUES (?, ?, ?, ?, ?, ?, ?)|;
325 my $sth = prepare_query($form, $dbh, $query);
327 foreach my $config (@configs) {
328 my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
330 my $value = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
332 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
333 push @values, undef, undef, $value, undef;
335 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
336 push @values, undef, conv_date($value), undef, undef;
338 } elsif ($config->{type} eq 'number') {
339 push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));
341 } elsif ($config->{type} eq 'bool') {
342 push @values, $value ? 't' : 'f', undef, undef, undef;
343 } elsif ($config->{type} eq 'customer') {
344 push @values, undef, undef, undef, $value * 1;
347 do_statement($form, $sth, $query, @values);
349 if ($params{save_validity}) {
350 my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
351 $self->save_custom_variables_validity(trans_id => $params{trans_id},
352 config_id => $config->{id},
353 validity => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
360 $dbh->commit() unless $params{dbh};
362 $main::lxdebug->leave_sub();
366 $main::lxdebug->enter_sub(2);
371 Common::check_params(\%params, qw(variables));
373 my $myconfig = \%main::myconfig;
374 my $form = $main::form;
376 my %options = ( name_prefix => "$params{name_prefix}",
377 name_postfix => "$params{name_postfix}",
378 hide_non_editable => $params{hide_non_editable},
379 show_disabled_message => $params{show_disabled_message},
382 foreach my $var (@{ $params{variables} }) {
383 $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { var => $var, %options });
384 $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
387 $main::lxdebug->leave_sub(2);
390 sub render_search_options {
391 $main::lxdebug->enter_sub();
396 Common::check_params(\%params, qw(variables));
398 my $myconfig = \%main::myconfig;
399 my $form = $main::form;
401 $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
403 $params{include_prefix} = 'l_' unless defined($params{include_prefix});
404 $params{include_value} ||= '1';
406 my $filter = $form->parse_html_template('amcvar/search_filter', \%params);
407 my $include = $form->parse_html_template('amcvar/search_include', \%params);
409 $main::lxdebug->leave_sub();
411 return ($filter, $include);
414 sub build_filter_query {
415 $main::lxdebug->enter_sub();
420 Common::check_params(\%params, qw(module trans_id_field filter));
422 my $myconfig = \%main::myconfig;
423 my $form = $main::form;
425 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
427 my $configs = $self->get_configs(%params);
429 my (@where, @values);
431 foreach my $config (@{ $configs }) {
432 next unless ($config->{searchable});
434 my $name = "cvar_$config->{name}";
436 my (@sub_values, @sub_where, $not);
438 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
439 next unless ($params{filter}->{$name});
441 push @sub_where, qq|cvar.text_value ILIKE ?|;
442 push @sub_values, '%' . $params{filter}->{$name} . '%'
444 } elsif ($config->{type} eq 'select') {
445 next unless ($params{filter}->{$name});
447 push @sub_where, qq|cvar.text_value = ?|;
448 push @sub_values, $params{filter}->{$name};
450 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
451 my $name_from = "${name}_from";
452 my $name_to = "${name}_to";
454 if ($params{filter}->{$name_from}) {
455 push @sub_where, qq|cvar.timestamp_value >= ?|;
456 push @sub_values, conv_date($params{filter}->{$name_from});
459 if ($params{filter}->{$name_to}) {
460 push @sub_where, qq|cvar.timestamp_value <= ?|;
461 push @sub_values, conv_date($params{filter}->{$name_to});
464 } elsif ($config->{type} eq 'number') {
465 next if ($params{filter}->{$name} eq '');
467 my $f_op = $params{filter}->{"${name}_qtyop"};
473 } elsif ($f_op eq '=/=') {
477 } elsif ($f_op eq '<') {
481 } elsif ($f_op eq '<=') {
485 } elsif (($f_op eq '>') || ($f_op eq '>=')) {
492 push @sub_where, qq|cvar.number_value $op ?|;
493 push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
495 } elsif ($config->{type} eq 'bool') {
496 next unless ($params{filter}->{$name});
498 $not = 'NOT' if ($params{filter}->{$name} eq 'no');
499 push @sub_where, qq|COALESCE(cvar.bool_value, false) = TRUE|;
500 } elsif ($config->{type} eq 'customer') {
501 next unless $params{filter}->{$name};
503 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM customer WHERE name ILIKE ?)|;
504 push @sub_values, "%$params{filter}->{$name}%";
508 add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
513 FROM custom_variables cvar
514 LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
515 WHERE (cvarcfg.module = ?)
517 AND (cvar.trans_id = $params{trans_id_field})
518 AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
519 push @values, $params{module}, conv_i($config->{id}), @sub_values;
523 my $query = join ' AND ', @where;
525 $main::lxdebug->leave_sub();
527 return ($query, @values);
530 sub add_custom_variables_to_report {
531 $main::lxdebug->enter_sub();
536 Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
538 my $myconfig = \%main::myconfig;
539 my $form = $main::form;
540 my $locale = $main::locale;
542 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
544 my $configs = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
546 if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
547 $main::lxdebug->leave_sub();
551 # allow sub_module to be a coderef or a fixed value
552 if (ref $params{sub_module} ne 'CODE') {
553 my $sub_module = "$params{sub_module}";
554 $params{sub_module} = sub { $sub_module };
557 my %cfg_map = map { $_->{id} => $_ } @{ $configs };
558 my @cfg_ids = keys %cfg_map;
561 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
562 FROM custom_variables
563 WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
565 AND (sub_module = ?)|;
566 my $sth = prepare_query($form, $dbh, $query);
568 foreach my $row (@{ $params{data} }) {
569 do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
571 while (my $ref = $sth->fetchrow_hashref()) {
572 my $cfg = $cfg_map{$ref->{config_id}};
574 $row->{"cvar_$cfg->{name}"} =
575 $cfg->{type} eq 'date' ? $ref->{date_value}
576 : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
577 : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
578 : $cfg->{type} eq 'customer' ? (SL::DB::Manager::Customer->find_by(id => 1*$ref->{number_value}) || SL::DB::Customer->new)->name
579 : $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
580 : $ref->{text_value};
586 $main::lxdebug->leave_sub();
589 sub get_field_format_list {
590 $main::lxdebug->enter_sub();
595 Common::check_params(\%params, qw(module));
597 my $myconfig = \%main::myconfig;
598 my $form = $main::form;
600 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
602 my $configs = $self->get_configs(%params);
604 my $date_fields = [];
605 my $number_fields = {};
607 foreach my $config (@{ $configs }) {
608 my $name = "$params{prefix}cvar_$config->{name}";
610 if ($config->{type} eq 'date') {
611 push @{ $date_fields }, $name;
613 } elsif ($config->{type} eq 'number') {
614 $number_fields->{$config->{precision}} ||= [];
615 push @{ $number_fields->{$config->{precision}} }, $name;
619 $main::lxdebug->leave_sub();
621 return ($date_fields, $number_fields);
624 sub save_custom_variables_validity {
625 $main::lxdebug->enter_sub();
630 Common::check_params(\%params, qw(config_id trans_id validity));
632 my $myconfig = \%main::myconfig;
633 my $form = $main::form;
635 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
637 my (@where, @values);
638 add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
639 add_token(\@where, \@values, col => "trans_id", val => $params{trans_id}, esc => \&conv_i);
641 my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
642 my $query = qq|DELETE FROM custom_variables_validity $where|;
644 do_query($form, $dbh, $query, @values);
647 qq|INSERT INTO custom_variables_validity (config_id, trans_id)
649 my $sth = prepare_query($form, $dbh, $query);
651 unless ($params{validity}) {
652 foreach my $config_id (listify $params{config_id}) {
653 foreach my $trans_id (listify $params{trans_id}) {
654 do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
661 $dbh->commit() unless $params{dbh};
663 $main::lxdebug->leave_sub();
666 sub get_custom_variables_validity {
667 $main::lxdebug->enter_sub(2);
672 Common::check_params(\%params, qw(config_id trans_id));
674 my $myconfig = \%main::myconfig;
675 my $form = $main::form;
677 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
679 my $query = qq|SELECT COUNT(*) FROM custom_variables_validity WHERE config_id = ? AND trans_id = ?|;
681 my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
683 $main::lxdebug->leave_sub(2);
688 sub custom_variables_validity_by_trans_id {
689 $main::lxdebug->enter_sub(2);
694 return sub { 0 } unless $params{trans_id};
696 my $myconfig = \%main::myconfig;
697 my $form = $main::form;
699 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
701 my $query = qq|SELECT config_id, COUNT(*) FROM custom_variables_validity WHERE trans_id = ? GROUP BY config_id|;
703 my %invalids = selectall_as_map($form, $dbh, $query, 'config_id', 'count', $params{trans_id});
705 $main::lxdebug->leave_sub(2);
707 return sub { !$invalids{+shift} };
711 my ($self, $value, $config) = @_;
713 return $::form->parse_amount(\%::myconfig, $value) if $config->{type} eq 'number';
714 return DateTime->from_lxoffice($value) if $config->{type} eq 'date';
715 return !ref $value ? SL::DB::Manager::Customer->find_by(id => $value * 1) : $value if $config->{type} eq 'customer';
719 sub format_to_template {
720 my ($self, $value, $config) = @_;
721 # stupid template expects everything formated. except objects
722 # do not use outside of print routines for legacy templates
724 return $::form->format_amount(\%::myconfig, $value) if $config->{type} eq 'number';
725 return $value->to_lxoffice if $config->{type} eq 'date' && blessed $value && $value->can('to_lxoffice');
735 SL::CVar.pm - Custom Variables module
739 # dealing with configs
741 my $all_configs = CVar->get_configs()
742 my $config = CVar->get_config(id => '1234')
744 CVar->save_config($config);
745 CVar->delete->config($config)
747 # dealing with custom vars
749 CVar->get_custom_variables(module => 'ic')
753 Suppose the following scenario:
755 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.
757 Validity is assumed. If you modify validity, you actually save B<invalidity>.
758 Invalidity is saved as a function of config_id, and the trans_id
760 In the naive way, disable an attribute for a specific id (simple)