26ff748f782d735d7a8a24af443dc0565d6690d4
[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
8 use List::MoreUtils qw(any);
9
10 use SL::DB::MetaSetup::CustomVariable;
11
12 __PACKAGE__->meta->initialize;
13
14 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
15 __PACKAGE__->meta->make_manager_class;
16
17 sub unparsed_value {
18   my ($self, $new) = @_;
19
20   $self->{__unparsed_value} = $new;
21 }
22
23 sub _ensure_config {
24   my ($self) = @_;
25
26   return $self->config if  defined $self->{config};
27   return undef         if !defined $self->config_id;
28
29   no warnings 'once';
30   return $::request->cache('config_by_id')->{$self->config_id} //= SL::DB::CustomVariableConfig->new(id => $self->config_id)->load;
31 }
32
33 sub parse_value {
34   my ($self) = @_;
35   my $type   = $self->_ensure_config->type;
36
37   return unless exists $self->{__unparsed_value};
38
39   my $unparsed = delete $self->{__unparsed_value};
40
41   if ($type =~ m{^(?:customer|vendor|part|number)}) {
42     return $self->number_value(!defined($unparsed) ? undef
43                                : (any { ref($unparsed) eq $_ } qw(SL::DB::Customer SL::DB::Vendor SL::DB::Part)) ? $unparsed->id * 1
44                                : $unparsed * 1);
45   }
46
47   if ($type =~ m{^(?:bool)}) {
48     return $self->bool_value(defined($unparsed) ? !!$unparsed : undef);
49   }
50
51   if ($type =~ m{^(?:date|timestamp)}) {
52     return $self->timestamp_value(!defined($unparsed) ? undef : ref($unparsed) eq 'DateTime' ? $unparsed->clone : DateTime->from_kivitendo($unparsed));
53   }
54
55   # text, textfield, select
56   $self->text_value($unparsed);
57 }
58
59 sub value {
60   my $self = $_[0];
61   my $type = $self->_ensure_config->type;
62
63   if (scalar(@_) > 1) {
64     $self->unparsed_value($_[1]);
65     $self->parse_value;
66     @_ = ($self);
67   }
68
69   goto &bool_value      if $type eq 'bool';
70   goto &timestamp_value if $type eq 'timestamp';
71
72   if ($type eq 'number') {
73     return defined($self->number_value) ? $self->number_value * 1 : undef;
74   }
75
76   if ( $type eq 'customer' ) {
77     require SL::DB::Customer;
78
79     my $id = int($self->number_value);
80     return $id ? SL::DB::Customer->new(id => $id)->load() : undef;
81   } elsif ( $type eq 'vendor' ) {
82     require SL::DB::Vendor;
83
84     my $id = int($self->number_value);
85     return $id ? SL::DB::Vendor->new(id => $id)->load() : undef;
86   } elsif ( $type eq 'part' ) {
87     require SL::DB::Part;
88
89     my $id = int($self->number_value);
90     return $id ? SL::DB::Part->new(id => $id)->load() : undef;
91   } elsif ( $type eq 'date' ) {
92     return $self->timestamp_value ? $self->timestamp_value->clone->truncate(to => 'day') : undef;
93   }
94
95   goto &text_value; # text, textfield and select
96 }
97
98 sub value_as_text {
99   my $self = $_[0];
100   my $cfg  = $self->_ensure_config;
101   my $type = $cfg->type;
102
103   die 'not an accessor' if @_ > 1;
104
105   if ($type eq 'bool') {
106     return $self->bool_value ? $::locale->text('Yes') : $::locale->text('No');
107   } elsif ($type =~ m{^(?:timestamp|date)}) {
108     return '' if !$self->timestamp_value;
109     return $::locale->reformat_date( { dateformat => 'yy-mm-dd' }, $self->timestamp_value->ymd, $::myconfig{dateformat});
110   } elsif ($type eq 'number') {
111     return $::form->format_amount(\%::myconfig, $self->number_value, $cfg->processed_options->{PRECISION});
112   } elsif ( $type =~ m{^(?:customer|vendor|part)$}) {
113     my $class = "SL::DB::" . ucfirst($type);
114     eval "require $class";
115     my $object =  $class->_get_manager_class->find_by(id => int($self->number_value));
116     return $object ? $object->displayable_name : '';
117   }
118
119   goto &text_value; # text, textfield and select
120 }
121
122 sub is_valid {
123   my ($self) = @_;
124
125   require SL::DB::CustomVariableValidity;
126
127   my $query = [config_id => $self->config_id, trans_id => $self->trans_id];
128   return (SL::DB::Manager::CustomVariableValidity->get_all_count(query => $query) == 0) ? 1 : 0;
129 }
130
131 1;