Files
firefly-iii/app/Support/Http/Controllers/AugumentData.php

281 lines
9.7 KiB
PHP
Raw Normal View History

<?php
/**
* AugumentData.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Http\Controllers;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
2018-12-31 08:11:57 +01:00
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2019-08-30 08:09:39 +02:00
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2018-12-31 07:58:13 +01:00
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
/**
* Trait AugumentData
*/
trait AugumentData
{
/**
* Searches for the opposing account.
*/
protected function combineAccounts(Collection $accounts): array // filter + group data
{
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$combined = [];
2023-12-20 19:35:52 +01:00
/** @var Account $expenseAccount */
foreach ($accounts as $expenseAccount) {
$collection = new Collection();
$collection->push($expenseAccount);
$revenue = $repository->findByName($expenseAccount->name, [AccountType::REVENUE]);
if (null !== $revenue) {
$collection->push($revenue);
}
$combined[$expenseAccount->name] = $collection;
}
return $combined;
}
2018-12-31 07:58:13 +01:00
/**
* Small helper function for the revenue and expense account charts.
2023-12-23 06:22:47 +01:00
*
* @param array<array> $names
2018-12-31 07:58:13 +01:00
*/
protected function expandNames(array $names): array
{
$result = [];
foreach ($names as $entry) {
$result[$entry['name']] = 0;
}
return $result;
}
/**
* Small helper function for the revenue and expense account charts.
*/
protected function extractNames(Collection $accounts): array
{
$return = [];
2023-12-20 19:35:52 +01:00
2018-12-31 07:58:13 +01:00
/** @var Account $account */
foreach ($accounts as $account) {
$return[$account->id] = $account->name;
}
return $return;
}
/**
* Get the account names belonging to a bunch of account ID's.
*/
protected function getAccountNames(array $accountIds): array // extract info from array.
{
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$accounts = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT, AccountType::EXPENSE, AccountType::CASH]);
$grouped = $accounts->groupBy('id')->toArray();
$return = [];
foreach ($accountIds as $combinedId) {
$parts = explode('-', $combinedId);
2022-12-29 19:42:26 +01:00
$accountId = (int)$parts[0];
2021-04-06 17:00:16 +02:00
if (array_key_exists($accountId, $grouped)) {
$return[$accountId] = $grouped[$accountId][0]['name'];
}
}
$return[0] = '(no name)';
return $return;
}
/**
* Get the budget names from a set of budget ID's.
*/
protected function getBudgetNames(array $budgetIds): array // extract info from array.
{
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$budgets = $repository->getBudgets();
$grouped = $budgets->groupBy('id')->toArray();
$return = [];
foreach ($budgetIds as $budgetId) {
2021-04-06 17:00:16 +02:00
if (array_key_exists($budgetId, $grouped)) {
$return[$budgetId] = $grouped[$budgetId][0]['name'];
}
}
$return[0] = (string)trans('firefly.no_budget');
return $return;
}
/**
* Get the category names from a set of category ID's. Small helper function for some of the charts.
*/
protected function getCategoryNames(array $categoryIds): array // extract info from array.
{
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$categories = $repository->getCategories();
$grouped = $categories->groupBy('id')->toArray();
$return = [];
foreach ($categoryIds as $combinedId) {
$parts = explode('-', $combinedId);
2022-12-29 19:42:26 +01:00
$categoryId = (int)$parts[0];
2021-04-06 17:00:16 +02:00
if (array_key_exists($categoryId, $grouped)) {
$return[$categoryId] = $grouped[$categoryId][0]['name'];
}
}
$return[0] = (string)trans('firefly.no_category');
return $return;
}
2018-12-31 07:58:13 +01:00
/**
* Gets all budget limits for a budget.
*/
protected function getLimits(Budget $budget, Carbon $start, Carbon $end): Collection // get data + augment with info
{
/** @var OperationsRepositoryInterface $opsRepository */
$opsRepository = app(OperationsRepositoryInterface::class);
2019-08-30 08:09:39 +02:00
/** @var BudgetLimitRepositoryInterface $blRepository */
$blRepository = app(BudgetLimitRepositoryInterface::class);
2018-12-31 07:58:13 +01:00
// properties for cache
$cache = new CacheProperties();
2018-12-31 07:58:13 +01:00
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($budget->id);
$cache->addProperty('get-limits');
if ($cache->has()) {
return $cache->get();
2018-12-31 07:58:13 +01:00
}
2020-11-08 14:13:21 +01:00
$set = $blRepository->getBudgetLimits($budget, $start, $end);
$limits = new Collection();
$budgetCollection = new Collection([$budget]);
2018-12-31 07:58:13 +01:00
/** @var BudgetLimit $entry */
foreach ($set as $entry) {
$currency = $entry->transactionCurrency;
2023-11-29 06:30:35 +01:00
2023-12-10 06:51:59 +01:00
if (null === $currency) {
2023-11-29 06:30:35 +01:00
$currency = app('amount')->getDefaultCurrency();
}
2020-11-08 14:13:21 +01:00
// clone because these objects change each other.
$currentStart = clone $entry->start_date;
2023-11-29 06:30:35 +01:00
$currentEnd = null === $entry->end_date ? null : clone $entry->end_date;
2023-12-10 06:51:59 +01:00
if (null === $currentEnd) {
2023-11-29 06:30:35 +01:00
$currentEnd = clone $currentStart;
$currentEnd->addMonth();
}
2020-11-08 14:13:21 +01:00
$expenses = $opsRepository->sumExpenses($currentStart, $currentEnd, null, $budgetCollection, $currency);
2023-11-05 19:41:37 +01:00
$spent = $expenses[$currency->id]['sum'] ?? '0';
2020-11-08 14:13:21 +01:00
$entry->spent = $spent;
2018-12-31 07:58:13 +01:00
$limits->push($entry);
}
$cache->store($limits);
return $set;
}
/**
* Group set of transactions by name of opposing account.
*/
2019-05-31 13:35:33 +02:00
protected function groupByName(array $array): array // filter + group data
{
// group by opposing account name.
$grouped = [];
2023-12-20 19:35:52 +01:00
2019-05-31 13:35:33 +02:00
/** @var array $journal */
foreach ($array as $journal) {
$name = '(no name)';
2019-05-31 13:35:33 +02:00
if (TransactionType::WITHDRAWAL === $journal['transaction_type_type']) {
$name = $journal['destination_account_name'];
}
if (TransactionType::WITHDRAWAL !== $journal['transaction_type_type']) {
$name = $journal['source_account_name'];
}
2023-12-10 06:45:59 +01:00
$grouped[$name] ??= '0';
2019-05-31 13:35:33 +02:00
$grouped[$name] = bcadd($journal['amount'], $grouped[$name]);
}
return $grouped;
}
2018-12-31 08:11:57 +01:00
/**
* Spent in a period.
*/
protected function spentInPeriod(Collection $assets, Collection $opposing, Carbon $start, Carbon $end): array // get data + augment with info
{
2019-05-31 13:35:33 +02:00
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$total = $assets->merge($opposing);
2019-05-31 13:35:33 +02:00
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setAccounts($total);
$journals = $collector->getExtractedJournals();
$sum = [
2018-12-31 08:11:57 +01:00
'grand_sum' => '0',
'per_currency' => [],
];
// loop to support multi currency
2019-05-31 13:35:33 +02:00
foreach ($journals as $journal) {
$currencyId = (int)$journal['currency_id'];
2018-12-31 08:11:57 +01:00
// if not set, set to zero:
2021-04-06 17:00:16 +02:00
if (!array_key_exists($currencyId, $sum['per_currency'])) {
2018-12-31 08:11:57 +01:00
$sum['per_currency'][$currencyId] = [
'sum' => '0',
'currency' => [
2019-05-31 13:35:33 +02:00
'name' => $journal['currency_name'],
'symbol' => $journal['currency_symbol'],
'decimal_places' => $journal['currency_decimal_places'],
2018-12-31 08:11:57 +01:00
],
];
}
// add amount
2019-05-31 13:35:33 +02:00
$sum['per_currency'][$currencyId]['sum'] = bcadd($sum['per_currency'][$currencyId]['sum'], $journal['amount']);
$sum['grand_sum'] = bcadd($sum['grand_sum'], $journal['amount']);
2018-12-31 08:11:57 +01:00
}
return $sum;
}
2018-12-31 07:58:13 +01:00
}