]> wagnertech.de Git - mfinanz.git/blobdiff - SL/DB/PeriodicInvoicesConfig.pm
Finanzcontrollingbericht: wiederkehrende Rechnungen berücksichtigen
[mfinanz.git] / SL / DB / PeriodicInvoicesConfig.pm
index d49595bb53e39d6bf1158c73367feed0293a17e5..45536752c0fe60ec81fd9eb4e8faa9958802da83 100644 (file)
@@ -2,25 +2,17 @@ package SL::DB::PeriodicInvoicesConfig;
 
 use strict;
 
-use Readonly;
-
 use SL::DB::MetaSetup::PeriodicInvoicesConfig;
 
-__PACKAGE__->meta->add_relationships(
-  order        => {
-    type       => 'one to one',
-    class      => 'SL::DB::Order',
-    column_map => { oe_id => 'id' },
-  },
-);
+use List::Util qw(max min);
 
 __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 );
+our @PERIODICITIES  = qw(m q f b y);
+our %PERIOD_LENGTHS = ( m => 1, q => 3, f => 4, b => 6, y => 12 );
 
 sub get_period_length {
   my $self = shift;
@@ -84,4 +76,27 @@ SQL
   return ref $max_transdate ? $max_transdate : $self->db->parse_date($max_transdate);
 }
 
+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->end_date                  || DateTime->today_local->add(years => 10);
+  my $start_date = $params{past_dates} ? undef : $self->get_previous_invoice_date;
+  $start_date  ||= $cur_date->clone->subtract(days => 1);
+
+  $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;