Skip to content

Firefly III: a personal finances manager

4.8 KB
Raw Blame
<?php

/*
 * TransactionGroupEventObjects.php
 * Copyright (c) 2026 [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/>.
 */

declare(strict_types=1);

namespace FireflyIII\Events\Model\TransactionGroup;

use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;

/**
 * This class collects all objects before and after the creation, removal or updating
 * of a transaction group. The idea is that this class contains all relevant objects.
 * Right now, that means journals, tags, accounts, budgets and categories.
 *
 * By collecting these objects (in case of an update: before AND after update) there
 * is a unified set of objects to manage: update balances, recalculate credits, etc.
 */
class TransactionGroupEventObjects
{
    public Collection $accounts;
    public Collection $budgets;
    public Collection $categories;
    public Collection $tags;
    public Collection $transactionGroups;
    public Collection $transactionJournals;

    public function __construct()
    {
        $this->accounts            = new Collection();
        $this->budgets             = new Collection();
        $this->categories          = new Collection();
        $this->tags                = new Collection();
        $this->transactionGroups   = new Collection();
        $this->transactionJournals = new Collection();
    }

    public static function collectFromTransactionGroup(TransactionGroup $transactionGroup): self
    {
        Log::debug(sprintf('collectFromTransactionGroup(#%d)', $transactionGroup->id));
        $object = new self();
        $object->appendFromTransactionGroup($transactionGroup);

        return $object;
    }

    public function appendFromTransactionGroup(TransactionGroup $transactionGroup): void
    {
        // Log::debug(sprintf('Appended transaction group #%d', $transactionGroup->id));
        $this->transactionGroups->push($transactionGroup);

        /** @var TransactionJournal $journal */
        foreach ($transactionGroup->transactionJournals as $journal) {
            $journal->refresh();
            // Log::debug(sprintf('Appended transaction journal #%d (%s)', $journal->id, $journal->date->format('Y-m-d H:i:s')));
            $this->transactionJournals->put($journal->id, $journal);
            $this->budgets    = $this->budgets->merge($journal->budgets);
            $this->categories = $this->categories->merge($journal->categories);
            $this->tags       = $this->tags->merge($journal->tags);

            /** @var Transaction $transaction */
            foreach ($journal->transactions as $transaction) {
                // Log::debug(sprintf('Appended account #%d', $transaction->account->id));
                $this->accounts->push($transaction->account);
            }
        }
        $this->transactionGroups   = $this->transactionGroups->unique('id');
        $this->transactionJournals = $this->transactionJournals->unique('id');
        $this->budgets             = $this->budgets->unique('id');
        $this->categories          = $this->categories->unique('id');
        $this->tags                = $this->tags->unique('id');
        $this->accounts            = $this->accounts->unique('id');
    }

    public function collectFromCollection(Collection $collection): void
    {
        Log::debug('Will now collect info from collection.');

        /** @var array|TransactionGroup $object */
        foreach ($collection as $object) {
            if ($object instanceof TransactionGroup) {
                Log::debug(sprintf('Added group #%d', $object->id));
                $this->appendFromTransactionGroup($object);
            }
            if (is_array($object) && array_key_exists('id', $object)) {
                // FIXME technically speaking not sure of this is the user's transaction group.
                $group = TransactionGroup::find((int) $object['id']);
                if (null !== $group) {
                    Log::debug(sprintf('Added group #%d', $group->id));
                    $this->appendFromTransactionGroup($group);
                }
            }
        }
    }
}