From: Moritz Bunkus Date: Tue, 15 Oct 2013 15:49:55 +0000 (+0200) Subject: SL::DB::PeriodicInvoicesConfig: Datumsberechnung aus Backgroundjob verschoben X-Git-Tag: release-3.2.0beta~476^2~12 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=fde528b67be0e4375fdee44b2cadbfb7e0adadbd;p=kivitendo-erp.git SL::DB::PeriodicInvoicesConfig: Datumsberechnung aus Backgroundjob verschoben --- diff --git a/SL/BackgroundJob/CreatePeriodicInvoices.pm b/SL/BackgroundJob/CreatePeriodicInvoices.pm index ccb0b27f0..dc740cc30 100644 --- a/SL/BackgroundJob/CreatePeriodicInvoices.pm +++ b/SL/BackgroundJob/CreatePeriodicInvoices.pm @@ -188,27 +188,8 @@ sub _create_periodic_invoice { } sub _calculate_dates { - my $config = shift; - - my $cur_date = $config->first_billing_date || $config->start_date; - my $start_date = $config->get_previous_invoice_date || DateTime->new(year => 1970, month => 1, day => 1); - my $end_date = $config->end_date || DateTime->new(year => 2100, month => 1, day => 1); - my $tomorrow = DateTime->today_local->add(days => 1); - my $period_len = $config->get_period_length; - - $end_date = $tomorrow if $end_date > $tomorrow; - - my @dates; - - while (1) { - last if $cur_date >= $end_date; - - push @dates, $cur_date->clone if $cur_date > $start_date; - - $cur_date->add(months => $period_len); - } - - return @dates; + my ($config) = @_; + return $config->calculate_invoice_dates(end_date => DateTime->today_local->add(days => 1)); } sub _send_email { diff --git a/SL/DB/PeriodicInvoicesConfig.pm b/SL/DB/PeriodicInvoicesConfig.pm index 7480c3a5f..7270c8f42 100644 --- a/SL/DB/PeriodicInvoicesConfig.pm +++ b/SL/DB/PeriodicInvoicesConfig.pm @@ -4,6 +4,8 @@ use strict; use SL::DB::MetaSetup::PeriodicInvoicesConfig; +use List::Util qw(max min); + __PACKAGE__->meta->initialize; # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all. @@ -74,4 +76,26 @@ SQL return ref $max_transdate ? $max_transdate : $self->db->parse_date($max_transdate); } +sub calculate_invoice_dates { + my ($self, %params) = @_; + + my $cur_date = $self->first_billing_date || $self->start_date; + my $start_date = $self->get_previous_invoice_date || DateTime->new(year => 1970, month => 1, day => 1); + my $end_date = $self->end_date || DateTime->today_local->add(years => 10); + my $period_len = $self->get_period_length; + + $start_date = max($start_date, $params{start_date}) if $params{start_date}; + $end_date = min($end_date, $params{end_date}) if $params{end_date}; + + my @dates; + + while ($cur_date < $end_date) { + push @dates, $cur_date->clone if $cur_date > $start_date; + + $cur_date->add(months => $period_len); + } + + return @dates; +} + 1;