Files
firefly-iii/app/Console/Commands/Upgrade/UpgradesBudgetLimitPeriods.php

157 lines
4.8 KiB
PHP
Raw Normal View History

2020-11-20 06:24:08 +01:00
<?php
2021-01-29 18:50:35 +01:00
/*
* AppendBudgetLimitPeriods.php
* Copyright (c) 2021 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/>.
*/
2020-12-22 05:35:06 +01:00
declare(strict_types=1);
2020-11-20 06:24:08 +01:00
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2020-11-20 06:24:08 +01:00
use FireflyIII\Models\BudgetLimit;
use Illuminate\Console\Command;
2024-12-27 06:48:58 +01:00
class UpgradesBudgetLimitPeriods extends Command
2020-11-20 06:24:08 +01:00
{
use ShowsFriendlyMessages;
2023-12-02 12:56:48 +01:00
public const string CONFIG_NAME = '550_budget_limit_periods';
2023-11-05 09:54:53 +01:00
protected $description = 'Append budget limits with their (estimated) timeframe.';
2023-11-05 09:54:53 +01:00
2024-12-27 06:48:58 +01:00
protected $signature = 'upgrade:550-budget-limit-periods {--F|force : Force the execution of this command.}';
2020-11-20 06:24:08 +01:00
/**
* Execute the console command.
*/
public function handle(): int
{
if ($this->isExecuted() && true !== $this->option('force')) {
$this->friendlyInfo('This command has already been executed.');
2020-11-20 06:24:08 +01:00
return 0;
}
$this->theresNoLimit();
2021-03-12 06:30:40 +01:00
$this->markAsExecuted();
2020-11-20 06:24:08 +01:00
return 0;
}
2023-06-21 12:34:58 +02:00
private function isExecuted(): bool
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
2024-12-22 08:43:12 +01:00
return (bool) $configVar->data;
2023-06-21 12:34:58 +02:00
}
private function theresNoLimit(): void
{
$limits = BudgetLimit::whereNull('period')->get();
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
/** @var BudgetLimit $limit */
foreach ($limits as $limit) {
$this->fixLimit($limit);
}
}
2023-11-04 11:31:14 +01:00
private function fixLimit(BudgetLimit $limit): void
2021-03-12 06:30:40 +01:00
{
$period = $this->getLimitPeriod($limit);
2021-03-12 06:30:40 +01:00
if (null === $period) {
$message = sprintf(
2022-10-30 12:23:16 +01:00
'Could not guesstimate budget limit #%d (%s - %s) period.',
$limit->id,
$limit->start_date->format('Y-m-d'),
$limit->end_date->format('Y-m-d')
2021-03-12 06:30:40 +01:00
);
$this->friendlyWarning($message);
2022-10-30 14:44:49 +01:00
app('log')->warning($message);
2021-03-12 06:30:40 +01:00
return;
}
$limit->period = $period;
$limit->save();
$msg = sprintf(
2022-10-30 12:23:16 +01:00
'Budget limit #%d (%s - %s) period is "%s".',
$limit->id,
$limit->start_date->format('Y-m-d'),
$limit->end_date->format('Y-m-d'),
$period
2021-03-12 06:30:40 +01:00
);
2023-10-29 06:33:43 +01:00
app('log')->debug($msg);
2021-03-12 06:30:40 +01:00
}
2020-11-20 06:24:08 +01:00
private function getLimitPeriod(BudgetLimit $limit): ?string
{
// is daily
if ($limit->end_date->isSameDay($limit->start_date)) {
return 'daily';
}
// is weekly
2024-12-22 08:43:12 +01:00
if ('1' === $limit->start_date->format('N') && '7' === $limit->end_date->format('N') && 6 === (int) $limit->end_date->diffInDays($limit->start_date, true)) {
2020-11-20 06:24:08 +01:00
return 'weekly';
}
// is monthly
if (
'1' === $limit->start_date->format('j') // first day
&& $limit->end_date->format('j') === $limit->end_date->format('t') // last day
&& $limit->start_date->isSameMonth($limit->end_date)
) {
return 'monthly';
}
// is quarter
$start = ['1-1', '1-4', '1-7', '1-10'];
$end = ['31-3', '30-6', '30-9', '31-12'];
if (
in_array($limit->start_date->format('j-n'), $start, true) // start of quarter
&& in_array($limit->end_date->format('j-n'), $end, true) // end of quarter
2024-12-22 08:43:12 +01:00
&& 2 === (int) $limit->start_date->diffInMonths($limit->end_date, true)
2020-11-20 06:24:08 +01:00
) {
return 'quarterly';
}
// is half year
$start = ['1-1', '1-7'];
$end = ['30-6', '31-12'];
if (
2022-10-30 12:23:16 +01:00
in_array($limit->start_date->format('j-n'), $start, true) // start of quarter
&& in_array($limit->end_date->format('j-n'), $end, true) // end of quarter
2024-12-22 08:43:12 +01:00
&& 5 === (int) $limit->start_date->diffInMonths($limit->end_date, true)
2020-11-20 06:24:08 +01:00
) {
return 'half_year';
}
// is yearly
if ('1-1' === $limit->start_date->format('j-n') && '31-12' === $limit->end_date->format('j-n')) {
return 'yearly';
}
return null;
}
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
}