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.
4 package SL::DB::CustomVariable;
7 use SL::DB::MetaSetup::CustomVariable;
9 __PACKAGE__->meta->initialize;
11 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
12 __PACKAGE__->meta->make_manager_class;
15 my ($self, $new) = @_;
17 $self->{__unparsed_value} = $new;
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 );
30 my $type = $self->_ensure_config->type;
32 return unless exists $self->{__unparsed_value};
34 my $unparsed = delete $self->{__unparsed_value};
36 if ($type =~ m{^(?:customer|vendor|part|bool|number)}) {
37 return $self->number_value(defined($unparsed) ? $unparsed * 1 : undef);
40 if ($type =~ m{^(?:date|timestamp)}) {
41 return $self->timestamp_value(defined($unparsed) ? DateTime->from_kivitendo($unparsed) : undef);
44 # text, textfield, select
45 $self->text_value($unparsed);
50 my $type = $self->_ensure_config->type;
53 $self->unparsed_value($_[1]);
57 goto &bool_value if $type eq 'bool';
58 goto ×tamp_value if $type eq 'timestamp';
59 goto &number_value if $type eq 'number';
61 if ( $type eq 'customer' ) {
62 require SL::DB::Customer;
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;
69 my $id = int($self->number_value);
70 return $id ? SL::DB::Vendor->new(id => $id)->load() : 0;
71 } elsif ( $type eq 'part' ) {
74 my $id = int($self->number_value);
75 return $id ? SL::DB::Part->new(id => $id)->load() : 0;
78 goto &text_value; # text, textfield, date and select
83 my $type = $self->config->type;
85 die 'not an accessor' if @_ > 1;
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;
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;
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;
107 my $id = int($self->number_value);
108 my $part = $id ? SL::DB::Part->new(id => $id)->load() : 0;
109 return $part ? $part->description : '';
112 goto &text_value; # text, textfield, date and select
118 require SL::DB::CustomVariableValidity;
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;