CustomVariable RDBO: falsches Dispatching für Boolean-Variablen gefixt
[kivitendo-erp.git] / SL / DB / CustomVariable.pm
1 # This file has been auto-generated only because it didn't exist.
2 # Feel free to modify it at will; it will not be overwritten automatically.
3
4 package SL::DB::CustomVariable;
5
6 use strict;
7 use SL::DB::MetaSetup::CustomVariable;
8
9 __PACKAGE__->meta->initialize;
10
11 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
12 __PACKAGE__->meta->make_manager_class;
13
14 sub value {
15   my $self = $_[0];
16   my $type = $self->config->type;
17
18   goto &bool_value      if $type eq 'bool';
19   goto &timestamp_value if $type eq 'timestamp';
20   goto &number_value    if $type eq 'number';
21
22   if ( $_[1] && ($type eq 'customer' || $type eq 'vendor' || $type eq 'part') ) {
23     $self->number_value($_[1]);
24   }
25
26   if ( $type eq 'customer' ) {
27     require SL::DB::Customer;
28
29     my $id = int($self->number_value);
30     return $id ? SL::DB::Customer->new(id => $id)->load() : 0;
31   } elsif ( $type eq 'vendor' ) {
32     require SL::DB::Vendor;
33
34     my $id = int($self->number_value);
35     return $id ? SL::DB::Vendor->new(id => $id)->load() : 0;
36   } elsif ( $type eq 'part' ) {
37     require SL::DB::Part;
38
39     my $id = int($self->number_value);
40     return $id ? SL::DB::Part->new(id => $id)->load() : 0;
41   }
42
43   goto &text_value; # text, textfield, date and select
44 }
45
46 sub value_as_text {
47   my $self = $_[0];
48   my $type = $self->config->type;
49
50   die 'not an accessor' if @_ > 1;
51
52   if ($type eq 'boolean') {
53     return $self->bool_value ? $::locale->text('Yes') : $::locale->text('No');
54   } elsif ($type eq 'timestamp') {
55     return $::locale->reformat_date( { dateformat => 'yy-mm-dd' }, $self->timestamp_value->ymd, $::myconfig{dateformat});
56   } elsif ($type eq 'number') {
57     return $::form->format_amount(\%::myconfig, $self->number_value, $self->config->processed_options->{PRECISION});
58   } elsif ( $type eq 'customer' ) {
59     require SL::DB::Customer;
60
61     my $id = int($self->number_value);
62     my $customer =  $id ? SL::DB::Customer->new(id => $id)->load() : 0;
63     return $customer ? $customer->name : '';
64   } elsif ( $type eq 'vendor' ) {
65     require SL::DB::Vendor;
66
67     my $id = int($self->number_value);
68     return $id ? SL::DB::Vendor->new(id => $id)->load() : 0;
69   } elsif ( $type eq 'part' ) {
70     require SL::DB::Part;
71
72     my $id = int($self->number_value);
73     my $part = $id ? SL::DB::Part->new(id => $id)->load() : 0;
74     return $part ? $part->description : '';
75   }
76
77   goto &text_value; # text, textfield, date and select
78 }
79
80 sub is_valid {
81   my ($self) = @_;
82
83   require SL::DB::CustomVariableValidity;
84
85   my $query = [config_id => $self->config_id, trans_id => $self->trans_id];
86   return SL::DB::Manager::CustomVariableValidity->get_all_count(query => $query) == 0;
87 }
88
89 1;