You are here

Rolling.php in Commerce Recurring Framework 8

File

src/Plugin/Commerce/BillingSchedule/Rolling.php
View source
<?php

namespace Drupal\commerce_recurring\Plugin\Commerce\BillingSchedule;

use Drupal\commerce_recurring\BillingPeriod;
use Drupal\Core\Datetime\DrupalDateTime;

/**
 * Provides a rolling interval billing schedule.
 *
 * Billing periods generated by this plugin always start at the beginning
 * of the subscription. For example, if a monthly subscription is
 * opened on Oct 12th, the generated billing period will be Oct 12th - Nov 12th.
 *
 * @CommerceBillingSchedule(
 *   id = "rolling",
 *   label = @Translation("Rolling interval"),
 * )
 */
class Rolling extends IntervalBase {

  /**
   * {@inheritdoc}
   */
  public function generateFirstBillingPeriod(DrupalDateTime $start_date) {
    return new BillingPeriod($start_date, $this
      ->getInterval()
      ->add($start_date));
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

}

Classes

Namesort descending Description
Rolling Provides a rolling interval billing schedule.