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