View source
<?php
namespace Drupal\commerce_recurring\Plugin\Commerce\BillingSchedule;
use Drupal\commerce_recurring\BillingPeriod;
use Drupal\Core\Datetime\DateHelper;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\FormStateInterface;
class Fixed extends IntervalBase {
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'start_month' => 1,
'start_day' => 1,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['start_month'] = [
'#type' => 'select',
'#title' => $this
->t('Start month'),
'#description' => $this
->t('The month in which the billing period will start.'),
'#options' => DateHelper::monthNames(TRUE),
'#default_value' => $this->configuration['start_month'],
'#states' => [
'visible' => [
':input[name="configuration[fixed][interval][unit]"]' => [
'value' => 'year',
],
],
],
];
$form['start_day'] = [
'#type' => 'select',
'#title' => $this
->t('Start day'),
'#description' => $this
->t('The day on which the billing period will start.'),
'#options' => DateHelper::days(TRUE),
'#default_value' => $this->configuration['start_day'],
'#states' => [
'visible' => [
':input[name="configuration[fixed][interval][unit]"]' => [
[
'value' => 'month',
],
[
'value' => 'year',
],
],
],
],
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if (!$form_state
->getErrors()) {
$interval_unit = $this->configuration['interval']['unit'];
$values = $form_state
->getValue($form['#parents']);
$this->configuration['start_month'] = 1;
$this->configuration['start_day'] = 1;
if (in_array($interval_unit, [
'month',
'year',
])) {
if ($interval_unit == 'year') {
$this->configuration['start_month'] = $values['start_month'];
}
$this->configuration['start_day'] = $values['start_day'];
}
}
}
public function generateFirstBillingPeriod(DrupalDateTime $start_date) {
$interval = $this
->getInterval();
$adjusted_start_date = $this
->adjustStartDate($start_date);
return new BillingPeriod($adjusted_start_date, $interval
->add($adjusted_start_date));
}
public function generateNextBillingPeriod(DrupalDateTime $start_date, BillingPeriod $billing_period) {
$next_start_date = $billing_period
->getEndDate();
return new BillingPeriod($next_start_date, $this
->getInterval()
->add($next_start_date));
}
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);
if ($adjusted_start_date
->format('U') > $start_date
->format('U')) {
$adjusted_start_date = $interval
->subtract($adjusted_start_date);
}
return $adjusted_start_date;
}
}