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