You are here

public function Rolling::generateNextBillingPeriod in Commerce Recurring Framework 8

Generates the next billing period.

Parameters

\Drupal\Core\Datetime\DrupalDateTime $start_date: The billing start date/time.

\Drupal\commerce_recurring\BillingPeriod $billing_period: The current billing period.

Return value

\Drupal\commerce_recurring\BillingPeriod The billing period.

Overrides BillingScheduleInterface::generateNextBillingPeriod

File

src/Plugin/Commerce/BillingSchedule/Rolling.php, line 32

Class

Rolling
Provides a rolling interval billing schedule.

Namespace

Drupal\commerce_recurring\Plugin\Commerce\BillingSchedule

Code

public function generateNextBillingPeriod(DrupalDateTime $start_date, BillingPeriod $billing_period) {
  $next_start_date = $billing_period
    ->getEndDate();
  $next_end_date = $this
    ->getInterval()
    ->add($next_start_date);

  // Retain the original billing day when possible.
  // Jan 31st -> Feb 28th -> March 31st (not March 28th).
  $billing_day = $start_date
    ->format('d');
  if ($this
    ->getInterval()
    ->getUnit() == 'month' && $next_end_date
    ->format('d') != $billing_day) {
    if ($billing_day <= $next_end_date
      ->format('t')) {
      $next_end_date
        ->setDate($next_end_date
        ->format('Y'), $next_end_date
        ->format('m'), $billing_day);
    }
  }
  return new BillingPeriod($next_start_date, $next_end_date);
}