6 use List::MoreUtils qw(any);
7 use List::Util qw(first);
8 use Scalar::Util qw(blessed);
12 use SL::MoreCommon qw(listify);
13 use SL::Util qw(trim);
17 $main::lxdebug->enter_sub();
22 my $myconfig = \%main::myconfig;
23 my $form = $main::form;
25 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
28 if ($params{module}) {
29 $where = 'WHERE module = ?';
30 push @values, $params{module};
34 SELECT *, date_trunc('seconds', localtimestamp) AS current_timestamp
35 FROM custom_variable_configs $where ORDER BY sortkey
38 $::form->{CVAR_CONFIGS} = {} unless 'HASH' eq ref $::form->{CVAR_CONFIGS};
39 if (!$::form->{CVAR_CONFIGS}->{$params{module}}) {
40 my $configs = selectall_hashref_query($form, $dbh, $query, @values);
42 foreach my $config (@{ $configs }) {
43 if ($config->{type} eq 'select') {
44 $config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
46 } elsif ($config->{type} eq 'number') {
47 $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
49 } elsif ($config->{type} eq 'textfield') {
50 $config->{width} = 30;
51 $config->{height} = 5;
52 $config->{width} = $1 if ($config->{options} =~ m/width=(\d+)/i);
53 $config->{height} = $1 if ($config->{options} =~ m/height=(\d+)/i);
55 } elsif ($config->{type} eq 'text') {
56 $config->{maxlength} = $1 if ($config->{options} =~ m/maxlength=(\d+)/i);
60 $self->_unpack_flags($config);
62 my $cvar_config = SL::DB::CustomVariableConfig->new(id => $config->{id})->load;
63 @{$config->{'partsgroups'}} = map {$_->id} @{$cvar_config->partsgroups};
66 $::form->{CVAR_CONFIGS}->{$params{module}} = $configs;
69 $main::lxdebug->leave_sub();
71 return $::form->{CVAR_CONFIGS}->{$params{module}};
75 $main::lxdebug->enter_sub();
80 foreach my $flag (split m/:/, $config->{flags}) {
81 if ($flag =~ m/(.*?)=(.*)/) {
82 $config->{"flag_${1}"} = $2;
84 $config->{"flag_${flag}"} = 1;
88 $main::lxdebug->leave_sub();
91 sub get_custom_variables {
92 $main::lxdebug->enter_sub();
97 Common::check_params(\%params, qw(module));
99 my $myconfig = \%main::myconfig;
100 my $form = $main::form;
102 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
104 my $sub_module = $params{sub_module} ? $params{sub_module} : '';
107 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value
108 FROM custom_variables
109 WHERE (config_id = ?) AND (trans_id = ?) AND (sub_module = ?)|;
110 my $h_var = prepare_query($form, $dbh, $q_var);
112 my $custom_variables = $self->get_configs(module => $params{module});
114 foreach my $cvar (@{ $custom_variables }) {
115 if ($cvar->{type} eq 'textfield') {
119 $cvar->{width} = $1 if ($cvar->{options} =~ m/width=(\d+)/i);
120 $cvar->{height} = $1 if ($cvar->{options} =~ m/height=(\d+)/i);
122 } elsif ($cvar->{type} eq 'text') {
123 $cvar->{maxlength} = $1 if ($cvar->{options} =~ m/maxlength=(\d+)/i);
125 } elsif ($cvar->{type} eq 'number') {
126 $cvar->{precision} = $1 if ($cvar->{options} =~ m/precision=(\d+)/i);
128 } elsif ($cvar->{type} eq 'select') {
129 $cvar->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $cvar->{options}) ];
132 my ($act_var, $valid);
133 if ($params{trans_id}) {
134 my @values = (conv_i($cvar->{id}), conv_i($params{trans_id}), $sub_module);
136 do_statement($form, $h_var, $q_var, @values);
137 $act_var = $h_var->fetchrow_hashref();
139 $valid = $self->get_custom_variables_validity(config_id => $cvar->{id}, trans_id => $params{trans_id}, sub_module => $params{sub_module});
141 $valid = !$cvar->{flag_defaults_to_invalid};
145 $cvar->{value} = $cvar->{type} eq 'date' ? $act_var->{date_value}
146 : $cvar->{type} eq 'timestamp' ? $act_var->{timestamp_value}
147 : $cvar->{type} eq 'number' ? $act_var->{number_value}
148 : $cvar->{type} eq 'customer' ? $act_var->{number_value}
149 : $cvar->{type} eq 'vendor' ? $act_var->{number_value}
150 : $cvar->{type} eq 'part' ? $act_var->{number_value}
151 : $cvar->{type} eq 'bool' ? $act_var->{bool_value}
152 : $act_var->{text_value};
153 $cvar->{valid} = $valid;
155 $cvar->{valid} = $valid // 1;
157 if ($cvar->{type} eq 'date') {
158 if ($cvar->{default_value} eq 'NOW') {
159 $cvar->{value} = $cvar->{current_date};
161 $cvar->{value} = $cvar->{default_value};
164 } elsif ($cvar->{type} eq 'timestamp') {
165 if ($cvar->{default_value} eq 'NOW') {
166 $cvar->{value} = $cvar->{current_timestamp};
168 $cvar->{value} = $cvar->{default_value};
171 } elsif ($cvar->{type} eq 'bool') {
172 $cvar->{value} = $cvar->{default_value} * 1;
174 } elsif ($cvar->{type} eq 'number') {
175 $cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');
178 $cvar->{value} = $cvar->{default_value};
182 if ($cvar->{type} eq 'number') {
183 $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
184 } elsif ($cvar->{type} eq 'customer') {
185 require SL::DB::Customer;
186 $cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1);
187 } elsif ($cvar->{type} eq 'vendor') {
188 require SL::DB::Vendor;
189 $cvar->{value} = SL::DB::Manager::Vendor->find_by(id => $cvar->{value} * 1);
190 } elsif ($cvar->{type} eq 'part') {
191 require SL::DB::Part;
192 $cvar->{value} = SL::DB::Manager::Part->find_by(id => $cvar->{value} * 1);
198 $main::lxdebug->leave_sub();
200 return $custom_variables;
203 sub save_custom_variables {
204 my ($self, %params) = @_;
205 $main::lxdebug->enter_sub();
207 my $rc = SL::DB->client->with_transaction(\&_save_custom_variables, $self, %params);
209 $::lxdebug->leave_sub;
213 sub _save_custom_variables {
217 Common::check_params(\%params, qw(module trans_id variables));
219 my $myconfig = \%main::myconfig;
220 my $form = $main::form;
222 my $dbh = $params{dbh} || SL::DB->client->dbh;
224 my @configs = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
227 qq|DELETE FROM custom_variables
229 AND (config_id IN (SELECT DISTINCT id
230 FROM custom_variable_configs
232 my @values = (conv_i($params{trans_id}), $params{module});
234 if ($params{sub_module}) {
235 $query .= qq| AND (sub_module = ?)|;
236 push @values, $params{sub_module};
239 do_query($form, $dbh, $query, @values);
242 qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
243 VALUES (?, ?, ?, ?, ?, ?, ?)|;
244 my $sth = prepare_query($form, $dbh, $query);
246 foreach my $config (@configs) {
247 my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
249 my $value = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
251 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
252 push @values, undef, undef, $value, undef;
254 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
255 push @values, undef, conv_date($value), undef, undef;
257 } elsif ($config->{type} eq 'number') {
258 push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));
260 } elsif ($config->{type} eq 'bool') {
261 push @values, $value ? 't' : 'f', undef, undef, undef;
262 } elsif (any { $config->{type} eq $_ } qw(customer vendor part)) {
263 push @values, undef, undef, undef, $value * 1;
266 do_statement($form, $sth, $query, @values);
268 if ($params{save_validity}) {
269 my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
270 $self->save_custom_variables_validity(trans_id => $params{trans_id},
271 config_id => $config->{id},
272 validity => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
283 $main::lxdebug->enter_sub(2);
288 Common::check_params(\%params, qw(variables));
290 my $myconfig = \%main::myconfig;
291 my $form = $main::form;
293 my %options = ( name_prefix => "$params{name_prefix}",
294 name_postfix => "$params{name_postfix}",
295 hide_non_editable => $params{hide_non_editable},
296 show_disabled_message => $params{show_disabled_message},
299 # should this cvar be filtered by partsgroups?
300 foreach my $var (@{ $params{variables} }) {
301 if ($var->{flag_partsgroup_filter}) {
302 if (!$params{partsgroup_id} || (!grep {$params{partsgroup_id} == $_} @{ $var->{partsgroups} })) {
303 $var->{partsgroup_filtered} = 1;
307 $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { var => $var, %options });
308 $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
311 $main::lxdebug->leave_sub(2);
314 sub render_search_options {
315 $main::lxdebug->enter_sub();
320 Common::check_params(\%params, qw(variables));
322 my $myconfig = \%main::myconfig;
323 my $form = $main::form;
325 $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
327 $params{include_prefix} = 'l_' unless defined($params{include_prefix});
328 $params{include_value} ||= '1';
329 $params{filter_prefix} ||= '';
331 my $filter = $form->parse_html_template('amcvar/search_filter', \%params);
332 my $include = $form->parse_html_template('amcvar/search_include', \%params);
334 $main::lxdebug->leave_sub();
336 return ($filter, $include);
339 sub build_filter_query {
340 $main::lxdebug->enter_sub();
345 Common::check_params(\%params, qw(module trans_id_field filter));
347 my $myconfig = \%main::myconfig;
348 my $form = $main::form;
350 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
352 my $configs = $self->get_configs(%params);
354 my (@where, @values);
356 foreach my $config (@{ $configs }) {
357 next unless ($config->{searchable});
359 my $name = "cvar_$config->{name}";
361 my (@sub_values, @sub_where, $not);
363 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
364 next unless ($params{filter}->{$name});
366 push @sub_where, qq|cvar.text_value ILIKE ?|;
367 push @sub_values, like($params{filter}->{$name});
369 } elsif ($config->{type} eq 'select') {
370 next unless ($params{filter}->{$name});
372 push @sub_where, qq|cvar.text_value = ?|;
373 push @sub_values, $params{filter}->{$name};
375 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
376 my $name_from = "${name}_from";
377 my $name_to = "${name}_to";
379 if ($params{filter}->{$name_from}) {
380 push @sub_where, qq|cvar.timestamp_value >= ?|;
381 push @sub_values, conv_date($params{filter}->{$name_from});
384 if ($params{filter}->{$name_to}) {
385 push @sub_where, qq|cvar.timestamp_value <= ?|;
386 push @sub_values, conv_date($params{filter}->{$name_to});
389 } elsif ($config->{type} eq 'number') {
390 next if ($params{filter}->{$name} eq '');
392 my $f_op = $params{filter}->{"${name}_qtyop"};
398 } elsif ($f_op eq '=/=') {
402 } elsif ($f_op eq '<') {
406 } elsif ($f_op eq '<=') {
410 } elsif (($f_op eq '>') || ($f_op eq '>=')) {
417 push @sub_where, qq|cvar.number_value $op ?|;
418 push @sub_values, $form->parse_amount($myconfig, trim($params{filter}->{$name}));
420 } elsif ($config->{type} eq 'bool') {
421 next unless ($params{filter}->{$name});
423 $not = 'NOT' if ($params{filter}->{$name} eq 'no');
424 push @sub_where, qq|COALESCE(cvar.bool_value, false) = TRUE|;
425 } elsif (any { $config->{type} eq $_ } qw(customer vendor)) {
426 next unless $params{filter}->{$name};
428 my $table = $config->{type};
429 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM $table WHERE name ILIKE ?)|;
430 push @sub_values, like($params{filter}->{$name});
431 } elsif ($config->{type} eq 'part') {
432 next unless $params{filter}->{$name};
434 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM parts WHERE partnumber ILIKE ?)|;
435 push @sub_values, like($params{filter}->{$name});
439 add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
444 FROM custom_variables cvar
445 LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
446 WHERE (cvarcfg.module = ?)
448 AND (cvar.trans_id = $params{trans_id_field})
449 AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
450 push @values, $params{module}, conv_i($config->{id}), @sub_values;
454 my $query = join ' AND ', @where;
456 $main::lxdebug->leave_sub();
458 return ($query, @values);
461 sub add_custom_variables_to_report {
462 $main::lxdebug->enter_sub();
467 Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
469 my $myconfig = \%main::myconfig;
470 my $form = $main::form;
471 my $locale = $main::locale;
473 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
475 my $configs = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
477 if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
478 $main::lxdebug->leave_sub();
482 # allow sub_module to be a coderef or a fixed value
483 if (ref $params{sub_module} ne 'CODE') {
484 my $sub_module = "$params{sub_module}";
485 $params{sub_module} = sub { $sub_module };
488 my %cfg_map = map { $_->{id} => $_ } @{ $configs };
489 my @cfg_ids = keys %cfg_map;
492 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
493 FROM custom_variables
494 WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
496 AND (sub_module = ?)|;
497 my $sth = prepare_query($form, $dbh, $query);
499 foreach my $row (@{ $params{data} }) {
500 do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
502 while (my $ref = $sth->fetchrow_hashref()) {
503 my $cfg = $cfg_map{$ref->{config_id}};
505 $row->{"cvar_$cfg->{name}"} =
506 $cfg->{type} eq 'date' ? $ref->{date_value}
507 : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
508 : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
509 : $cfg->{type} eq 'customer' ? (SL::DB::Manager::Customer->find_by(id => 1*$ref->{number_value}) || SL::DB::Customer->new)->name
510 : $cfg->{type} eq 'vendor' ? (SL::DB::Manager::Vendor->find_by(id => 1*$ref->{number_value}) || SL::DB::Vendor->new)->name
511 : $cfg->{type} eq 'part' ? (SL::DB::Manager::Part->find_by(id => 1*$ref->{number_value}) || SL::DB::Part->new)->partnumber
512 : $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
513 : $ref->{text_value};
519 $main::lxdebug->leave_sub();
522 sub get_field_format_list {
523 $main::lxdebug->enter_sub();
528 Common::check_params(\%params, qw(module));
530 my $myconfig = \%main::myconfig;
531 my $form = $main::form;
533 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
535 my $configs = $self->get_configs(%params);
537 my $date_fields = [];
538 my $number_fields = {};
540 foreach my $config (@{ $configs }) {
541 my $name = "$params{prefix}cvar_$config->{name}";
543 if ($config->{type} eq 'date') {
544 push @{ $date_fields }, $name;
546 } elsif ($config->{type} eq 'number') {
547 $number_fields->{$config->{precision}} ||= [];
548 push @{ $number_fields->{$config->{precision}} }, $name;
552 $main::lxdebug->leave_sub();
554 return ($date_fields, $number_fields);
557 sub save_custom_variables_validity {
558 my ($self, %params) = @_;
559 $main::lxdebug->enter_sub();
561 my $rc = SL::DB->client->with_transaction(\&_save_custom_variables_validity, $self, %params);
563 $::lxdebug->leave_sub;
567 sub _save_custom_variables_validity {
571 Common::check_params(\%params, qw(config_id trans_id validity));
573 my $myconfig = \%main::myconfig;
574 my $form = $main::form;
576 my $dbh = $params{dbh} || SL::DB->client->dbh;
578 my (@where, @values);
579 add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
580 add_token(\@where, \@values, col => "trans_id", val => $params{trans_id}, esc => \&conv_i);
582 my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
583 my $query = qq|DELETE FROM custom_variables_validity $where|;
585 do_query($form, $dbh, $query, @values);
588 qq|INSERT INTO custom_variables_validity (config_id, trans_id)
590 my $sth = prepare_query($form, $dbh, $query);
592 unless ($params{validity}) {
593 foreach my $config_id (listify($params{config_id})) {
594 foreach my $trans_id (listify($params{trans_id})) {
595 do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
605 my %_validity_sub_module_mapping = (
606 orderitems => { table => 'orderitems', result_column => 'parts_id', trans_id_column => 'id', },
607 delivery_order_items => { table => 'delivery_order_items', result_column => 'parts_id', trans_id_column => 'id', },
608 invoice => { table => 'invoice', result_column => 'parts_id', trans_id_column => 'id', },
611 sub get_custom_variables_validity {
615 Common::check_params(\%params, qw(config_id trans_id));
617 my $myconfig = \%main::myconfig;
618 my $form = $main::form;
620 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
624 if ($params{sub_module}) {
625 my %mapping = %{ $_validity_sub_module_mapping{ $params{sub_module} } || croak("Invalid sub_module '" . $params{sub_module} . "'") };
628 FROM $mapping{table} mt
629 LEFT JOIN custom_variables_validity cvv ON (cvv.trans_id = mt.$mapping{result_column})
630 WHERE (cvv.config_id = ?)
631 AND (mt.$mapping{trans_id_column} = ?)
637 FROM custom_variables_validity
638 WHERE (config_id = ?)
644 my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
649 sub custom_variables_validity_by_trans_id {
650 $main::lxdebug->enter_sub(2);
655 return sub { 0 } unless $params{trans_id};
657 my $myconfig = \%main::myconfig;
658 my $form = $main::form;
660 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
662 my $query = qq|SELECT DISTINCT config_id FROM custom_variables_validity WHERE trans_id = ?|;
664 my %invalids = map { +($_->{config_id} => 1) } selectall_hashref_query($form, $dbh, $query, $params{trans_id});
666 $main::lxdebug->leave_sub(2);
668 return sub { !$invalids{+shift} };
672 my ($self, $value, $config) = @_;
674 return $::form->parse_amount(\%::myconfig, $value) if $config->{type} eq 'number';
675 return DateTime->from_lxoffice($value) if $config->{type} eq 'date';
676 return !ref $value ? SL::DB::Manager::Customer->find_by(id => $value * 1) : $value if $config->{type} eq 'customer';
677 return !ref $value ? SL::DB::Manager::Vendor->find_by(id => $value * 1) : $value if $config->{type} eq 'vendor';
678 return !ref $value ? SL::DB::Manager::Part->find_by(id => $value * 1) : $value if $config->{type} eq 'part';
682 sub format_to_template {
683 my ($self, $value, $config) = @_;
684 # stupid template expects everything formated. except objects
685 # do not use outside of print routines for legacy templates
687 return $::form->format_amount(\%::myconfig, $value) if $config->{type} eq 'number';
688 return $value->to_lxoffice if $config->{type} eq 'date' && blessed $value && $value->can('to_lxoffice');
692 sub get_non_editable_ic_cvars {
693 $main::lxdebug->enter_sub(2);
697 Common::check_params(\%params, qw(form dbh row sub_module may_converted_from));
698 my $form = $params{form};
699 my $dbh = $params{dbh};
700 my $row = $params{row};
701 my $sub_module = $params{sub_module};
702 my $may_converted_from = $params{may_converted_from};
705 if (! $form->{"${sub_module}_id_${row}"}) {
707 foreach (@{ $may_converted_from }) {
708 if ($form->{"converted_from_${_}_id_$row"}) {
709 $cvars = CVar->get_custom_variables(dbh => $dbh,
712 trans_id => $form->{"converted_from_${_}_id_$row"},
718 # get values for CVars from master data for new items
720 $cvars = CVar->get_custom_variables(dbh => $dbh,
722 trans_id => $form->{"id_$row"},
726 # get values for CVars from custom_variables for existing items
727 $cvars = CVar->get_custom_variables(dbh => $dbh,
729 sub_module => $sub_module,
730 trans_id => $form->{"${sub_module}_id_${row}"},
733 # map only non-editable CVars to form
734 foreach (@{ $cvars }) {
735 next if $_->{flag_editable};
736 $form->{"ic_cvar_$_->{name}_$row"} = $_->{value}
739 $main::lxdebug->leave_sub(2);
748 SL::CVar.pm - Custom Variables module
752 # dealing with configs
754 my $all_configs = CVar->get_configs()
756 # dealing with custom vars
758 CVar->get_custom_variables(module => 'ic')
762 Suppose the following scenario:
764 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.
766 Validity is assumed. If you modify validity, you actually save B<invalidity>.
767 Invalidity is saved as a function of config_id, and the trans_id
769 In the naive way, disable an attribute for a specific id (simple)