Files
firefly-iii/app/Http/Controllers/Recurring/TriggerController.php

94 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2023-01-15 15:47:25 +01:00
2023-07-15 16:02:42 +02:00
/*
* TriggerController.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program 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 Affero General Public License for more details.
*
* 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/>.
*/
2022-12-29 15:44:13 +01:00
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Recurring;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\TriggerRecurrenceRequest;
use FireflyIII\Jobs\CreateRecurringTransactions;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
/**
* Class TriggerController
*/
class TriggerController extends Controller
{
/**
2023-06-21 12:34:58 +02:00
* @param Recurrence $recurrence
* @param TriggerRecurrenceRequest $request
2023-07-15 16:02:42 +02:00
*
* @return RedirectResponse
*/
public function trigger(Recurrence $recurrence, TriggerRecurrenceRequest $request): RedirectResponse
{
$all = $request->getAll();
$date = $all['date'];
// grab the date from the last time the recurrence fired:
$backupDate = $recurrence->latest_date;
// fire the recurring cron job on the given date, then post-date the created transaction.
Log::info(sprintf('Trigger: will now fire recurring cron job task for date "%s".', $date->format('Y-m-d H:i:s')));
/** @var CreateRecurringTransactions $job */
$job = app(CreateRecurringTransactions::class);
$job->setRecurrences(new Collection([$recurrence]));
$job->setDate($date);
$job->setForce(false);
$job->handle();
Log::debug('Done with recurrence.');
$groups = $job->getGroups();
/** @var TransactionGroup $group */
foreach ($groups as $group) {
/** @var TransactionJournal $journal */
foreach ($group->transactionJournals as $journal) {
2022-12-30 09:28:03 +01:00
Log::debug(sprintf('Set date of journal #%d to today!', $journal->id));
2023-02-11 07:36:45 +01:00
$journal->date = today(config('app.timezone'));
$journal->save();
}
}
$recurrence->latest_date = $backupDate;
$recurrence->save();
app('preferences')->mark();
if (0 === $groups->count()) {
$request->session()->flash('info', (string)trans('firefly.no_new_transaction_in_recurrence'));
}
if (1 === $groups->count()) {
$first = $groups->first();
$request->session()->flash('success', (string)trans('firefly.stored_journal_no_descr'));
$request->session()->flash('success_url', route('transactions.show', [$first->id]));
}
return redirect(route('recurring.show', [$recurrence->id]));
}
}