This commit is contained in:
James Cole
2024-11-03 08:16:46 +01:00
parent dc1ecf6a42
commit 7028cb1546
7 changed files with 118 additions and 101 deletions

View File

@@ -46,10 +46,15 @@ class Cron extends Command
protected $signature = 'firefly-iii:cron
{--F|force : Force the cron job(s) to execute.}
{--date= : Set the date in YYYY-MM-DD to make Firefly III think that\'s the current date.}
{--download-cer : Download exchange rates. Other tasks will be skipped unless also requested.}
{--create-recurring : Create recurring transactions. Other tasks will be skipped unless also requested.}
{--create-auto-budgets : Create auto budgets. Other tasks will be skipped unless also requested.}
{--send-bill-warnings : Send bill warnings. Other tasks will be skipped unless also requested.}
';
public function handle(): int
{
$doAll = !$this->option('download-cer') && !$this->option('create-recurring') && !$this->option('create-auto-budgets') && !$this->option('send-bill-warnings');
$date = null;
try {
@@ -60,7 +65,7 @@ class Cron extends Command
$force = (bool)$this->option('force'); // @phpstan-ignore-line
// Fire exchange rates cron job.
if (true === config('cer.download_enabled')) {
if (true === config('cer.download_enabled') && ($doAll || $this->option('download-cer'))) {
try {
$this->exchangeRatesCronJob($force, $date);
} catch (FireflyException $e) {
@@ -71,6 +76,7 @@ class Cron extends Command
}
// Fire recurring transaction cron job.
if($doAll || $this->option('create-recurring')) {
try {
$this->recurringCronJob($force, $date);
} catch (FireflyException $e) {
@@ -78,8 +84,10 @@ class Cron extends Command
app('log')->error($e->getTraceAsString());
$this->friendlyError($e->getMessage());
}
}
// Fire auto-budget cron job:
if($doAll || $this->option('create-auto-budgets')) {
try {
$this->autoBudgetCronJob($force, $date);
} catch (FireflyException $e) {
@@ -87,8 +95,10 @@ class Cron extends Command
app('log')->error($e->getTraceAsString());
$this->friendlyError($e->getMessage());
}
}
// Fire bill warning cron job
if($doAll || $this->option('send-bill-warnings')) {
try {
$this->billWarningCronJob($force, $date);
} catch (FireflyException $e) {
@@ -96,6 +106,7 @@ class Cron extends Command
app('log')->error($e->getTraceAsString());
$this->friendlyError($e->getMessage());
}
}
$this->friendlyInfo('More feedback on the cron jobs can be found in the log files.');

View File

@@ -88,6 +88,7 @@ class ShowController extends Controller
$groups = $this->recurring->getTransactions($recurrence);
$today = today(config('app.timezone'));
$array['repeat_until'] = null !== $array['repeat_until'] ? new Carbon($array['repeat_until']) : null;
$array['journal_count'] = $this->recurring->getJournalCount($recurrence);
// transform dates back to Carbon objects and expand information
foreach ($array['repetitions'] as $index => $repetition) {

View File

@@ -177,10 +177,11 @@ class CreateRecurringTransactions implements ShouldQueue
// has repeated X times.
$journalCount = $this->repository->getJournalCount($recurrence);
if (0 !== $recurrence->repetitions && $journalCount >= $recurrence->repetitions && false === $this->force) {
app('log')->info(sprintf('Recurrence #%d has run %d times, so will run no longer.', $recurrence->id, $recurrence->repetitions));
app('log')->info(sprintf('Recurrence #%d has run %d times, so will run no longer.', $recurrence->id, $journalCount));
return false;
}
app('log')->debug(sprintf('Recurrence #%d has run %d times, max is %d times.', $recurrence->id, $journalCount, $recurrence->repetitions));
// is no longer running
if ($this->repeatUntilHasPassed($recurrence)) {
@@ -202,8 +203,8 @@ class CreateRecurringTransactions implements ShouldQueue
sprintf(
'Recurrence #%d is set to run on %s, and today\'s date is %s. Skipped.',
$recurrence->id,
$recurrence->first_date->format('Y-m-d'),
$this->date->format('Y-m-d')
$recurrence->first_date->format('Y-m-d H:i:s'),
$this->date->format('Y-m-d H:i:s')
)
);
@@ -244,9 +245,10 @@ class CreateRecurringTransactions implements ShouldQueue
private function hasNotStartedYet(Recurrence $recurrence): bool
{
$startDate = $this->getStartDate($recurrence);
app('log')->debug(sprintf('Start date is %s', $startDate->format('Y-m-d')));
app('log')->debug(sprintf('Start date is %s', $startDate->format('Y-m-d H:i:s')));
app('log')->debug(sprintf('Ask date is %s', $this->date->format('Y-m-d H:i:s')));
return $startDate->gt($this->date);
return $startDate->gte($this->date);
}
/**

View File

@@ -79,8 +79,7 @@ class RecurringRepository implements RecurringRepositoryInterface
$q2->where('data', json_encode($string));
})
->where('transaction_journal_id', $journalMeta->transaction_journal_id)
->count()
;
->count();
if ($count > 0) {
app('log')->debug(sprintf('Looks like journal #%d was already created', $journalMeta->transaction_journal_id));
@@ -101,8 +100,7 @@ class RecurringRepository implements RecurringRepositoryInterface
->orderBy('active', 'DESC')
->orderBy('transaction_type_id', 'ASC')
->orderBy('title', 'ASC')
->get()
;
->get();
}
/**
@@ -130,8 +128,7 @@ class RecurringRepository implements RecurringRepositoryInterface
return Recurrence::with(['TransactionCurrency', 'TransactionType', 'RecurrenceRepetitions', 'RecurrenceTransactions'])
->orderBy('active', 'DESC')
->orderBy('title', 'ASC')
->get()
;
->get();
}
public function getBillId(RecurrenceTransaction $recTransaction): ?int
@@ -204,20 +201,21 @@ class RecurringRepository implements RecurringRepositoryInterface
*/
public function getJournalCount(Recurrence $recurrence, ?Carbon $start = null, ?Carbon $end = null): int
{
Log::debug(sprintf('Now in getJournalCount(#%d, "%s", "%s")', $recurrence->id, $start?->format('Y-m-d H:i:s'), $end?->format('Y-m-d H:i:s')));
$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.'"')
;
->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'));
}
return $query->count('transaction_journals.id');
$count = $query->count('transaction_journals.id');
Log::debug(sprintf('Count is %d', $count));
return $count;
}
/**
@@ -229,8 +227,7 @@ class RecurringRepository implements RecurringRepositoryInterface
->where('transaction_journals.user_id', $this->user->id)
->where('journal_meta.name', '=', 'recurrence_id')
->where('journal_meta.data', '=', json_encode((string) $recurrence->id))
->get(['journal_meta.transaction_journal_id'])->pluck('transaction_journal_id')->toArray()
;
->get(['journal_meta.transaction_journal_id'])->pluck('transaction_journal_id')->toArray();
}
/**
@@ -282,8 +279,7 @@ class RecurringRepository implements RecurringRepositoryInterface
->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()
;
->get()->pluck('transaction_journal_id')->toArray();
$search = [];
foreach ($journalMeta as $journalId) {
$search[] = (int) $journalId;
@@ -294,8 +290,7 @@ class RecurringRepository implements RecurringRepositoryInterface
$collector->setUser($recurrence->user);
$collector->withCategoryInformation()->withBudgetInformation()->setLimit($pageSize)->setPage($page)
->withAccountInformation()
;
->withAccountInformation();
$collector->setJournalIds($search);
return $collector->getPaginatedGroups();
@@ -315,8 +310,7 @@ class RecurringRepository implements RecurringRepositoryInterface
->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()
;
->get()->pluck('transaction_journal_id')->toArray();
$search = [];
foreach ($journalMeta as $journalId) {
@@ -499,8 +493,7 @@ class RecurringRepository implements RecurringRepositoryInterface
$search->whereLike('recurrences.title', sprintf('%%%s%%', $query));
}
$search
->orderBy('recurrences.title', 'ASC')
;
->orderBy('recurrences.title', 'ASC');
return $search->take($limit)->get(['id', 'title', 'description']);
}

View File

@@ -77,9 +77,7 @@ class RecurringCronjob extends AbstractCronjob
{
app('log')->info(sprintf('Will now fire recurring cron job task for date "%s".', $this->date->format('Y-m-d H:i:s')));
/** @var CreateRecurringTransactions $job */
$job = app(CreateRecurringTransactions::class);
$job->setDate($this->date);
$job = new CreateRecurringTransactions($this->date);
$job->setForce($this->force);
$job->handle();

View File

@@ -2627,6 +2627,7 @@ return [
'no_bills_create_default' => 'Create a bill',
// recurring transactions
'recurrence_max_count' => 'This recurring transactions will be created at most :max time(s), and has been created :count time(s) already.',
'create_right_now' => 'Create right now',
'no_new_transaction_in_recurrence' => 'No new transaction was created. Perhaps it was already fired for this date?',
'recurrences' => 'Recurring transactions',

View File

@@ -22,6 +22,17 @@
</div>
<div class="box-body">
<h4>{{ 'transaction_journal_meta'|_ }}</h4>
{% if array.nr_of_repetitions > 0 %}
<p>
{% if array.journal_count >= array.nr_of_repetitions %}
<span class="text-danger">{{ trans('firefly.recurrence_max_count', {count: array.journal_count, max: array.nr_of_repetitions}) }}</span>
{% endif %}
{% if array.journal_count < array.nr_of_repetitions %}
{{ trans('firefly.recurrence_max_count', {count: array.journal_count, max: array.nr_of_repetitions}) }}
{% endif %}
</p>
{% endif %}
<p>{{ 'description'|_ }}: <em>{{ array.description }}</em></p>
{% if array.active == false %}