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