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});
138 $cvar->{value} = $cvar->{type} eq 'date' ? $act_var->{date_value}
139 : $cvar->{type} eq 'timestamp' ? $act_var->{timestamp_value}
140 : $cvar->{type} eq 'number' ? $act_var->{number_value}
141 : $cvar->{type} eq 'customer' ? $act_var->{number_value}
142 : $cvar->{type} eq 'vendor' ? $act_var->{number_value}
143 : $cvar->{type} eq 'part' ? $act_var->{number_value}
144 : $cvar->{type} eq 'bool' ? $act_var->{bool_value}
145 : $act_var->{text_value};
146 $cvar->{valid} = $valid;
150 if ($cvar->{type} eq 'date') {
151 if ($cvar->{default_value} eq 'NOW') {
152 $cvar->{value} = $cvar->{current_date};
154 $cvar->{value} = $cvar->{default_value};
157 } elsif ($cvar->{type} eq 'timestamp') {
158 if ($cvar->{default_value} eq 'NOW') {
159 $cvar->{value} = $cvar->{current_timestamp};
161 $cvar->{value} = $cvar->{default_value};
164 } elsif ($cvar->{type} eq 'bool') {
165 $cvar->{value} = $cvar->{default_value} * 1;
167 } elsif ($cvar->{type} eq 'number') {
168 $cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');
171 $cvar->{value} = $cvar->{default_value};
175 if ($cvar->{type} eq 'number') {
176 $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
177 } elsif ($cvar->{type} eq 'customer') {
178 require SL::DB::Customer;
179 $cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1);
180 } elsif ($cvar->{type} eq 'vendor') {
181 require SL::DB::Vendor;
182 $cvar->{value} = SL::DB::Manager::Vendor->find_by(id => $cvar->{value} * 1);
183 } elsif ($cvar->{type} eq 'part') {
184 require SL::DB::Part;
185 $cvar->{value} = SL::DB::Manager::Part->find_by(id => $cvar->{value} * 1);
191 $main::lxdebug->leave_sub();
193 return $custom_variables;
196 sub save_custom_variables {
197 $main::lxdebug->enter_sub();
202 Common::check_params(\%params, qw(module trans_id variables));
204 my $myconfig = \%main::myconfig;
205 my $form = $main::form;
207 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
209 my @configs = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
212 qq|DELETE FROM custom_variables
214 AND (config_id IN (SELECT DISTINCT id
215 FROM custom_variable_configs
217 my @values = (conv_i($params{trans_id}), $params{module});
219 if ($params{sub_module}) {
220 $query .= qq| AND (sub_module = ?)|;
221 push @values, $params{sub_module};
224 do_query($form, $dbh, $query, @values);
227 qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
228 VALUES (?, ?, ?, ?, ?, ?, ?)|;
229 my $sth = prepare_query($form, $dbh, $query);
231 foreach my $config (@configs) {
232 my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
234 my $value = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
236 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
237 push @values, undef, undef, $value, undef;
239 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
240 push @values, undef, conv_date($value), undef, undef;
242 } elsif ($config->{type} eq 'number') {
243 push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));
245 } elsif ($config->{type} eq 'bool') {
246 push @values, $value ? 't' : 'f', undef, undef, undef;
247 } elsif (any { $config->{type} eq $_ } qw(customer vendor part)) {
248 push @values, undef, undef, undef, $value * 1;
251 do_statement($form, $sth, $query, @values);
253 if ($params{save_validity}) {
254 my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
255 $self->save_custom_variables_validity(trans_id => $params{trans_id},
256 config_id => $config->{id},
257 validity => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
264 $dbh->commit() unless $params{dbh};
266 $main::lxdebug->leave_sub();
270 $main::lxdebug->enter_sub(2);
275 Common::check_params(\%params, qw(variables));
277 my $myconfig = \%main::myconfig;
278 my $form = $main::form;
280 my %options = ( name_prefix => "$params{name_prefix}",
281 name_postfix => "$params{name_postfix}",
282 hide_non_editable => $params{hide_non_editable},
283 show_disabled_message => $params{show_disabled_message},
286 foreach my $var (@{ $params{variables} }) {
287 $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { var => $var, %options });
288 $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
291 $main::lxdebug->leave_sub(2);
294 sub render_search_options {
295 $main::lxdebug->enter_sub();
300 Common::check_params(\%params, qw(variables));
302 my $myconfig = \%main::myconfig;
303 my $form = $main::form;
305 $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
307 $params{include_prefix} = 'l_' unless defined($params{include_prefix});
308 $params{include_value} ||= '1';
309 $params{filter_prefix} ||= '';
311 my $filter = $form->parse_html_template('amcvar/search_filter', \%params);
312 my $include = $form->parse_html_template('amcvar/search_include', \%params);
314 $main::lxdebug->leave_sub();
316 return ($filter, $include);
319 sub build_filter_query {
320 $main::lxdebug->enter_sub();
325 Common::check_params(\%params, qw(module trans_id_field filter));
327 my $myconfig = \%main::myconfig;
328 my $form = $main::form;
330 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
332 my $configs = $self->get_configs(%params);
334 my (@where, @values);
336 foreach my $config (@{ $configs }) {
337 next unless ($config->{searchable});
339 my $name = "cvar_$config->{name}";
341 my (@sub_values, @sub_where, $not);
343 if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
344 next unless ($params{filter}->{$name});
346 push @sub_where, qq|cvar.text_value ILIKE ?|;
347 push @sub_values, '%' . $params{filter}->{$name} . '%'
349 } elsif ($config->{type} eq 'select') {
350 next unless ($params{filter}->{$name});
352 push @sub_where, qq|cvar.text_value = ?|;
353 push @sub_values, $params{filter}->{$name};
355 } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
356 my $name_from = "${name}_from";
357 my $name_to = "${name}_to";
359 if ($params{filter}->{$name_from}) {
360 push @sub_where, qq|cvar.timestamp_value >= ?|;
361 push @sub_values, conv_date($params{filter}->{$name_from});
364 if ($params{filter}->{$name_to}) {
365 push @sub_where, qq|cvar.timestamp_value <= ?|;
366 push @sub_values, conv_date($params{filter}->{$name_to});
369 } elsif ($config->{type} eq 'number') {
370 next if ($params{filter}->{$name} eq '');
372 my $f_op = $params{filter}->{"${name}_qtyop"};
378 } elsif ($f_op eq '=/=') {
382 } elsif ($f_op eq '<') {
386 } elsif ($f_op eq '<=') {
390 } elsif (($f_op eq '>') || ($f_op eq '>=')) {
397 push @sub_where, qq|cvar.number_value $op ?|;
398 push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
400 } elsif ($config->{type} eq 'bool') {
401 next unless ($params{filter}->{$name});
403 $not = 'NOT' if ($params{filter}->{$name} eq 'no');
404 push @sub_where, qq|COALESCE(cvar.bool_value, false) = TRUE|;
405 } elsif (any { $config->{type} eq $_ } qw(customer vendor)) {
406 next unless $params{filter}->{$name};
408 my $table = $config->{type};
409 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM $table WHERE name ILIKE ?)|;
410 push @sub_values, "%$params{filter}->{$name}%";
411 } elsif ($config->{type} eq 'part') {
412 next unless $params{filter}->{$name};
414 push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM parts WHERE partnumber ILIKE ?)|;
415 push @sub_values, "%$params{filter}->{$name}%";
419 add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
424 FROM custom_variables cvar
425 LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
426 WHERE (cvarcfg.module = ?)
428 AND (cvar.trans_id = $params{trans_id_field})
429 AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
430 push @values, $params{module}, conv_i($config->{id}), @sub_values;
434 my $query = join ' AND ', @where;
436 $main::lxdebug->leave_sub();
438 return ($query, @values);
441 sub add_custom_variables_to_report {
442 $main::lxdebug->enter_sub();
447 Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
449 my $myconfig = \%main::myconfig;
450 my $form = $main::form;
451 my $locale = $main::locale;
453 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
455 my $configs = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
457 if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
458 $main::lxdebug->leave_sub();
462 # allow sub_module to be a coderef or a fixed value
463 if (ref $params{sub_module} ne 'CODE') {
464 my $sub_module = "$params{sub_module}";
465 $params{sub_module} = sub { $sub_module };
468 my %cfg_map = map { $_->{id} => $_ } @{ $configs };
469 my @cfg_ids = keys %cfg_map;
472 qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
473 FROM custom_variables
474 WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
476 AND (sub_module = ?)|;
477 my $sth = prepare_query($form, $dbh, $query);
479 foreach my $row (@{ $params{data} }) {
480 do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
482 while (my $ref = $sth->fetchrow_hashref()) {
483 my $cfg = $cfg_map{$ref->{config_id}};
485 $row->{"cvar_$cfg->{name}"} =
486 $cfg->{type} eq 'date' ? $ref->{date_value}
487 : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
488 : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
489 : $cfg->{type} eq 'customer' ? (SL::DB::Manager::Customer->find_by(id => 1*$ref->{number_value}) || SL::DB::Customer->new)->name
490 : $cfg->{type} eq 'vendor' ? (SL::DB::Manager::Vendor->find_by(id => 1*$ref->{number_value}) || SL::DB::Vendor->new)->name
491 : $cfg->{type} eq 'part' ? (SL::DB::Manager::Part->find_by(id => 1*$ref->{number_value}) || SL::DB::Part->new)->partnumber
492 : $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
493 : $ref->{text_value};
499 $main::lxdebug->leave_sub();
502 sub get_field_format_list {
503 $main::lxdebug->enter_sub();
508 Common::check_params(\%params, qw(module));
510 my $myconfig = \%main::myconfig;
511 my $form = $main::form;
513 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
515 my $configs = $self->get_configs(%params);
517 my $date_fields = [];
518 my $number_fields = {};
520 foreach my $config (@{ $configs }) {
521 my $name = "$params{prefix}cvar_$config->{name}";
523 if ($config->{type} eq 'date') {
524 push @{ $date_fields }, $name;
526 } elsif ($config->{type} eq 'number') {
527 $number_fields->{$config->{precision}} ||= [];
528 push @{ $number_fields->{$config->{precision}} }, $name;
532 $main::lxdebug->leave_sub();
534 return ($date_fields, $number_fields);
537 sub save_custom_variables_validity {
538 $main::lxdebug->enter_sub();
543 Common::check_params(\%params, qw(config_id trans_id validity));
545 my $myconfig = \%main::myconfig;
546 my $form = $main::form;
548 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
550 my (@where, @values);
551 add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
552 add_token(\@where, \@values, col => "trans_id", val => $params{trans_id}, esc => \&conv_i);
554 my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
555 my $query = qq|DELETE FROM custom_variables_validity $where|;
557 do_query($form, $dbh, $query, @values);
560 qq|INSERT INTO custom_variables_validity (config_id, trans_id)
562 my $sth = prepare_query($form, $dbh, $query);
564 unless ($params{validity}) {
565 foreach my $config_id (listify($params{config_id})) {
566 foreach my $trans_id (listify($params{trans_id})) {
567 do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
574 $dbh->commit() unless $params{dbh};
576 $main::lxdebug->leave_sub();
579 sub get_custom_variables_validity {
580 $main::lxdebug->enter_sub(2);
585 Common::check_params(\%params, qw(config_id trans_id));
587 my $myconfig = \%main::myconfig;
588 my $form = $main::form;
590 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
592 my $query = qq|SELECT id FROM custom_variables_validity WHERE config_id = ? AND trans_id = ? LIMIT 1|;
594 my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
596 $main::lxdebug->leave_sub(2);
601 sub custom_variables_validity_by_trans_id {
602 $main::lxdebug->enter_sub(2);
607 return sub { 0 } unless $params{trans_id};
609 my $myconfig = \%main::myconfig;
610 my $form = $main::form;
612 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
614 my $query = qq|SELECT DISTINCT config_id FROM custom_variables_validity WHERE trans_id = ?|;
616 my %invalids = map { +($_->{config_id} => 1) } selectall_hashref_query($form, $dbh, $query, $params{trans_id});
618 $main::lxdebug->leave_sub(2);
620 return sub { !$invalids{+shift} };
624 my ($self, $value, $config) = @_;
626 return $::form->parse_amount(\%::myconfig, $value) if $config->{type} eq 'number';
627 return DateTime->from_lxoffice($value) if $config->{type} eq 'date';
628 return !ref $value ? SL::DB::Manager::Customer->find_by(id => $value * 1) : $value if $config->{type} eq 'customer';
629 return !ref $value ? SL::DB::Manager::Vendor->find_by(id => $value * 1) : $value if $config->{type} eq 'vendor';
630 return !ref $value ? SL::DB::Manager::Part->find_by(id => $value * 1) : $value if $config->{type} eq 'part';
634 sub format_to_template {
635 my ($self, $value, $config) = @_;
636 # stupid template expects everything formated. except objects
637 # do not use outside of print routines for legacy templates
639 return $::form->format_amount(\%::myconfig, $value) if $config->{type} eq 'number';
640 return $value->to_lxoffice if $config->{type} eq 'date' && blessed $value && $value->can('to_lxoffice');
650 SL::CVar.pm - Custom Variables module
654 # dealing with configs
656 my $all_configs = CVar->get_configs()
658 # dealing with custom vars
660 CVar->get_custom_variables(module => 'ic')
664 Suppose the following scenario:
666 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.
668 Validity is assumed. If you modify validity, you actually save B<invalidity>.
669 Invalidity is saved as a function of config_id, and the trans_id
671 In the naive way, disable an attribute for a specific id (simple)