X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/2d7e4203055156dccb2ca3fba82bbb32873ecd2e..eebe8e90991eacadb6fbd20a648c152017a620c7:/SL/DB/PeriodicInvoicesConfig.pm diff --git a/SL/DB/PeriodicInvoicesConfig.pm b/SL/DB/PeriodicInvoicesConfig.pm index 7480c3a5f..8013cd940 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. @@ -58,20 +60,43 @@ sub handle_automatic_extension { return $end_date; } -sub get_previous_invoice_date { +sub get_previous_billed_period_start_date { my $self = shift; my $query = <dbh->selectrow_array($query, undef, $self->id); + my ($date) = $self->dbh->selectrow_array($query, undef, $self->id); + + return undef unless $date; + return ref $date ? $date : $self->db->parse_date($date); +} + +sub calculate_invoice_dates { + my ($self, %params) = @_; + + my $period_len = $self->get_period_length; + my $cur_date = $self->first_billing_date || $self->start_date; + my $end_date = $self->terminated ? $self->end_date : undef; + $end_date //= DateTime->today_local->add(years => 100); + my $start_date = $params{past_dates} ? undef : $self->get_previous_billed_period_start_date; + $start_date = $start_date ? $start_date->add(days => 1) : $cur_date->clone; + + $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 undef unless $max_transdate; - return ref $max_transdate ? $max_transdate : $self->db->parse_date($max_transdate); + return @dates; } 1;