Files
firefly-iii/app/Support/Repositories/Recurring/CalculateXOccurrencesSince.php

207 lines
8.1 KiB
PHP
Raw Normal View History

2019-09-26 19:16:17 +02:00
<?php
/**
* CalculateXOccurrencesSince.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2019-09-26 19:16:17 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-09-26 19:16:17 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2019-09-26 19:16:17 +02:00
*
* This program is distributed in the hope that it will be useful,
2019-09-26 19:16:17 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2019-09-26 19:16:17 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-09-26 19:16:17 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Support\Repositories\Recurring;
2019-09-26 19:16:17 +02:00
use Carbon\Carbon;
2019-10-22 06:09:34 +02:00
2019-09-26 19:16:17 +02:00
/**
* Class CalculateXOccurrencesSince
*/
trait CalculateXOccurrencesSince
{
/**
2023-06-21 12:34:58 +02:00
* Calculates the number of daily occurrences for a recurring transaction, starting at the date, until $count is
* reached. It will skip over $skipMod -1 recurrences.
2019-09-26 19:16:17 +02:00
*/
protected function getXDailyOccurrencesSince(Carbon $date, Carbon $afterDate, int $count, int $skipMod): array
{
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Now in %s', __METHOD__));
2019-09-26 19:16:17 +02:00
$return = [];
$mutator = clone $date;
$total = 0;
$attempts = 0;
while ($total < $count) {
if (0 === $attempts % $skipMod && $mutator->gt($afterDate)) {
$return[] = clone $mutator;
2023-12-20 19:35:52 +01:00
++$total;
2019-09-26 19:16:17 +02:00
}
$mutator->addDay();
2023-12-20 19:35:52 +01:00
++$attempts;
2019-09-26 19:16:17 +02:00
}
return $return;
}
2019-09-26 19:16:17 +02:00
/**
2023-06-21 12:34:58 +02:00
* Calculates the number of monthly occurrences for a recurring transaction, starting at the date, until $count is
* reached. It will skip over $skipMod -1 recurrences.
2019-09-26 19:16:17 +02:00
*
2023-12-01 05:15:59 +01:00
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
2019-09-26 19:16:17 +02:00
*/
protected function getXMonthlyOccurrencesSince(Carbon $date, Carbon $afterDate, int $count, int $skipMod, string $moment): array
{
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Now in %s(%s, %s, %d)', __METHOD__, $date->format('Y-m-d'), $afterDate->format('Y-m-d'), $count));
2019-09-26 19:16:17 +02:00
$return = [];
$mutator = clone $date;
$total = 0;
$attempts = 0;
2022-12-29 19:42:26 +01:00
$dayOfMonth = (int)$moment;
2021-03-14 20:03:27 +01:00
$dayOfMonth = 0 === $dayOfMonth ? 1 : $dayOfMonth;
2019-09-26 19:16:17 +02:00
if ($mutator->day > $dayOfMonth) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('%d is after %d, add a month. Mutator is now', $mutator->day, $dayOfMonth));
2019-09-26 19:16:17 +02:00
// day has passed already, add a month.
$mutator->addMonth();
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('%s', $mutator->format('Y-m-d')));
2019-09-26 19:16:17 +02:00
}
while ($total < $count) {
$domCorrected = min($dayOfMonth, $mutator->daysInMonth);
$mutator->day = $domCorrected;
2024-03-04 20:41:34 +01:00
app('log')->debug(sprintf('Mutator is now %s', $mutator->format('Y-m-d')));
2019-09-26 19:16:17 +02:00
if (0 === $attempts % $skipMod && $mutator->gte($afterDate)) {
2024-03-04 20:41:34 +01:00
app('log')->debug('Is added to the list.');
2019-09-26 19:16:17 +02:00
$return[] = clone $mutator;
2023-12-20 19:35:52 +01:00
++$total;
2019-09-26 19:16:17 +02:00
}
2023-12-20 19:35:52 +01:00
++$attempts;
$mutator = $mutator->endOfMonth()->addDay();
2019-09-26 19:16:17 +02:00
}
return $return;
}
2019-09-26 19:16:17 +02:00
/**
2023-06-21 12:34:58 +02:00
* Calculates the number of NDOM occurrences for a recurring transaction, starting at the date, until $count is
* reached. It will skip over $skipMod -1 recurrences.
2019-09-26 19:16:17 +02:00
*
2023-12-01 05:15:59 +01:00
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
2019-09-26 19:16:17 +02:00
*/
protected function getXNDomOccurrencesSince(Carbon $date, Carbon $afterDate, int $count, int $skipMod, string $moment): array
{
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Now in %s', __METHOD__));
$return = [];
$total = 0;
$attempts = 0;
$mutator = clone $date;
2019-09-26 19:16:17 +02:00
$mutator->addDay(); // always assume today has passed.
$mutator->startOfMonth();
// this feels a bit like a cop out but why reinvent the wheel?
2023-12-20 19:35:52 +01:00
$counters = [1 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth', 5 => 'fifth'];
$daysOfWeek = [1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 7 => 'Sunday'];
2019-09-26 19:16:17 +02:00
$parts = explode(',', $moment);
while ($total < $count) {
$string = sprintf('%s %s of %s %s', $counters[$parts[0]], $daysOfWeek[$parts[1]], $mutator->format('F'), $mutator->format('Y'));
$newCarbon = new Carbon($string);
if (0 === $attempts % $skipMod && $mutator->gte($afterDate)) {
$return[] = clone $newCarbon;
2023-12-20 19:35:52 +01:00
++$total;
2019-09-26 19:16:17 +02:00
}
2023-12-20 19:35:52 +01:00
++$attempts;
2019-09-26 19:16:17 +02:00
$mutator->endOfMonth()->addDay();
}
return $return;
}
2019-09-26 19:16:17 +02:00
/**
2023-06-21 12:34:58 +02:00
* Calculates the number of weekly occurrences for a recurring transaction, starting at the date, until $count is
* reached. It will skip over $skipMod -1 recurrences.
2019-09-26 19:16:17 +02:00
*
2023-12-01 05:15:59 +01:00
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
2019-09-26 19:16:17 +02:00
*/
protected function getXWeeklyOccurrencesSince(Carbon $date, Carbon $afterDate, int $count, int $skipMod, string $moment): array
{
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Now in %s', __METHOD__));
$return = [];
$total = 0;
$attempts = 0;
$mutator = clone $date;
2019-09-26 19:16:17 +02:00
// monday = 1
// sunday = 7
2021-05-25 06:18:40 +02:00
// Removed assumption today has passed, see issue https://github.com/firefly-iii/firefly-iii/issues/4798
2023-12-20 19:35:52 +01:00
// $mutator->addDay(); // always assume today has passed.
$dayOfWeek = (int)$moment;
2019-09-26 19:16:17 +02:00
if ($mutator->dayOfWeekIso > $dayOfWeek) {
// day has already passed this week, add one week:
$mutator->addWeek();
}
// today is wednesday (3), expected is friday (5): add two days.
// today is friday (5), expected is monday (1), subtract four days.
$dayDifference = $dayOfWeek - $mutator->dayOfWeekIso;
$mutator->addDays($dayDifference);
while ($total < $count) {
if (0 === $attempts % $skipMod && $mutator->gte($afterDate)) {
$return[] = clone $mutator;
2023-12-20 19:35:52 +01:00
++$total;
2019-09-26 19:16:17 +02:00
}
2023-12-20 19:35:52 +01:00
++$attempts;
2019-09-26 19:16:17 +02:00
$mutator->addWeek();
}
return $return;
}
2019-09-26 19:16:17 +02:00
/**
2023-06-21 12:34:58 +02:00
* Calculates the number of yearly occurrences for a recurring transaction, starting at the date, until $count is
* reached. It will skip over $skipMod -1 recurrences.
2019-09-26 19:16:17 +02:00
*
2023-12-01 05:15:59 +01:00
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
2019-09-26 19:16:17 +02:00
*/
protected function getXYearlyOccurrencesSince(Carbon $date, Carbon $afterDate, int $count, int $skipMod, string $moment): array
{
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Now in %s(%s, %d, %d, %s)', __METHOD__, $date->format('Y-m-d'), $date->format('Y-m-d'), $count, $skipMod));
2019-09-26 19:16:17 +02:00
$return = [];
$mutator = clone $date;
$total = 0;
$attempts = 0;
$date = new Carbon($moment);
$date->year = $mutator->year;
if ($mutator > $date) {
2023-10-29 06:33:43 +01:00
app('log')->debug(
2021-05-24 08:50:17 +02:00
sprintf('mutator (%s) > date (%s), so add a year to date (%s)', $mutator->format('Y-m-d'), $date->format('Y-m-d'), $date->format('Y-m-d'))
2021-03-14 20:03:27 +01:00
);
2019-09-26 19:16:17 +02:00
$date->addYear();
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Date is now %s', $date->format('Y-m-d')));
2019-09-26 19:16:17 +02:00
}
$obj = clone $date;
2019-09-26 19:16:17 +02:00
while ($total < $count) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('total (%d) < count (%d) so go.', $total, $count));
app('log')->debug(sprintf('attempts (%d) %% skipmod (%d) === %d', $attempts, $skipMod, $attempts % $skipMod));
app('log')->debug(sprintf('Obj (%s) gte afterdate (%s)? %s', $obj->format('Y-m-d'), $afterDate->format('Y-m-d'), var_export($obj->gte($afterDate), true)));
2019-10-22 06:09:34 +02:00
if (0 === $attempts % $skipMod && $obj->gte($afterDate)) {
2023-10-29 06:33:43 +01:00
app('log')->debug('All conditions true, add obj.');
2019-09-26 19:16:17 +02:00
$return[] = clone $obj;
2023-12-20 19:35:52 +01:00
++$total;
2019-09-26 19:16:17 +02:00
}
2021-05-24 08:50:17 +02:00
$obj->addYears();
2023-12-20 19:35:52 +01:00
++$attempts;
2019-09-26 19:16:17 +02:00
}
return $return;
}
}