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;
8 use List::MoreUtils qw(any);
10 use SL::DB::MetaSetup::CustomVariable;
12 __PACKAGE__->meta->initialize;
14 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
15 __PACKAGE__->meta->make_manager_class;
18 my ($self, $new) = @_;
20 $self->{__unparsed_value} = $new;
26 return $self->config if defined $self->{config};
27 return undef if !defined $self->config_id;
30 return $::request->cache('config_by_id')->{$self->config_id} //= SL::DB::CustomVariableConfig->new(id => $self->config_id)->load;
35 my $type = $self->_ensure_config->type;
37 return unless exists $self->{__unparsed_value};
39 my $unparsed = delete $self->{__unparsed_value};
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
47 if ($type =~ m{^(?:bool)}) {
48 return $self->bool_value(defined($unparsed) ? !!$unparsed : undef);
51 if ($type =~ m{^(?:date|timestamp)}) {
52 return $self->timestamp_value(!defined($unparsed) ? undef : ref($unparsed) eq 'DateTime' ? $unparsed->clone : DateTime->from_kivitendo($unparsed));
55 # text, textfield, select
56 $self->text_value($unparsed);
61 my $type = $self->_ensure_config->type;
64 $self->unparsed_value($_[1]);
69 goto &bool_value if $type eq 'bool';
70 goto ×tamp_value if $type eq 'timestamp';
72 if ($type eq 'number') {
73 return defined($self->number_value) ? $self->number_value * 1 : undef;
76 if ( $type eq 'customer' ) {
77 require SL::DB::Customer;
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;
84 my $id = int($self->number_value);
85 return $id ? SL::DB::Vendor->new(id => $id)->load() : undef;
86 } elsif ( $type eq 'part' ) {
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;
95 goto &text_value; # text, textfield and select
100 my $cfg = $self->_ensure_config;
101 my $type = $cfg->type;
103 die 'not an accessor' if @_ > 1;
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 : '';
119 goto &text_value; # text, textfield and select
125 require SL::DB::CustomVariableValidity;
127 # only base level custom variables can be invalid. ovverloaded ones could potentially clash on trans_id, so disallow them
128 return 1 if $self->sub_module;
130 $self->{is_valid} //= do {
131 my $query = [config_id => $self->config_id, trans_id => $self->trans_id];
132 (SL::DB::Manager::CustomVariableValidity->get_all_count(query => $query) == 0) ? 1 : 0;