filter_prefix für CVar->render_search_options
[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() unless $params{dbh};
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   $params{filter_prefix}  ||= '';
406
407   my $filter  = $form->parse_html_template('amcvar/search_filter',  \%params);
408   my $include = $form->parse_html_template('amcvar/search_include', \%params);
409
410   $main::lxdebug->leave_sub();
411
412   return ($filter, $include);
413 }
414
415 sub build_filter_query {
416   $main::lxdebug->enter_sub();
417
418   my $self     = shift;
419   my %params   = @_;
420
421   Common::check_params(\%params, qw(module trans_id_field filter));
422
423   my $myconfig = \%main::myconfig;
424   my $form     = $main::form;
425
426   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
427
428   my $configs  = $self->get_configs(%params);
429
430   my (@where, @values);
431
432   foreach my $config (@{ $configs }) {
433     next unless ($config->{searchable});
434
435     my $name = "cvar_$config->{name}";
436
437     my (@sub_values, @sub_where, $not);
438
439     if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
440       next unless ($params{filter}->{$name});
441
442       push @sub_where,  qq|cvar.text_value ILIKE ?|;
443       push @sub_values, '%' . $params{filter}->{$name} . '%'
444
445     } elsif ($config->{type} eq 'select') {
446       next unless ($params{filter}->{$name});
447
448       push @sub_where,  qq|cvar.text_value = ?|;
449       push @sub_values, $params{filter}->{$name};
450
451     } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
452       my $name_from = "${name}_from";
453       my $name_to   = "${name}_to";
454
455       if ($params{filter}->{$name_from}) {
456         push @sub_where,  qq|cvar.timestamp_value >= ?|;
457         push @sub_values, conv_date($params{filter}->{$name_from});
458       }
459
460       if ($params{filter}->{$name_to}) {
461         push @sub_where,  qq|cvar.timestamp_value <= ?|;
462         push @sub_values, conv_date($params{filter}->{$name_to});
463       }
464
465     } elsif ($config->{type} eq 'number') {
466       next if ($params{filter}->{$name} eq '');
467
468       my $f_op = $params{filter}->{"${name}_qtyop"};
469
470       my $op;
471       if ($f_op eq '==') {
472         $op  = '=';
473
474       } elsif ($f_op eq '=/=') {
475         $not = 'NOT';
476         $op  = '<>';
477
478       } elsif ($f_op eq '<') {
479         $not = 'NOT';
480         $op  = '>=';
481
482       } elsif ($f_op eq '<=') {
483         $not = 'NOT';
484         $op  = '>';
485
486       } elsif (($f_op eq '>') || ($f_op eq '>=')) {
487         $op  = $f_op;
488
489       } else {
490         $op  = '=';
491       }
492
493       push @sub_where,  qq|cvar.number_value $op ?|;
494       push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
495
496     } elsif ($config->{type} eq 'bool') {
497       next unless ($params{filter}->{$name});
498
499       $not = 'NOT' if ($params{filter}->{$name} eq 'no');
500       push @sub_where,  qq|COALESCE(cvar.bool_value, false) = TRUE|;
501     } elsif ($config->{type} eq 'customer') {
502       next unless $params{filter}->{$name};
503
504       push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM customer WHERE name ILIKE ?)|;
505       push @sub_values, "%$params{filter}->{$name}%";
506     }
507
508     if (@sub_where) {
509       add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
510
511       push @where,
512         qq|$not EXISTS(
513              SELECT cvar.id
514              FROM custom_variables cvar
515              LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
516              WHERE (cvarcfg.module = ?)
517                AND (cvarcfg.id     = ?)
518                AND (cvar.trans_id  = $params{trans_id_field})
519                AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
520       push @values, $params{module}, conv_i($config->{id}), @sub_values;
521     }
522   }
523
524   my $query = join ' AND ', @where;
525
526   $main::lxdebug->leave_sub();
527
528   return ($query, @values);
529 }
530
531 sub add_custom_variables_to_report {
532   $main::lxdebug->enter_sub();
533
534   my $self      = shift;
535   my %params    = @_;
536
537   Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
538
539   my $myconfig  = \%main::myconfig;
540   my $form      = $main::form;
541   my $locale    = $main::locale;
542
543   my $dbh       = $params{dbh} || $form->get_standard_dbh($myconfig);
544
545   my $configs   = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
546
547   if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
548     $main::lxdebug->leave_sub();
549     return;
550   }
551
552   # allow sub_module to be a coderef or a fixed value
553   if (ref $params{sub_module} ne 'CODE') {
554     my $sub_module = "$params{sub_module}";
555     $params{sub_module} = sub { $sub_module };
556   }
557
558   my %cfg_map   = map { $_->{id} => $_ } @{ $configs };
559   my @cfg_ids   = keys %cfg_map;
560
561   my $query     =
562     qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
563        FROM custom_variables
564        WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
565          AND (trans_id = ?)
566          AND (sub_module = ?)|;
567   my $sth       = prepare_query($form, $dbh, $query);
568
569   foreach my $row (@{ $params{data} }) {
570     do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
571
572     while (my $ref = $sth->fetchrow_hashref()) {
573       my $cfg = $cfg_map{$ref->{config_id}};
574
575       $row->{"cvar_$cfg->{name}"} =
576           $cfg->{type} eq 'date'      ? $ref->{date_value}
577         : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
578         : $cfg->{type} eq 'number'    ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
579         : $cfg->{type} eq 'customer'  ? (SL::DB::Manager::Customer->find_by(id => 1*$ref->{number_value}) || SL::DB::Customer->new)->name
580         : $cfg->{type} eq 'bool'      ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
581         :                               $ref->{text_value};
582     }
583   }
584
585   $sth->finish();
586
587   $main::lxdebug->leave_sub();
588 }
589
590 sub get_field_format_list {
591   $main::lxdebug->enter_sub();
592
593   my $self          = shift;
594   my %params        = @_;
595
596   Common::check_params(\%params, qw(module));
597
598   my $myconfig      = \%main::myconfig;
599   my $form          = $main::form;
600
601   my $dbh           = $params{dbh} || $form->get_standard_dbh($myconfig);
602
603   my $configs       = $self->get_configs(%params);
604
605   my $date_fields   = [];
606   my $number_fields = {};
607
608   foreach my $config (@{ $configs }) {
609     my $name = "$params{prefix}cvar_$config->{name}";
610
611     if ($config->{type} eq 'date') {
612       push @{ $date_fields }, $name;
613
614     } elsif ($config->{type} eq 'number') {
615       $number_fields->{$config->{precision}} ||= [];
616       push @{ $number_fields->{$config->{precision}} }, $name;
617     }
618   }
619
620   $main::lxdebug->leave_sub();
621
622   return ($date_fields, $number_fields);
623 }
624
625 sub save_custom_variables_validity {
626   $main::lxdebug->enter_sub();
627
628   my $self     = shift;
629   my %params   = @_;
630
631   Common::check_params(\%params, qw(config_id trans_id validity));
632
633   my $myconfig = \%main::myconfig;
634   my $form     = $main::form;
635
636   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
637
638   my (@where, @values);
639   add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
640   add_token(\@where, \@values, col => "trans_id",  val => $params{trans_id},  esc => \&conv_i);
641
642   my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
643   my $query = qq|DELETE FROM custom_variables_validity $where|;
644
645   do_query($form, $dbh, $query, @values);
646
647   $query  =
648     qq|INSERT INTO custom_variables_validity (config_id, trans_id)
649        VALUES                                (?,         ?       )|;
650   my $sth = prepare_query($form, $dbh, $query);
651
652   unless ($params{validity}) {
653     foreach my $config_id (listify $params{config_id}) {
654       foreach my $trans_id (listify $params{trans_id}) {
655         do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
656       }
657     }
658   }
659
660   $sth->finish();
661
662   $dbh->commit() unless $params{dbh};
663
664   $main::lxdebug->leave_sub();
665 }
666
667 sub get_custom_variables_validity {
668   $main::lxdebug->enter_sub(2);
669
670   my $self     = shift;
671   my %params   = @_;
672
673   Common::check_params(\%params, qw(config_id trans_id));
674
675   my $myconfig = \%main::myconfig;
676   my $form     = $main::form;
677
678   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
679
680   my $query    = qq|SELECT COUNT(*) FROM custom_variables_validity WHERE config_id = ? AND trans_id = ?|;
681
682   my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
683
684   $main::lxdebug->leave_sub(2);
685
686   return !$invalid;
687 }
688
689 sub custom_variables_validity_by_trans_id {
690   $main::lxdebug->enter_sub(2);
691
692   my $self     = shift;
693   my %params   = @_;
694
695   return sub { 0 } unless $params{trans_id};
696
697   my $myconfig = \%main::myconfig;
698   my $form     = $main::form;
699
700   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
701
702   my $query    = qq|SELECT config_id, COUNT(*) FROM custom_variables_validity WHERE trans_id = ? GROUP BY config_id|;
703
704   my %invalids = selectall_as_map($form, $dbh, $query, 'config_id', 'count', $params{trans_id});
705
706   $main::lxdebug->leave_sub(2);
707
708   return sub { !$invalids{+shift} };
709 }
710
711 sub parse {
712   my ($self, $value, $config) = @_;
713
714   return $::form->parse_amount(\%::myconfig, $value)          if $config->{type} eq 'number';
715   return DateTime->from_lxoffice($value)                      if $config->{type} eq 'date';
716   return !ref $value ? SL::DB::Manager::Customer->find_by(id => $value * 1) : $value  if $config->{type} eq 'customer';
717   return $value;
718 }
719
720 sub format_to_template {
721   my ($self, $value, $config) = @_;
722   # stupid template expects everything formated. except objects
723   # do not use outside of print routines for legacy templates
724
725   return $::form->format_amount(\%::myconfig, $value) if $config->{type} eq 'number';
726   return $value->to_lxoffice if $config->{type} eq 'date' && blessed $value && $value->can('to_lxoffice');
727   return $value;
728 }
729
730 1;
731
732 __END__
733
734 =head1 NAME
735
736 SL::CVar.pm - Custom Variables module
737
738 =head1 SYNOPSIS
739
740   # dealing with configs
741
742   my $all_configs = CVar->get_configs()
743   my $config      = CVar->get_config(id => '1234')
744
745   CVar->save_config($config);
746   CVar->delete->config($config)
747
748   # dealing with custom vars
749
750   CVar->get_custom_variables(module => 'ic')
751
752 =head2 VALIDITY
753
754 Suppose the following scenario:
755
756 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.
757
758 Validity is assumed. If you modify validity, you actually save B<invalidity>.
759 Invalidity is saved as a function of config_id, and the trans_id
760
761 In the naive way, disable an attribute for a specific id (simple)
762
763 =cut