SL::DB::Part: Cachen von Objekten gefixt Teil 2
[kivitendo-erp.git] / SL / DB / Chart.pm
1 package SL::DB::Chart;
2
3 use strict;
4
5 use SL::DB::MetaSetup::Chart;
6 use SL::DB::Manager::Chart;
7
8 __PACKAGE__->meta->add_relationships(taxkeys => { type         => 'one to many',
9                                                   class        => 'SL::DB::TaxKey',
10                                                   column_map   => { id => 'chart_id' },
11                                                 },
12                                     );
13
14 __PACKAGE__->meta->initialize;
15
16 sub get_active_taxkey {
17   my ($self, $date) = @_;
18   $date ||= DateTime->today_local;
19
20   my $cache = $::request->cache("get_active_taxkey")->{$date} //= {};
21   return $cache->{$self->id} if $cache->{$self->id};
22
23   require SL::DB::TaxKey;
24   return $cache->{$self->id} = SL::DB::Manager::TaxKey->get_all(
25     query   => [ and => [ chart_id  => $self->id,
26                           startdate => { le => $date } ] ],
27     sort_by => "startdate DESC")->[0];
28 }
29
30 1;
31
32 __END__
33
34 =pod
35
36 =encoding utf8
37
38 =head1 NAME
39
40 SL::DB::Chart - Rose database model for the "chart" table
41
42 =head1 FUNCTIONS
43
44 =over 4
45
46 =item C<get_active_taxkey $date>
47
48 Returns the active tax key object for a given date. C<$date> defaults
49 to the current date if undefined.
50
51 =back
52
53 =head1 BUGS
54
55 Nothing here yet.
56
57 =head1 AUTHOR
58
59 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
60
61 =cut