From 3c082dcf0e199ecbbbffdb7acf46056bbfd1a036 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 16 May 2023 21:04:06 +0200 Subject: [PATCH] Restore cron options. --- app/Console/Commands/Tools/Cron.php | 6 +- app/Jobs/CreateAutoBudgetLimits.php | 13 ++-- app/Support/Http/Controllers/CronRunner.php | 68 +++++++++++++++++++++ 3 files changed, 80 insertions(+), 7 deletions(-) diff --git a/app/Console/Commands/Tools/Cron.php b/app/Console/Commands/Tools/Cron.php index 0ff0874a63..83c9d1f222 100644 --- a/app/Console/Commands/Tools/Cron.php +++ b/app/Console/Commands/Tools/Cron.php @@ -77,7 +77,7 @@ class Cron extends Command */ if (true === config('cer.enabled')) { try { - //$this->exchangeRatesCronJob($force, $date); + $this->exchangeRatesCronJob($force, $date); } catch (FireflyException $e) { Log::error($e->getMessage()); Log::error($e->getTraceAsString()); @@ -89,7 +89,7 @@ class Cron extends Command * Fire recurring transaction cron job. */ try { - //$this->recurringCronJob($force, $date); + $this->recurringCronJob($force, $date); } catch (FireflyException $e) { Log::error($e->getMessage()); Log::error($e->getTraceAsString()); @@ -111,7 +111,7 @@ class Cron extends Command * Fire bill warning cron job */ try { - //$this->billWarningCronJob($force, $date); + $this->billWarningCronJob($force, $date); } catch (FireflyException $e) { Log::error($e->getMessage()); Log::error($e->getTraceAsString()); diff --git a/app/Jobs/CreateAutoBudgetLimits.php b/app/Jobs/CreateAutoBudgetLimits.php index fba256ac57..9ac67dfa13 100644 --- a/app/Jobs/CreateAutoBudgetLimits.php +++ b/app/Jobs/CreateAutoBudgetLimits.php @@ -128,20 +128,25 @@ class CreateAutoBudgetLimits implements ShouldQueue // what you spent in previous period PLUS the amount for the current period, // if that is more than zero, that's the amount that will be set. - $budgetAvailable = bcadd(bcadd($budgetLimit->amount, $autoBudget->amount), $spentAmount); - $totalAmount = $autoBudget->amount; + $budgetAvailable = bcadd(bcadd($budgetLimit->amount, $autoBudget->amount), $spentAmount); + $totalAmount = $autoBudget->amount; Log::debug(sprintf('Total amount available for current budget period is %s', $budgetAvailable)); - if (-1 !== bccomp( $budgetAvailable, $totalAmount)) { + if (-1 !== bccomp($budgetAvailable, $totalAmount)) { Log::info(sprintf('There is no overspending, no need to adjust. Budget limit amount will be %s.', $totalAmount)); // create budget limit: $this->createBudgetLimit($autoBudget, $start, $end, $totalAmount); } - if (1 !== bccomp($budgetAvailable, $totalAmount)) { + if (1 !== bccomp($budgetAvailable, $totalAmount) && 1 === bccomp($budgetAvailable, '0')) { Log::info(sprintf('There was overspending, so the new amount will be %s.', $budgetAvailable)); // create budget limit: $this->createBudgetLimit($autoBudget, $start, $end, $budgetAvailable); } + if (1 !== bccomp($budgetAvailable, $totalAmount) && -1 === bccomp($budgetAvailable, '0')) { + Log::info('There was overspending, but so much even this period cant fix that. Reset it to 1.'); + // create budget limit: + $this->createBudgetLimit($autoBudget, $start, $end, '1'); + } Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id)); } diff --git a/app/Support/Http/Controllers/CronRunner.php b/app/Support/Http/Controllers/CronRunner.php index fe43ad27d6..b8f2e97f6e 100644 --- a/app/Support/Http/Controllers/CronRunner.php +++ b/app/Support/Http/Controllers/CronRunner.php @@ -26,6 +26,8 @@ namespace FireflyIII\Support\Http\Controllers; use Carbon\Carbon; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Support\Cronjobs\AutoBudgetCronjob; +use FireflyIII\Support\Cronjobs\BillWarningCronjob; +use FireflyIII\Support\Cronjobs\ExchangeRatesCronjob; use FireflyIII\Support\Cronjobs\RecurringCronjob; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -98,4 +100,70 @@ trait CronRunner 'message' => $recurring->message, ]; } + + + /** + * @param bool $force + * @param Carbon $date + * + * @return array + */ + protected function exchangeRatesCronJob(bool $force, Carbon $date): array + { + /** @var ExchangeRatesCronjob $exchangeRates */ + $exchangeRates = app(ExchangeRatesCronjob::class); + $exchangeRates->setForce($force); + $exchangeRates->setDate($date); + try { + $exchangeRates->fire(); + } catch (FireflyException $e) { + return [ + 'job_fired' => false, + 'job_succeeded' => false, + 'job_errored' => true, + 'message' => $e->getMessage(), + ]; + } + + return [ + 'job_fired' => $exchangeRates->jobFired, + 'job_succeeded' => $exchangeRates->jobSucceeded, + 'job_errored' => $exchangeRates->jobErrored, + 'message' => $exchangeRates->message, + ]; + } + + + /** + * @param bool $force + * @param Carbon $date + * + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + protected function billWarningCronJob(bool $force, Carbon $date): array + { + /** @var BillWarningCronjob $billWarning */ + $billWarning = app(BillWarningCronjob::class); + $billWarning->setForce($force); + $billWarning->setDate($date); + try { + $billWarning->fire(); + } catch (FireflyException $e) { + return [ + 'job_fired' => false, + 'job_succeeded' => false, + 'job_errored' => true, + 'message' => $e->getMessage(), + ]; + } + + return [ + 'job_fired' => $billWarning->jobFired, + 'job_succeeded' => $billWarning->jobSucceeded, + 'job_errored' => $billWarning->jobErrored, + 'message' => $billWarning->message, + ]; + } }