/** * @param Bill $bill * * @return \Carbon\Carbon */ public function nextExpectedMatch(Bill $bill) : Carbon { $finalDate = Carbon::now(); $finalDate->year = 1900; if ($bill->active == 0) { return $finalDate; } /* * $today is the start of the next period, to make sure FF3 won't miss anything * when the current period has a transaction journal. */ /** @var \Carbon\Carbon $obj */ $obj = new Carbon(); $today = Navigation::addPeriod($obj, $bill->repeat_freq, 0); $skip = $bill->skip + 1; $start = Navigation::startOfPeriod($obj, $bill->repeat_freq); /* * go back exactly one month/week/etc because FF3 does not care about 'next' * bills if they're too far into the past. */ $counter = 0; while ($start <= $today) { if ($counter % $skip == 0) { // do something. $end = Navigation::endOfPeriod(clone $start, $bill->repeat_freq); $journalCount = $bill->transactionJournals()->before($end)->after($start)->count(); if ($journalCount == 0) { $finalDate = new Carbon($start->format('Y-m-d')); break; } } // add period for next round! $start = Navigation::addPeriod($start, $bill->repeat_freq, 0); $counter++; } return $finalDate; }