5 use List::MoreUtils qw(any);
6 use List::Util qw(first);
7 use Scalar::Util qw(blessed);
11 use SL::MoreCommon qw(listify);
14 $main::lxdebug->enter_sub();
19 my $myconfig = \%main::myconfig;
20 my $form = $main::form;
22 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
25 if ($params{module}) {
26 $where = 'WHERE module = ?';
27 push @values, $params{module};
31 SELECT *, date_trunc('seconds', localtimestamp) AS current_timestamp
32 FROM custom_variable_configs $where ORDER BY sortkey
35 $::form->{CVAR_CONFIGS} = {} unless 'HASH' eq ref $::form->{CVAR_CONFIGS};
36 if (!$::form->{CVAR_CONFIGS}->{$params{module}}) {
37 my $configs = selectall_hashref_query($form, $dbh, $query, @values);
39 foreach my $config (@{ $configs }) {
40 if ($config->{type} eq 'select') {
41 $config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
43 } elsif ($config->{type} eq 'number') {
44 $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
46 } elsif ($config->{type} eq 'textfield') {
47 $config->{width} = 30;
48 $config->{height} = 5;
49 $config->{width} = $1 if ($config->{options} =~ m/width=(\d+)/i);
50 $config->{height} = $1 if ($config->{options} =~ m/height=(\d+)/i);
52 } elsif ($config->{type} eq 'text') {
53 $config->{maxlength} = $1 if ($config->{options} =~ m/maxlength=(\d+)/i);
57 $self->_unpack_flags($config);
59 $::form->{CVAR_CONFIGS}->{$params{module}} = $configs;
62 $main::lxdebug->leave_sub();
64 return $::form->{CVAR_CONFIGS}->{$params{module}};
68 $main::lxdebug->enter_sub();
73 foreach my $flag (split m/:/, $config->{flags}) {
74 if ($flag =~ m/(.*?)=(.*)/) {
75 $config->{"flag_${1}"} = $2;
77 $config->{"flag_${flag}"} = 1;
81 $main::lxdebug->leave_sub();
84 sub get_custom_variables {
85 $main::lxdebug->enter_sub();
90 Common::check_params(\%params, qw(module));
92 my $myconfig = \%main::myconfig;
93 my $form = $main::form;
95 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
97 my $trans_id = $params{trans_id} ? 'OR (v.trans_id = ?) ' : '';
100 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value
101 FROM custom_variables
102 WHERE (config_id = ?) AND (trans_id = ?)|;
103 $q_var .= qq| AND (sub_module = ?)| if $params{sub_module};
104 my $h_var = prepare_query($form, $dbh, $q_var);
106 my $custom_variables = $self->get_configs(module => $params{module});
108 foreach my $cvar (@{ $custom_variables }) {
109 if ($cvar->{type} eq 'textfield') {
113 $cvar->{width} = $1 if ($cvar->{options} =~ m/width=(\d+)/i);
114 $cvar->{height} = $1 if ($cvar->{options} =~ m/height=(\d+)/i);
116 } elsif ($cvar->{type} eq 'text') {
117 $cvar->{maxlength} = $1 if ($cvar->{options} =~ m/maxlength=(\d+)/i);
119 } elsif ($cvar->{type} eq 'number') {
120 $cvar->{precision} = $1 if ($cvar->{options} =~ m/precision=(\d+)/i);
122 } elsif ($cvar->{type} eq 'select') {
123 $cvar->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $cvar->{options}) ];
126 my ($act_var, $valid);
127 if ($params{trans_id}) {
128 my @values = (conv_i($cvar->{id}), conv_i($params{trans_id}));
129 push @values, $params{sub_module} if $params{sub_module};
131 do_statement($form, $h_var, $q_var, @values);
132 $act_var = $h_var->fetchrow_hashref();
134 $valid = $self->get_custom_variables_validity(config_id => $cvar->{id}, trans_id => $params{trans_id});
136 $valid = !$cvar->{flag_defaults_to_invalid};
140 $cvar->{value} = $cvar->{type} eq 'date' ? $act_var->{date_value}
141 : $cvar->{type} eq 'timestamp' ? $act_var->{timestamp_value}
142 : $cvar->{type} eq 'number' ? $act_var->{number_value}
143 : $cvar->{type} eq 'customer' ? $act_var->{number_value}
144 : $cvar->{type} eq 'vendor' ? $act_var->{number_value}
145 : $cvar->{type} eq 'part' ? $act_var->{number_value}
146 : $cvar->{type} eq 'bool' ? $act_var->{bool_value}
147 : $act_var->{text_value};
148 $cvar->{valid} = $valid;
150 $cvar->{valid} = $valid // 1;
152 if ($cvar->{type} eq 'date') {
153 if ($cvar->{default_value} eq 'NOW') {
154 $cvar->{value} = $cvar->{current_date};
156 $cvar->{value} = $cvar->{default_value};
159 } elsif ($cvar->{type} eq 'timestamp') {
160 if ($cvar->{default_value} eq 'NOW') {
161 $cvar->{value} = $cvar->{current_timestamp};
163 $cvar->{value} = $cvar->{default_value};
166 } elsif ($cvar->{type} eq 'bool') {
167 $cvar->{value} = $cvar->{default_value} * 1;
169 } elsif ($cvar->{type} eq 'number') {
170 $cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');
173 $cvar->{value} = $cvar->{default_value};
177 if ($cvar->{type} eq 'number') {
178 $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
179 } elsif ($cvar->{type} eq 'customer') {
180 require SL::DB::Customer;
181 $cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1);
182 } elsif ($cvar->{type} eq 'vendor') {
183 require SL::DB::Vendor;
184 $cvar->{value} = SL::DB::Manager::Vendor->find_by(id => $cvar->{value} * 1);
185 } elsif ($cvar->{type} eq 'part') {
186 require SL::DB::Part;
187 $cvar->{value} = SL::DB::Manager::Part->find_by(id => $cvar->{value} * 1);
193 $main::lxdebug->leave_sub();
195 return $custom_variables;
198 sub save_custom_variables {
199 $main::lxdebug->enter_sub();
204 Common::check_params(\%params, qw(module trans_id variables));
206 my $myconfig = \%main::myconfig;
207 my $form = $main::form;
209 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
211 my @configs = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
214 qq|DELETE FROM custom_variables
216 AND (config_id IN (SELECT DISTINCT id
217 FROM custom_variable_configs
219 my @values = (conv_i($params{trans_id}), $params{module});
221 if ($params{sub_module}) {
222 $query .= qq| AND (sub_module = ?)|;
223 push @values, $params{sub_module};
226 do_query($form, $dbh, $query, @values);
229 qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
230 VALUES (?, ?, ?, ?, ?, ?, ?)|;
231 my $sth = prepare_query($form, $dbh, $query);
233 foreach my $config (@configs) {
234 my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
236 my $value = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
238 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
239 push @values, undef, undef, $value, undef;
241 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
242 push @values, undef, conv_date($value), undef, undef;
244 } elsif ($config->{type} eq 'number') {
245 push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));
247 } elsif ($config->{type} eq 'bool') {
248 push @values, $value ? 't' : 'f', undef, undef, undef;
249 } elsif (any { $config->{type} eq $_ } qw(customer vendor part)) {
250 push @values, undef, undef, undef, $value * 1;
253 do_statement($form, $sth, $query, @values);
255 if ($params{save_validity}) {
256 my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
257 $self->save_custom_variables_validity(trans_id => $params{trans_id},
258 config_id => $config->{id},
259 validity => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
266 $dbh->commit() unless $params{dbh};
268 $main::lxdebug->leave_sub();
272 $main::lxdebug->enter_sub(2);
277 Common::check_params(\%params, qw(variables));
279 my $myconfig = \%main::myconfig;
280 my $form = $main::form;
282 my %options = ( name_prefix => "$params{name_prefix}",
283 name_postfix => "$params{name_postfix}",
284 hide_non_editable => $params{hide_non_editable},
285 show_disabled_message => $params{show_disabled_message},
288 foreach my $var (@{ $params{variables} }) {
289 $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { var => $var, %options });
290 $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
293 $main::lxdebug->leave_sub(2);
296 sub render_search_options {
297 $main::lxdebug->enter_sub();
302 Common::check_params(\%params, qw(variables));
304 my $myconfig = \%main::myconfig;
305 my $form = $main::form;
307 $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
309 $params{include_prefix} = 'l_' unless defined($params{include_prefix});
310 $params{include_value} ||= '1';
311 $params{filter_prefix} ||= '';
313 my $filter = $form->parse_html_template('amcvar/search_filter', \%params);
314 my $include = $form->parse_html_template('amcvar/search_include', \%params);
316 $main::lxdebug->leave_sub();
318 return ($filter, $include);
321 sub build_filter_query {
322 $main::lxdebug->enter_sub();
327 Common::check_params(\%params, qw(module trans_id_field filter));
329 my $myconfig = \%main::myconfig;
330 my $form = $main::form;
332 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
334 my $configs = $self->get_configs(%params);
336 my (@where, @values);
338 foreach my $config (@{ $configs }) {
339 next unless ($config->{searchable});
341 my $name = "cvar_$config->{name}";
343 my (@sub_values, @sub_where, $not);
345 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
346 next unless ($params{filter}->{$name});
348 push @sub_where, qq|cvar.text_value ILIKE ?|;
349 push @sub_values, '%' . $params{filter}->{$name} . '%'
351 } elsif ($config->{type} eq 'select') {
352 next unless ($params{filter}->{$name});
354 push @sub_where, qq|cvar.text_value = ?|;
355 push @sub_values, $params{filter}->{$name};
357 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
358 my $name_from = "${name}_from";
359 my $name_to = "${name}_to";
361 if ($params{filter}->{$name_from}) {
362 push @sub_where, qq|cvar.timestamp_value >= ?|;
363 push @sub_values, conv_date($params{filter}->{$name_from});
366 if ($params{filter}->{$name_to}) {
367 push @sub_where, qq|cvar.timestamp_value <= ?|;
368 push @sub_values, conv_date($params{filter}->{$name_to});
371 } elsif ($config->{type} eq 'number') {
372 next if ($params{filter}->{$name} eq '');
374 my $f_op = $params{filter}->{"${name}_qtyop"};
380 } elsif ($f_op eq '=/=') {
384 } elsif ($f_op eq '<') {
388 } elsif ($f_op eq '<=') {
392 } elsif (($f_op eq '>') || ($f_op eq '>=')) {
399 push @sub_where, qq|cvar.number_value $op ?|;
400 push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
402 } elsif ($config->{type} eq 'bool') {
403 next unless ($params{filter}->{$name});
405 $not = 'NOT' if ($params{filter}->{$name} eq 'no');
406 push @sub_where, qq|COALESCE(cvar.bool_value, false) = TRUE|;
407 } elsif (any { $config->{type} eq $_ } qw(customer vendor)) {
408 next unless $params{filter}->{$name};
410 my $table = $config->{type};
411 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM $table WHERE name ILIKE ?)|;
412 push @sub_values, "%$params{filter}->{$name}%";
413 } elsif ($config->{type} eq 'part') {
414 next unless $params{filter}->{$name};
416 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM parts WHERE partnumber ILIKE ?)|;
417 push @sub_values, "%$params{filter}->{$name}%";
421 add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
426 FROM custom_variables cvar
427 LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
428 WHERE (cvarcfg.module = ?)
430 AND (cvar.trans_id = $params{trans_id_field})
431 AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
432 push @values, $params{module}, conv_i($config->{id}), @sub_values;
436 my $query = join ' AND ', @where;
438 $main::lxdebug->leave_sub();
440 return ($query, @values);
443 sub add_custom_variables_to_report {
444 $main::lxdebug->enter_sub();
449 Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
451 my $myconfig = \%main::myconfig;
452 my $form = $main::form;
453 my $locale = $main::locale;
455 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
457 my $configs = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
459 if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
460 $main::lxdebug->leave_sub();
464 # allow sub_module to be a coderef or a fixed value
465 if (ref $params{sub_module} ne 'CODE') {
466 my $sub_module = "$params{sub_module}";
467 $params{sub_module} = sub { $sub_module };
470 my %cfg_map = map { $_->{id} => $_ } @{ $configs };
471 my @cfg_ids = keys %cfg_map;
474 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
475 FROM custom_variables
476 WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
478 AND (sub_module = ?)|;
479 my $sth = prepare_query($form, $dbh, $query);
481 foreach my $row (@{ $params{data} }) {
482 do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
484 while (my $ref = $sth->fetchrow_hashref()) {
485 my $cfg = $cfg_map{$ref->{config_id}};
487 $row->{"cvar_$cfg->{name}"} =
488 $cfg->{type} eq 'date' ? $ref->{date_value}
489 : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
490 : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
491 : $cfg->{type} eq 'customer' ? (SL::DB::Manager::Customer->find_by(id => 1*$ref->{number_value}) || SL::DB::Customer->new)->name
492 : $cfg->{type} eq 'vendor' ? (SL::DB::Manager::Vendor->find_by(id => 1*$ref->{number_value}) || SL::DB::Vendor->new)->name
493 : $cfg->{type} eq 'part' ? (SL::DB::Manager::Part->find_by(id => 1*$ref->{number_value}) || SL::DB::Part->new)->partnumber
494 : $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
495 : $ref->{text_value};
501 $main::lxdebug->leave_sub();
504 sub get_field_format_list {
505 $main::lxdebug->enter_sub();
510 Common::check_params(\%params, qw(module));
512 my $myconfig = \%main::myconfig;
513 my $form = $main::form;
515 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
517 my $configs = $self->get_configs(%params);
519 my $date_fields = [];
520 my $number_fields = {};
522 foreach my $config (@{ $configs }) {
523 my $name = "$params{prefix}cvar_$config->{name}";
525 if ($config->{type} eq 'date') {
526 push @{ $date_fields }, $name;
528 } elsif ($config->{type} eq 'number') {
529 $number_fields->{$config->{precision}} ||= [];
530 push @{ $number_fields->{$config->{precision}} }, $name;
534 $main::lxdebug->leave_sub();
536 return ($date_fields, $number_fields);
539 sub save_custom_variables_validity {
540 $main::lxdebug->enter_sub();
545 Common::check_params(\%params, qw(config_id trans_id validity));
547 my $myconfig = \%main::myconfig;
548 my $form = $main::form;
550 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
552 my (@where, @values);
553 add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
554 add_token(\@where, \@values, col => "trans_id", val => $params{trans_id}, esc => \&conv_i);
556 my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
557 my $query = qq|DELETE FROM custom_variables_validity $where|;
559 do_query($form, $dbh, $query, @values);
562 qq|INSERT INTO custom_variables_validity (config_id, trans_id)
564 my $sth = prepare_query($form, $dbh, $query);
566 unless ($params{validity}) {
567 foreach my $config_id (listify($params{config_id})) {
568 foreach my $trans_id (listify($params{trans_id})) {
569 do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
576 $dbh->commit() unless $params{dbh};
578 $main::lxdebug->leave_sub();
581 sub get_custom_variables_validity {
582 $main::lxdebug->enter_sub(2);
587 Common::check_params(\%params, qw(config_id trans_id));
589 my $myconfig = \%main::myconfig;
590 my $form = $main::form;
592 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
594 my $query = qq|SELECT id FROM custom_variables_validity WHERE config_id = ? AND trans_id = ? LIMIT 1|;
596 my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
598 $main::lxdebug->leave_sub(2);
603 sub custom_variables_validity_by_trans_id {
604 $main::lxdebug->enter_sub(2);
609 return sub { 0 } unless $params{trans_id};
611 my $myconfig = \%main::myconfig;
612 my $form = $main::form;
614 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
616 my $query = qq|SELECT DISTINCT config_id FROM custom_variables_validity WHERE trans_id = ?|;
618 my %invalids = map { +($_->{config_id} => 1) } selectall_hashref_query($form, $dbh, $query, $params{trans_id});
620 $main::lxdebug->leave_sub(2);
622 return sub { !$invalids{+shift} };
626 my ($self, $value, $config) = @_;
628 return $::form->parse_amount(\%::myconfig, $value) if $config->{type} eq 'number';
629 return DateTime->from_lxoffice($value) if $config->{type} eq 'date';
630 return !ref $value ? SL::DB::Manager::Customer->find_by(id => $value * 1) : $value if $config->{type} eq 'customer';
631 return !ref $value ? SL::DB::Manager::Vendor->find_by(id => $value * 1) : $value if $config->{type} eq 'vendor';
632 return !ref $value ? SL::DB::Manager::Part->find_by(id => $value * 1) : $value if $config->{type} eq 'part';
636 sub format_to_template {
637 my ($self, $value, $config) = @_;
638 # stupid template expects everything formated. except objects
639 # do not use outside of print routines for legacy templates
641 return $::form->format_amount(\%::myconfig, $value) if $config->{type} eq 'number';
642 return $value->to_lxoffice if $config->{type} eq 'date' && blessed $value && $value->can('to_lxoffice');
652 SL::CVar.pm - Custom Variables module
656 # dealing with configs
658 my $all_configs = CVar->get_configs()
660 # dealing with custom vars
662 CVar->get_custom_variables(module => 'ic')
666 Suppose the following scenario:
668 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.
670 Validity is assumed. If you modify validity, you actually save B<invalidity>.
671 Invalidity is saved as a function of config_id, and the trans_id
673 In the naive way, disable an attribute for a specific id (simple)