mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 16:57:09 +00:00
Basic cron job for budgeting.
This commit is contained in:
@@ -28,8 +28,10 @@ use Carbon\Carbon;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Support\Cronjobs\RecurringCronjob;
|
use FireflyIII\Support\Cronjobs\RecurringCronjob;
|
||||||
|
use FireflyIII\Support\Cronjobs\AutoBudgetCronjob;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
use Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Cron
|
* Class Cron
|
||||||
@@ -56,39 +58,38 @@ class Cron extends Command
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int
|
* @return int
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
$date = null;
|
$date = null;
|
||||||
try {
|
try {
|
||||||
$date = new Carbon($this->option('date'));
|
$date = new Carbon($this->option('date'));
|
||||||
} catch (InvalidArgumentException $e) {
|
} catch (InvalidArgumentException|Exception $e) {
|
||||||
$this->error(sprintf('"%s" is not a valid date', $this->option('date')));
|
$this->error(sprintf('"%s" is not a valid date', $this->option('date')));
|
||||||
$e->getMessage();
|
$e->getMessage();
|
||||||
}
|
}
|
||||||
|
$force = (bool)$this->option('force');
|
||||||
|
|
||||||
|
/*
|
||||||
$recurring = new RecurringCronjob;
|
* Fire recurring transaction cron job.
|
||||||
$recurring->setForce($this->option('force'));
|
*/
|
||||||
|
|
||||||
// set date in cron job:
|
|
||||||
if (null !== $date) {
|
|
||||||
$recurring->setDate($date);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$result = $recurring->fire();
|
//$this->recurringCronJob($force, $date);
|
||||||
} catch (FireflyException $e) {
|
} catch (FireflyException $e) {
|
||||||
|
Log::error($e->getMessage());
|
||||||
|
Log::error($e->getTraceAsString());
|
||||||
$this->error($e->getMessage());
|
$this->error($e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
/*
|
||||||
}
|
* Fire auto-budget cron job:
|
||||||
if (false === $result) {
|
*/
|
||||||
$this->line('The recurring transaction cron job did not fire.');
|
try {
|
||||||
}
|
$this->autoBudgetCronJob($force, $date);
|
||||||
if (true === $result) {
|
} catch (FireflyException $e) {
|
||||||
$this->line('The recurring transaction cron job fired successfully.');
|
Log::error($e->getMessage());
|
||||||
|
Log::error($e->getTraceAsString());
|
||||||
|
$this->error($e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->info('More feedback on the cron jobs can be found in the log files.');
|
$this->info('More feedback on the cron jobs can be found in the log files.');
|
||||||
@@ -96,5 +97,54 @@ class Cron extends Command
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $force
|
||||||
|
* @param Carbon|null $date
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
private function autoBudgetCronJob(bool $force, ?Carbon $date)
|
||||||
|
{
|
||||||
|
$autoBudget = new AutoBudgetCronjob;
|
||||||
|
$autoBudget->setForce($force);
|
||||||
|
// set date in cron job:
|
||||||
|
if (null !== $date) {
|
||||||
|
$autoBudget->setDate($date);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $autoBudget->fire();
|
||||||
|
|
||||||
|
if (false === $result) {
|
||||||
|
$this->line('The auto budget cron job did not fire.');
|
||||||
|
}
|
||||||
|
if (true === $result) {
|
||||||
|
$this->line('The auto budget cron job fired successfully.');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $force
|
||||||
|
* @param Carbon|null $date
|
||||||
|
*
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
private function recurringCronJob(bool $force, ?Carbon $date): void
|
||||||
|
{
|
||||||
|
$recurring = new RecurringCronjob;
|
||||||
|
$recurring->setForce($force);
|
||||||
|
|
||||||
|
// set date in cron job:
|
||||||
|
if (null !== $date) {
|
||||||
|
$recurring->setDate($date);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $recurring->fire();
|
||||||
|
|
||||||
|
if (false === $result) {
|
||||||
|
$this->line('The recurring transaction cron job did not fire.');
|
||||||
|
}
|
||||||
|
if (true === $result) {
|
||||||
|
$this->line('The recurring transaction cron job fired successfully.');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
276
app/Jobs/CreateAutoBudgetLimits.php
Normal file
276
app/Jobs/CreateAutoBudgetLimits.php
Normal file
@@ -0,0 +1,276 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
/**
|
||||||
|
* CreateAutoBudgetLimits.php
|
||||||
|
* Copyright (c) 2020 thegrumpydictator@gmail.com
|
||||||
|
*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FireflyIII\Jobs;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
|
use FireflyIII\Models\AutoBudget;
|
||||||
|
use FireflyIII\Models\Budget;
|
||||||
|
use FireflyIII\Models\BudgetLimit;
|
||||||
|
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class CreateAutoBudgetLimits
|
||||||
|
*/
|
||||||
|
class CreateAutoBudgetLimits implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/** @var Carbon The current date */
|
||||||
|
private $date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @codeCoverageIgnore
|
||||||
|
*
|
||||||
|
* @param Carbon $date
|
||||||
|
*/
|
||||||
|
public function __construct(?Carbon $date)
|
||||||
|
{
|
||||||
|
if (null !== $date) {
|
||||||
|
$date->startOfDay();
|
||||||
|
$this->date = $date;
|
||||||
|
}
|
||||||
|
Log::debug(sprintf('Created new CreateAutoBudgetLimits("%s")', $this->date->format('Y-m-d')));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
Log::debug(sprintf('Now at start of CreateAutoBudgetLimits() job for %s.', $this->date->format('D d M Y')));
|
||||||
|
$autoBudgets = AutoBudget::get();
|
||||||
|
Log::debug(sprintf('Found %d auto budgets.', $autoBudgets->count()));
|
||||||
|
foreach ($autoBudgets as $autoBudget) {
|
||||||
|
$this->handleAutoBudget($autoBudget);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Carbon $date
|
||||||
|
*/
|
||||||
|
public function setDate(Carbon $date): void
|
||||||
|
{
|
||||||
|
$date->startOfDay();
|
||||||
|
$this->date = $date;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param AutoBudget $autoBudget
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
* @param string|null $amount
|
||||||
|
*/
|
||||||
|
private function createBudgetLimit(AutoBudget $autoBudget, Carbon $start, Carbon $end, ?string $amount = null)
|
||||||
|
{
|
||||||
|
Log::debug(sprintf('No budget limit exist. Must create one for auto-budget #%d', $autoBudget->id));
|
||||||
|
if (null !== $amount) {
|
||||||
|
Log::debug(sprintf('Amount is overruled and will be set to %s', $amount));
|
||||||
|
}
|
||||||
|
$budgetLimit = new BudgetLimit;
|
||||||
|
$budgetLimit->budget()->associate($autoBudget->budget);
|
||||||
|
$budgetLimit->transactionCurrency()->associate($autoBudget->transactionCurrency);
|
||||||
|
$budgetLimit->start_date = $start;
|
||||||
|
$budgetLimit->end_date = $end;
|
||||||
|
$budgetLimit->amount = $amount ?? $autoBudget->amount;
|
||||||
|
$budgetLimit->save();
|
||||||
|
|
||||||
|
Log::debug(sprintf('Created budget limit #%d.', $budgetLimit->id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param AutoBudget $autoBudget
|
||||||
|
*/
|
||||||
|
private function createRollover(AutoBudget $autoBudget): void
|
||||||
|
{
|
||||||
|
Log::debug(sprintf('Will now manage rollover for auto budget #%d', $autoBudget->id));
|
||||||
|
// current period:
|
||||||
|
$start = app('navigation')->startOfPeriod($this->date, $autoBudget->period);
|
||||||
|
$end = app('navigation')->endOfPeriod($start, $autoBudget->period);
|
||||||
|
|
||||||
|
// which means previous period:
|
||||||
|
$previousStart = app('navigation')->subtractPeriod($start, $autoBudget->period);
|
||||||
|
$previousEnd = app('navigation')->endOfPeriod($previousStart, $autoBudget->period);
|
||||||
|
|
||||||
|
Log::debug(
|
||||||
|
sprintf(
|
||||||
|
'Current period is %s-%s, so previous period is %s-%s',
|
||||||
|
$start->format('Y-m-d'), $end->format('Y-m-d'),
|
||||||
|
$previousStart->format('Y-m-d'), $previousEnd->format('Y-m-d')
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// has budget limit in previous period?
|
||||||
|
$budgetLimit = $this->findBudgetLimit($autoBudget->budget, $previousStart, $previousEnd);
|
||||||
|
|
||||||
|
if (null === $budgetLimit) {
|
||||||
|
Log::debug('No budget limit exists in previous period, so create one.');
|
||||||
|
// if not, create it and we're done.
|
||||||
|
$this->createBudgetLimit($autoBudget, $start, $end);
|
||||||
|
Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Log::debug('Budget limit exists for previous period.');
|
||||||
|
// if has one, calculate expenses and use that as a base.
|
||||||
|
$repository = app(OperationsRepositoryInterface::class);
|
||||||
|
$repository->setUser($autoBudget->budget->user);
|
||||||
|
$spent = $repository->sumExpenses($previousStart, $previousEnd, null, new Collection([$autoBudget->budget]), $autoBudget->transactionCurrency);
|
||||||
|
$currencyId = (int)$autoBudget->transaction_currency_id;
|
||||||
|
$spentAmount = $spent[$currencyId]['sum'] ?? '0';
|
||||||
|
Log::debug(sprintf('Spent in previous budget period (%s-%s) is %s', $previousStart->format('Y-m-d'), $previousEnd->format('Y-m-d'), $spentAmount));
|
||||||
|
|
||||||
|
// previous budget limit + this period + spent
|
||||||
|
$totalAmount = bcadd(bcadd($budgetLimit->amount, $autoBudget->amount), $spentAmount);
|
||||||
|
Log::debug(sprintf('Total amount for current budget period will be %s', $totalAmount));
|
||||||
|
|
||||||
|
if (1 !== bccomp($totalAmount, '0')) {
|
||||||
|
Log::info(sprintf('The total amount is negative, so it will be reset to %s.', $totalAmount));
|
||||||
|
$totalAmount = $autoBudget->amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
// create budget limit:
|
||||||
|
$this->createBudgetLimit($autoBudget, $start, $end, $totalAmount);
|
||||||
|
Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Budget $budget
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return BudgetLimit|null
|
||||||
|
*/
|
||||||
|
private function findBudgetLimit(Budget $budget, Carbon $start, Carbon $end): ?BudgetLimit
|
||||||
|
{
|
||||||
|
Log::debug(
|
||||||
|
sprintf(
|
||||||
|
'Going to find a budget limit for budget #%d ("%s") between %s and %s', $budget->id, $budget->name, $start->format('Y-m-d'),
|
||||||
|
$end->format('Y-m-d')
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
return $budget->budgetlimits()
|
||||||
|
->where('start_date', $start->format('Y-m-d'))
|
||||||
|
->where('end_date', $end->format('Y-m-d'))->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param AutoBudget $autoBudget
|
||||||
|
*
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
private function handleAutoBudget(AutoBudget $autoBudget): void
|
||||||
|
{
|
||||||
|
if (!$this->isMagicDay($autoBudget)) {
|
||||||
|
Log::info(
|
||||||
|
sprintf(
|
||||||
|
'Today (%s) is not a magic day for %s auto-budget #%d (part of budget #%d "%s")',
|
||||||
|
$this->date->format('Y-m-d'), $autoBudget->period, $autoBudget->id, $autoBudget->budget->id, $autoBudget->budget->name
|
||||||
|
)
|
||||||
|
);
|
||||||
|
Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Log::info(
|
||||||
|
sprintf(
|
||||||
|
'Today (%s) is a magic day for %s auto-budget #%d (part of budget #%d "%s")',
|
||||||
|
$this->date->format('Y-m-d'), $autoBudget->period, $autoBudget->id, $autoBudget->budget->id, $autoBudget->budget->name
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// get date range for budget limit, based on range in auto-budget
|
||||||
|
$start = app('navigation')->startOfPeriod($this->date, $autoBudget->period);
|
||||||
|
$end = app('navigation')->endOfPeriod($start, $autoBudget->period);
|
||||||
|
|
||||||
|
// find budget limit:
|
||||||
|
$budgetLimit = $this->findBudgetLimit($autoBudget->budget, $start, $end);
|
||||||
|
|
||||||
|
if (null === $budgetLimit && AutoBudget::AUTO_BUDGET_RESET === $autoBudget->auto_budget_type) {
|
||||||
|
// that's easy: create one.
|
||||||
|
// do nothing else.
|
||||||
|
$this->createBudgetLimit($autoBudget, $start, $end);
|
||||||
|
Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null === $budgetLimit && AutoBudget::AUTO_BUDGET_ROLLOVER === $autoBudget->auto_budget_type) {
|
||||||
|
// budget limit exists already,
|
||||||
|
$this->createRollover($autoBudget);
|
||||||
|
Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Log::debug(sprintf('Done with auto budget #%d', $autoBudget->id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param AutoBudget $autoBudget
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
private function isMagicDay(AutoBudget $autoBudget): bool
|
||||||
|
{
|
||||||
|
switch ($autoBudget->period) {
|
||||||
|
default:
|
||||||
|
throw new FireflyException(sprintf('isMagicDay() can\'t handle period "%s"', $autoBudget->period));
|
||||||
|
case 'daily':
|
||||||
|
// every day is magic!
|
||||||
|
return true;
|
||||||
|
case 'weekly':
|
||||||
|
// fire on Monday.
|
||||||
|
return $this->date->isMonday();
|
||||||
|
case 'monthly':
|
||||||
|
return 1 === $this->date->day;
|
||||||
|
case 'quarterly':
|
||||||
|
$format = 'm-d';
|
||||||
|
$value = $this->date->format($format);
|
||||||
|
|
||||||
|
return in_array($value, ['01-01', '04-01', '07-01', '10-01'], true);
|
||||||
|
case 'half_year':
|
||||||
|
$format = 'm-d';
|
||||||
|
$value = $this->date->format($format);
|
||||||
|
|
||||||
|
return in_array($value, ['01-01', '07-01'], true);
|
||||||
|
break;
|
||||||
|
case 'yearly':
|
||||||
|
$format = 'm-d';
|
||||||
|
$value = $this->date->format($format);
|
||||||
|
|
||||||
|
return '01-01' === $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -23,8 +23,11 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Support\Cronjobs;
|
namespace FireflyIII\Support\Cronjobs;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Exception;
|
||||||
/**
|
/**
|
||||||
* Class AbstractCronjob
|
* Class AbstractCronjob
|
||||||
|
*
|
||||||
* @codeCoverageIgnore
|
* @codeCoverageIgnore
|
||||||
*/
|
*/
|
||||||
abstract class AbstractCronjob
|
abstract class AbstractCronjob
|
||||||
@@ -32,6 +35,41 @@ abstract class AbstractCronjob
|
|||||||
/** @var int */
|
/** @var int */
|
||||||
public $timeBetweenRuns = 43200;
|
public $timeBetweenRuns = 43200;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
protected $force;
|
||||||
|
|
||||||
|
/** @var Carbon */
|
||||||
|
protected $date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AbstractCronjob constructor.
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->force = false;
|
||||||
|
$this->date = new Carbon;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $force
|
||||||
|
*/
|
||||||
|
public function setForce(bool $force): void
|
||||||
|
{
|
||||||
|
$this->force = $force;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Carbon $date
|
||||||
|
*/
|
||||||
|
public function setDate(Carbon $date): void
|
||||||
|
{
|
||||||
|
$this->date = $date;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
|
88
app/Support/Cronjobs/AutoBudgetCronjob.php
Normal file
88
app/Support/Cronjobs/AutoBudgetCronjob.php
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* AutoBudgetCronjob.php
|
||||||
|
* Copyright (c) 2020 thegrumpydictator@gmail.com
|
||||||
|
*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FireflyIII\Support\Cronjobs;
|
||||||
|
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use FireflyIII\Jobs\CreateAutoBudgetLimits;
|
||||||
|
use FireflyIII\Models\Configuration;
|
||||||
|
use Log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class AutoBudgetCronjob
|
||||||
|
*/
|
||||||
|
class AutoBudgetCronjob extends AbstractCronjob
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function fire(): bool
|
||||||
|
{
|
||||||
|
/** @var Configuration $config */
|
||||||
|
$config = app('fireflyconfig')->get('last_ab_job', 0);
|
||||||
|
$lastTime = (int)$config->data;
|
||||||
|
$diff = time() - $lastTime;
|
||||||
|
$diffForHumans = Carbon::now()->diffForHumans(Carbon::createFromTimestamp($lastTime), true);
|
||||||
|
if (0 === $lastTime) {
|
||||||
|
Log::info('Auto budget cron-job has never fired before.');
|
||||||
|
}
|
||||||
|
// less than half a day ago:
|
||||||
|
if ($lastTime > 0 && $diff <= 43200) {
|
||||||
|
Log::info(sprintf('It has been %s since the auto budget cron-job has fired.', $diffForHumans));
|
||||||
|
if (false === $this->force) {
|
||||||
|
Log::info('The auto budget cron-job will not fire now.');
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// fire job regardless.
|
||||||
|
if (true === $this->force) {
|
||||||
|
Log::info('Execution of the auto budget cron-job has been FORCED.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($lastTime > 0 && $diff > 43200) {
|
||||||
|
Log::info(sprintf('It has been %s since the auto budget cron-job has fired. It will fire now!', $diffForHumans));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fireAutoBudget();
|
||||||
|
|
||||||
|
app('preferences')->mark();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private function fireAutoBudget(): void
|
||||||
|
{
|
||||||
|
Log::info(sprintf('Will now fire auto budget cron job task for date "%s".', $this->date->format('Y-m-d')));
|
||||||
|
/** @var CreateAutoBudgetLimits $job */
|
||||||
|
$job = app(CreateAutoBudgetLimits::class);
|
||||||
|
$job->setDate($this->date);
|
||||||
|
$job->handle();
|
||||||
|
app('fireflyconfig')->set('last_ab_job', (int)$this->date->format('U'));
|
||||||
|
Log::info('Done with auto budget cron job task.');
|
||||||
|
}
|
||||||
|
}
|
@@ -34,38 +34,6 @@ use Log;
|
|||||||
*/
|
*/
|
||||||
class RecurringCronjob extends AbstractCronjob
|
class RecurringCronjob extends AbstractCronjob
|
||||||
{
|
{
|
||||||
/** @var bool */
|
|
||||||
private $force;
|
|
||||||
|
|
||||||
/** @var Carbon */
|
|
||||||
private $date;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* RecurringCronjob constructor.
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
$this->force = false;
|
|
||||||
$this->date = new Carbon;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param bool $force
|
|
||||||
*/
|
|
||||||
public function setForce(bool $force): void
|
|
||||||
{
|
|
||||||
$this->force = $force;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Carbon $date
|
|
||||||
*/
|
|
||||||
public function setDate(Carbon $date): void
|
|
||||||
{
|
|
||||||
$this->date = $date;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
|
@@ -183,6 +183,7 @@ class Navigation
|
|||||||
'quarterly' => 'addMonths',
|
'quarterly' => 'addMonths',
|
||||||
'6M' => 'addMonths',
|
'6M' => 'addMonths',
|
||||||
'half-year' => 'addMonths',
|
'half-year' => 'addMonths',
|
||||||
|
'half_year' => 'addMonths',
|
||||||
'year' => 'addYear',
|
'year' => 'addYear',
|
||||||
'yearly' => 'addYear',
|
'yearly' => 'addYear',
|
||||||
'1Y' => 'addYear',
|
'1Y' => 'addYear',
|
||||||
@@ -192,10 +193,11 @@ class Navigation
|
|||||||
'3M' => 3,
|
'3M' => 3,
|
||||||
'quarterly' => 3,
|
'quarterly' => 3,
|
||||||
'half-year' => 6,
|
'half-year' => 6,
|
||||||
|
'half_year' => 6,
|
||||||
'6M' => 6,
|
'6M' => 6,
|
||||||
];
|
];
|
||||||
|
|
||||||
$subDay = ['week', 'weekly', '1W', 'month', 'monthly', '1M', '3M', 'quarter', 'quarterly', '6M', 'half-year', '1Y', 'year', 'yearly'];
|
$subDay = ['week', 'weekly', '1W', 'month', 'monthly', '1M', '3M', 'quarter', 'quarterly', '6M', 'half-year', 'half_year', '1Y', 'year', 'yearly'];
|
||||||
|
|
||||||
// if the range is custom, the end of the period
|
// if the range is custom, the end of the period
|
||||||
// is another X days (x is the difference between start)
|
// is another X days (x is the difference between start)
|
||||||
|
Reference in New Issue
Block a user