Dokumentation: HTML- & PDF-Versionen neu gebaut
[kivitendo-erp.git] / SL / DB / PeriodicInvoicesConfig.pm
index 0d66085..0523348 100644 (file)
@@ -11,7 +11,7 @@ __PACKAGE__->meta->initialize;
 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
 __PACKAGE__->meta->make_manager_class;
 
 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
 __PACKAGE__->meta->make_manager_class;
 
-our %PERIOD_LENGTHS             = ( m => 1, q => 3, b => 6, y => 12 );
+our %PERIOD_LENGTHS             = ( o => 0, m => 1, q => 3, b => 6, y => 12 );
 our %ORDER_VALUE_PERIOD_LENGTHS = ( %PERIOD_LENGTHS, 2 => 24, 3 => 36, 4 => 48, 5 => 60 );
 our @PERIODICITIES              = keys %PERIOD_LENGTHS;
 our @ORDER_VALUE_PERIODICITIES  = keys %ORDER_VALUE_PERIOD_LENGTHS;
 our %ORDER_VALUE_PERIOD_LENGTHS = ( %PERIOD_LENGTHS, 2 => 24, 3 => 36, 4 => 48, 5 => 60 );
 our @PERIODICITIES              = keys %PERIOD_LENGTHS;
 our @ORDER_VALUE_PERIODICITIES  = keys %ORDER_VALUE_PERIOD_LENGTHS;
@@ -88,7 +88,7 @@ sub calculate_invoice_dates {
 
   my $period_len = $self->get_billing_period_length;
   my $cur_date   = ($self->first_billing_date || $self->start_date)->clone;
 
   my $period_len = $self->get_billing_period_length;
   my $cur_date   = ($self->first_billing_date || $self->start_date)->clone;
-  my $end_date   = $self->terminated ? $self->end_date : undef;
+  my $end_date   = $self->terminated || !$self->extend_automatically_by ? $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->clone->add(days => 1) : $cur_date->clone;
   $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->clone->add(days => 1) : $cur_date->clone;
@@ -96,6 +96,10 @@ sub calculate_invoice_dates {
   $start_date    = max($start_date, $params{start_date}) if $params{start_date};
   $end_date      = min($end_date,   $params{end_date})   if $params{end_date};
 
   $start_date    = max($start_date, $params{start_date}) if $params{start_date};
   $end_date      = min($end_date,   $params{end_date})   if $params{end_date};
 
+  if ($self->periodicity eq 'o') {
+    return ($cur_date >= $start_date) && ($cur_date <= $end_date) ? ($cur_date) : ();
+  }
+
   my @dates;
 
   while ($cur_date <= $end_date) {
   my @dates;
 
   while ($cur_date <= $end_date) {
@@ -128,6 +132,28 @@ sub is_last_bill_date_in_order_value_cycle {
   return $date_itr == $next_billing_date;
 }
 
   return $date_itr == $next_billing_date;
 }
 
+sub disable_one_time_config {
+  my $self = shift;
+
+  _log_msg("check one time for " . $self->id . "\n");
+
+  # A periodicity of one time was set. Deactivate this config now.
+  if ($self->periodicity eq 'o') {
+    _log_msg("setting inactive\n");
+    if (!$self->db->with_transaction(sub {
+      1;                          # make Emacs happy
+      $self->active(0);
+      $self->order->update_attributes(closed => 1);
+      $self->save;
+      1;
+    })) {
+      $::lxdebug->message(LXDebug->WARN(), "disalbe_one_time config failed: " . join("\n", (split(/\n/, $self->{db_obj}->db->error))[0..2]));
+      return undef;
+    }
+    return $self->order->ordnumber;
+  }
+  return undef;
+}
 1;
 __END__
 
 1;
 __END__
 
@@ -235,6 +261,15 @@ date given by the C<date> parameter plus the billing period length
 equals one of those dates then the given date is indeed the date of
 the last invoice in that particular order value cycle.
 
 equals one of those dates then the given date is indeed the date of
 the last invoice in that particular order value cycle.
 
+=item C<sub disable_one_time_config>
+
+Sets the state of the periodic_invoices_configs to inactive
+(active => false) and closes the source order (closed => true)
+if the periodicity is <Co> (one time).
+
+Returns undef if the periodicity is not 'one time' otherwise the
+order number of the deactivated periodic order.
+
 =back
 
 =head1 BUGS
 =back
 
 =head1 BUGS