2018-06-10 09:59:03 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* RecurringRepository.php
|
|
|
|
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III.
|
|
|
|
*
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Recurring;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2018-06-16 14:47:51 -05:00
|
|
|
use FireflyIII\Factory\RecurrenceFactory;
|
2018-07-02 13:39:45 -05:00
|
|
|
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
2018-07-02 13:41:28 -05:00
|
|
|
use FireflyIII\Helpers\Filter\InternalTransferFilter;
|
2018-06-10 09:59:03 -05:00
|
|
|
use FireflyIII\Models\Note;
|
|
|
|
use FireflyIII\Models\Preference;
|
|
|
|
use FireflyIII\Models\Recurrence;
|
2018-06-22 11:42:23 -05:00
|
|
|
use FireflyIII\Models\RecurrenceMeta;
|
2018-06-10 09:59:03 -05:00
|
|
|
use FireflyIII\Models\RecurrenceRepetition;
|
2018-06-22 11:42:23 -05:00
|
|
|
use FireflyIII\Models\RecurrenceTransaction;
|
|
|
|
use FireflyIII\Models\RecurrenceTransactionMeta;
|
2018-06-26 14:17:50 -05:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2018-07-02 13:39:45 -05:00
|
|
|
use FireflyIII\Models\TransactionJournalMeta;
|
2018-06-23 01:19:29 -05:00
|
|
|
use FireflyIII\Services\Internal\Destroy\RecurrenceDestroyService;
|
2018-06-21 11:57:51 -05:00
|
|
|
use FireflyIII\Services\Internal\Update\RecurrenceUpdateService;
|
2018-06-10 09:59:03 -05:00
|
|
|
use FireflyIII\User;
|
2018-07-02 13:39:45 -05:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
2018-06-10 09:59:03 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2018-06-22 11:42:23 -05:00
|
|
|
use Log;
|
2018-06-10 09:59:03 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Class RecurringRepository
|
|
|
|
*/
|
|
|
|
class RecurringRepository implements RecurringRepositoryInterface
|
|
|
|
{
|
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
2018-06-23 01:19:29 -05:00
|
|
|
/**
|
|
|
|
* Destroy a recurring transaction.
|
|
|
|
*
|
|
|
|
* @param Recurrence $recurrence
|
|
|
|
*/
|
|
|
|
public function destroy(Recurrence $recurrence): void
|
|
|
|
{
|
|
|
|
/** @var RecurrenceDestroyService $service */
|
|
|
|
$service = app(RecurrenceDestroyService::class);
|
|
|
|
$service->destroy($recurrence);
|
|
|
|
}
|
|
|
|
|
2018-06-10 09:59:03 -05:00
|
|
|
/**
|
|
|
|
* Returns all of the user's recurring transactions.
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2018-06-22 11:42:23 -05:00
|
|
|
public function get(): Collection
|
|
|
|
{
|
|
|
|
return $this->user->recurrences()
|
|
|
|
->with(['TransactionCurrency', 'TransactionType', 'RecurrenceRepetitions', 'RecurrenceTransactions'])
|
|
|
|
->orderBy('active', 'DESC')
|
2018-07-01 14:32:48 -05:00
|
|
|
->orderBy('transaction_type_id', 'ASC')
|
2018-06-22 11:42:23 -05:00
|
|
|
->orderBy('title', 'ASC')
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get ALL recurring transactions.
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getAll(): Collection
|
|
|
|
{
|
|
|
|
// grab ALL recurring transactions:
|
|
|
|
return Recurrence
|
|
|
|
::with(['TransactionCurrency', 'TransactionType', 'RecurrenceRepetitions', 'RecurrenceTransactions'])
|
|
|
|
->orderBy('active', 'DESC')
|
|
|
|
->orderBy('title', 'ASC')
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the budget ID from a recurring transaction transaction.
|
|
|
|
*
|
|
|
|
* @param RecurrenceTransaction $recurrenceTransaction
|
|
|
|
*
|
|
|
|
* @return null|int
|
|
|
|
*/
|
|
|
|
public function getBudget(RecurrenceTransaction $recurrenceTransaction): ?int
|
|
|
|
{
|
|
|
|
$return = 0;
|
|
|
|
/** @var RecurrenceTransactionMeta $meta */
|
|
|
|
foreach ($recurrenceTransaction->recurrenceTransactionMeta as $meta) {
|
|
|
|
if ($meta->name === 'budget_id') {
|
|
|
|
$return = (int)$meta->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return === 0 ? null : $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the category from a recurring transaction transaction.
|
|
|
|
*
|
|
|
|
* @param RecurrenceTransaction $recurrenceTransaction
|
|
|
|
*
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getCategory(RecurrenceTransaction $recurrenceTransaction): ?string
|
2018-06-10 09:59:03 -05:00
|
|
|
{
|
2018-06-22 11:42:23 -05:00
|
|
|
$return = '';
|
|
|
|
/** @var RecurrenceTransactionMeta $meta */
|
|
|
|
foreach ($recurrenceTransaction->recurrenceTransactionMeta as $meta) {
|
|
|
|
if ($meta->name === 'category_name') {
|
|
|
|
$return = (string)$meta->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return === '' ? null : $return;
|
2018-06-10 09:59:03 -05:00
|
|
|
}
|
|
|
|
|
2018-06-26 14:17:50 -05:00
|
|
|
/**
|
|
|
|
* Returns the journals created for this recurrence, possibly limited by time.
|
|
|
|
*
|
|
|
|
* @param Recurrence $recurrence
|
|
|
|
* @param Carbon|null $start
|
|
|
|
* @param Carbon|null $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getJournals(Recurrence $recurrence, Carbon $start = null, Carbon $end = null): Collection
|
|
|
|
{
|
|
|
|
$query = TransactionJournal
|
|
|
|
::leftJoin('journal_meta', 'journal_meta.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->where('transaction_journals.user_id', $recurrence->user_id)
|
|
|
|
->whereNull('transaction_journals.deleted_at')
|
|
|
|
->where('journal_meta.name', 'recurrence_id')
|
|
|
|
->where('journal_meta.data', '"' . $recurrence->id . '"');
|
|
|
|
if (null !== $start) {
|
|
|
|
$query->where('transaction_journals.date', '>=', $start->format('Y-m-d 00:00:00'));
|
|
|
|
}
|
|
|
|
if (null !== $end) {
|
|
|
|
$query->where('transaction_journals.date', '<=', $end->format('Y-m-d 00:00:00'));
|
|
|
|
}
|
|
|
|
$result = $query->get(['transaction_journals.*']);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-06-10 09:59:03 -05:00
|
|
|
/**
|
|
|
|
* Get the notes.
|
|
|
|
*
|
|
|
|
* @param Recurrence $recurrence
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getNoteText(Recurrence $recurrence): string
|
|
|
|
{
|
|
|
|
/** @var Note $note */
|
|
|
|
$note = $recurrence->notes()->first();
|
|
|
|
if (null !== $note) {
|
|
|
|
return (string)$note->text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2018-06-16 14:47:51 -05:00
|
|
|
/**
|
|
|
|
* Generate events in the date range.
|
|
|
|
*
|
|
|
|
* @param RecurrenceRepetition $repetition
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @throws FireflyException
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getOccurrencesInRange(RecurrenceRepetition $repetition, Carbon $start, Carbon $end): array
|
|
|
|
{
|
|
|
|
$return = [];
|
|
|
|
$mutator = clone $start;
|
|
|
|
$mutator->startOfDay();
|
|
|
|
$skipMod = $repetition->repetition_skip + 1;
|
|
|
|
$attempts = 0;
|
2018-06-22 11:42:23 -05:00
|
|
|
Log::debug(sprintf('Calculating occurrences for rep type "%s"', $repetition->repetition_type));
|
2018-06-26 22:37:56 -05:00
|
|
|
Log::debug(sprintf('Mutator is now: %s', $mutator->format('Y-m-d')));
|
2018-06-16 14:47:51 -05:00
|
|
|
switch ($repetition->repetition_type) {
|
|
|
|
default:
|
|
|
|
throw new FireflyException(
|
|
|
|
sprintf('Cannot calculate occurrences for recurring transaction repetition type "%s"', $repetition->repetition_type)
|
|
|
|
);
|
|
|
|
case 'daily':
|
2018-06-26 22:37:56 -05:00
|
|
|
Log::debug('Rep is daily. Start of loop.');
|
2018-06-16 14:47:51 -05:00
|
|
|
while ($mutator <= $end) {
|
2018-06-26 22:37:56 -05:00
|
|
|
Log::debug(sprintf('Mutator is now: %s', $mutator->format('Y-m-d')));
|
2018-06-16 14:47:51 -05:00
|
|
|
if ($attempts % $skipMod === 0) {
|
2018-06-26 22:37:56 -05:00
|
|
|
Log::debug(sprintf('Attempts modulo skipmod is zero, include %s', $mutator->format('Y-m-d')));
|
2018-06-16 14:47:51 -05:00
|
|
|
$return[] = clone $mutator;
|
|
|
|
}
|
|
|
|
$mutator->addDay();
|
|
|
|
$attempts++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'weekly':
|
2018-06-26 22:37:56 -05:00
|
|
|
Log::debug('Rep is weekly.');
|
2018-06-16 14:47:51 -05:00
|
|
|
// monday = 1
|
|
|
|
// sunday = 7
|
|
|
|
$dayOfWeek = (int)$repetition->repetition_moment;
|
2018-06-26 22:40:14 -05:00
|
|
|
Log::debug(sprintf('DoW in repetition is %d, in mutator is %d', $dayOfWeek, $mutator->dayOfWeekIso));
|
2018-06-16 14:47:51 -05:00
|
|
|
if ($mutator->dayOfWeekIso > $dayOfWeek) {
|
|
|
|
// day has already passed this week, add one week:
|
|
|
|
$mutator->addWeek();
|
2018-06-26 22:40:14 -05:00
|
|
|
Log::debug(sprintf('Jump to next week, so mutator is now: %s', $mutator->format('Y-m-d')));
|
2018-06-16 14:47:51 -05:00
|
|
|
}
|
|
|
|
// today is wednesday (3), expected is friday (5): add two days.
|
|
|
|
// today is friday (5), expected is monday (1), subtract four days.
|
2018-06-27 11:30:53 -05:00
|
|
|
Log::debug(sprintf('Mutator is now: %s', $mutator->format('Y-m-d')));
|
2018-06-16 14:47:51 -05:00
|
|
|
$dayDifference = $dayOfWeek - $mutator->dayOfWeekIso;
|
|
|
|
$mutator->addDays($dayDifference);
|
2018-06-27 11:30:53 -05:00
|
|
|
Log::debug(sprintf('Mutator is now: %s', $mutator->format('Y-m-d')));
|
2018-06-16 14:47:51 -05:00
|
|
|
while ($mutator <= $end) {
|
2018-06-22 11:42:23 -05:00
|
|
|
if ($attempts % $skipMod === 0 && $start->lte($mutator) && $end->gte($mutator)) {
|
2018-06-27 11:30:53 -05:00
|
|
|
Log::debug('Date is in range of start+end, add to set.');
|
2018-06-16 14:47:51 -05:00
|
|
|
$return[] = clone $mutator;
|
|
|
|
}
|
|
|
|
$attempts++;
|
|
|
|
$mutator->addWeek();
|
2018-06-27 11:30:53 -05:00
|
|
|
Log::debug(sprintf('Mutator is now (end of loop): %s', $mutator->format('Y-m-d')));
|
2018-06-16 14:47:51 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'monthly':
|
|
|
|
$dayOfMonth = (int)$repetition->repetition_moment;
|
2018-06-22 11:42:23 -05:00
|
|
|
Log::debug(sprintf('Day of month in repetition is %d', $dayOfMonth));
|
|
|
|
Log::debug(sprintf('Start is %s.', $start->format('Y-m-d')));
|
|
|
|
Log::debug(sprintf('End is %s.', $end->format('Y-m-d')));
|
2018-06-16 14:47:51 -05:00
|
|
|
if ($mutator->day > $dayOfMonth) {
|
2018-06-22 11:42:23 -05:00
|
|
|
Log::debug('Add a month.');
|
2018-06-16 14:47:51 -05:00
|
|
|
// day has passed already, add a month.
|
|
|
|
$mutator->addMonth();
|
|
|
|
}
|
2018-06-22 11:42:23 -05:00
|
|
|
Log::debug(sprintf('Start is now %s.', $mutator->format('Y-m-d')));
|
|
|
|
Log::debug('Start loop.');
|
2018-06-16 14:47:51 -05:00
|
|
|
while ($mutator < $end) {
|
2018-06-22 11:42:23 -05:00
|
|
|
Log::debug(sprintf('Mutator is now %s.', $mutator->format('Y-m-d')));
|
2018-06-16 14:47:51 -05:00
|
|
|
$domCorrected = min($dayOfMonth, $mutator->daysInMonth);
|
2018-06-22 11:42:23 -05:00
|
|
|
Log::debug(sprintf('DoM corrected is %d', $domCorrected));
|
2018-06-16 14:47:51 -05:00
|
|
|
$mutator->day = $domCorrected;
|
2018-06-22 11:42:23 -05:00
|
|
|
Log::debug(sprintf('Mutator is now %s.', $mutator->format('Y-m-d')));
|
|
|
|
Log::debug(sprintf('$attempts %% $skipMod === 0 is %s', var_export($attempts % $skipMod === 0, true)));
|
|
|
|
Log::debug(sprintf('$start->lte($mutator) is %s', var_export($start->lte($mutator), true)));
|
|
|
|
Log::debug(sprintf('$end->gte($mutator) is %s', var_export($end->gte($mutator), true)));
|
|
|
|
if ($attempts % $skipMod === 0 && $start->lte($mutator) && $end->gte($mutator)) {
|
|
|
|
Log::debug(sprintf('ADD %s to return!', $mutator->format('Y-m-d')));
|
2018-06-16 14:47:51 -05:00
|
|
|
$return[] = clone $mutator;
|
|
|
|
}
|
|
|
|
$attempts++;
|
2018-06-22 11:42:23 -05:00
|
|
|
$mutator->endOfMonth()->startOfDay()->addDay();
|
2018-06-16 14:47:51 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'ndom':
|
|
|
|
$mutator->startOfMonth();
|
|
|
|
// this feels a bit like a cop out but why reinvent the wheel?
|
|
|
|
$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',];
|
|
|
|
$parts = explode(',', $repetition->repetition_moment);
|
|
|
|
while ($mutator <= $end) {
|
|
|
|
$string = sprintf('%s %s of %s %s', $counters[$parts[0]], $daysOfWeek[$parts[1]], $mutator->format('F'), $mutator->format('Y'));
|
|
|
|
$newCarbon = new Carbon($string);
|
|
|
|
$return[] = clone $newCarbon;
|
|
|
|
$mutator->endOfMonth()->addDay();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'yearly':
|
|
|
|
$date = new Carbon($repetition->repetition_moment);
|
|
|
|
$date->year = $mutator->year;
|
|
|
|
if ($mutator > $date) {
|
|
|
|
$date->addYear();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// is $date between $start and $end?
|
|
|
|
$obj = clone $date;
|
|
|
|
$count = 0;
|
|
|
|
while ($obj <= $end && $obj >= $mutator && $count < 10) {
|
|
|
|
|
|
|
|
$return[] = clone $obj;
|
|
|
|
$obj->addYears(1);
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-06-26 11:49:33 -05:00
|
|
|
// filter out all the weekend days:
|
|
|
|
$return = $this->filterWeekends($repetition, $return);
|
2018-06-16 14:47:51 -05:00
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2018-06-22 11:42:23 -05:00
|
|
|
/**
|
|
|
|
* Get the tags from the recurring transaction.
|
|
|
|
*
|
|
|
|
* @param Recurrence $recurrence
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getTags(Recurrence $recurrence): array
|
|
|
|
{
|
|
|
|
$tags = [];
|
|
|
|
/** @var RecurrenceMeta $meta */
|
|
|
|
foreach ($recurrence->recurrenceMeta as $meta) {
|
|
|
|
if ($meta->name === 'tags' && '' !== $meta->value) {
|
|
|
|
$tags = explode(',', $meta->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
2018-07-02 13:39:45 -05:00
|
|
|
/**
|
|
|
|
* @param Recurrence $recurrence
|
|
|
|
*
|
|
|
|
* @param int $page
|
|
|
|
* @param int $pageSize
|
|
|
|
*
|
|
|
|
* @return LengthAwarePaginator
|
|
|
|
*/
|
|
|
|
public function getTransactions(Recurrence $recurrence, int $page, int $pageSize): LengthAwarePaginator
|
|
|
|
{
|
|
|
|
$journalMeta = TransactionJournalMeta
|
|
|
|
::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'journal_meta.transaction_journal_id')
|
|
|
|
->whereNull('transaction_journals.deleted_at')
|
|
|
|
->where('transaction_journals.user_id', $this->user->id)
|
|
|
|
->where('name', 'recurrence_id')
|
|
|
|
->where('data', json_encode((string)$recurrence->id))
|
|
|
|
->get()->pluck('transaction_journal_id')->toArray();
|
|
|
|
$search = [];
|
|
|
|
foreach ($journalMeta as $journalId) {
|
|
|
|
$search[] = ['id' => (int)$journalId];
|
|
|
|
}
|
|
|
|
/** @var JournalCollectorInterface $collector */
|
|
|
|
$collector = app(JournalCollectorInterface::class);
|
|
|
|
$collector->setUser($recurrence->user);
|
|
|
|
$collector->withOpposingAccount()->setAllAssetAccounts()->
|
|
|
|
withCategoryInformation()->withBudgetInformation()->setLimit($pageSize)->setPage($page);
|
|
|
|
// filter on specific journals.
|
2018-07-02 13:41:28 -05:00
|
|
|
$collector->removeFilter(InternalTransferFilter::class);
|
2018-07-02 13:39:45 -05:00
|
|
|
$collector->setJournals(new Collection($search));
|
|
|
|
|
|
|
|
return $collector->getPaginatedJournals();
|
|
|
|
}
|
|
|
|
|
2018-06-10 09:59:03 -05:00
|
|
|
/**
|
|
|
|
* Calculate the next X iterations starting on the date given in $date.
|
|
|
|
*
|
|
|
|
* @param RecurrenceRepetition $repetition
|
|
|
|
* @param Carbon $date
|
|
|
|
* @param int $count
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
2018-06-16 14:47:51 -05:00
|
|
|
public function getXOccurrences(RecurrenceRepetition $repetition, Carbon $date, int $count): array
|
2018-06-10 09:59:03 -05:00
|
|
|
{
|
2018-06-16 14:47:51 -05:00
|
|
|
$return = [];
|
|
|
|
$mutator = clone $date;
|
|
|
|
$skipMod = $repetition->repetition_skip + 1;
|
|
|
|
$total = 0;
|
|
|
|
$attempts = 0;
|
2018-06-10 09:59:03 -05:00
|
|
|
switch ($repetition->repetition_type) {
|
|
|
|
default:
|
|
|
|
throw new FireflyException(
|
|
|
|
sprintf('Cannot calculate occurrences for recurring transaction repetition type "%s"', $repetition->repetition_type)
|
|
|
|
);
|
|
|
|
case 'daily':
|
2018-06-16 14:47:51 -05:00
|
|
|
while ($total < $count) {
|
2018-06-10 09:59:03 -05:00
|
|
|
$mutator->addDay();
|
2018-06-16 14:47:51 -05:00
|
|
|
if ($attempts % $skipMod === 0) {
|
|
|
|
$return[] = clone $mutator;
|
|
|
|
$total++;
|
|
|
|
}
|
|
|
|
$attempts++;
|
2018-06-10 09:59:03 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'weekly':
|
|
|
|
// monday = 1
|
|
|
|
// sunday = 7
|
|
|
|
$mutator->addDay(); // always assume today has passed.
|
|
|
|
$dayOfWeek = (int)$repetition->repetition_moment;
|
|
|
|
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);
|
2018-06-16 14:47:51 -05:00
|
|
|
|
|
|
|
while ($total < $count) {
|
|
|
|
if ($attempts % $skipMod === 0) {
|
|
|
|
$return[] = clone $mutator;
|
|
|
|
$total++;
|
|
|
|
}
|
|
|
|
$attempts++;
|
2018-06-10 09:59:03 -05:00
|
|
|
$mutator->addWeek();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'monthly':
|
|
|
|
$mutator->addDay(); // always assume today has passed.
|
|
|
|
$dayOfMonth = (int)$repetition->repetition_moment;
|
|
|
|
if ($mutator->day > $dayOfMonth) {
|
|
|
|
// day has passed already, add a month.
|
|
|
|
$mutator->addMonth();
|
|
|
|
}
|
|
|
|
|
2018-06-16 14:47:51 -05:00
|
|
|
while ($total < $count) {
|
2018-06-10 09:59:03 -05:00
|
|
|
$domCorrected = min($dayOfMonth, $mutator->daysInMonth);
|
|
|
|
$mutator->day = $domCorrected;
|
2018-06-16 14:47:51 -05:00
|
|
|
if ($attempts % $skipMod === 0) {
|
|
|
|
$return[] = clone $mutator;
|
|
|
|
$total++;
|
|
|
|
}
|
|
|
|
$attempts++;
|
2018-06-10 09:59:03 -05:00
|
|
|
$mutator->endOfMonth()->addDay();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'ndom':
|
|
|
|
$mutator->addDay(); // always assume today has passed.
|
|
|
|
$mutator->startOfMonth();
|
|
|
|
// this feels a bit like a cop out but why reinvent the wheel?
|
|
|
|
$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',];
|
|
|
|
$parts = explode(',', $repetition->repetition_moment);
|
2018-06-16 14:47:51 -05:00
|
|
|
|
|
|
|
while ($total < $count) {
|
2018-06-10 09:59:03 -05:00
|
|
|
$string = sprintf('%s %s of %s %s', $counters[$parts[0]], $daysOfWeek[$parts[1]], $mutator->format('F'), $mutator->format('Y'));
|
|
|
|
$newCarbon = new Carbon($string);
|
2018-06-16 14:47:51 -05:00
|
|
|
if ($attempts % $skipMod === 0) {
|
|
|
|
$return[] = clone $newCarbon;
|
|
|
|
$total++;
|
|
|
|
}
|
|
|
|
$attempts++;
|
2018-06-10 09:59:03 -05:00
|
|
|
$mutator->endOfMonth()->addDay();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'yearly':
|
|
|
|
$date = new Carbon($repetition->repetition_moment);
|
|
|
|
$date->year = $mutator->year;
|
|
|
|
if ($mutator > $date) {
|
|
|
|
$date->addYear();
|
|
|
|
}
|
2018-06-16 14:47:51 -05:00
|
|
|
$obj = clone $date;
|
|
|
|
while ($total < $count) {
|
|
|
|
if ($attempts % $skipMod === 0) {
|
|
|
|
$return[] = clone $obj;
|
|
|
|
$total++;
|
|
|
|
}
|
|
|
|
$obj->addYears(1);
|
|
|
|
$attempts++;
|
2018-06-10 09:59:03 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-06-26 14:17:50 -05:00
|
|
|
// filter out all the weekend days:
|
|
|
|
$return = $this->filterWeekends($repetition, $return);
|
2018-06-10 09:59:03 -05:00
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the repetition in a string that is user readable.
|
|
|
|
*
|
|
|
|
* @param RecurrenceRepetition $repetition
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function repetitionDescription(RecurrenceRepetition $repetition): string
|
|
|
|
{
|
|
|
|
/** @var Preference $pref */
|
|
|
|
$pref = app('preferences')->getForUser($this->user, 'language', config('firefly.default_language', 'en_US'));
|
|
|
|
$language = $pref->data;
|
|
|
|
switch ($repetition->repetition_type) {
|
|
|
|
default:
|
|
|
|
throw new FireflyException(sprintf('Cannot translate recurring transaction repetition type "%s"', $repetition->repetition_type));
|
|
|
|
break;
|
|
|
|
case 'daily':
|
|
|
|
return trans('firefly.recurring_daily', [], $language);
|
|
|
|
break;
|
|
|
|
case 'weekly':
|
|
|
|
$dayOfWeek = trans(sprintf('config.dow_%s', $repetition->repetition_moment), [], $language);
|
|
|
|
|
|
|
|
return trans('firefly.recurring_weekly', ['weekday' => $dayOfWeek], $language);
|
|
|
|
break;
|
|
|
|
case 'monthly':
|
|
|
|
// format a date:
|
|
|
|
return trans('firefly.recurring_monthly', ['dayOfMonth' => $repetition->repetition_moment], $language);
|
|
|
|
break;
|
|
|
|
case 'ndom':
|
|
|
|
$parts = explode(',', $repetition->repetition_moment);
|
|
|
|
// first part is number of week, second is weekday.
|
|
|
|
$dayOfWeek = trans(sprintf('config.dow_%s', $parts[1]), [], $language);
|
|
|
|
|
|
|
|
return trans('firefly.recurring_ndom', ['weekday' => $dayOfWeek, 'dayOfMonth' => $parts[0]], $language);
|
|
|
|
break;
|
|
|
|
case 'yearly':
|
|
|
|
//
|
|
|
|
$today = new Carbon;
|
|
|
|
$today->endOfYear();
|
|
|
|
$repDate = Carbon::createFromFormat('Y-m-d', $repetition->repetition_moment);
|
|
|
|
$diffInYears = $today->diffInYears($repDate);
|
|
|
|
$repDate->addYears($diffInYears); // technically not necessary.
|
|
|
|
$string = $repDate->formatLocalized(trans('config.month_and_day_no_year'));
|
|
|
|
|
|
|
|
return trans('firefly.recurring_yearly', ['date' => $string], $language);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set user for in repository.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user): void
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
2018-06-16 14:47:51 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Recurrence
|
|
|
|
*/
|
|
|
|
public function store(array $data): Recurrence
|
|
|
|
{
|
|
|
|
$factory = new RecurrenceFactory;
|
|
|
|
$factory->setUser($this->user);
|
|
|
|
|
|
|
|
return $factory->create($data);
|
|
|
|
}
|
2018-06-21 11:57:51 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a recurring transaction.
|
|
|
|
*
|
|
|
|
* @param Recurrence $recurrence
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Recurrence
|
2018-06-22 11:42:23 -05:00
|
|
|
* @throws FireflyException
|
2018-06-21 11:57:51 -05:00
|
|
|
*/
|
|
|
|
public function update(Recurrence $recurrence, array $data): Recurrence
|
|
|
|
{
|
|
|
|
/** @var RecurrenceUpdateService $service */
|
|
|
|
$service = app(RecurrenceUpdateService::class);
|
|
|
|
|
|
|
|
return $service->update($recurrence, $data);
|
|
|
|
}
|
2018-06-26 11:49:33 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters out all weekend entries, if necessary.
|
|
|
|
*
|
|
|
|
* @param RecurrenceRepetition $repetition
|
|
|
|
* @param array $dates
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function filterWeekends(RecurrenceRepetition $repetition, array $dates): array
|
|
|
|
{
|
2018-07-01 02:27:22 -05:00
|
|
|
if ((int)$repetition->weekend === RecurrenceRepetition::WEEKEND_DO_NOTHING) {
|
2018-06-27 23:31:31 -05:00
|
|
|
Log::debug('Repetition will not be filtered on weekend days.');
|
|
|
|
|
2018-06-26 11:49:33 -05:00
|
|
|
return $dates;
|
|
|
|
}
|
|
|
|
$return = [];
|
|
|
|
/** @var Carbon $date */
|
|
|
|
foreach ($dates as $date) {
|
|
|
|
$isWeekend = $date->isWeekend();
|
2018-06-26 14:17:50 -05:00
|
|
|
if (!$isWeekend) {
|
|
|
|
$return[] = clone $date;
|
2018-06-27 23:31:31 -05:00
|
|
|
Log::debug(sprintf('Date is %s, not a weekend date.', $date->format('D d M Y')));
|
2018-06-26 14:17:50 -05:00
|
|
|
continue;
|
|
|
|
}
|
2018-06-26 11:49:33 -05:00
|
|
|
|
2018-06-26 14:17:50 -05:00
|
|
|
// is weekend and must set back to Friday?
|
2018-06-27 23:31:31 -05:00
|
|
|
if ($repetition->weekend === RecurrenceRepetition::WEEKEND_TO_FRIDAY) {
|
2018-06-26 11:49:33 -05:00
|
|
|
$clone = clone $date;
|
2018-06-26 14:17:50 -05:00
|
|
|
$clone->addDays(5 - $date->dayOfWeekIso);
|
2018-06-27 23:31:31 -05:00
|
|
|
Log::debug(
|
|
|
|
sprintf('Date is %s, and this is in the weekend, so corrected to %s (Friday).', $date->format('D d M Y'), $clone->format('D d M Y'))
|
|
|
|
);
|
2018-06-26 14:17:50 -05:00
|
|
|
$return[] = clone $clone;
|
2018-06-27 23:31:31 -05:00
|
|
|
continue;
|
2018-06-26 11:49:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// postpone to Monday?
|
2018-06-27 23:31:31 -05:00
|
|
|
if ($repetition->weekend === RecurrenceRepetition::WEEKEND_TO_MONDAY) {
|
2018-06-26 11:49:33 -05:00
|
|
|
$clone = clone $date;
|
|
|
|
$clone->addDays(8 - $date->dayOfWeekIso);
|
2018-06-27 23:31:31 -05:00
|
|
|
Log::debug(
|
|
|
|
sprintf('Date is %s, and this is in the weekend, so corrected to %s (Monday).', $date->format('D d M Y'), $clone->format('D d M Y'))
|
|
|
|
);
|
2018-06-26 11:49:33 -05:00
|
|
|
$return[] = $clone;
|
2018-06-27 23:31:31 -05:00
|
|
|
continue;
|
2018-06-26 11:49:33 -05:00
|
|
|
}
|
2018-06-27 23:31:31 -05:00
|
|
|
Log::debug(sprintf('Date is %s, removed from final result', $date->format('D d M Y')));
|
2018-06-26 11:49:33 -05:00
|
|
|
}
|
|
|
|
|
2018-06-26 14:17:50 -05:00
|
|
|
// filter unique dates
|
2018-06-27 23:31:31 -05:00
|
|
|
Log::debug(sprintf('Count before filtering: %d', \count($dates)));
|
2018-06-26 14:17:50 -05:00
|
|
|
$collection = new Collection($return);
|
|
|
|
$filtered = $collection->unique();
|
|
|
|
$return = $filtered->toArray();
|
2018-06-26 11:49:33 -05:00
|
|
|
|
2018-06-27 23:31:31 -05:00
|
|
|
Log::debug(sprintf('Count after filtering: %d', \count($return)));
|
|
|
|
|
2018-06-26 11:49:33 -05:00
|
|
|
return $return;
|
|
|
|
}
|
2018-06-10 09:59:03 -05:00
|
|
|
}
|