X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FPeriodicInvoicesConfig.pm;h=d49595bb53e39d6bf1158c73367feed0293a17e5;hb=89c9ff022d3f13e27ba6bda085df15707fcfb0eb;hp=3ed93bcf8666d020f73387ca2735d4f24cbadc59;hpb=f9c7abfae65b79945beb7e9260942bc94876248a;p=kivitendo-erp.git diff --git a/SL/DB/PeriodicInvoicesConfig.pm b/SL/DB/PeriodicInvoicesConfig.pm index 3ed93bcf8..d49595bb5 100644 --- a/SL/DB/PeriodicInvoicesConfig.pm +++ b/SL/DB/PeriodicInvoicesConfig.pm @@ -2,6 +2,8 @@ package SL::DB::PeriodicInvoicesConfig; use strict; +use Readonly; + use SL::DB::MetaSetup::PeriodicInvoicesConfig; __PACKAGE__->meta->add_relationships( @@ -17,4 +19,69 @@ __PACKAGE__->meta->initialize; # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all. __PACKAGE__->meta->make_manager_class; +Readonly our @PERIODICITIES => qw(m q f b y); +Readonly our %PERIOD_LENGTHS => ( m => 1, q => 3, f => 4, b => 6, y => 12 ); + +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 = <dbh->selectrow_array($query, undef, $self->id); + + return undef unless $max_transdate; + return ref $max_transdate ? $max_transdate : $self->db->parse_date($max_transdate); +} + 1;