Skip to content

Firefly III: a personal finances manager

Add timezones to existing objects.

James Cole8b2f1d0b4f49
diff --git a/app/Console/Commands/Integrity/AddTimezonesToDates.php b/app/Console/Commands/Integrity/AddTimezonesToDates.php
new file mode 100644
index 0000000000..bf5001c5e4
--- /dev/null
+++ b/app/Console/Commands/Integrity/AddTimezonesToDates.php
@@ -0,0 +1,113 @@
+<?php
+/*
+ * AddTimezonesToDates.php
+ * Copyright (c) 2024 [email protected].
+ *
+ * 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;
+use Illuminate\Console\Command;
+use Illuminate\Database\QueryException;
+use Illuminate\Support\Collection;
+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';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Make sure all dates have a timezone.';
+
+ /**
+ * Execute the console command.
+ */
+ public function handle()
+ {
+ $models = [
+ 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'],
+ ];
+ foreach ($models as $model => $fields) {
+ $this->addTimezoneToModel($model, $fields);
+ }
+ }
+
+ private function addTimezoneToModel(string $model, array $fields): void
+ {
+ foreach ($fields as $field) {
+ $this->addTimezoneToModelField($model, $field);
+ }
+ }
+
+ private function addTimezoneToModelField(string $model, string $field): void
+ {
+ $shortModel = str_replace('FireflyIII\\Models\\','', $model);
+ $timezoneField = sprintf('%s_tz', $field);
+ $items = new Collection();
+ try {
+ $items = $model::whereNull($timezoneField)->get();
+ } catch (QueryException $e) {
+ $this->friendlyError(sprintf('Cannot add timezone to field "%s" of model "%s". Field does not exist.', $field, $shortModel));
+ Log::error($e->getMessage());
+ }
+ if(0 === $items->count()) {
+ $this->friendlyPositive(sprintf('Timezone is present in field "%s" of model "%s".', $field, $shortModel));
+ return;
+ }
+ $this->friendlyInfo(sprintf('Adding timezone to field "%s" of model "%s".', $field, $shortModel));
+
+ foreach ($items as $item) {
+ $item->{$timezoneField} = config('app.timezone');
+ $item->saveQuietly();
+ }
+ }
+}
diff --git a/app/Helpers/Collector/GroupCollector.php b/app/Helpers/Collector/GroupCollector.php
index 508eecfc08..47f51eba3f 100644
--- a/app/Helpers/Collector/GroupCollector.php
+++ b/app/Helpers/Collector/GroupCollector.php
@@ -117,6 +117,7 @@ class GroupCollector implements GroupCollectorInterface
'transaction_journals.transaction_type_id',
'transaction_journals.description',
'transaction_journals.date',
+ 'transaction_journals.date_tz',
'transaction_journals.order',
// types