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