+sub unparsed_value {
+ my ($self, $new) = @_;
+
+ $self->{__unparsed_value} = $new;
+}
+
+sub _ensure_config {
+ my ($self) = @_;
+
+ return $self->config if defined $self->{config};
+ return undef if !defined $self->config_id;
+
+ no warnings 'once';
+ return $::request->cache('config_by_id')->{$self->config_id} //= SL::DB::CustomVariableConfig->new(id => $self->config_id)->load;
+}
+
+sub parse_value {
+ my ($self) = @_;
+ my $type = $self->_ensure_config->type;
+
+ return unless exists $self->{__unparsed_value};
+
+ my $unparsed = delete $self->{__unparsed_value};
+
+ if ($type =~ m{^(?:customer|vendor|part|number)}) {
+ return $self->number_value(!defined($unparsed) ? undef
+ : (any { ref($unparsed) eq $_ } qw(SL::DB::Customer SL::DB::Vendor SL::DB::Part)) ? $unparsed->id * 1
+ : $unparsed * 1);
+ }
+
+ if ($type =~ m{^(?:bool)}) {
+ return $self->bool_value(defined($unparsed) ? !!$unparsed : undef);
+ }
+
+ if ($type =~ m{^(?:date|timestamp)}) {
+ return $self->timestamp_value(!defined($unparsed) ? undef : ref($unparsed) eq 'DateTime' ? $unparsed->clone : DateTime->from_kivitendo($unparsed));
+ }
+
+ # text, textfield, htmlfield and select
+ $self->text_value($unparsed);
+}
+