5 use List::MoreUtils qw(any);
6 use List::Util qw(first);
7 use Scalar::Util qw(blessed);
11 use SL::MoreCommon qw(listify);
12 use SL::Util qw(trim);
15 $main::lxdebug->enter_sub();
20 my $myconfig = \%main::myconfig;
21 my $form = $main::form;
23 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
26 if ($params{module}) {
27 $where = 'WHERE module = ?';
28 push @values, $params{module};
32 SELECT *, date_trunc('seconds', localtimestamp) AS current_timestamp
33 FROM custom_variable_configs $where ORDER BY sortkey
36 $::form->{CVAR_CONFIGS} = {} unless 'HASH' eq ref $::form->{CVAR_CONFIGS};
37 if (!$::form->{CVAR_CONFIGS}->{$params{module}}) {
38 my $configs = selectall_hashref_query($form, $dbh, $query, @values);
40 foreach my $config (@{ $configs }) {
41 if ($config->{type} eq 'select') {
42 $config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
44 } elsif ($config->{type} eq 'number') {
45 $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
47 } elsif ($config->{type} eq 'textfield') {
48 $config->{width} = 30;
49 $config->{height} = 5;
50 $config->{width} = $1 if ($config->{options} =~ m/width=(\d+)/i);
51 $config->{height} = $1 if ($config->{options} =~ m/height=(\d+)/i);
53 } elsif ($config->{type} eq 'text') {
54 $config->{maxlength} = $1 if ($config->{options} =~ m/maxlength=(\d+)/i);
58 $self->_unpack_flags($config);
60 my $cvar_config = SL::DB::CustomVariableConfig->new(id => $config->{id})->load;
61 @{$config->{'partsgroups'}} = map {$_->id} @{$cvar_config->partsgroups};
64 $::form->{CVAR_CONFIGS}->{$params{module}} = $configs;
67 $main::lxdebug->leave_sub();
69 return $::form->{CVAR_CONFIGS}->{$params{module}};
73 $main::lxdebug->enter_sub();
78 foreach my $flag (split m/:/, $config->{flags}) {
79 if ($flag =~ m/(.*?)=(.*)/) {
80 $config->{"flag_${1}"} = $2;
82 $config->{"flag_${flag}"} = 1;
86 $main::lxdebug->leave_sub();
89 sub get_custom_variables {
90 $main::lxdebug->enter_sub();
95 Common::check_params(\%params, qw(module));
97 my $myconfig = \%main::myconfig;
98 my $form = $main::form;
100 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
102 my $sub_module = $params{sub_module} ? $params{sub_module} : '';
105 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value
106 FROM custom_variables
107 WHERE (config_id = ?) AND (trans_id = ?) AND (sub_module = ?)|;
108 my $h_var = prepare_query($form, $dbh, $q_var);
110 my $custom_variables = $self->get_configs(module => $params{module});
112 foreach my $cvar (@{ $custom_variables }) {
113 if ($cvar->{type} eq 'textfield') {
117 $cvar->{width} = $1 if ($cvar->{options} =~ m/width=(\d+)/i);
118 $cvar->{height} = $1 if ($cvar->{options} =~ m/height=(\d+)/i);
120 } elsif ($cvar->{type} eq 'text') {
121 $cvar->{maxlength} = $1 if ($cvar->{options} =~ m/maxlength=(\d+)/i);
123 } elsif ($cvar->{type} eq 'number') {
124 $cvar->{precision} = $1 if ($cvar->{options} =~ m/precision=(\d+)/i);
126 } elsif ($cvar->{type} eq 'select') {
127 $cvar->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $cvar->{options}) ];
130 my ($act_var, $valid);
131 if ($params{trans_id}) {
132 my @values = (conv_i($cvar->{id}), conv_i($params{trans_id}), $sub_module);
134 do_statement($form, $h_var, $q_var, @values);
135 $act_var = $h_var->fetchrow_hashref();
137 $valid = $self->get_custom_variables_validity(config_id => $cvar->{id}, trans_id => $params{trans_id});
139 $valid = !$cvar->{flag_defaults_to_invalid};
143 $cvar->{value} = $cvar->{type} eq 'date' ? $act_var->{date_value}
144 : $cvar->{type} eq 'timestamp' ? $act_var->{timestamp_value}
145 : $cvar->{type} eq 'number' ? $act_var->{number_value}
146 : $cvar->{type} eq 'customer' ? $act_var->{number_value}
147 : $cvar->{type} eq 'vendor' ? $act_var->{number_value}
148 : $cvar->{type} eq 'part' ? $act_var->{number_value}
149 : $cvar->{type} eq 'bool' ? $act_var->{bool_value}
150 : $act_var->{text_value};
151 $cvar->{valid} = $valid;
153 $cvar->{valid} = $valid // 1;
155 if ($cvar->{type} eq 'date') {
156 if ($cvar->{default_value} eq 'NOW') {
157 $cvar->{value} = $cvar->{current_date};
159 $cvar->{value} = $cvar->{default_value};
162 } elsif ($cvar->{type} eq 'timestamp') {
163 if ($cvar->{default_value} eq 'NOW') {
164 $cvar->{value} = $cvar->{current_timestamp};
166 $cvar->{value} = $cvar->{default_value};
169 } elsif ($cvar->{type} eq 'bool') {
170 $cvar->{value} = $cvar->{default_value} * 1;
172 } elsif ($cvar->{type} eq 'number') {
173 $cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');
176 $cvar->{value} = $cvar->{default_value};
180 if ($cvar->{type} eq 'number') {
181 $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
182 } elsif ($cvar->{type} eq 'customer') {
183 require SL::DB::Customer;
184 $cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1);
185 } elsif ($cvar->{type} eq 'vendor') {
186 require SL::DB::Vendor;
187 $cvar->{value} = SL::DB::Manager::Vendor->find_by(id => $cvar->{value} * 1);
188 } elsif ($cvar->{type} eq 'part') {
189 require SL::DB::Part;
190 $cvar->{value} = SL::DB::Manager::Part->find_by(id => $cvar->{value} * 1);
196 $main::lxdebug->leave_sub();
198 return $custom_variables;
201 sub save_custom_variables {
202 $main::lxdebug->enter_sub();
207 Common::check_params(\%params, qw(module trans_id variables));
209 my $myconfig = \%main::myconfig;
210 my $form = $main::form;
212 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
214 my @configs = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
217 qq|DELETE FROM custom_variables
219 AND (config_id IN (SELECT DISTINCT id
220 FROM custom_variable_configs
222 my @values = (conv_i($params{trans_id}), $params{module});
224 if ($params{sub_module}) {
225 $query .= qq| AND (sub_module = ?)|;
226 push @values, $params{sub_module};
229 do_query($form, $dbh, $query, @values);
232 qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
233 VALUES (?, ?, ?, ?, ?, ?, ?)|;
234 my $sth = prepare_query($form, $dbh, $query);
236 foreach my $config (@configs) {
237 my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
239 my $value = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
241 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
242 push @values, undef, undef, $value, undef;
244 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
245 push @values, undef, conv_date($value), undef, undef;
247 } elsif ($config->{type} eq 'number') {
248 push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));
250 } elsif ($config->{type} eq 'bool') {
251 push @values, $value ? 't' : 'f', undef, undef, undef;
252 } elsif (any { $config->{type} eq $_ } qw(customer vendor part)) {
253 push @values, undef, undef, undef, $value * 1;
256 do_statement($form, $sth, $query, @values);
258 if ($params{save_validity}) {
259 my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
260 $self->save_custom_variables_validity(trans_id => $params{trans_id},
261 config_id => $config->{id},
262 validity => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
269 $dbh->commit() unless $params{dbh};
271 $main::lxdebug->leave_sub();
275 $main::lxdebug->enter_sub(2);
280 Common::check_params(\%params, qw(variables));
282 my $myconfig = \%main::myconfig;
283 my $form = $main::form;
285 my %options = ( name_prefix => "$params{name_prefix}",
286 name_postfix => "$params{name_postfix}",
287 hide_non_editable => $params{hide_non_editable},
288 show_disabled_message => $params{show_disabled_message},
291 # should this cvar be filtered by partsgroups?
292 foreach my $var (@{ $params{variables} }) {
293 if ($var->{flag_partsgroup_filter}) {
294 if (!$params{partsgroup_id} || (!grep {$params{partsgroup_id} == $_} @{ $var->{partsgroups} })) {
295 $var->{partsgroup_filtered} = 1;
299 $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { var => $var, %options });
300 $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
303 $main::lxdebug->leave_sub(2);
306 sub render_search_options {
307 $main::lxdebug->enter_sub();
312 Common::check_params(\%params, qw(variables));
314 my $myconfig = \%main::myconfig;
315 my $form = $main::form;
317 $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
319 $params{include_prefix} = 'l_' unless defined($params{include_prefix});
320 $params{include_value} ||= '1';
321 $params{filter_prefix} ||= '';
323 my $filter = $form->parse_html_template('amcvar/search_filter', \%params);
324 my $include = $form->parse_html_template('amcvar/search_include', \%params);
326 $main::lxdebug->leave_sub();
328 return ($filter, $include);
331 sub build_filter_query {
332 $main::lxdebug->enter_sub();
337 Common::check_params(\%params, qw(module trans_id_field filter));
339 my $myconfig = \%main::myconfig;
340 my $form = $main::form;
342 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
344 my $configs = $self->get_configs(%params);
346 my (@where, @values);
348 foreach my $config (@{ $configs }) {
349 next unless ($config->{searchable});
351 my $name = "cvar_$config->{name}";
353 my (@sub_values, @sub_where, $not);
355 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
356 next unless ($params{filter}->{$name});
358 push @sub_where, qq|cvar.text_value ILIKE ?|;
359 push @sub_values, '%' . trim($params{filter}->{$name}) . '%'
361 } elsif ($config->{type} eq 'select') {
362 next unless ($params{filter}->{$name});
364 push @sub_where, qq|cvar.text_value = ?|;
365 push @sub_values, $params{filter}->{$name};
367 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
368 my $name_from = "${name}_from";
369 my $name_to = "${name}_to";
371 if ($params{filter}->{$name_from}) {
372 push @sub_where, qq|cvar.timestamp_value >= ?|;
373 push @sub_values, conv_date($params{filter}->{$name_from});
376 if ($params{filter}->{$name_to}) {
377 push @sub_where, qq|cvar.timestamp_value <= ?|;
378 push @sub_values, conv_date($params{filter}->{$name_to});
381 } elsif ($config->{type} eq 'number') {
382 next if ($params{filter}->{$name} eq '');
384 my $f_op = $params{filter}->{"${name}_qtyop"};
390 } elsif ($f_op eq '=/=') {
394 } elsif ($f_op eq '<') {
398 } elsif ($f_op eq '<=') {
402 } elsif (($f_op eq '>') || ($f_op eq '>=')) {
409 push @sub_where, qq|cvar.number_value $op ?|;
410 push @sub_values, $form->parse_amount($myconfig, trim($params{filter}->{$name}));
412 } elsif ($config->{type} eq 'bool') {
413 next unless ($params{filter}->{$name});
415 $not = 'NOT' if ($params{filter}->{$name} eq 'no');
416 push @sub_where, qq|COALESCE(cvar.bool_value, false) = TRUE|;
417 } elsif (any { $config->{type} eq $_ } qw(customer vendor)) {
418 next unless $params{filter}->{$name};
420 my $table = $config->{type};
421 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM $table WHERE name ILIKE ?)|;
422 push @sub_values, "%" . trim($params{filter}->{$name}) . "%";
423 } elsif ($config->{type} eq 'part') {
424 next unless $params{filter}->{$name};
426 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM parts WHERE partnumber ILIKE ?)|;
427 push @sub_values, "%" . trim($params{filter}->{$name}) . "%";
431 add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
436 FROM custom_variables cvar
437 LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
438 WHERE (cvarcfg.module = ?)
440 AND (cvar.trans_id = $params{trans_id_field})
441 AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
442 push @values, $params{module}, conv_i($config->{id}), @sub_values;
446 my $query = join ' AND ', @where;
448 $main::lxdebug->leave_sub();
450 return ($query, @values);
453 sub add_custom_variables_to_report {
454 $main::lxdebug->enter_sub();
459 Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
461 my $myconfig = \%main::myconfig;
462 my $form = $main::form;
463 my $locale = $main::locale;
465 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
467 my $configs = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
469 if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
470 $main::lxdebug->leave_sub();
474 # allow sub_module to be a coderef or a fixed value
475 if (ref $params{sub_module} ne 'CODE') {
476 my $sub_module = "$params{sub_module}";
477 $params{sub_module} = sub { $sub_module };
480 my %cfg_map = map { $_->{id} => $_ } @{ $configs };
481 my @cfg_ids = keys %cfg_map;
484 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
485 FROM custom_variables
486 WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
488 AND (sub_module = ?)|;
489 my $sth = prepare_query($form, $dbh, $query);
491 foreach my $row (@{ $params{data} }) {
492 do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
494 while (my $ref = $sth->fetchrow_hashref()) {
495 my $cfg = $cfg_map{$ref->{config_id}};
497 $row->{"cvar_$cfg->{name}"} =
498 $cfg->{type} eq 'date' ? $ref->{date_value}
499 : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
500 : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
501 : $cfg->{type} eq 'customer' ? (SL::DB::Manager::Customer->find_by(id => 1*$ref->{number_value}) || SL::DB::Customer->new)->name
502 : $cfg->{type} eq 'vendor' ? (SL::DB::Manager::Vendor->find_by(id => 1*$ref->{number_value}) || SL::DB::Vendor->new)->name
503 : $cfg->{type} eq 'part' ? (SL::DB::Manager::Part->find_by(id => 1*$ref->{number_value}) || SL::DB::Part->new)->partnumber
504 : $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
505 : $ref->{text_value};
511 $main::lxdebug->leave_sub();
514 sub get_field_format_list {
515 $main::lxdebug->enter_sub();
520 Common::check_params(\%params, qw(module));
522 my $myconfig = \%main::myconfig;
523 my $form = $main::form;
525 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
527 my $configs = $self->get_configs(%params);
529 my $date_fields = [];
530 my $number_fields = {};
532 foreach my $config (@{ $configs }) {
533 my $name = "$params{prefix}cvar_$config->{name}";
535 if ($config->{type} eq 'date') {
536 push @{ $date_fields }, $name;
538 } elsif ($config->{type} eq 'number') {
539 $number_fields->{$config->{precision}} ||= [];
540 push @{ $number_fields->{$config->{precision}} }, $name;
544 $main::lxdebug->leave_sub();
546 return ($date_fields, $number_fields);
549 sub save_custom_variables_validity {
550 $main::lxdebug->enter_sub();
555 Common::check_params(\%params, qw(config_id trans_id validity));
557 my $myconfig = \%main::myconfig;
558 my $form = $main::form;
560 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
562 my (@where, @values);
563 add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
564 add_token(\@where, \@values, col => "trans_id", val => $params{trans_id}, esc => \&conv_i);
566 my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
567 my $query = qq|DELETE FROM custom_variables_validity $where|;
569 do_query($form, $dbh, $query, @values);
572 qq|INSERT INTO custom_variables_validity (config_id, trans_id)
574 my $sth = prepare_query($form, $dbh, $query);
576 unless ($params{validity}) {
577 foreach my $config_id (listify($params{config_id})) {
578 foreach my $trans_id (listify($params{trans_id})) {
579 do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
586 $dbh->commit() unless $params{dbh};
588 $main::lxdebug->leave_sub();
591 sub get_custom_variables_validity {
592 $main::lxdebug->enter_sub(2);
597 Common::check_params(\%params, qw(config_id trans_id));
599 my $myconfig = \%main::myconfig;
600 my $form = $main::form;
602 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
604 my $query = qq|SELECT id FROM custom_variables_validity WHERE config_id = ? AND trans_id = ? LIMIT 1|;
606 my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
608 $main::lxdebug->leave_sub(2);
613 sub custom_variables_validity_by_trans_id {
614 $main::lxdebug->enter_sub(2);
619 return sub { 0 } unless $params{trans_id};
621 my $myconfig = \%main::myconfig;
622 my $form = $main::form;
624 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
626 my $query = qq|SELECT DISTINCT config_id FROM custom_variables_validity WHERE trans_id = ?|;
628 my %invalids = map { +($_->{config_id} => 1) } selectall_hashref_query($form, $dbh, $query, $params{trans_id});
630 $main::lxdebug->leave_sub(2);
632 return sub { !$invalids{+shift} };
636 my ($self, $value, $config) = @_;
638 return $::form->parse_amount(\%::myconfig, $value) if $config->{type} eq 'number';
639 return DateTime->from_lxoffice($value) if $config->{type} eq 'date';
640 return !ref $value ? SL::DB::Manager::Customer->find_by(id => $value * 1) : $value if $config->{type} eq 'customer';
641 return !ref $value ? SL::DB::Manager::Vendor->find_by(id => $value * 1) : $value if $config->{type} eq 'vendor';
642 return !ref $value ? SL::DB::Manager::Part->find_by(id => $value * 1) : $value if $config->{type} eq 'part';
646 sub format_to_template {
647 my ($self, $value, $config) = @_;
648 # stupid template expects everything formated. except objects
649 # do not use outside of print routines for legacy templates
651 return $::form->format_amount(\%::myconfig, $value) if $config->{type} eq 'number';
652 return $value->to_lxoffice if $config->{type} eq 'date' && blessed $value && $value->can('to_lxoffice');
656 sub get_non_editable_ic_cvars {
657 $main::lxdebug->enter_sub(2);
661 Common::check_params(\%params, qw(form dbh row sub_module may_converted_from));
662 my $form = $params{form};
663 my $dbh = $params{dbh};
664 my $row = $params{row};
665 my $sub_module = $params{sub_module};
666 my $may_converted_from = $params{may_converted_from};
669 if (! $form->{"${sub_module}_id_${row}"}) {
671 foreach (@{ $may_converted_from }) {
672 if ($form->{"converted_from_${_}_id_$row"}) {
673 $cvars = CVar->get_custom_variables(dbh => $dbh,
676 trans_id => $form->{"converted_from_${_}_id_$row"},
682 # get values for CVars from master data for new items
684 $cvars = CVar->get_custom_variables(dbh => $dbh,
686 trans_id => $form->{"id_$row"},
690 # get values for CVars from custom_variables for existing items
691 $cvars = CVar->get_custom_variables(dbh => $dbh,
693 sub_module => $sub_module,
694 trans_id => $form->{"${sub_module}_id_${row}"},
697 # map only non-editable CVars to form
698 foreach (@{ $cvars }) {
699 next if $_->{flag_editable};
700 $form->{"ic_cvar_$_->{name}_$row"} = $_->{value}
703 $main::lxdebug->leave_sub(2);
712 SL::CVar.pm - Custom Variables module
716 # dealing with configs
718 my $all_configs = CVar->get_configs()
720 # dealing with custom vars
722 CVar->get_custom_variables(module => 'ic')
726 Suppose the following scenario:
728 You have a lot of parts in your database, and a set of properties configured. 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.
730 Validity is assumed. If you modify validity, you actually save B<invalidity>.
731 Invalidity is saved as a function of config_id, and the trans_id
733 In the naive way, disable an attribute for a specific id (simple)