Files
firefly-iii/app/Console/Commands/Integrity/AddTimezonesToDates.php

118 lines
4.1 KiB
PHP
Raw Normal View History

2024-11-06 19:22:10 +01:00
<?php
2024-11-06 19:32:32 +01:00
declare(strict_types=1);
2024-11-06 19:22:10 +01:00
/*
* AddTimezonesToDates.php
* Copyright (c) 2024 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/.
*/
namespace FireflyIII\Console\Commands\Integrity;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Models\AccountBalance;
use FireflyIII\Models\AvailableBudget;
use FireflyIII\Models\Bill;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\CurrencyExchangeRate;
use FireflyIII\Models\InvitedUser;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\PiggyBankRepetition;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal;
2024-11-22 06:03:29 +01:00
use FireflyIII\Support\Facades\FireflyConfig;
2024-11-06 19:22:10 +01:00
use Illuminate\Console\Command;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Log;
class AddTimezonesToDates extends Command
{
use ShowsFriendlyMessages;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:add-timezones-to-dates';
2024-11-06 19:22:10 +01:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Make sure all dates have a timezone.';
2024-11-06 19:22:10 +01:00
public static array $models = [
2024-11-09 12:19:01 +01:00
AccountBalance::class => ['date'], // done
AvailableBudget::class => ['start_date', 'end_date'], // done
Bill::class => ['date', 'end_date', 'extension_date'], // done
BudgetLimit::class => ['start_date', 'end_date'], // done
CurrencyExchangeRate::class => ['date'], // done
InvitedUser::class => ['expires'],
PiggyBankEvent::class => ['date'],
PiggyBankRepetition::class => ['startdate', 'targetdate'],
PiggyBank::class => ['startdate', 'targetdate'], // done
Recurrence::class => ['first_date', 'repeat_until', 'latest_date'],
Tag::class => ['date'],
TransactionJournal::class => ['date'],
];
2024-11-06 19:22:10 +01:00
/**
* Execute the console command.
*/
2024-11-06 19:32:32 +01:00
public function handle(): void
2024-11-06 19:22:10 +01:00
{
2024-11-09 12:19:01 +01:00
foreach (self::$models as $model => $fields) {
2024-11-06 19:22:10 +01:00
$this->addTimezoneToModel($model, $fields);
}
2024-11-22 06:03:29 +01:00
// not yet in UTC mode
FireflyConfig::set('utc', false);
2024-11-06 19:22:10 +01:00
}
private function addTimezoneToModel(string $model, array $fields): void
{
foreach ($fields as $field) {
$this->addTimezoneToModelField($model, $field);
}
}
private function addTimezoneToModelField(string $model, string $field): void
{
2024-11-06 19:32:32 +01:00
$shortModel = str_replace('FireflyIII\Models\\', '', $model);
2024-11-06 19:22:10 +01:00
$timezoneField = sprintf('%s_tz', $field);
$count = 0;
2024-11-06 19:32:32 +01:00
2024-11-06 19:22:10 +01:00
try {
$count = $model::whereNull($timezoneField)->count();
2024-11-06 19:22:10 +01:00
} catch (QueryException $e) {
2024-11-06 19:44:08 +01:00
$this->friendlyError(sprintf('Cannot add timezone information to field "%s" of model "%s". Field does not exist.', $field, $shortModel));
2024-11-06 19:22:10 +01:00
Log::error($e->getMessage());
}
if (0 === $count) {
2024-11-06 19:44:08 +01:00
$this->friendlyPositive(sprintf('Timezone information is present in field "%s" of model "%s".', $field, $shortModel));
2024-11-06 19:32:32 +01:00
2024-11-06 19:22:10 +01:00
return;
}
2024-11-06 19:44:08 +01:00
$this->friendlyInfo(sprintf('Adding timezone information to field "%s" of model "%s".', $field, $shortModel));
2024-11-06 19:22:10 +01:00
$model::whereNull($timezoneField)->update([$timezoneField => config('app.timezone')]);
2024-11-06 19:22:10 +01:00
}
}