Files
firefly-iii/app/Support/Cronjobs/AutoBudgetCronjob.php

86 lines
3.1 KiB
PHP
Raw Normal View History

2020-03-14 10:25:12 +01:00
<?php
2020-06-30 19:05:35 +02:00
2020-03-14 10:25:12 +01:00
/**
* AutoBudgetCronjob.php
2020-06-30 19:05:35 +02:00
* Copyright (c) 2020 james@firefly-iii.org
2020-03-14 10:25:12 +01:00
*
* 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/>.
*/
2020-06-30 19:05:35 +02:00
declare(strict_types=1);
2020-03-14 10:25:12 +01:00
namespace FireflyIII\Support\Cronjobs;
2020-03-14 10:25:12 +01:00
use Carbon\Carbon;
use FireflyIII\Jobs\CreateAutoBudgetLimits;
use FireflyIII\Models\Configuration;
2025-06-25 19:43:03 +02:00
use FireflyIII\Support\Facades\FireflyConfig;
2020-03-14 10:25:12 +01:00
/**
* Class AutoBudgetCronjob
*/
class AutoBudgetCronjob extends AbstractCronjob
{
2021-03-21 11:06:08 +01:00
public function fire(): void
2020-03-14 10:25:12 +01:00
{
/** @var Configuration $config */
2025-06-25 19:43:03 +02:00
$config = FireflyConfig::get('last_ab_job', 0);
2024-12-22 08:43:12 +01:00
$lastTime = (int) $config->data;
2025-05-27 17:06:15 +02:00
$diff = Carbon::now()->getTimestamp() - $lastTime;
2023-02-11 07:36:45 +01:00
$diffForHumans = today(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true);
2020-03-14 10:25:12 +01:00
if (0 === $lastTime) {
2023-10-29 06:31:27 +01:00
app('log')->info('Auto budget cron-job has never fired before.');
2020-03-14 10:25:12 +01:00
}
// less than half a day ago:
if ($lastTime > 0 && $diff <= 43200) {
2023-10-29 06:31:27 +01:00
app('log')->info(sprintf('It has been %s since the auto budget cron-job has fired.', $diffForHumans));
2020-03-14 10:25:12 +01:00
if (false === $this->force) {
2023-10-29 06:31:27 +01:00
app('log')->info('The auto budget cron-job will not fire now.');
2021-03-21 11:06:08 +01:00
$this->message = sprintf('It has been %s since the auto budget cron-job has fired. It will not fire now.', $diffForHumans);
2021-03-21 11:06:08 +01:00
return;
2020-03-14 10:25:12 +01:00
}
2023-11-05 16:55:16 +01:00
app('log')->info('Execution of the auto budget cron-job has been FORCED.');
2020-03-14 10:25:12 +01:00
}
if ($lastTime > 0 && $diff > 43200) {
2023-10-29 06:31:27 +01:00
app('log')->info(sprintf('It has been %s since the auto budget cron-job has fired. It will fire now!', $diffForHumans));
2020-03-14 10:25:12 +01:00
}
$this->fireAutoBudget();
app('preferences')->mark();
}
private function fireAutoBudget(): void
{
2023-10-29 06:31:27 +01:00
app('log')->info(sprintf('Will now fire auto budget cron job task for date "%s".', $this->date->format('Y-m-d')));
2023-12-20 19:35:52 +01:00
2020-03-14 10:25:12 +01:00
/** @var CreateAutoBudgetLimits $job */
$job = app(CreateAutoBudgetLimits::class, [$this->date]);
2020-03-14 10:25:12 +01:00
$job->setDate($this->date);
$job->handle();
2021-03-21 11:06:08 +01:00
// get stuff from job:
$this->jobFired = true;
$this->jobErrored = false;
$this->jobSucceeded = true;
$this->message = 'Auto-budget cron job fired successfully.';
2025-06-25 19:43:03 +02:00
FireflyConfig::set('last_ab_job', (int) $this->date->format('U'));
2023-10-29 06:31:27 +01:00
app('log')->info('Done with auto budget cron job task.');
2020-03-14 10:25:12 +01:00
}
}