From 96717bed8061efaa4d626d5da34122ef0cb3e8d2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Fri, 13 Jun 2014 20:10:46 +0200 Subject: [PATCH] PriceTaxCalculator: chart->taxkey lookup vorberechnen --- SL/DB/Chart.pm | 6 ++++++ SL/DB/Helper/PriceTaxCalculator.pm | 2 ++ SL/DB/Manager/Chart.pm | 23 +++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/SL/DB/Chart.pm b/SL/DB/Chart.pm index 718d42251..db8a3b945 100644 --- a/SL/DB/Chart.pm +++ b/SL/DB/Chart.pm @@ -16,6 +16,12 @@ __PACKAGE__->meta->initialize; sub get_active_taxkey { my ($self, $date) = @_; $date ||= DateTime->today_local; + + my $cache = $::request->{cache}{chart}{$date}; + if ($cache->{$self->id}) { + return $cache->{$self->id}; + } + require SL::DB::TaxKey; return SL::DB::Manager::TaxKey->get_all(query => [ and => [ chart_id => $self->id, startdate => { le => $date } ] ], diff --git a/SL/DB/Helper/PriceTaxCalculator.pm b/SL/DB/Helper/PriceTaxCalculator.pm index d95d14309..673bb248a 100644 --- a/SL/DB/Helper/PriceTaxCalculator.pm +++ b/SL/DB/Helper/PriceTaxCalculator.pm @@ -37,6 +37,8 @@ sub calculate_prices_and_taxes { $self->netamount( 0); $self->marge_total(0); + SL::DB::Manager::Chart->cache_taxkeys(date => $self->transdate); + my $idx = 0; foreach my $item ($self->items) { $idx++; diff --git a/SL/DB/Manager/Chart.pm b/SL/DB/Manager/Chart.pm index a0167fd23..81a65b5e8 100644 --- a/SL/DB/Manager/Chart.pm +++ b/SL/DB/Manager/Chart.pm @@ -6,6 +6,8 @@ use SL::DB::Helper::Manager; use base qw(SL::DB::Helper::Manager); use SL::DB::Helper::Sorted; +use DateTime; +use SL::DBUtils; sub object_class { 'SL::DB::Chart' } @@ -20,6 +22,27 @@ sub link_filter { link => { like => "\%:${link}:\%" } ]); } +sub cache_taxkeys { + my ($self, %params) = @_; + + my $date = $params{date} || DateTime->today; + my $cache = $::request->{cache}{chart}{$date} ||= {}; + + require SL::DB::TaxKey; + my $tks = SL::DB::Manager::TaxKey->get_all; + my %tks_by_id = map { $_->id => $_ } @$tks; + + my $rows = selectall_hashref_query($::form, $::form->get_standard_dbh, <<"", $date); + SELECT DISTINCT ON (chart_id) chart_id, startdate, id + FROM taxkeys + WHERE startdate < ? + ORDER BY chart_id, startdate DESC; + + for (@$rows) { + $cache->{$_->{chart_id}} = $tks_by_id{$_->{id}}; + } +} + 1; __END__ -- 2.20.1