Various extensions to recurring transactions.

This commit is contained in:
James Cole
2018-06-26 18:49:33 +02:00
parent 7591f3fa29
commit 5d01955133
12 changed files with 88 additions and 17 deletions

View File

@@ -262,6 +262,8 @@ class RecurringRepository implements RecurringRepositoryInterface
}
break;
}
// filter out all the weekend days:
$return = $this->filterWeekends($repetition, $return);
return $return;
}
@@ -492,4 +494,43 @@ class RecurringRepository implements RecurringRepositoryInterface
return $service->update($recurrence, $data);
}
/**
* Filters out all weekend entries, if necessary.
*
* @param RecurrenceRepetition $repetition
* @param array $dates
*
* @return array
*/
protected function filterWeekends(RecurrenceRepetition $repetition, array $dates): array
{
if ($repetition->weekend === RecurrenceRepetition::WEEKEND_DO_NOTHING) {
return $dates;
}
$return = [];
/** @var Carbon $date */
foreach ($dates as $date) {
$isWeekend = $date->isWeekend();
// set back to Friday?
if ($isWeekend && $repetition->weekend === RecurrenceRepetition::WEEKEND_TO_FRIDAY) {
$clone = clone $date;
$clone->subDays(7 - $date->dayOfWeekIso);
$return[] = $clone;
}
// postpone to Monday?
if ($isWeekend && $repetition->weekend === RecurrenceRepetition::WEEKEND_TO_MONDAY) {
$clone = clone $date;
$clone->addDays(8 - $date->dayOfWeekIso);
$return[] = $clone;
}
// otherwise, ignore the date!
}
// filter unique dates?
return $return;
}
}