CVars für Kunden
[kivitendo-erp.git] / SL / CVar.pm
1 package CVar;
2
3 use strict;
4
5 use List::Util qw(first);
6 use Data::Dumper;
7
8 use SL::DBUtils;
9 use SL::MoreCommon qw(listify);
10
11 sub get_configs {
12   $main::lxdebug->enter_sub();
13
14   my $self     = shift;
15   my %params   = @_;
16
17   my $myconfig = \%main::myconfig;
18   my $form     = $main::form;
19
20   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
21
22   my ($where, @values);
23   if ($params{module}) {
24     $where = 'WHERE module = ?';
25     push @values, $params{module};
26   }
27
28   my $query    = <<SQL;
29     SELECT *, date_trunc('seconds', localtimestamp) AS current_timestamp
30     FROM custom_variable_configs $where ORDER BY sortkey
31 SQL
32
33   $::form->{CVAR_CONFIGS} = {} unless 'HASH' eq ref $::form->{CVAR_CONFIGS};
34   if (!$::form->{CVAR_CONFIGS}->{$params{module}}) {
35     my $configs  = selectall_hashref_query($form, $dbh, $query, @values);
36
37     foreach my $config (@{ $configs }) {
38       if ($config->{type} eq 'select') {
39         $config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
40
41       } elsif ($config->{type} eq 'number') {
42         $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
43
44       }
45
46       $self->_unpack_flags($config);
47     }
48     $::form->{CVAR_CONFIGS}->{$params{module}} = $configs;
49   }
50
51   $main::lxdebug->leave_sub();
52
53   return $::form->{CVAR_CONFIGS}->{$params{module}};
54 }
55
56 sub get_config {
57   $main::lxdebug->enter_sub();
58
59   my $self     = shift;
60   my %params   = @_;
61
62   Common::check_params(\%params, qw(id));
63
64   my $myconfig = \%main::myconfig;
65   my $form     = $main::form;
66
67   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
68
69   my $query    = qq|SELECT * FROM custom_variable_configs WHERE id = ?|;
70
71   my $config   = selectfirst_hashref_query($form, $dbh, $query, conv_i($params{id})) || { };
72
73   $self->_unpack_flags($config);
74
75   $main::lxdebug->leave_sub();
76
77   return $config;
78 }
79
80 sub _unpack_flags {
81   $main::lxdebug->enter_sub();
82
83   my $self   = shift;
84   my $config = shift;
85
86   foreach my $flag (split m/:/, $config->{flags}) {
87     if ($flag =~ m/(.*?)=(.*)/) {
88       $config->{"flag_${1}"}    = $2;
89     } else {
90       $config->{"flag_${flag}"} = 1;
91     }
92   }
93
94   $main::lxdebug->leave_sub();
95 }
96
97 sub save_config {
98   $main::lxdebug->enter_sub();
99
100   my $self     = shift;
101   my %params   = @_;
102
103   Common::check_params(\%params, qw(module config));
104
105   my $myconfig = \%main::myconfig;
106   my $form     = $main::form;
107
108   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
109
110   my $q_id     = qq|SELECT nextval('custom_variable_configs_id')|;
111   my $h_id     = prepare_query($form, $dbh, $q_id);
112
113   my $q_new    =
114     qq|INSERT INTO custom_variable_configs (name, description, type, default_value, options, searchable, includeable, included_by_default, module, flags, id, sortkey)
115        VALUES                              (?,    ?,           ?,    ?,             ?,       ?,          ?,           ?,                   ?,      ?,     ?,
116          (SELECT COALESCE(MAX(sortkey) + 1, 1) FROM custom_variable_configs))|;
117   my $h_new    = prepare_query($form, $dbh, $q_new);
118
119   my $q_update =
120     qq|UPDATE custom_variable_configs SET
121          name        = ?, description         = ?,
122          type        = ?, default_value       = ?,
123          options     = ?, searchable          = ?,
124          includeable = ?, included_by_default = ?,
125          module      = ?, flags               = ?
126        WHERE id  = ?|;
127   my $h_update = prepare_query($form, $dbh, $q_update);
128
129   my @configs;
130   if ('ARRAY' eq ref $params{config}) {
131     @configs = @{ $params{config} };
132   } else {
133     @configs = ($params{config});
134   }
135
136   foreach my $config (@configs) {
137     my ($h_actual, $q_actual);
138
139     if (!$config->{id}) {
140       do_statement($form, $h_id, $q_id);
141       ($config->{id}) = $h_id->fetchrow_array();
142
143       $h_actual       = $h_new;
144       $q_actual       = $q_new;
145
146     } else {
147       $h_actual       = $h_update;
148       $q_actual       = $q_update;
149     }
150
151     do_statement($form, $h_actual, $q_actual, @{$config}{qw(name description type default_value options)},
152                  $config->{searchable} ? 't' : 'f', $config->{includeable} ? 't' : 'f', $config->{included_by_default} ? 't' : 'f',
153                  $params{module}, $config->{flags}, conv_i($config->{id}));
154   }
155
156   $h_id->finish();
157   $h_new->finish();
158   $h_update->finish();
159
160   $dbh->commit();
161
162   $main::lxdebug->leave_sub();
163 }
164
165 sub delete_config {
166   $main::lxdebug->enter_sub();
167
168   my $self     = shift;
169   my %params   = @_;
170
171   Common::check_params(\%params, qw(id));
172
173   my $myconfig = \%main::myconfig;
174   my $form     = $main::form;
175
176   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
177
178   do_query($form, $dbh, qq|DELETE FROM custom_variables        WHERE config_id = ?|, conv_i($params{id}));
179   do_query($form, $dbh, qq|DELETE FROM custom_variable_configs WHERE id        = ?|, conv_i($params{id}));
180
181   $dbh->commit();
182
183   $main::lxdebug->leave_sub();
184 }
185
186 sub get_custom_variables {
187   $main::lxdebug->enter_sub();
188
189   my $self     = shift;
190   my %params   = @_;
191
192   Common::check_params(\%params, qw(module));
193
194   my $myconfig = \%main::myconfig;
195   my $form     = $main::form;
196
197   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
198
199   my $trans_id = $params{trans_id} ? 'OR (v.trans_id = ?) ' : '';
200
201   my $q_var    =
202     qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value
203        FROM custom_variables
204        WHERE (config_id = ?) AND (trans_id = ?)|;
205   $q_var      .= qq| AND (sub_module = ?)| if $params{sub_module};
206   my $h_var    = prepare_query($form, $dbh, $q_var);
207
208   my $custom_variables = $self->get_configs(module => $params{module});
209
210   foreach my $cvar (@{ $custom_variables }) {
211     if ($cvar->{type} eq 'textfield') {
212       $cvar->{width}  = 30;
213       $cvar->{height} =  5;
214
215       $cvar->{width}  = $1 if ($cvar->{options} =~ m/width=(\d+)/i);
216       $cvar->{height} = $1 if ($cvar->{options} =~ m/height=(\d+)/i);
217
218     } elsif ($cvar->{type} eq 'text') {
219       $cvar->{maxlength} = $1 if ($cvar->{options} =~ m/maxlength=(\d+)/i);
220
221     } elsif ($cvar->{type} eq 'number') {
222       $cvar->{precision} = $1 if ($cvar->{options} =~ m/precision=(\d+)/i);
223
224     } elsif ($cvar->{type} eq 'select') {
225       $cvar->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $cvar->{options}) ];
226     }
227
228     my ($act_var, $valid);
229     if ($params{trans_id}) {
230       my @values = (conv_i($cvar->{id}), conv_i($params{trans_id}));
231       push @values, $params{sub_module} if $params{sub_module};
232
233       do_statement($form, $h_var, $q_var, @values);
234       $act_var = $h_var->fetchrow_hashref();
235
236       $valid = $self->get_custom_variables_validity(config_id => $cvar->{id}, trans_id => $params{trans_id});
237     }
238
239     if ($act_var) {
240       $cvar->{value} = $cvar->{type} eq 'date'      ? $act_var->{date_value}
241                      : $cvar->{type} eq 'timestamp' ? $act_var->{timestamp_value}
242                      : $cvar->{type} eq 'number'    ? $act_var->{number_value}
243                      : $cvar->{type} eq 'customer'  ? $act_var->{number_value}
244                      : $cvar->{type} eq 'bool'      ? $act_var->{bool_value}
245                      :                                $act_var->{text_value};
246       $cvar->{valid} = $valid;
247     } else {
248       $cvar->{valid}  =  1;
249
250       if ($cvar->{type} eq 'date') {
251         if ($cvar->{default_value} eq 'NOW') {
252           $cvar->{value} = $cvar->{current_date};
253         } else {
254           $cvar->{value} = $cvar->{default_value};
255         }
256
257       } elsif ($cvar->{type} eq 'timestamp') {
258         if ($cvar->{default_value} eq 'NOW') {
259           $cvar->{value} = $cvar->{current_timestamp};
260         } else {
261           $cvar->{value} = $cvar->{default_value};
262         }
263
264       } elsif ($cvar->{type} eq 'bool') {
265         $cvar->{value} = $cvar->{default_value} * 1;
266
267       } elsif ($cvar->{type} eq 'number') {
268         $cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');
269
270       } else {
271         $cvar->{value} = $cvar->{default_value};
272       }
273     }
274
275     if ($cvar->{type} eq 'number') {
276       $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
277     } elsif ($cvar->{type} eq 'customer') {
278       require SL::DB::Customer;
279       $cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1);
280     }
281   }
282
283   $h_var->finish();
284
285   $main::lxdebug->leave_sub();
286
287   return $custom_variables;
288 }
289
290 sub save_custom_variables {
291   $main::lxdebug->enter_sub();
292
293   my $self     = shift;
294   my %params   = @_;
295
296   Common::check_params(\%params, qw(module trans_id variables));
297
298   my $myconfig = \%main::myconfig;
299   my $form     = $main::form;
300
301   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
302
303   my @configs  = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
304
305   my $query    =
306     qq|DELETE FROM custom_variables
307        WHERE (trans_id  = ?)
308          AND (config_id IN (SELECT DISTINCT id
309                             FROM custom_variable_configs
310                             WHERE module = ?))|;
311   my @values   = (conv_i($params{trans_id}), $params{module});
312
313   if ($params{sub_module}) {
314     $query .= qq| AND (sub_module = ?)|;
315     push @values, $params{sub_module};
316   }
317
318   do_query($form, $dbh, $query, @values);
319
320   $query  =
321     qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
322        VALUES                       (?,         ?,          ?,        ?,          ?,               ?,          ?)|;
323   my $sth = prepare_query($form, $dbh, $query);
324
325   foreach my $config (@configs) {
326     my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
327
328     my $value  = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
329
330     if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
331       push @values, undef, undef, $value, undef;
332
333     } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
334       push @values, undef, conv_date($value), undef, undef;
335
336     } elsif ($config->{type} eq 'number') {
337       push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));
338
339     } elsif ($config->{type} eq 'bool') {
340       push @values, $value ? 't' : 'f', undef, undef, undef;
341     } elsif ($config->{type} eq 'customer') {
342       push @values, undef, undef, undef, $value * 1;
343     }
344
345     do_statement($form, $sth, $query, @values);
346
347     if ($params{save_validity}) {
348       my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
349       $self->save_custom_variables_validity(trans_id  => $params{trans_id},
350                                             config_id => $config->{id},
351                                             validity  => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
352                                            );
353     }
354   }
355
356   $sth->finish();
357
358   $dbh->commit();
359
360   $main::lxdebug->leave_sub();
361 }
362
363 sub render_inputs {
364   $main::lxdebug->enter_sub(2);
365
366   my $self     = shift;
367   my %params   = @_;
368
369   Common::check_params(\%params, qw(variables));
370
371   my $myconfig = \%main::myconfig;
372   my $form     = $main::form;
373
374   my %options  = ( name_prefix       => "$params{name_prefix}",
375                    name_postfix      => "$params{name_postfix}",
376                    hide_non_editable => $params{hide_non_editable},
377                    show_disabled_message => $params{show_disabled_message},
378                  );
379
380   foreach my $var (@{ $params{variables} }) {
381     $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs',     { var => $var, %options });
382     $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
383   }
384
385   $main::lxdebug->leave_sub(2);
386 }
387
388 sub render_search_options {
389   $main::lxdebug->enter_sub();
390
391   my $self     = shift;
392   my %params   = @_;
393
394   Common::check_params(\%params, qw(variables));
395
396   my $myconfig = \%main::myconfig;
397   my $form     = $main::form;
398
399   $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
400
401   $params{include_prefix}   = 'l_' unless defined($params{include_prefix});
402   $params{include_value}  ||= '1';
403
404   my $filter  = $form->parse_html_template('amcvar/search_filter',  \%params);
405   my $include = $form->parse_html_template('amcvar/search_include', \%params);
406
407   $main::lxdebug->leave_sub();
408
409   return ($filter, $include);
410 }
411
412 sub build_filter_query {
413   $main::lxdebug->enter_sub();
414
415   my $self     = shift;
416   my %params   = @_;
417
418   Common::check_params(\%params, qw(module trans_id_field filter));
419
420   my $myconfig = \%main::myconfig;
421   my $form     = $main::form;
422
423   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
424
425   my $configs  = $self->get_configs(%params);
426
427   my (@where, @values);
428
429   foreach my $config (@{ $configs }) {
430     next unless ($config->{searchable});
431
432     my $name = "cvar_$config->{name}";
433
434     my (@sub_values, @sub_where, $not);
435
436     if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
437       next unless ($params{filter}->{$name});
438
439       push @sub_where,  qq|cvar.text_value ILIKE ?|;
440       push @sub_values, '%' . $params{filter}->{$name} . '%'
441
442     } elsif ($config->{type} eq 'select') {
443       next unless ($params{filter}->{$name});
444
445       push @sub_where,  qq|cvar.text_value = ?|;
446       push @sub_values, $params{filter}->{$name};
447
448     } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
449       my $name_from = "${name}_from";
450       my $name_to   = "${name}_to";
451
452       if ($params{filter}->{$name_from}) {
453         push @sub_where,  qq|cvar.timestamp_value >= ?|;
454         push @sub_values, conv_date($params{filter}->{$name_from});
455       }
456
457       if ($params{filter}->{$name_to}) {
458         push @sub_where,  qq|cvar.timestamp_value <= ?|;
459         push @sub_values, conv_date($params{filter}->{$name_to});
460       }
461
462     } elsif ($config->{type} eq 'number') {
463       next if ($params{filter}->{$name} eq '');
464
465       my $f_op = $params{filter}->{"${name}_qtyop"};
466
467       my $op;
468       if ($f_op eq '==') {
469         $op  = '=';
470
471       } elsif ($f_op eq '=/=') {
472         $not = 'NOT';
473         $op  = '<>';
474
475       } elsif ($f_op eq '<') {
476         $not = 'NOT';
477         $op  = '>=';
478
479       } elsif ($f_op eq '<=') {
480         $not = 'NOT';
481         $op  = '>';
482
483       } elsif (($f_op eq '>') || ($f_op eq '>=')) {
484         $op  = $f_op;
485
486       } else {
487         $op  = '=';
488       }
489
490       push @sub_where,  qq|cvar.number_value $op ?|;
491       push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
492
493     } elsif ($config->{type} eq 'bool') {
494       next unless ($params{filter}->{$name});
495
496       $not = 'NOT' if ($params{filter}->{$name} eq 'no');
497       push @sub_where,  qq|COALESCE(cvar.bool_value, false) = TRUE|;
498     } elsif ($config->{type} eq 'customer') {
499       next unless $params{filter}->{$name};
500
501       push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM customer WHERE name ILIKE ?)|;
502       push @sub_values, "%$params{filter}->{$name}%";
503     }
504
505     if (@sub_where) {
506       add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
507
508       push @where,
509         qq|$not EXISTS(
510              SELECT cvar.id
511              FROM custom_variables cvar
512              LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
513              WHERE (cvarcfg.module = ?)
514                AND (cvarcfg.id     = ?)
515                AND (cvar.trans_id  = $params{trans_id_field})
516                AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
517       push @values, $params{module}, conv_i($config->{id}), @sub_values;
518     }
519   }
520
521   my $query = join ' AND ', @where;
522
523   $main::lxdebug->leave_sub();
524
525   return ($query, @values);
526 }
527
528 sub add_custom_variables_to_report {
529   $main::lxdebug->enter_sub();
530
531   my $self      = shift;
532   my %params    = @_;
533
534   Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
535
536   my $myconfig  = \%main::myconfig;
537   my $form      = $main::form;
538   my $locale    = $main::locale;
539
540   my $dbh       = $params{dbh} || $form->get_standard_dbh($myconfig);
541
542   my $configs   = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
543
544   if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
545     $main::lxdebug->leave_sub();
546     return;
547   }
548
549   # allow sub_module to be a coderef or a fixed value
550   if (ref $params{sub_module} ne 'CODE') {
551     my $sub_module = "$params{sub_module}";
552     $params{sub_module} = sub { $sub_module };
553   }
554
555   my %cfg_map   = map { $_->{id} => $_ } @{ $configs };
556   my @cfg_ids   = keys %cfg_map;
557
558   my $query     =
559     qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
560        FROM custom_variables
561        WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
562          AND (trans_id = ?)
563          AND (sub_module = ?)|;
564   my $sth       = prepare_query($form, $dbh, $query);
565
566   foreach my $row (@{ $params{data} }) {
567     do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
568
569     while (my $ref = $sth->fetchrow_hashref()) {
570       my $cfg = $cfg_map{$ref->{config_id}};
571
572       $row->{"cvar_$cfg->{name}"} =
573           $cfg->{type} eq 'date'      ? $ref->{date_value}
574         : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
575         : $cfg->{type} eq 'number'    ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
576         : $cfg->{type} eq 'customer'  ? SL::DB::Manager::Customer->find_by(id => 1* $ref->{number_value})->name
577         : $cfg->{type} eq 'bool'      ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
578         :                               $ref->{text_value};
579     }
580   }
581
582   $sth->finish();
583
584   $main::lxdebug->leave_sub();
585 }
586
587 sub get_field_format_list {
588   $main::lxdebug->enter_sub();
589
590   my $self          = shift;
591   my %params        = @_;
592
593   Common::check_params(\%params, qw(module));
594
595   my $myconfig      = \%main::myconfig;
596   my $form          = $main::form;
597
598   my $dbh           = $params{dbh} || $form->get_standard_dbh($myconfig);
599
600   my $configs       = $self->get_configs(%params);
601
602   my $date_fields   = [];
603   my $number_fields = {};
604
605   foreach my $config (@{ $configs }) {
606     my $name = "$params{prefix}cvar_$config->{name}";
607
608     if ($config->{type} eq 'date') {
609       push @{ $date_fields }, $name;
610
611     } elsif ($config->{type} eq 'number') {
612       $number_fields->{$config->{precision}} ||= [];
613       push @{ $number_fields->{$config->{precision}} }, $name;
614     }
615   }
616
617   $main::lxdebug->leave_sub();
618
619   return ($date_fields, $number_fields);
620 }
621
622 sub save_custom_variables_validity {
623   $main::lxdebug->enter_sub();
624
625   my $self     = shift;
626   my %params   = @_;
627
628   Common::check_params(\%params, qw(config_id trans_id validity));
629
630   my $myconfig = \%main::myconfig;
631   my $form     = $main::form;
632
633   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
634
635   my (@where, @values);
636   add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
637   add_token(\@where, \@values, col => "trans_id",  val => $params{trans_id},  esc => \&conv_i);
638
639   my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
640   my $query = qq|DELETE FROM custom_variables_validity $where|;
641
642   do_query($form, $dbh, $query, @values);
643
644   $query  =
645     qq|INSERT INTO custom_variables_validity (config_id, trans_id)
646        VALUES                                (?,         ?       )|;
647   my $sth = prepare_query($form, $dbh, $query);
648
649   unless ($params{validity}) {
650     foreach my $config_id (listify $params{config_id}) {
651       foreach my $trans_id (listify $params{trans_id}) {
652         do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
653       }
654     }
655   }
656
657   $sth->finish();
658
659   $dbh->commit();
660
661   $main::lxdebug->leave_sub();
662 }
663
664 sub get_custom_variables_validity {
665   $main::lxdebug->enter_sub(2);
666
667   my $self     = shift;
668   my %params   = @_;
669
670   Common::check_params(\%params, qw(config_id trans_id));
671
672   my $myconfig = \%main::myconfig;
673   my $form     = $main::form;
674
675   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
676
677   my $query    = qq|SELECT COUNT(*) FROM custom_variables_validity WHERE config_id = ? AND trans_id = ?|;
678
679   my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
680
681   $main::lxdebug->leave_sub(2);
682
683   return !$invalid;
684 }
685
686 sub custom_variables_validity_by_trans_id {
687   $main::lxdebug->enter_sub(2);
688
689   my $self     = shift;
690   my %params   = @_;
691
692   return sub { 0 } unless $params{trans_id};
693
694   my $myconfig = \%main::myconfig;
695   my $form     = $main::form;
696
697   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
698
699   my $query    = qq|SELECT config_id, COUNT(*) FROM custom_variables_validity WHERE trans_id = ? GROUP BY config_id|;
700
701   my %invalids = selectall_as_map($form, $dbh, $query, 'config_id', 'count', $params{trans_id});
702
703   $main::lxdebug->leave_sub(2);
704
705   return sub { !$invalids{+shift} };
706 }
707
708 1;
709
710 __END__
711
712 =head1 NAME
713
714 SL::CVar.pm - Custom Variables module
715
716 =head1 SYNOPSIS
717
718   # dealing with configs
719
720   my $all_configs = CVar->get_configs()
721   my $config      = CVar->get_config(id => '1234')
722
723   CVar->save_config($config);
724   CVar->delete->config($config)
725
726   # dealing with custom vars
727
728   CVar->get_custom_variables(module => 'ic')
729
730 =head2 VALIDITY
731
732 Suppose the following scenario:
733
734 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.
735
736 Validity is assumed. If you modify validity, you actually save B<invalidity>.
737 Invalidity is saved as a function of config_id, and the trans_id
738
739 In the naive way, disable an attribute for a specific id (simple)
740
741 =cut