Files
firefly-iii/app/Http/Controllers/Json/BoxController.php

191 lines
7.6 KiB
PHP
Raw Normal View History

2017-09-27 08:45:27 +02:00
<?php
2022-12-29 19:41:57 +01:00
2017-09-27 08:45:27 +02:00
/**
* BoxController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2017-09-27 08:45:27 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +02:00
*
* 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.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* 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.
2017-10-21 08:40:00 +02:00
*
* 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/>.
2017-09-27 08:45:27 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
use Carbon\Carbon;
2025-10-05 12:59:43 +02:00
use Deprecated;
2025-01-03 09:15:52 +01:00
use FireflyIII\Enums\AccountTypeEnum;
2024-12-24 19:03:47 +01:00
use FireflyIII\Enums\TransactionTypeEnum;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Helpers\Report\NetWorthInterface;
2017-09-27 08:45:27 +02:00
use FireflyIII\Http\Controllers\Controller;
2018-01-13 18:40:28 +01:00
use FireflyIII\Models\Account;
2017-09-27 08:45:27 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2025-02-23 12:35:13 +01:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-09-27 08:45:27 +02:00
use FireflyIII\Support\CacheProperties;
2024-12-24 06:34:12 +01:00
use FireflyIII\Support\Facades\Amount;
2025-09-05 05:09:46 +02:00
use FireflyIII\Support\Facades\Steam;
use FireflyIII\Support\Http\Controllers\DateCalculation;
2018-07-08 12:28:42 +02:00
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
2017-09-27 08:45:27 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class BoxController.
2017-09-27 08:45:27 +02:00
*/
class BoxController extends Controller
{
use DateCalculation;
2017-09-27 08:45:27 +02:00
/**
* Deprecated method, no longer in use.
2017-09-27 08:45:27 +02:00
*/
2025-06-14 10:18:58 +02:00
#[Deprecated]
public function available(): JsonResponse
2017-09-27 08:45:27 +02:00
{
2025-10-05 12:57:58 +02:00
return response()->json();
2017-09-27 08:45:27 +02:00
}
2021-03-28 11:46:23 +02:00
2017-09-27 08:45:27 +02:00
/**
2018-07-21 08:06:24 +02:00
* Current total balance.
2017-09-27 08:45:27 +02:00
*/
2018-07-08 12:28:42 +02:00
public function balance(CurrencyRepositoryInterface $repository): JsonResponse
2017-09-27 08:45:27 +02:00
{
2018-03-10 06:49:03 +01:00
// Cache result, return cache if present.
2018-07-27 04:46:21 +02:00
/** @var Carbon $start */
$start = session('start', today(config('app.timezone'))->startOfMonth());
2023-12-20 19:35:52 +01:00
2018-07-27 04:46:21 +02:00
/** @var Carbon $end */
$end = session('end', today(config('app.timezone'))->endOfMonth());
$cache = new CacheProperties();
2017-09-27 08:45:27 +02:00
$cache->addProperty($start);
$cache->addProperty($end);
2025-08-01 06:12:36 +02:00
$cache->addProperty($this->convertToPrimary);
2017-09-27 08:45:27 +02:00
$cache->addProperty('box-balance');
if ($cache->has()) {
return response()->json($cache->get());
2017-09-27 08:45:27 +02:00
}
2018-03-10 06:49:03 +01:00
// prep some arrays:
$incomes = [];
$expenses = [];
$sums = [];
$currency = $this->primaryCurrency;
2018-03-10 06:49:03 +01:00
// collect income of user:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2026-01-23 15:09:50 +01:00
$collector->setRange($start, $end)->setTypes([TransactionTypeEnum::DEPOSIT->value]);
$set = $collector->getExtractedJournals();
2023-12-20 19:35:52 +01:00
/** @var array $journal */
foreach ($set as $journal) {
$currencyId = $this->convertToPrimary && $this->primaryCurrency->id !== (int) $journal['currency_id']
2026-01-23 15:09:50 +01:00
? $this->primaryCurrency->id
: (int) $journal['currency_id'];
$amount = Amount::getAmountFromJournal($journal);
2023-12-10 06:45:59 +01:00
$incomes[$currencyId] ??= '0';
2025-09-05 05:09:46 +02:00
$incomes[$currencyId] = bcadd($incomes[$currencyId], Steam::positive($amount));
$sums[$currencyId] ??= '0';
$sums[$currencyId] = bcadd($sums[$currencyId], Steam::positive($amount));
2018-03-10 06:49:03 +01:00
}
2017-09-27 08:45:27 +02:00
2018-03-10 06:49:03 +01:00
// collect expenses
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2026-01-23 15:09:50 +01:00
$collector->setRange($start, $end)->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
$set = $collector->getExtractedJournals();
2023-12-20 19:35:52 +01:00
/** @var array $journal */
foreach ($set as $journal) {
$currencyId = $this->convertToPrimary ? $this->primaryCurrency->id : (int) $journal['currency_id'];
$amount = Amount::getAmountFromJournal($journal);
2023-12-10 06:45:59 +01:00
$expenses[$currencyId] ??= '0';
$expenses[$currencyId] = bcadd($expenses[$currencyId], $amount);
$sums[$currencyId] ??= '0';
$sums[$currencyId] = bcadd($sums[$currencyId], $amount);
2018-03-10 06:49:03 +01:00
}
// format amounts:
$keys = array_keys($sums);
2018-07-20 14:34:56 +02:00
foreach ($keys as $currencyId) {
$currency = $repository->find($currencyId);
$sums[$currencyId] = Amount::formatAnything($currency, $sums[$currencyId], false);
$incomes[$currencyId] = Amount::formatAnything($currency, $incomes[$currencyId] ?? '0', false);
$expenses[$currencyId] = Amount::formatAnything($currency, $expenses[$currencyId] ?? '0', false);
2018-03-10 06:49:03 +01:00
}
2022-11-04 05:11:05 +01:00
if (0 === count($sums)) {
$currency = $this->primaryCurrency;
$sums[$this->primaryCurrency->id] = Amount::formatAnything($this->primaryCurrency, '0', false);
$incomes[$this->primaryCurrency->id] = Amount::formatAnything($this->primaryCurrency, '0', false);
$expenses[$this->primaryCurrency->id] = Amount::formatAnything($this->primaryCurrency, '0', false);
2018-06-17 07:45:10 +02:00
}
2017-09-27 08:45:27 +02:00
$response = ['incomes' => $incomes, 'expenses' => $expenses, 'sums' => $sums, 'size' => count($sums), 'preferred' => $currency->id];
2017-09-27 08:45:27 +02:00
$cache->store($response);
2018-03-10 20:30:09 +01:00
return response()->json($response);
2017-09-27 08:45:27 +02:00
}
/**
2018-07-21 08:06:24 +02:00
* Total user net worth.
2017-09-27 08:45:27 +02:00
*/
public function netWorth(): JsonResponse
2017-09-27 08:45:27 +02:00
{
$date = today(config('app.timezone'))->endOfDay();
2017-10-16 19:01:26 +02:00
// start and end in the future? use $end
2018-07-20 14:34:56 +02:00
if ($this->notInSessionRange($date)) {
/** @var Carbon $date */
2023-02-11 07:36:45 +01:00
$date = session('end', today(config('app.timezone'))->endOfMonth());
2017-10-16 19:01:26 +02:00
}
2018-07-20 14:34:56 +02:00
/** @var NetWorthInterface $netWorthHelper */
$netWorthHelper = app(NetWorthInterface::class);
$netWorthHelper->setUser(auth()->user());
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
2026-01-23 15:09:50 +01:00
$allAccounts = $accountRepository->getActiveAccountsByType([
AccountTypeEnum::DEFAULT->value,
AccountTypeEnum::ASSET->value,
AccountTypeEnum::LOAN->value,
AccountTypeEnum::DEBT->value,
AccountTypeEnum::MORTGAGE->value,
2026-01-23 15:09:50 +01:00
]);
Log::debug(sprintf('Found %d accounts.', $allAccounts->count()));
// filter list on preference of being included.
$filtered = $allAccounts->filter(static function (Account $account) use ($accountRepository): bool {
2026-01-23 15:09:50 +01:00
$includeNetWorth = $accountRepository->getMetaValue($account, 'include_net_worth');
$result = null === $includeNetWorth || '1' === $includeNetWorth;
if (false === $result) {
Log::debug(sprintf('Will not include "%s" in net worth charts.', $account->name));
}
2026-01-23 15:09:50 +01:00
return $result;
});
$netWorthSet = $netWorthHelper->byAccounts($filtered, $date);
$return = [];
2023-10-29 05:54:01 +01:00
foreach ($netWorthSet as $key => $data) {
2025-08-01 06:12:36 +02:00
if ('primary' === $key) {
2023-10-29 05:54:01 +01:00
continue;
}
$return[$data['currency_id']] = Amount::formatFlat($data['currency_symbol'], $data['currency_decimal_places'], $data['balance'], false);
2018-01-13 18:40:28 +01:00
}
$return = ['net_worths' => array_values($return)];
2017-09-27 08:45:27 +02:00
2018-03-10 20:30:09 +01:00
return response()->json($return);
2017-09-27 08:45:27 +02:00
}
2017-11-08 09:05:10 +01:00
}