X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FPeriodicInvoicesConfig.pm;h=0523348c88bd8d9ad01ad2cdbe32ee2498144080;hb=b775c378552e6b5bf59f98046cdf4e577cd351df;hp=8013cd94092e156de6d2a0a4cee0e4afd36bfc17;hpb=89360aadb2c3e0854a2815e5d0ed8a9efd1c5889;p=kivitendo-erp.git diff --git a/SL/DB/PeriodicInvoicesConfig.pm b/SL/DB/PeriodicInvoicesConfig.pm index 8013cd940..0523348c8 100644 --- a/SL/DB/PeriodicInvoicesConfig.pm +++ b/SL/DB/PeriodicInvoicesConfig.pm @@ -11,16 +11,24 @@ __PACKAGE__->meta->initialize; # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all. __PACKAGE__->meta->make_manager_class; -our @PERIODICITIES = qw(m q f b y); -our %PERIOD_LENGTHS = ( m => 1, q => 3, f => 4, 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; -sub get_period_length { +sub get_billing_period_length { my $self = shift; return $PERIOD_LENGTHS{ $self->periodicity } || 1; } +sub get_order_value_period_length { + my $self = shift; + return $self->get_billing_period_length if $self->order_value_periodicity eq 'p'; + return $ORDER_VALUE_PERIOD_LENGTHS{ $self->order_value_periodicity } || 1; +} + sub _log_msg { - $::lxdebug->message(LXDebug->DEBUG1(), join('', @_)); + $::lxdebug->message(LXDebug->DEBUG1(), join('', 'SL::DB::PeriodicInvoicesConfig: ', @_)); } sub handle_automatic_extension { @@ -78,16 +86,20 @@ SQL 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->terminated ? $self->end_date : undef; + 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->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->add(days => 1) : $cur_date->clone; + 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; $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) { @@ -99,4 +111,173 @@ sub calculate_invoice_dates { return @dates; } +sub is_last_bill_date_in_order_value_cycle { + my ($self, %params) = @_; + + my $months_billing = $self->get_billing_period_length; + my $months_order_value = $self->get_order_value_period_length; + + return 1 if $months_billing >= $months_order_value; + + my $next_billing_date = $params{date}->clone->add(months => $months_billing); + my $date_itr = max($self->start_date, $self->first_billing_date || $self->start_date)->clone; + + _log_msg("is_last_billing_date_in_order_value_cycle start: id " . $self->id . " date_itr $date_itr start " . $self->start_date); + + $date_itr->add(months => $months_order_value) while $date_itr < $next_billing_date; + + _log_msg("is_last_billing_date_in_order_value_cycle end: refdate $params{date} next_billing_date $next_billing_date date_itr $date_itr months_billing $months_billing months_order_value $months_order_value result " + . ($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__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::DB::PeriodicInvoicesConfig - DB model for the configuration for periodic invoices + +=head1 FUNCTIONS + +=over 4 + +=item C + +Calculates dates for which invoices will have to be created. Returns a +list of L objects. + +This function looks at the configuration settings and at the list of +invoices that have already been created for this configuration. The +date range for which dates are created are controlled by several +values: + +=over 2 + +=item * The properties C and C +determine the start date. + +=item * The properties C and C determine the end +date. + +=item * The optional parameter C determines whether or not +dates for which invoices have already been created will be included in +the list. The default is not to include them. + +=item * The optional parameters C and C override +the start and end dates from the configuration. + +=item * If no end date is set or implied via the configuration and no +C parameter is given then the function will use 100 years +in the future as the end date. + +=back + +=item C + +Returns the number of months corresponding to the billing +periodicity. This means that a new invoice has to be created every x +months starting with the value in C (or +C if C is unset). + +=item C + +Returns the number of months the order's value refers to. This looks +at the C. + +Each invoice's value is calculated as C. + +=item C + +Returns the highest date (as an instance of L) for which an +invoice has been created from this configuration. + +=item C + +Configurations which haven't been terminated and which have an end +date set may be eligible for automatic extension by a certain number +of months. This what the function implements. + +If the configuration is not eligible or if the C hasn't been +reached yet then nothing is done and C is returned. Otherwise +its behavior is determined by the C property. + +If the property C is not 0 then the +C will be extended by C months, and +the configuration will be saved. In this case the new end date will be +returned. + +Otherwise (if C is 0) the property C +will be set to 1, and the configuration will be saved. In this case +C will be returned. + +=item C + +Determines whether or not the mandatory parameter C, an instance +of L, is the last billing date within the cycle given by the +order value periodicity. Returns a truish value if this is the case +and a falsish value otherwise. + +This check is always true if the billing periodicity is longer than or +equal to the order value periodicity. For example, if you have an +order whose value is given for three months and you bill every six +months and you have twice the order value on each invoice, meaning +each invoice is itself the last invoice for not only one but two order +value cycles. + +Otherwise (if the order value periodicity is longer than the billing +periodicity) this function iterates over all eligible dates starting +with C (or C if C +is unset) and adding the order value length with each step. If the +date given by the C 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. + +=item C + +Sets the state of the periodic_invoices_configs to inactive +(active => false) and closes the source order (closed => true) +if the periodicity is (one time). + +Returns undef if the periodicity is not 'one time' otherwise the +order number of the deactivated periodic order. + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut