Wiederkehrende Rechnungen: Berechnung maximales Enddatum gefixt
[kivitendo-erp.git] / SL / DB / PeriodicInvoicesConfig.pm
1 package SL::DB::PeriodicInvoicesConfig;
2
3 use strict;
4
5 use SL::DB::MetaSetup::PeriodicInvoicesConfig;
6
7 use List::Util qw(max min);
8
9 __PACKAGE__->meta->initialize;
10
11 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
12 __PACKAGE__->meta->make_manager_class;
13
14 our @PERIODICITIES  = qw(m q f b y);
15 our %PERIOD_LENGTHS = ( m => 1, q => 3, f => 4, b => 6, y => 12 );
16
17 sub get_period_length {
18   my $self = shift;
19   return $PERIOD_LENGTHS{ $self->periodicity } || 1;
20 }
21
22 sub _log_msg {
23   $::lxdebug->message(LXDebug->DEBUG1(), join('', @_));
24 }
25
26 sub handle_automatic_extension {
27   my $self = shift;
28
29   _log_msg("HAE for " . $self->id . "\n");
30   # Don't extend configs that have been terminated. There's nothing to
31   # extend if there's no end date.
32   return if $self->terminated || !$self->end_date;
33
34   my $today    = DateTime->now_local;
35   my $end_date = $self->end_date;
36
37   _log_msg("today $today end_date $end_date\n");
38
39   # The end date has not been reached yet, therefore no extension is
40   # needed.
41   return if $today <= $end_date;
42
43   # The end date has been reached. If no automatic extension has been
44   # set then terminate the config and return.
45   if (!$self->extend_automatically_by) {
46     _log_msg("setting inactive\n");
47     $self->active(0);
48     $self->save;
49     return;
50   }
51
52   # Add the automatic extension period to the new end date as long as
53   # the new end date is in the past. Then save it and get out.
54   $end_date->add(months => $self->extend_automatically_by) while $today > $end_date;
55   _log_msg("new end date $end_date\n");
56
57   $self->end_date($end_date);
58   $self->save;
59
60   return $end_date;
61 }
62
63 sub get_previous_billed_period_start_date {
64   my $self  = shift;
65
66   my $query = <<SQL;
67     SELECT MAX(period_start_date)
68     FROM periodic_invoices
69     WHERE config_id = ?
70 SQL
71
72   my ($date) = $self->dbh->selectrow_array($query, undef, $self->id);
73
74   return undef unless $date;
75   return ref $date ? $date : $self->db->parse_date($date);
76 }
77
78 sub calculate_invoice_dates {
79   my ($self, %params) = @_;
80
81   my $period_len = $self->get_period_length;
82   my $cur_date   = $self->first_billing_date || $self->start_date;
83   my $end_date   = $self->terminated ? $self->end_date : undef;
84   $end_date    //= DateTime->today_local->add(years => 100);
85   my $start_date = $params{past_dates} ? undef                       : $self->get_previous_billed_period_start_date;
86   $start_date    = $start_date         ? $start_date->add(days => 1) : $cur_date->clone;
87
88   $start_date    = max($start_date, $params{start_date}) if $params{start_date};
89   $end_date      = min($end_date,   $params{end_date})   if $params{end_date};
90
91   my @dates;
92
93   while ($cur_date <= $end_date) {
94     push @dates, $cur_date->clone if $cur_date >= $start_date;
95
96     $cur_date->add(months => $period_len);
97   }
98
99   return @dates;
100 }
101
102 1;