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 my $cvar_config = SL::DB::CustomVariableConfig->new(id => $config->{id})->load;
60 @{$config->{'partsgroups'}} = map {$_->id} @{$cvar_config->partsgroups};
63 $::form->{CVAR_CONFIGS}->{$params{module}} = $configs;
66 $main::lxdebug->leave_sub();
68 return $::form->{CVAR_CONFIGS}->{$params{module}};
72 $main::lxdebug->enter_sub();
77 foreach my $flag (split m/:/, $config->{flags}) {
78 if ($flag =~ m/(.*?)=(.*)/) {
79 $config->{"flag_${1}"} = $2;
81 $config->{"flag_${flag}"} = 1;
85 $main::lxdebug->leave_sub();
88 sub get_custom_variables {
89 $main::lxdebug->enter_sub();
94 Common::check_params(\%params, qw(module));
96 my $myconfig = \%main::myconfig;
97 my $form = $main::form;
99 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
101 my $sub_module = $params{sub_module} ? $params{sub_module} : '';
104 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value
105 FROM custom_variables
106 WHERE (config_id = ?) AND (trans_id = ?) AND (sub_module = ?)|;
107 my $h_var = prepare_query($form, $dbh, $q_var);
109 my $custom_variables = $self->get_configs(module => $params{module});
111 foreach my $cvar (@{ $custom_variables }) {
112 if ($cvar->{type} eq 'textfield') {
116 $cvar->{width} = $1 if ($cvar->{options} =~ m/width=(\d+)/i);
117 $cvar->{height} = $1 if ($cvar->{options} =~ m/height=(\d+)/i);
119 } elsif ($cvar->{type} eq 'text') {
120 $cvar->{maxlength} = $1 if ($cvar->{options} =~ m/maxlength=(\d+)/i);
122 } elsif ($cvar->{type} eq 'number') {
123 $cvar->{precision} = $1 if ($cvar->{options} =~ m/precision=(\d+)/i);
125 } elsif ($cvar->{type} eq 'select') {
126 $cvar->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $cvar->{options}) ];
129 my ($act_var, $valid);
130 if ($params{trans_id}) {
131 my @values = (conv_i($cvar->{id}), conv_i($params{trans_id}), $sub_module);
133 do_statement($form, $h_var, $q_var, @values);
134 $act_var = $h_var->fetchrow_hashref();
136 $valid = $self->get_custom_variables_validity(config_id => $cvar->{id}, trans_id => $params{trans_id});
138 $valid = !$cvar->{flag_defaults_to_invalid};
142 $cvar->{value} = $cvar->{type} eq 'date' ? $act_var->{date_value}
143 : $cvar->{type} eq 'timestamp' ? $act_var->{timestamp_value}
144 : $cvar->{type} eq 'number' ? $act_var->{number_value}
145 : $cvar->{type} eq 'customer' ? $act_var->{number_value}
146 : $cvar->{type} eq 'vendor' ? $act_var->{number_value}
147 : $cvar->{type} eq 'part' ? $act_var->{number_value}
148 : $cvar->{type} eq 'bool' ? $act_var->{bool_value}
149 : $act_var->{text_value};
150 $cvar->{valid} = $valid;
152 $cvar->{valid} = $valid // 1;
154 if ($cvar->{type} eq 'date') {
155 if ($cvar->{default_value} eq 'NOW') {
156 $cvar->{value} = $cvar->{current_date};
158 $cvar->{value} = $cvar->{default_value};
161 } elsif ($cvar->{type} eq 'timestamp') {
162 if ($cvar->{default_value} eq 'NOW') {
163 $cvar->{value} = $cvar->{current_timestamp};
165 $cvar->{value} = $cvar->{default_value};
168 } elsif ($cvar->{type} eq 'bool') {
169 $cvar->{value} = $cvar->{default_value} * 1;
171 } elsif ($cvar->{type} eq 'number') {
172 $cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');
175 $cvar->{value} = $cvar->{default_value};
179 if ($cvar->{type} eq 'number') {
180 $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
181 } elsif ($cvar->{type} eq 'customer') {
182 require SL::DB::Customer;
183 $cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1);
184 } elsif ($cvar->{type} eq 'vendor') {
185 require SL::DB::Vendor;
186 $cvar->{value} = SL::DB::Manager::Vendor->find_by(id => $cvar->{value} * 1);
187 } elsif ($cvar->{type} eq 'part') {
188 require SL::DB::Part;
189 $cvar->{value} = SL::DB::Manager::Part->find_by(id => $cvar->{value} * 1);
195 $main::lxdebug->leave_sub();
197 return $custom_variables;
200 sub save_custom_variables {
201 $main::lxdebug->enter_sub();
206 Common::check_params(\%params, qw(module trans_id variables));
208 my $myconfig = \%main::myconfig;
209 my $form = $main::form;
211 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
213 my @configs = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
216 qq|DELETE FROM custom_variables
218 AND (config_id IN (SELECT DISTINCT id
219 FROM custom_variable_configs
221 my @values = (conv_i($params{trans_id}), $params{module});
223 if ($params{sub_module}) {
224 $query .= qq| AND (sub_module = ?)|;
225 push @values, $params{sub_module};
228 do_query($form, $dbh, $query, @values);
231 qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
232 VALUES (?, ?, ?, ?, ?, ?, ?)|;
233 my $sth = prepare_query($form, $dbh, $query);
235 foreach my $config (@configs) {
236 my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
238 my $value = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
240 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
241 push @values, undef, undef, $value, undef;
243 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
244 push @values, undef, conv_date($value), undef, undef;
246 } elsif ($config->{type} eq 'number') {
247 push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));
249 } elsif ($config->{type} eq 'bool') {
250 push @values, $value ? 't' : 'f', undef, undef, undef;
251 } elsif (any { $config->{type} eq $_ } qw(customer vendor part)) {
252 push @values, undef, undef, undef, $value * 1;
255 do_statement($form, $sth, $query, @values);
257 if ($params{save_validity}) {
258 my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
259 $self->save_custom_variables_validity(trans_id => $params{trans_id},
260 config_id => $config->{id},
261 validity => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
268 $dbh->commit() unless $params{dbh};
270 $main::lxdebug->leave_sub();
274 $main::lxdebug->enter_sub(2);
279 Common::check_params(\%params, qw(variables));
281 my $myconfig = \%main::myconfig;
282 my $form = $main::form;
284 my %options = ( name_prefix => "$params{name_prefix}",
285 name_postfix => "$params{name_postfix}",
286 hide_non_editable => $params{hide_non_editable},
287 show_disabled_message => $params{show_disabled_message},
290 # should this cvar be filtered by partsgroups?
291 foreach my $var (@{ $params{variables} }) {
292 if ($var->{flag_partsgroup_filter}) {
293 if (!$params{partsgroup_id} || (!grep {$params{partsgroup_id} == $_} @{ $var->{partsgroups} })) {
294 $var->{partsgroup_filtered} = 1;
298 $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { var => $var, %options });
299 $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
302 $main::lxdebug->leave_sub(2);
305 sub render_search_options {
306 $main::lxdebug->enter_sub();
311 Common::check_params(\%params, qw(variables));
313 my $myconfig = \%main::myconfig;
314 my $form = $main::form;
316 $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
318 $params{include_prefix} = 'l_' unless defined($params{include_prefix});
319 $params{include_value} ||= '1';
320 $params{filter_prefix} ||= '';
322 my $filter = $form->parse_html_template('amcvar/search_filter', \%params);
323 my $include = $form->parse_html_template('amcvar/search_include', \%params);
325 $main::lxdebug->leave_sub();
327 return ($filter, $include);
330 sub build_filter_query {
331 $main::lxdebug->enter_sub();
336 Common::check_params(\%params, qw(module trans_id_field filter));
338 my $myconfig = \%main::myconfig;
339 my $form = $main::form;
341 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
343 my $configs = $self->get_configs(%params);
345 my (@where, @values);
347 foreach my $config (@{ $configs }) {
348 next unless ($config->{searchable});
350 my $name = "cvar_$config->{name}";
352 my (@sub_values, @sub_where, $not);
354 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
355 next unless ($params{filter}->{$name});
357 push @sub_where, qq|cvar.text_value ILIKE ?|;
358 push @sub_values, '%' . $params{filter}->{$name} . '%'
360 } elsif ($config->{type} eq 'select') {
361 next unless ($params{filter}->{$name});
363 push @sub_where, qq|cvar.text_value = ?|;
364 push @sub_values, $params{filter}->{$name};
366 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
367 my $name_from = "${name}_from";
368 my $name_to = "${name}_to";
370 if ($params{filter}->{$name_from}) {
371 push @sub_where, qq|cvar.timestamp_value >= ?|;
372 push @sub_values, conv_date($params{filter}->{$name_from});
375 if ($params{filter}->{$name_to}) {
376 push @sub_where, qq|cvar.timestamp_value <= ?|;
377 push @sub_values, conv_date($params{filter}->{$name_to});
380 } elsif ($config->{type} eq 'number') {
381 next if ($params{filter}->{$name} eq '');
383 my $f_op = $params{filter}->{"${name}_qtyop"};
389 } elsif ($f_op eq '=/=') {
393 } elsif ($f_op eq '<') {
397 } elsif ($f_op eq '<=') {
401 } elsif (($f_op eq '>') || ($f_op eq '>=')) {
408 push @sub_where, qq|cvar.number_value $op ?|;
409 push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
411 } elsif ($config->{type} eq 'bool') {
412 next unless ($params{filter}->{$name});
414 $not = 'NOT' if ($params{filter}->{$name} eq 'no');
415 push @sub_where, qq|COALESCE(cvar.bool_value, false) = TRUE|;
416 } elsif (any { $config->{type} eq $_ } qw(customer vendor)) {
417 next unless $params{filter}->{$name};
419 my $table = $config->{type};
420 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM $table WHERE name ILIKE ?)|;
421 push @sub_values, "%$params{filter}->{$name}%";
422 } elsif ($config->{type} eq 'part') {
423 next unless $params{filter}->{$name};
425 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM parts WHERE partnumber ILIKE ?)|;
426 push @sub_values, "%$params{filter}->{$name}%";
430 add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
435 FROM custom_variables cvar
436 LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
437 WHERE (cvarcfg.module = ?)
439 AND (cvar.trans_id = $params{trans_id_field})
440 AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
441 push @values, $params{module}, conv_i($config->{id}), @sub_values;
445 my $query = join ' AND ', @where;
447 $main::lxdebug->leave_sub();
449 return ($query, @values);
452 sub add_custom_variables_to_report {
453 $main::lxdebug->enter_sub();
458 Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
460 my $myconfig = \%main::myconfig;
461 my $form = $main::form;
462 my $locale = $main::locale;
464 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
466 my $configs = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
468 if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
469 $main::lxdebug->leave_sub();
473 # allow sub_module to be a coderef or a fixed value
474 if (ref $params{sub_module} ne 'CODE') {
475 my $sub_module = "$params{sub_module}";
476 $params{sub_module} = sub { $sub_module };
479 my %cfg_map = map { $_->{id} => $_ } @{ $configs };
480 my @cfg_ids = keys %cfg_map;
483 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
484 FROM custom_variables
485 WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
487 AND (sub_module = ?)|;
488 my $sth = prepare_query($form, $dbh, $query);
490 foreach my $row (@{ $params{data} }) {
491 do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
493 while (my $ref = $sth->fetchrow_hashref()) {
494 my $cfg = $cfg_map{$ref->{config_id}};
496 $row->{"cvar_$cfg->{name}"} =
497 $cfg->{type} eq 'date' ? $ref->{date_value}
498 : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
499 : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
500 : $cfg->{type} eq 'customer' ? (SL::DB::Manager::Customer->find_by(id => 1*$ref->{number_value}) || SL::DB::Customer->new)->name
501 : $cfg->{type} eq 'vendor' ? (SL::DB::Manager::Vendor->find_by(id => 1*$ref->{number_value}) || SL::DB::Vendor->new)->name
502 : $cfg->{type} eq 'part' ? (SL::DB::Manager::Part->find_by(id => 1*$ref->{number_value}) || SL::DB::Part->new)->partnumber
503 : $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
504 : $ref->{text_value};
510 $main::lxdebug->leave_sub();
513 sub get_field_format_list {
514 $main::lxdebug->enter_sub();
519 Common::check_params(\%params, qw(module));
521 my $myconfig = \%main::myconfig;
522 my $form = $main::form;
524 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
526 my $configs = $self->get_configs(%params);
528 my $date_fields = [];
529 my $number_fields = {};
531 foreach my $config (@{ $configs }) {
532 my $name = "$params{prefix}cvar_$config->{name}";
534 if ($config->{type} eq 'date') {
535 push @{ $date_fields }, $name;
537 } elsif ($config->{type} eq 'number') {
538 $number_fields->{$config->{precision}} ||= [];
539 push @{ $number_fields->{$config->{precision}} }, $name;
543 $main::lxdebug->leave_sub();
545 return ($date_fields, $number_fields);
548 sub save_custom_variables_validity {
549 $main::lxdebug->enter_sub();
554 Common::check_params(\%params, qw(config_id trans_id validity));
556 my $myconfig = \%main::myconfig;
557 my $form = $main::form;
559 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
561 my (@where, @values);
562 add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
563 add_token(\@where, \@values, col => "trans_id", val => $params{trans_id}, esc => \&conv_i);
565 my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
566 my $query = qq|DELETE FROM custom_variables_validity $where|;
568 do_query($form, $dbh, $query, @values);
571 qq|INSERT INTO custom_variables_validity (config_id, trans_id)
573 my $sth = prepare_query($form, $dbh, $query);
575 unless ($params{validity}) {
576 foreach my $config_id (listify($params{config_id})) {
577 foreach my $trans_id (listify($params{trans_id})) {
578 do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
585 $dbh->commit() unless $params{dbh};
587 $main::lxdebug->leave_sub();
590 sub get_custom_variables_validity {
591 $main::lxdebug->enter_sub(2);
596 Common::check_params(\%params, qw(config_id trans_id));
598 my $myconfig = \%main::myconfig;
599 my $form = $main::form;
601 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
603 my $query = qq|SELECT id FROM custom_variables_validity WHERE config_id = ? AND trans_id = ? LIMIT 1|;
605 my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
607 $main::lxdebug->leave_sub(2);
612 sub custom_variables_validity_by_trans_id {
613 $main::lxdebug->enter_sub(2);
618 return sub { 0 } unless $params{trans_id};
620 my $myconfig = \%main::myconfig;
621 my $form = $main::form;
623 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
625 my $query = qq|SELECT DISTINCT config_id FROM custom_variables_validity WHERE trans_id = ?|;
627 my %invalids = map { +($_->{config_id} => 1) } selectall_hashref_query($form, $dbh, $query, $params{trans_id});
629 $main::lxdebug->leave_sub(2);
631 return sub { !$invalids{+shift} };
635 my ($self, $value, $config) = @_;
637 return $::form->parse_amount(\%::myconfig, $value) if $config->{type} eq 'number';
638 return DateTime->from_lxoffice($value) if $config->{type} eq 'date';
639 return !ref $value ? SL::DB::Manager::Customer->find_by(id => $value * 1) : $value if $config->{type} eq 'customer';
640 return !ref $value ? SL::DB::Manager::Vendor->find_by(id => $value * 1) : $value if $config->{type} eq 'vendor';
641 return !ref $value ? SL::DB::Manager::Part->find_by(id => $value * 1) : $value if $config->{type} eq 'part';
645 sub format_to_template {
646 my ($self, $value, $config) = @_;
647 # stupid template expects everything formated. except objects
648 # do not use outside of print routines for legacy templates
650 return $::form->format_amount(\%::myconfig, $value) if $config->{type} eq 'number';
651 return $value->to_lxoffice if $config->{type} eq 'date' && blessed $value && $value->can('to_lxoffice');
655 sub get_non_editable_ic_cvars {
656 $main::lxdebug->enter_sub(2);
660 Common::check_params(\%params, qw(form dbh row sub_module may_converted_from));
661 my $form = $params{form};
662 my $dbh = $params{dbh};
663 my $row = $params{row};
664 my $sub_module = $params{sub_module};
665 my $may_converted_from = $params{may_converted_from};
668 if (! $form->{"${sub_module}_id_${row}"}) {
670 foreach (@{ $may_converted_from }) {
671 if ($form->{"converted_from_${_}_id_$row"}) {
672 $cvars = CVar->get_custom_variables(dbh => $dbh,
675 trans_id => $form->{"converted_from_${_}_id_$row"},
681 # get values for CVars from master data for new items
683 $cvars = CVar->get_custom_variables(dbh => $dbh,
685 trans_id => $form->{"id_$row"},
689 # get values for CVars from custom_variables for existing items
690 $cvars = CVar->get_custom_variables(dbh => $dbh,
692 sub_module => $sub_module,
693 trans_id => $form->{"${sub_module}_id_${row}"},
696 # map only non-editable CVars to form
697 foreach (@{ $cvars }) {
698 next if $_->{flag_editable};
699 $form->{"ic_cvar_$_->{name}_$row"} = $_->{value}
702 $main::lxdebug->leave_sub(2);
711 SL::CVar.pm - Custom Variables module
715 # dealing with configs
717 my $all_configs = CVar->get_configs()
719 # dealing with custom vars
721 CVar->get_custom_variables(module => 'ic')
725 Suppose the following scenario:
727 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.
729 Validity is assumed. If you modify validity, you actually save B<invalidity>.
730 Invalidity is saved as a function of config_id, and the trans_id
732 In the naive way, disable an attribute for a specific id (simple)