Waren und Lieferanten als benutzerdefinierte Variablen hinzugefĆ¼gt.
[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(any 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 'vendor'    ? $act_var->{number_value}
247                      : $cvar->{type} eq 'part'      ? $act_var->{number_value}
248                      : $cvar->{type} eq 'bool'      ? $act_var->{bool_value}
249                      :                                $act_var->{text_value};
250       $cvar->{valid} = $valid;
251     } else {
252       $cvar->{valid}  =  1;
253
254       if ($cvar->{type} eq 'date') {
255         if ($cvar->{default_value} eq 'NOW') {
256           $cvar->{value} = $cvar->{current_date};
257         } else {
258           $cvar->{value} = $cvar->{default_value};
259         }
260
261       } elsif ($cvar->{type} eq 'timestamp') {
262         if ($cvar->{default_value} eq 'NOW') {
263           $cvar->{value} = $cvar->{current_timestamp};
264         } else {
265           $cvar->{value} = $cvar->{default_value};
266         }
267
268       } elsif ($cvar->{type} eq 'bool') {
269         $cvar->{value} = $cvar->{default_value} * 1;
270
271       } elsif ($cvar->{type} eq 'number') {
272         $cvar->{value} = $cvar->{default_value} * 1 if ($cvar->{default_value} ne '');
273
274       } else {
275         $cvar->{value} = $cvar->{default_value};
276       }
277     }
278
279     if ($cvar->{type} eq 'number') {
280       $cvar->{value} = $form->format_amount($myconfig, $cvar->{value} * 1, $cvar->{precision});
281     } elsif ($cvar->{type} eq 'customer') {
282       require SL::DB::Customer;
283       $cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1);
284     } elsif ($cvar->{type} eq 'vendor') {
285       require SL::DB::Vendor;
286       $cvar->{value} = SL::DB::Manager::Vendor->find_by(id => $cvar->{value} * 1);
287     } elsif ($cvar->{type} eq 'part') {
288       require SL::DB::Part;
289       $cvar->{value} = SL::DB::Manager::Part->find_by(id => $cvar->{value} * 1);
290     }
291   }
292
293   $h_var->finish();
294
295   $main::lxdebug->leave_sub();
296
297   return $custom_variables;
298 }
299
300 sub save_custom_variables {
301   $main::lxdebug->enter_sub();
302
303   my $self     = shift;
304   my %params   = @_;
305
306   Common::check_params(\%params, qw(module trans_id variables));
307
308   my $myconfig = \%main::myconfig;
309   my $form     = $main::form;
310
311   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
312
313   my @configs  = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
314
315   my $query    =
316     qq|DELETE FROM custom_variables
317        WHERE (trans_id  = ?)
318          AND (config_id IN (SELECT DISTINCT id
319                             FROM custom_variable_configs
320                             WHERE module = ?))|;
321   my @values   = (conv_i($params{trans_id}), $params{module});
322
323   if ($params{sub_module}) {
324     $query .= qq| AND (sub_module = ?)|;
325     push @values, $params{sub_module};
326   }
327
328   do_query($form, $dbh, $query, @values);
329
330   $query  =
331     qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value)
332        VALUES                       (?,         ?,          ?,        ?,          ?,               ?,          ?)|;
333   my $sth = prepare_query($form, $dbh, $query);
334
335   foreach my $config (@configs) {
336     my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
337
338     my $value  = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
339
340     if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
341       push @values, undef, undef, $value, undef;
342
343     } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
344       push @values, undef, conv_date($value), undef, undef;
345
346     } elsif ($config->{type} eq 'number') {
347       push @values, undef, undef, undef, conv_i($form->parse_amount($myconfig, $value));
348
349     } elsif ($config->{type} eq 'bool') {
350       push @values, $value ? 't' : 'f', undef, undef, undef;
351     } elsif (any { $config->{type} eq $_ } qw(customer vendor part)) {
352       push @values, undef, undef, undef, $value * 1;
353     }
354
355     do_statement($form, $sth, $query, @values);
356
357     if ($params{save_validity}) {
358       my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
359       $self->save_custom_variables_validity(trans_id  => $params{trans_id},
360                                             config_id => $config->{id},
361                                             validity  => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
362                                            );
363     }
364   }
365
366   $sth->finish();
367
368   $dbh->commit() unless $params{dbh};
369
370   $main::lxdebug->leave_sub();
371 }
372
373 sub render_inputs {
374   $main::lxdebug->enter_sub(2);
375
376   my $self     = shift;
377   my %params   = @_;
378
379   Common::check_params(\%params, qw(variables));
380
381   my $myconfig = \%main::myconfig;
382   my $form     = $main::form;
383
384   my %options  = ( name_prefix       => "$params{name_prefix}",
385                    name_postfix      => "$params{name_postfix}",
386                    hide_non_editable => $params{hide_non_editable},
387                    show_disabled_message => $params{show_disabled_message},
388                  );
389
390   foreach my $var (@{ $params{variables} }) {
391     $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs',     { var => $var, %options });
392     $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options });
393   }
394
395   $main::lxdebug->leave_sub(2);
396 }
397
398 sub render_search_options {
399   $main::lxdebug->enter_sub();
400
401   my $self     = shift;
402   my %params   = @_;
403
404   Common::check_params(\%params, qw(variables));
405
406   my $myconfig = \%main::myconfig;
407   my $form     = $main::form;
408
409   $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options};
410
411   $params{include_prefix}   = 'l_' unless defined($params{include_prefix});
412   $params{include_value}  ||= '1';
413   $params{filter_prefix}  ||= '';
414
415   my $filter  = $form->parse_html_template('amcvar/search_filter',  \%params);
416   my $include = $form->parse_html_template('amcvar/search_include', \%params);
417
418   $main::lxdebug->leave_sub();
419
420   return ($filter, $include);
421 }
422
423 sub build_filter_query {
424   $main::lxdebug->enter_sub();
425
426   my $self     = shift;
427   my %params   = @_;
428
429   Common::check_params(\%params, qw(module trans_id_field filter));
430
431   my $myconfig = \%main::myconfig;
432   my $form     = $main::form;
433
434   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
435
436   my $configs  = $self->get_configs(%params);
437
438   my (@where, @values);
439
440   foreach my $config (@{ $configs }) {
441     next unless ($config->{searchable});
442
443     my $name = "cvar_$config->{name}";
444
445     my (@sub_values, @sub_where, $not);
446
447     if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
448       next unless ($params{filter}->{$name});
449
450       push @sub_where,  qq|cvar.text_value ILIKE ?|;
451       push @sub_values, '%' . $params{filter}->{$name} . '%'
452
453     } elsif ($config->{type} eq 'select') {
454       next unless ($params{filter}->{$name});
455
456       push @sub_where,  qq|cvar.text_value = ?|;
457       push @sub_values, $params{filter}->{$name};
458
459     } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
460       my $name_from = "${name}_from";
461       my $name_to   = "${name}_to";
462
463       if ($params{filter}->{$name_from}) {
464         push @sub_where,  qq|cvar.timestamp_value >= ?|;
465         push @sub_values, conv_date($params{filter}->{$name_from});
466       }
467
468       if ($params{filter}->{$name_to}) {
469         push @sub_where,  qq|cvar.timestamp_value <= ?|;
470         push @sub_values, conv_date($params{filter}->{$name_to});
471       }
472
473     } elsif ($config->{type} eq 'number') {
474       next if ($params{filter}->{$name} eq '');
475
476       my $f_op = $params{filter}->{"${name}_qtyop"};
477
478       my $op;
479       if ($f_op eq '==') {
480         $op  = '=';
481
482       } elsif ($f_op eq '=/=') {
483         $not = 'NOT';
484         $op  = '<>';
485
486       } elsif ($f_op eq '<') {
487         $not = 'NOT';
488         $op  = '>=';
489
490       } elsif ($f_op eq '<=') {
491         $not = 'NOT';
492         $op  = '>';
493
494       } elsif (($f_op eq '>') || ($f_op eq '>=')) {
495         $op  = $f_op;
496
497       } else {
498         $op  = '=';
499       }
500
501       push @sub_where,  qq|cvar.number_value $op ?|;
502       push @sub_values, $form->parse_amount($myconfig, $params{filter}->{$name});
503
504     } elsif ($config->{type} eq 'bool') {
505       next unless ($params{filter}->{$name});
506
507       $not = 'NOT' if ($params{filter}->{$name} eq 'no');
508       push @sub_where,  qq|COALESCE(cvar.bool_value, false) = TRUE|;
509     } elsif (any { $config->{type} eq $_ } qw(customer vendor part)) {
510       next unless $params{filter}->{$name};
511
512       my $table = $config->{type};
513       push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM $table WHERE name ILIKE ?)|;
514       push @sub_values, "%$params{filter}->{$name}%";
515     } elsif ($config->{type} eq 'part') {
516       next unless $params{filter}->{$name};
517
518       push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM parts WHERE partnumber ILIKE ?)|;
519       push @sub_values, "%$params{filter}->{$name}%";
520     }
521
522     if (@sub_where) {
523       add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
524
525       push @where,
526         qq|$not EXISTS(
527              SELECT cvar.id
528              FROM custom_variables cvar
529              LEFT JOIN custom_variable_configs cvarcfg ON (cvar.config_id = cvarcfg.id)
530              WHERE (cvarcfg.module = ?)
531                AND (cvarcfg.id     = ?)
532                AND (cvar.trans_id  = $params{trans_id_field})
533                AND | . join(' AND ', map { "($_)" } @sub_where) . qq|)|;
534       push @values, $params{module}, conv_i($config->{id}), @sub_values;
535     }
536   }
537
538   my $query = join ' AND ', @where;
539
540   $main::lxdebug->leave_sub();
541
542   return ($query, @values);
543 }
544
545 sub add_custom_variables_to_report {
546   $main::lxdebug->enter_sub();
547
548   my $self      = shift;
549   my %params    = @_;
550
551   Common::check_params(\%params, qw(module trans_id_field column_defs data configs));
552
553   my $myconfig  = \%main::myconfig;
554   my $form      = $main::form;
555   my $locale    = $main::locale;
556
557   my $dbh       = $params{dbh} || $form->get_standard_dbh($myconfig);
558
559   my $configs   = [ grep { $_->{includeable} && $params{column_defs}->{"cvar_$_->{name}"}->{visible} } @{ $params{configs} } ];
560
561   if (!scalar(@{ $params{data} }) || ! scalar(@{ $configs })) {
562     $main::lxdebug->leave_sub();
563     return;
564   }
565
566   # allow sub_module to be a coderef or a fixed value
567   if (ref $params{sub_module} ne 'CODE') {
568     my $sub_module = "$params{sub_module}";
569     $params{sub_module} = sub { $sub_module };
570   }
571
572   my %cfg_map   = map { $_->{id} => $_ } @{ $configs };
573   my @cfg_ids   = keys %cfg_map;
574
575   my $query     =
576     qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id
577        FROM custom_variables
578        WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|))
579          AND (trans_id = ?)
580          AND (sub_module = ?)|;
581   my $sth       = prepare_query($form, $dbh, $query);
582
583   foreach my $row (@{ $params{data} }) {
584     do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row));
585
586     while (my $ref = $sth->fetchrow_hashref()) {
587       my $cfg = $cfg_map{$ref->{config_id}};
588
589       $row->{"cvar_$cfg->{name}"} =
590           $cfg->{type} eq 'date'      ? $ref->{date_value}
591         : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value}
592         : $cfg->{type} eq 'number'    ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision})
593         : $cfg->{type} eq 'customer'  ? (SL::DB::Manager::Customer->find_by(id => 1*$ref->{number_value}) || SL::DB::Customer->new)->name
594         : $cfg->{type} eq 'vendor'    ? (SL::DB::Manager::Vendor->find_by(id => 1*$ref->{number_value})   || SL::DB::Vendor->new)->name
595         : $cfg->{type} eq 'part'      ? (SL::DB::Manager::Part->find_by(id => 1*$ref->{number_value})     || SL::DB::Part->new)->partnumber
596         : $cfg->{type} eq 'bool'      ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
597         :                               $ref->{text_value};
598     }
599   }
600
601   $sth->finish();
602
603   $main::lxdebug->leave_sub();
604 }
605
606 sub get_field_format_list {
607   $main::lxdebug->enter_sub();
608
609   my $self          = shift;
610   my %params        = @_;
611
612   Common::check_params(\%params, qw(module));
613
614   my $myconfig      = \%main::myconfig;
615   my $form          = $main::form;
616
617   my $dbh           = $params{dbh} || $form->get_standard_dbh($myconfig);
618
619   my $configs       = $self->get_configs(%params);
620
621   my $date_fields   = [];
622   my $number_fields = {};
623
624   foreach my $config (@{ $configs }) {
625     my $name = "$params{prefix}cvar_$config->{name}";
626
627     if ($config->{type} eq 'date') {
628       push @{ $date_fields }, $name;
629
630     } elsif ($config->{type} eq 'number') {
631       $number_fields->{$config->{precision}} ||= [];
632       push @{ $number_fields->{$config->{precision}} }, $name;
633     }
634   }
635
636   $main::lxdebug->leave_sub();
637
638   return ($date_fields, $number_fields);
639 }
640
641 sub save_custom_variables_validity {
642   $main::lxdebug->enter_sub();
643
644   my $self     = shift;
645   my %params   = @_;
646
647   Common::check_params(\%params, qw(config_id trans_id validity));
648
649   my $myconfig = \%main::myconfig;
650   my $form     = $main::form;
651
652   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
653
654   my (@where, @values);
655   add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
656   add_token(\@where, \@values, col => "trans_id",  val => $params{trans_id},  esc => \&conv_i);
657
658   my $where = scalar @where ? "WHERE " . join ' AND ', @where : '';
659   my $query = qq|DELETE FROM custom_variables_validity $where|;
660
661   do_query($form, $dbh, $query, @values);
662
663   $query  =
664     qq|INSERT INTO custom_variables_validity (config_id, trans_id)
665        VALUES                                (?,         ?       )|;
666   my $sth = prepare_query($form, $dbh, $query);
667
668   unless ($params{validity}) {
669     foreach my $config_id (listify $params{config_id}) {
670       foreach my $trans_id (listify $params{trans_id}) {
671         do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id));
672       }
673     }
674   }
675
676   $sth->finish();
677
678   $dbh->commit() unless $params{dbh};
679
680   $main::lxdebug->leave_sub();
681 }
682
683 sub get_custom_variables_validity {
684   $main::lxdebug->enter_sub(2);
685
686   my $self     = shift;
687   my %params   = @_;
688
689   Common::check_params(\%params, qw(config_id trans_id));
690
691   my $myconfig = \%main::myconfig;
692   my $form     = $main::form;
693
694   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
695
696   my $query    = qq|SELECT COUNT(*) FROM custom_variables_validity WHERE config_id = ? AND trans_id = ?|;
697
698   my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id}));
699
700   $main::lxdebug->leave_sub(2);
701
702   return !$invalid;
703 }
704
705 sub custom_variables_validity_by_trans_id {
706   $main::lxdebug->enter_sub(2);
707
708   my $self     = shift;
709   my %params   = @_;
710
711   return sub { 0 } unless $params{trans_id};
712
713   my $myconfig = \%main::myconfig;
714   my $form     = $main::form;
715
716   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
717
718   my $query    = qq|SELECT config_id, COUNT(*) FROM custom_variables_validity WHERE trans_id = ? GROUP BY config_id|;
719
720   my %invalids = selectall_as_map($form, $dbh, $query, 'config_id', 'count', $params{trans_id});
721
722   $main::lxdebug->leave_sub(2);
723
724   return sub { !$invalids{+shift} };
725 }
726
727 sub parse {
728   my ($self, $value, $config) = @_;
729
730   return $::form->parse_amount(\%::myconfig, $value)          if $config->{type} eq 'number';
731   return DateTime->from_lxoffice($value)                      if $config->{type} eq 'date';
732   return !ref $value ? SL::DB::Manager::Customer->find_by(id => $value * 1) : $value  if $config->{type} eq 'customer';
733   return $value;
734 }
735
736 sub format_to_template {
737   my ($self, $value, $config) = @_;
738   # stupid template expects everything formated. except objects
739   # do not use outside of print routines for legacy templates
740
741   return $::form->format_amount(\%::myconfig, $value) if $config->{type} eq 'number';
742   return $value->to_lxoffice if $config->{type} eq 'date' && blessed $value && $value->can('to_lxoffice');
743   return $value;
744 }
745
746 1;
747
748 __END__
749
750 =head1 NAME
751
752 SL::CVar.pm - Custom Variables module
753
754 =head1 SYNOPSIS
755
756   # dealing with configs
757
758   my $all_configs = CVar->get_configs()
759   my $config      = CVar->get_config(id => '1234')
760
761   CVar->save_config($config);
762   CVar->delete->config($config)
763
764   # dealing with custom vars
765
766   CVar->get_custom_variables(module => 'ic')
767
768 =head2 VALIDITY
769
770 Suppose the following scenario:
771
772 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.
773
774 Validity is assumed. If you modify validity, you actually save B<invalidity>.
775 Invalidity is saved as a function of config_id, and the trans_id
776
777 In the naive way, disable an attribute for a specific id (simple)
778
779 =cut