5 use List::Util qw(first);
6 use Scalar::Util qw(blessed);
10 use SL::MoreCommon qw(any 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 'vendor' ? $act_var->{number_value}
247 : $cvar->{type} eq 'part' ? $act_var->{number_value}
248 : $cvar->{type} eq 'bool' ? $act_var->{bool_value}
249 : $act_var->{text_value};
250 $cvar->{valid} = $valid;
254 if ($cvar->{type} eq 'date') {
255 if ($cvar->{default_value} eq 'NOW') {
256 $cvar->{value} = $cvar->{current_date};
258 $cvar->{value} = $cvar->{default_value};
261 } elsif ($cvar->{type} eq 'timestamp') {
262 if ($cvar->{default_value} eq 'NOW') {
263 $cvar->{value} = $cvar->{current_timestamp};
265 $cvar->{value} = $cvar->{default_value};
268 } elsif ($cvar->{type} eq 'bool') {
269 $cvar->{value} = $cvar->{default_value} * 1;
271 } elsif ($cvar->{type} eq 'number') {
272 $cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');
275 $cvar->{value} = $cvar->{default_value};
279 if ($cvar->{type} eq 'number') {
280 $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
281 } elsif ($cvar->{type} eq 'customer') {
282 require SL::DB::Customer;
283 $cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1);
284 } elsif ($cvar->{type} eq 'vendor') {
285 require SL::DB::Vendor;
286 $cvar->{value} = SL::DB::Manager::Vendor->find_by(id => $cvar->{value} * 1);
287 } elsif ($cvar->{type} eq 'part') {
288 require SL::DB::Part;
289 $cvar->{value} = SL::DB::Manager::Part->find_by(id => $cvar->{value} * 1);
295 $main::lxdebug->leave_sub();
297 return $custom_variables;
300 sub save_custom_variables {
301 $main::lxdebug->enter_sub();
306 Common::check_params(\%params, qw(module trans_id variables));
308 my $myconfig = \%main::myconfig;
309 my $form = $main::form;
311 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
313 my @configs = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
316 qq|DELETE FROM custom_variables
318 AND (config_id IN (SELECT DISTINCT id
319 FROM custom_variable_configs
321 my @values = (conv_i($params{trans_id}), $params{module});
323 if ($params{sub_module}) {
324 $query .= qq| AND (sub_module = ?)|;
325 push @values, $params{sub_module};
328 do_query($form, $dbh, $query, @values);
331 qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
332 VALUES (?, ?, ?, ?, ?, ?, ?)|;
333 my $sth = prepare_query($form, $dbh, $query);
335 foreach my $config (@configs) {
336 my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
338 my $value = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
340 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
341 push @values, undef, undef, $value, undef;
343 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
344 push @values, undef, conv_date($value), undef, undef;
346 } elsif ($config->{type} eq 'number') {
347 push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));
349 } elsif ($config->{type} eq 'bool') {
350 push @values, $value ? 't' : 'f', undef, undef, undef;
351 } elsif (any { $config->{type} eq $_ } qw(customer vendor part)) {
352 push @values, undef, undef, undef, $value * 1;
355 do_statement($form, $sth, $query, @values);
357 if ($params{save_validity}) {
358 my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
359 $self->save_custom_variables_validity(trans_id => $params{trans_id},
360 config_id => $config->{id},
361 validity => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
368 $dbh->commit() unless $params{dbh};
370 $main::lxdebug->leave_sub();
374 $main::lxdebug->enter_sub(2);
379 Common::check_params(\%params, qw(variables));
381 my $myconfig = \%main::myconfig;
382 my $form = $main::form;
384 my %options = ( name_prefix => "$params{name_prefix}",
385 name_postfix => "$params{name_postfix}",
386 hide_non_editable => $params{hide_non_editable},
387 show_disabled_message => $params{show_disabled_message},
390 foreach my $var (@{ $params{variables} }) {
391 $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { var => $var, %options });
392 $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
395 $main::lxdebug->leave_sub(2);
398 sub render_search_options {
399 $main::lxdebug->enter_sub();
404 Common::check_params(\%params, qw(variables));
406 my $myconfig = \%main::myconfig;
407 my $form = $main::form;
409 $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
411 $params{include_prefix} = 'l_' unless defined($params{include_prefix});
412 $params{include_value} ||= '1';
413 $params{filter_prefix} ||= '';
415 my $filter = $form->parse_html_template('amcvar/search_filter', \%params);
416 my $include = $form->parse_html_template('amcvar/search_include', \%params);
418 $main::lxdebug->leave_sub();
420 return ($filter, $include);
423 sub build_filter_query {
424 $main::lxdebug->enter_sub();
429 Common::check_params(\%params, qw(module trans_id_field filter));
431 my $myconfig = \%main::myconfig;
432 my $form = $main::form;
434 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
436 my $configs = $self->get_configs(%params);
438 my (@where, @values);
440 foreach my $config (@{ $configs }) {
441 next unless ($config->{searchable});
443 my $name = "cvar_$config->{name}";
445 my (@sub_values, @sub_where, $not);
447 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
448 next unless ($params{filter}->{$name});
450 push @sub_where, qq|cvar.text_value ILIKE ?|;
451 push @sub_values, '%' . $params{filter}->{$name} . '%'
453 } elsif ($config->{type} eq 'select') {
454 next unless ($params{filter}->{$name});
456 push @sub_where, qq|cvar.text_value = ?|;
457 push @sub_values, $params{filter}->{$name};
459 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
460 my $name_from = "${name}_from";
461 my $name_to = "${name}_to";
463 if ($params{filter}->{$name_from}) {
464 push @sub_where, qq|cvar.timestamp_value >= ?|;
465 push @sub_values, conv_date($params{filter}->{$name_from});
468 if ($params{filter}->{$name_to}) {
469 push @sub_where, qq|cvar.timestamp_value <= ?|;
470 push @sub_values, conv_date($params{filter}->{$name_to});
473 } elsif ($config->{type} eq 'number') {
474 next if ($params{filter}->{$name} eq '');
476 my $f_op = $params{filter}->{"${name}_qtyop"};
482 } elsif ($f_op eq '=/=') {
486 } elsif ($f_op eq '<') {
490 } elsif ($f_op eq '<=') {
494 } elsif (($f_op eq '>') || ($f_op eq '>=')) {
501 push @sub_where, qq|cvar.number_value $op ?|;
502 push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
504 } elsif ($config->{type} eq 'bool') {
505 next unless ($params{filter}->{$name});
507 $not = 'NOT' if ($params{filter}->{$name} eq 'no');
508 push @sub_where, qq|COALESCE(cvar.bool_value, false) = TRUE|;
509 } elsif (any { $config->{type} eq $_ } qw(customer vendor part)) {
510 next unless $params{filter}->{$name};
512 my $table = $config->{type};
513 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM $table WHERE name ILIKE ?)|;
514 push @sub_values, "%$params{filter}->{$name}%";
515 } elsif ($config->{type} eq 'part') {
516 next unless $params{filter}->{$name};
518 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM parts WHERE partnumber ILIKE ?)|;
519 push @sub_values, "%$params{filter}->{$name}%";
523 add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
528 FROM custom_variables cvar
529 LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
530 WHERE (cvarcfg.module = ?)
532 AND (cvar.trans_id = $params{trans_id_field})
533 AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
534 push @values, $params{module}, conv_i($config->{id}), @sub_values;
538 my $query = join ' AND ', @where;
540 $main::lxdebug->leave_sub();
542 return ($query, @values);
545 sub add_custom_variables_to_report {
546 $main::lxdebug->enter_sub();
551 Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
553 my $myconfig = \%main::myconfig;
554 my $form = $main::form;
555 my $locale = $main::locale;
557 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
559 my $configs = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
561 if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
562 $main::lxdebug->leave_sub();
566 # allow sub_module to be a coderef or a fixed value
567 if (ref $params{sub_module} ne 'CODE') {
568 my $sub_module = "$params{sub_module}";
569 $params{sub_module} = sub { $sub_module };
572 my %cfg_map = map { $_->{id} => $_ } @{ $configs };
573 my @cfg_ids = keys %cfg_map;
576 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
577 FROM custom_variables
578 WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
580 AND (sub_module = ?)|;
581 my $sth = prepare_query($form, $dbh, $query);
583 foreach my $row (@{ $params{data} }) {
584 do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
586 while (my $ref = $sth->fetchrow_hashref()) {
587 my $cfg = $cfg_map{$ref->{config_id}};
589 $row->{"cvar_$cfg->{name}"} =
590 $cfg->{type} eq 'date' ? $ref->{date_value}
591 : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
592 : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
593 : $cfg->{type} eq 'customer' ? (SL::DB::Manager::Customer->find_by(id => 1*$ref->{number_value}) || SL::DB::Customer->new)->name
594 : $cfg->{type} eq 'vendor' ? (SL::DB::Manager::Vendor->find_by(id => 1*$ref->{number_value}) || SL::DB::Vendor->new)->name
595 : $cfg->{type} eq 'part' ? (SL::DB::Manager::Part->find_by(id => 1*$ref->{number_value}) || SL::DB::Part->new)->partnumber
596 : $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
597 : $ref->{text_value};
603 $main::lxdebug->leave_sub();
606 sub get_field_format_list {
607 $main::lxdebug->enter_sub();
612 Common::check_params(\%params, qw(module));
614 my $myconfig = \%main::myconfig;
615 my $form = $main::form;
617 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
619 my $configs = $self->get_configs(%params);
621 my $date_fields = [];
622 my $number_fields = {};
624 foreach my $config (@{ $configs }) {
625 my $name = "$params{prefix}cvar_$config->{name}";
627 if ($config->{type} eq 'date') {
628 push @{ $date_fields }, $name;
630 } elsif ($config->{type} eq 'number') {
631 $number_fields->{$config->{precision}} ||= [];
632 push @{ $number_fields->{$config->{precision}} }, $name;
636 $main::lxdebug->leave_sub();
638 return ($date_fields, $number_fields);
641 sub save_custom_variables_validity {
642 $main::lxdebug->enter_sub();
647 Common::check_params(\%params, qw(config_id trans_id validity));
649 my $myconfig = \%main::myconfig;
650 my $form = $main::form;
652 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
654 my (@where, @values);
655 add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
656 add_token(\@where, \@values, col => "trans_id", val => $params{trans_id}, esc => \&conv_i);
658 my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
659 my $query = qq|DELETE FROM custom_variables_validity $where|;
661 do_query($form, $dbh, $query, @values);
664 qq|INSERT INTO custom_variables_validity (config_id, trans_id)
666 my $sth = prepare_query($form, $dbh, $query);
668 unless ($params{validity}) {
669 foreach my $config_id (listify $params{config_id}) {
670 foreach my $trans_id (listify $params{trans_id}) {
671 do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
678 $dbh->commit() unless $params{dbh};
680 $main::lxdebug->leave_sub();
683 sub get_custom_variables_validity {
684 $main::lxdebug->enter_sub(2);
689 Common::check_params(\%params, qw(config_id trans_id));
691 my $myconfig = \%main::myconfig;
692 my $form = $main::form;
694 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
696 my $query = qq|SELECT COUNT(*) FROM custom_variables_validity WHERE config_id = ? AND trans_id = ?|;
698 my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
700 $main::lxdebug->leave_sub(2);
705 sub custom_variables_validity_by_trans_id {
706 $main::lxdebug->enter_sub(2);
711 return sub { 0 } unless $params{trans_id};
713 my $myconfig = \%main::myconfig;
714 my $form = $main::form;
716 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
718 my $query = qq|SELECT config_id, COUNT(*) FROM custom_variables_validity WHERE trans_id = ? GROUP BY config_id|;
720 my %invalids = selectall_as_map($form, $dbh, $query, 'config_id', 'count', $params{trans_id});
722 $main::lxdebug->leave_sub(2);
724 return sub { !$invalids{+shift} };
728 my ($self, $value, $config) = @_;
730 return $::form->parse_amount(\%::myconfig, $value) if $config->{type} eq 'number';
731 return DateTime->from_lxoffice($value) if $config->{type} eq 'date';
732 return !ref $value ? SL::DB::Manager::Customer->find_by(id => $value * 1) : $value if $config->{type} eq 'customer';
736 sub format_to_template {
737 my ($self, $value, $config) = @_;
738 # stupid template expects everything formated. except objects
739 # do not use outside of print routines for legacy templates
741 return $::form->format_amount(\%::myconfig, $value) if $config->{type} eq 'number';
742 return $value->to_lxoffice if $config->{type} eq 'date' && blessed $value && $value->can('to_lxoffice');
752 SL::CVar.pm - Custom Variables module
756 # dealing with configs
758 my $all_configs = CVar->get_configs()
759 my $config = CVar->get_config(id => '1234')
761 CVar->save_config($config);
762 CVar->delete->config($config)
764 # dealing with custom vars
766 CVar->get_custom_variables(module => 'ic')
770 Suppose the following scenario:
772 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.
774 Validity is assumed. If you modify validity, you actually save B<invalidity>.
775 Invalidity is saved as a function of config_id, and the trans_id
777 In the naive way, disable an attribute for a specific id (simple)