Projekt

Allgemein

Profil

Herunterladen (2,79 KB) Statistiken
| Zweig: | Markierung: | Revision:
250fa402 Moritz Bunkus
package SL::DB::PeriodicInvoicesConfig;

use strict;

use SL::DB::MetaSetup::PeriodicInvoicesConfig;

fde528b6 Moritz Bunkus
use List::Util qw(max min);

2d7e4203 Sven Schöling
__PACKAGE__->meta->initialize;

250fa402 Moritz Bunkus
# Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
__PACKAGE__->meta->make_manager_class;

6df0942b Moritz Bunkus
our @PERIODICITIES = qw(m q f b y);
our %PERIOD_LENGTHS = ( m => 1, q => 3, f => 4, b => 6, y => 12 );
47d35d06 Moritz Bunkus
sub get_period_length {
my $self = shift;
return $PERIOD_LENGTHS{ $self->periodicity } || 1;
}

sub _log_msg {
$::lxdebug->message(LXDebug->DEBUG1(), join('', @_));
}

sub handle_automatic_extension {
my $self = shift;

_log_msg("HAE for " . $self->id . "\n");
# Don't extend configs that have been terminated. There's nothing to
# extend if there's no end date.
return if $self->terminated || !$self->end_date;

my $today = DateTime->now_local;
my $end_date = $self->end_date;

_log_msg("today $today end_date $end_date\n");

# The end date has not been reached yet, therefore no extension is
# needed.
return if $today <= $end_date;

# The end date has been reached. If no automatic extension has been
# set then terminate the config and return.
if (!$self->extend_automatically_by) {
_log_msg("setting inactive\n");
$self->active(0);
$self->save;
return;
}

# Add the automatic extension period to the new end date as long as
# the new end date is in the past. Then save it and get out.
$end_date->add(months => $self->extend_automatically_by) while $today > $end_date;
_log_msg("new end date $end_date\n");

$self->end_date($end_date);
$self->save;

return $end_date;
}

sub get_previous_invoice_date {
my $self = shift;

my $query = <<SQL;
SELECT MAX(ar.transdate)
FROM periodic_invoices
LEFT JOIN ar ON (ar.id = periodic_invoices.ar_id)
WHERE periodic_invoices.config_id = ?
SQL

my ($max_transdate) = $self->dbh->selectrow_array($query, undef, $self->id);

return undef unless $max_transdate;
return ref $max_transdate ? $max_transdate : $self->db->parse_date($max_transdate);
}

fde528b6 Moritz Bunkus
sub calculate_invoice_dates {
my ($self, %params) = @_;

24597232 Moritz Bunkus
my $period_len = $self->get_period_length;
180e8de7 Moritz Bunkus
my $cur_date = $self->first_billing_date || $self->start_date;
my $end_date = $self->end_date || DateTime->today_local->add(years => 10);
24597232 Moritz Bunkus
my $start_date = $params{past_dates} ? undef : $self->get_previous_invoice_date;
66f46ee9 Moritz Bunkus
$start_date = $start_date ? $start_date->subtract(days => 1) : $cur_date->clone;
fde528b6 Moritz Bunkus
$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;

24597232 Moritz Bunkus
while ($cur_date <= $end_date) {
66f46ee9 Moritz Bunkus
push @dates, $cur_date->clone if $cur_date >= $start_date;
fde528b6 Moritz Bunkus
$cur_date->add(months => $period_len);
}

return @dates;
}

250fa402 Moritz Bunkus
1;