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

265 lines
11 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;
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\Models\AccountType;
2020-02-01 06:19:12 +01:00
use FireflyIII\Models\AvailableBudget;
2018-07-20 14:34:56 +02:00
use FireflyIII\Models\TransactionCurrency;
2017-09-27 08:45:27 +02:00
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2019-08-30 08:00:52 +02:00
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
2018-01-13 18:40:28 +01:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-09-27 08:45:27 +02:00
use FireflyIII\Support\CacheProperties;
2018-07-08 12:28:42 +02:00
use Illuminate\Http\JsonResponse;
use 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
{
/**
2020-02-01 06:19:12 +01:00
* This box has three types of info to display:
* 0) If the user has available amount this period and has overspent: overspent box.
* 1) If the user has available amount this period and has NOT overspent: left to spend box.
* 2) if the user has no available amount set this period: spent per day
2018-07-21 08:06:24 +02:00
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2017-09-27 08:45:27 +02:00
*/
public function available(): JsonResponse
2017-09-27 08:45:27 +02:00
{
/** @var OperationsRepositoryInterface $opsRepository */
$opsRepository = app(OperationsRepositoryInterface::class);
2019-08-30 08:00:52 +02:00
/** @var AvailableBudgetRepositoryInterface $abRepository */
$abRepository = app(AvailableBudgetRepositoryInterface::class);
2018-07-27 04:46:21 +02:00
/** @var Carbon $start */
2023-02-11 07:36:45 +01:00
$start = session('start', today(config('app.timezone'))->startOfMonth());
2018-07-27 04:46:21 +02:00
/** @var Carbon $end */
2023-02-11 07:36:45 +01:00
$end = session('end', today(config('app.timezone'))->endOfMonth());
2020-09-11 07:12:33 +02:00
$today = today(config('app.timezone'));
2020-02-01 06:19:12 +01:00
$display = 2; // see method docs.
2022-12-29 19:41:57 +01:00
$boxTitle = (string)trans('firefly.spent');
2020-02-01 06:19:12 +01:00
2022-10-30 14:24:28 +01:00
$cache = new CacheProperties();
2017-09-27 08:45:27 +02:00
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($today);
$cache->addProperty('box-available');
if ($cache->has()) {
return response()->json($cache->get());
2017-09-27 08:45:27 +02:00
}
2020-02-01 06:19:12 +01:00
$leftPerDayAmount = '0';
$leftToSpendAmount = '0';
2017-09-27 08:45:27 +02:00
2020-02-01 06:19:12 +01:00
$currency = app('amount')->getDefaultCurrency();
$availableBudgets = $abRepository->getAvailableBudgetsByDate($start, $end);
$availableBudgets = $availableBudgets->filter(
static function (AvailableBudget $availableBudget) use ($currency) {
if ($availableBudget->transaction_currency_id === $currency->id) {
return $availableBudget;
}
return null;
}
);
// spent in this period, in budgets, for default currency.
// also calculate spent per day.
$spent = $opsRepository->sumExpenses($start, $end, null, null, $currency);
2022-12-29 19:41:57 +01:00
$spentAmount = $spent[(int)$currency->id]['sum'] ?? '0';
2021-01-02 14:33:19 +01:00
2021-03-21 09:15:40 +01:00
$days = $today->between($start, $end) ? $today->diffInDays($start) + 1 : $end->diffInDays($start) + 1;
2022-12-29 19:41:57 +01:00
$spentPerDay = bcdiv($spentAmount, (string)$days);
2020-03-07 12:09:18 +01:00
if ($availableBudgets->count() > 0) {
$display = 0; // assume user overspent
2022-12-29 19:41:57 +01:00
$boxTitle = (string)trans('firefly.overspent');
$totalAvailableSum = (string)$availableBudgets->sum('amount');
2020-02-01 06:19:12 +01:00
// calculate with available budget.
2020-03-07 12:09:18 +01:00
$leftToSpendAmount = bcadd($totalAvailableSum, $spentAmount);
2020-02-01 06:19:12 +01:00
if (1 === bccomp($leftToSpendAmount, '0')) {
2022-12-29 19:41:57 +01:00
$boxTitle = (string)trans('firefly.left_to_spend');
2020-02-01 06:19:12 +01:00
$days = $today->diffInDays($end) + 1;
$display = 1; // not overspent
2022-12-29 19:41:57 +01:00
$leftPerDayAmount = bcdiv($leftToSpendAmount, (string)$days);
2020-02-01 06:19:12 +01:00
}
2017-09-27 08:45:27 +02:00
}
$return = [
2020-02-01 06:19:12 +01:00
'display' => $display,
'spent_total' => app('amount')->formatAnything($currency, $spentAmount, false),
'spent_per_day' => app('amount')->formatAnything($currency, $spentPerDay, false),
'left_to_spend' => app('amount')->formatAnything($currency, $leftToSpendAmount, false),
'left_per_day' => app('amount')->formatAnything($currency, $leftPerDayAmount, false),
'title' => $boxTitle,
2017-09-27 08:45:27 +02:00
];
$cache->store($return);
2018-03-10 20:30:09 +01:00
return response()->json($return);
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.
*
2022-12-29 19:41:57 +01:00
* @param CurrencyRepositoryInterface $repository
2018-03-10 06:49:03 +01:00
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
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 */
2023-02-11 07:36:45 +01:00
$start = session('start', today(config('app.timezone'))->startOfMonth());
2018-07-27 04:46:21 +02:00
/** @var Carbon $end */
2023-02-11 07:36:45 +01:00
$end = session('end', today(config('app.timezone'))->endOfMonth());
2022-10-30 14:24:28 +01:00
$cache = new CacheProperties();
2017-09-27 08:45:27 +02:00
$cache->addProperty($start);
$cache->addProperty($end);
$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 = [];
2019-07-20 22:32:32 +02:00
$currency = app('amount')->getDefaultCurrency();
2017-09-27 08:45:27 +02:00
2018-03-10 06:49:03 +01:00
// collect income of user:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end)
->setTypes([TransactionType::DEPOSIT]);
$set = $collector->getExtractedJournals();
/** @var array $journal */
foreach ($set as $journal) {
2022-12-29 19:41:57 +01:00
$currencyId = (int)$journal['currency_id'];
2020-02-10 19:15:35 +01:00
$amount = $journal['amount'] ?? '0';
2018-03-10 06:49:03 +01:00
$incomes[$currencyId] = $incomes[$currencyId] ?? '0';
2020-02-10 19:15:35 +01:00
$incomes[$currencyId] = bcadd($incomes[$currencyId], app('steam')->positive($amount));
2018-03-10 06:49:03 +01:00
$sums[$currencyId] = $sums[$currencyId] ?? '0';
2020-02-10 19:15:35 +01:00
$sums[$currencyId] = bcadd($sums[$currencyId], app('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);
$collector->setRange($start, $end)
->setTypes([TransactionType::WITHDRAWAL]);
$set = $collector->getExtractedJournals();
/** @var array $journal */
foreach ($set as $journal) {
2022-12-29 19:41:57 +01:00
$currencyId = (int)$journal['currency_id'];
2018-03-10 06:49:03 +01:00
$expenses[$currencyId] = $expenses[$currencyId] ?? '0';
2020-02-28 18:53:20 +01:00
$expenses[$currencyId] = bcadd($expenses[$currencyId], $journal['amount'] ?? '0');
2018-03-10 06:49:03 +01:00
$sums[$currencyId] = $sums[$currencyId] ?? '0';
$sums[$currencyId] = bcadd($sums[$currencyId], $journal['amount']);
2018-03-10 06:49:03 +01:00
}
// format amounts:
2018-07-20 14:34:56 +02:00
$keys = array_keys($sums);
foreach ($keys as $currencyId) {
2021-06-30 06:17:38 +02:00
$currency = $repository->find($currencyId);
2018-07-20 14:34:56 +02:00
$sums[$currencyId] = app('amount')->formatAnything($currency, $sums[$currencyId], false);
$incomes[$currencyId] = app('amount')->formatAnything($currency, $incomes[$currencyId] ?? '0', false);
$expenses[$currencyId] = app('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)) {
2018-06-17 07:45:10 +02:00
$currency = app('amount')->getDefaultCurrency();
2018-07-20 14:34:56 +02:00
$sums[$currency->id] = app('amount')->formatAnything($currency, '0', false);
$incomes[$currency->id] = app('amount')->formatAnything($currency, '0', false);
$expenses[$currency->id] = app('amount')->formatAnything($currency, '0', false);
2018-06-17 07:45:10 +02:00
}
2017-09-27 08:45:27 +02:00
$response = [
2019-07-20 22:32:32 +02:00
'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.
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2017-09-27 08:45:27 +02:00
*/
public function netWorth(): JsonResponse
2017-09-27 08:45:27 +02:00
{
2023-02-11 07:36:45 +01: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);
2019-02-13 17:38:41 +01:00
$allAccounts = $accountRepository->getActiveAccountsByType(
[AccountType::DEFAULT, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]
);
Log::debug(sprintf('Found %d accounts.', $allAccounts->count()));
// filter list on preference of being included.
$filtered = $allAccounts->filter(
function (Account $account) use ($accountRepository) {
$includeNetWorth = $accountRepository->getMetaValue($account, 'include_net_worth');
$result = null === $includeNetWorth ? true : '1' === $includeNetWorth;
if (false === $result) {
Log::debug(sprintf('Will not include "%s" in net worth charts.', $account->name));
}
return $result;
}
);
$netWorthSet = $netWorthHelper->getNetWorthByCurrency($filtered, $date);
2021-03-28 11:46:23 +02:00
$return = [];
2020-10-26 19:15:57 +01:00
foreach ($netWorthSet as $data) {
/** @var TransactionCurrency $currency */
2019-02-13 17:38:41 +01:00
$currency = $data['currency'];
$return[$currency->id] = app('amount')->formatAnything($currency, $data['balance'], false);
2018-01-13 18:40:28 +01:00
}
2017-09-27 08:45:27 +02:00
$return = [
2018-01-13 18:40:28 +01:00
'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
}