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  defined $self->{config};
 
  24   return undef         if !defined $self->config_id;
 
  27   return $::request->cache('config_by_id')->{$self->config_id} //= SL::DB::CustomVariableConfig->new(id => $self->config_id)->load;
 
  32   my $type   = $self->_ensure_config->type;
 
  34   return unless exists $self->{__unparsed_value};
 
  36   my $unparsed = delete $self->{__unparsed_value};
 
  38   if ($type =~ m{^(?:customer|vendor|part|bool|number)}) {
 
  39     return $self->number_value(defined($unparsed) ? $unparsed * 1 : undef);
 
  42   if ($type =~ m{^(?:date|timestamp)}) {
 
  43     return $self->timestamp_value(defined($unparsed) ? DateTime->from_kivitendo($unparsed) : undef);
 
  46   # text, textfield, select
 
  47   $self->text_value($unparsed);
 
  52   my $type = $self->_ensure_config->type;
 
  55     $self->unparsed_value($_[1]);
 
  59   goto &bool_value      if $type eq 'bool';
 
  60   goto ×tamp_value if $type eq 'timestamp';
 
  61   goto &number_value    if $type eq 'number';
 
  63   if ( $type eq 'customer' ) {
 
  64     require SL::DB::Customer;
 
  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;
 
  71     my $id = int($self->number_value);
 
  72     return $id ? SL::DB::Vendor->new(id => $id)->load() : undef;
 
  73   } elsif ( $type eq 'part' ) {
 
  76     my $id = int($self->number_value);
 
  77     return $id ? SL::DB::Part->new(id => $id)->load() : undef;
 
  80   goto &text_value; # text, textfield, date and select
 
  85   my $cfg  = $self->_ensure_config;
 
  86   my $type = $cfg->type;
 
  88   die 'not an accessor' if @_ > 1;
 
  90   if ($type eq 'boolean') {
 
  91     return $self->bool_value ? $::locale->text('Yes') : $::locale->text('No');
 
  92   } elsif ($type eq 'timestamp') {
 
  93     return $::locale->reformat_date( { dateformat => 'yy-mm-dd' }, $self->timestamp_value->ymd, $::myconfig{dateformat});
 
  94   } elsif ($type eq 'number') {
 
  95     return $::form->format_amount(\%::myconfig, $self->number_value, $cfg->processed_options->{PRECISION});
 
  96   } elsif ( $type eq 'customer' ) {
 
  97     require SL::DB::Customer;
 
  99     my $id = int($self->number_value);
 
 100     my $customer =  $id ? SL::DB::Customer->new(id => $id)->load() : 0;
 
 101     return $customer ? $customer->name : '';
 
 102   } elsif ( $type eq 'vendor' ) {
 
 103     require SL::DB::Vendor;
 
 105     my $id = int($self->number_value);
 
 106     my $vendor =  $id ? SL::DB::Vendor->new(id => $id)->load() : 0;
 
 107     return $vendor ? $vendor->name : '';
 
 108   } elsif ( $type eq 'part' ) {
 
 109     require SL::DB::Part;
 
 111     my $id = int($self->number_value);
 
 112     my $part = $id ? SL::DB::Part->new(id => $id)->load() : 0;
 
 113     return $part ? $part->description : '';
 
 116   goto &text_value; # text, textfield, date and select
 
 122   require SL::DB::CustomVariableValidity;
 
 124   my $query = [config_id => $self->config_id, trans_id => $self->trans_id];
 
 125   return (SL::DB::Manager::CustomVariableValidity->get_all_count(query => $query) == 0) ? 1 : 0;