You are here

protected function Fixed::adjustStartDate in Commerce Recurring Framework 8

Adjusts the start date to the beginning of the configured interval.

In addition to flooring the start date (so that May 15th becomes May 1st for monthly intervals, etc), it also handles custom start months/days.

Parameters

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

Return value

\Drupal\Core\Datetime\DrupalDateTime The adjusted start date.

1 call to Fixed::adjustStartDate()
Fixed::generateFirstBillingPeriod in src/Plugin/Commerce/BillingSchedule/Fixed.php
Generates the first billing period.

File

src/Plugin/Commerce/BillingSchedule/Fixed.php, line 125

Class

Fixed
Provides a fixed interval billing schedule.

Namespace

Drupal\commerce_recurring\Plugin\Commerce\BillingSchedule

Code

protected function adjustStartDate(DrupalDateTime $start_date) {
  $interval = $this
    ->getInterval();
  $adjusted_start_date = $interval
    ->floor($start_date);
  if (!in_array($interval
    ->getUnit(), [
    'month',
    'year',
  ])) {
    return $adjusted_start_date;
  }
  $start_year = $start_date
    ->format('Y');
  $start_month = $start_date
    ->format('m');
  if ($interval
    ->getUnit() == 'year') {
    $start_month = $this->configuration['start_month'];
  }
  $start_day = $this->configuration['start_day'];
  $adjusted_start_date
    ->setDate($start_year, $start_month, $start_day);

  // Further adjust the date if it is now in the future, to account for
  // customers subscribing before the custom start month/day (which puts
  // them in the previous billing period).
  if ($adjusted_start_date
    ->format('U') > $start_date
    ->format('U')) {
    $adjusted_start_date = $interval
      ->subtract($adjusted_start_date);
  }
  return $adjusted_start_date;
}