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

315 lines
12 KiB
PHP
Raw Normal View History

2017-09-27 01:45:27 -05:00
<?php
/**
* BoxController.php
2020-01-31 00:32:04 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2017-09-27 01:45:27 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 01:40:00 -05: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 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05: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 01:40:00 -05: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 01:45:27 -05: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 01:45:27 -05:00
use FireflyIII\Http\Controllers\Controller;
2018-01-13 11:40:28 -06:00
use FireflyIII\Models\Account;
2017-09-27 01:45:27 -05:00
use FireflyIII\Models\AccountType;
2020-01-31 23:19:12 -06:00
use FireflyIII\Models\AvailableBudget;
2018-07-20 07:34:56 -05:00
use FireflyIII\Models\TransactionCurrency;
2017-09-27 01:45:27 -05:00
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2019-08-30 01:00:52 -05:00
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
2018-01-13 11:40:28 -06:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-09-27 01:45:27 -05:00
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Http\Controllers\RequestInformation;
2018-07-08 05:28:42 -05:00
use Illuminate\Http\JsonResponse;
use Log;
2017-09-27 01:45:27 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class BoxController.
2017-09-27 01:45:27 -05:00
*/
class BoxController extends Controller
{
use RequestInformation;
2018-07-08 05:08:53 -05:00
2017-09-27 01:45:27 -05:00
/**
2020-01-31 23:19:12 -06: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 01:06:24 -05:00
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2017-09-27 01:45:27 -05:00
*/
public function available(): JsonResponse
2017-09-27 01:45:27 -05:00
{
/** @var OperationsRepositoryInterface $opsRepository */
$opsRepository = app(OperationsRepositoryInterface::class);
2019-08-30 01:00:52 -05:00
/** @var AvailableBudgetRepositoryInterface $abRepository */
$abRepository = app(AvailableBudgetRepositoryInterface::class);
2018-07-26 21:46:21 -05:00
/** @var Carbon $start */
2017-09-27 01:45:27 -05:00
$start = session('start', Carbon::now()->startOfMonth());
2018-07-26 21:46:21 -05:00
/** @var Carbon $end */
2020-01-31 23:19:12 -06:00
$end = session('end', Carbon::now()->endOfMonth());
$today = new Carbon;
$display = 2; // see method docs.
$boxTitle = (string) trans('firefly.spent');
2020-01-31 23:19:12 -06:00
2017-09-27 01:45:27 -05:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($today);
$cache->addProperty('box-available');
if ($cache->has()) {
2018-03-10 13:30:09 -06:00
return response()->json($cache->get()); // @codeCoverageIgnore
2017-09-27 01:45:27 -05:00
}
2020-01-31 23:19:12 -06:00
$leftPerDayAmount = '0';
$leftToSpendAmount = '0';
2017-09-27 01:45:27 -05:00
2020-01-31 23:19:12 -06: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);
$spentAmount = $spent[(int) $currency->id]['sum'] ?? '0';
2020-01-31 23:19:12 -06:00
$spentPerDay = '-1';
2020-03-07 05:09:18 -06:00
if ($availableBudgets->count() > 0) {
$display = 0; // assume user overspent
$boxTitle = (string) trans('firefly.overspent');
$totalAvailableSum = (string) $availableBudgets->sum('amount');
2020-01-31 23:19:12 -06:00
// calculate with available budget.
2020-03-07 05:09:18 -06:00
$leftToSpendAmount = bcadd($totalAvailableSum, $spentAmount);
2020-01-31 23:19:12 -06:00
if (1 === bccomp($leftToSpendAmount, '0')) {
$boxTitle = (string) trans('firefly.left_to_spend');
2020-01-31 23:19:12 -06:00
$days = $today->diffInDays($end) + 1;
$display = 1; // not overspent
$leftPerDayAmount = bcdiv($leftToSpendAmount, (string) $days);
2020-01-31 23:19:12 -06:00
}
2017-09-27 01:45:27 -05:00
}
$return = [
2020-01-31 23:19:12 -06: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 01:45:27 -05:00
];
$cache->store($return);
2018-03-10 13:30:09 -06:00
return response()->json($return);
2017-09-27 01:45:27 -05:00
}
2018-07-08 05:08:53 -05:00
2017-09-27 01:45:27 -05:00
/**
2018-07-21 01:06:24 -05:00
* Current total balance.
*
2018-03-09 23:49:03 -06:00
* @param CurrencyRepositoryInterface $repository
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2017-09-27 01:45:27 -05:00
*/
2018-07-08 05:28:42 -05:00
public function balance(CurrencyRepositoryInterface $repository): JsonResponse
2017-09-27 01:45:27 -05:00
{
2018-03-09 23:49:03 -06:00
// Cache result, return cache if present.
2018-07-26 21:46:21 -05:00
/** @var Carbon $start */
2017-09-27 01:45:27 -05:00
$start = session('start', Carbon::now()->startOfMonth());
2018-07-26 21:46:21 -05:00
/** @var Carbon $end */
2017-09-27 01:45:27 -05:00
$end = session('end', Carbon::now()->endOfMonth());
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('box-balance');
if ($cache->has()) {
return response()->json($cache->get()); // @codeCoverageIgnore
2017-09-27 01:45:27 -05:00
}
2018-03-09 23:49:03 -06:00
// prep some arrays:
$incomes = [];
$expenses = [];
$sums = [];
2019-07-20 15:32:32 -05:00
$currency = app('amount')->getDefaultCurrency();
2017-09-27 01:45:27 -05:00
2018-03-09 23:49:03 -06: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) {
$currencyId = (int) $journal['currency_id'];
2020-02-10 12:15:35 -06:00
$amount = $journal['amount'] ?? '0';
2018-03-09 23:49:03 -06:00
$incomes[$currencyId] = $incomes[$currencyId] ?? '0';
2020-02-10 12:15:35 -06:00
$incomes[$currencyId] = bcadd($incomes[$currencyId], app('steam')->positive($amount));
2018-03-09 23:49:03 -06:00
$sums[$currencyId] = $sums[$currencyId] ?? '0';
2020-02-10 12:15:35 -06:00
$sums[$currencyId] = bcadd($sums[$currencyId], app('steam')->positive($amount));
2018-03-09 23:49:03 -06:00
}
2017-09-27 01:45:27 -05:00
2018-03-09 23:49:03 -06: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) {
$currencyId = (int) $journal['currency_id'];
2018-03-09 23:49:03 -06:00
$expenses[$currencyId] = $expenses[$currencyId] ?? '0';
2020-02-28 11:53:20 -06:00
$expenses[$currencyId] = bcadd($expenses[$currencyId], $journal['amount'] ?? '0');
2018-03-09 23:49:03 -06:00
$sums[$currencyId] = $sums[$currencyId] ?? '0';
$sums[$currencyId] = bcadd($sums[$currencyId], $journal['amount']);
2018-03-09 23:49:03 -06:00
}
// format amounts:
2018-07-20 07:34:56 -05:00
$keys = array_keys($sums);
foreach ($keys as $currencyId) {
2018-03-09 23:49:03 -06:00
$currency = $repository->findNull($currencyId);
2018-07-20 07:34:56 -05: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-09 23:49:03 -06:00
}
if (0 === count($sums)) {
2018-06-17 00:45:10 -05:00
$currency = app('amount')->getDefaultCurrency();
2018-07-20 07:34:56 -05: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 00:45:10 -05:00
}
2017-09-27 01:45:27 -05:00
$response = [
2019-07-20 15:32:32 -05:00
'incomes' => $incomes,
'expenses' => $expenses,
'sums' => $sums,
'size' => count($sums),
'preferred' => $currency->id,
2017-09-27 01:45:27 -05:00
];
2018-06-17 00:45:10 -05:00
2017-09-27 01:45:27 -05:00
$cache->store($response);
2018-03-10 13:30:09 -06:00
return response()->json($response);
2017-09-27 01:45:27 -05:00
}
2018-07-08 05:08:53 -05:00
2017-09-27 01:45:27 -05:00
/**
2018-07-21 01:06:24 -05:00
* Bills to pay and paid.
*
2017-09-27 01:45:27 -05:00
* @param BillRepositoryInterface $repository
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2017-09-27 01:45:27 -05:00
*/
2018-07-08 05:28:42 -05:00
public function bills(BillRepositoryInterface $repository): JsonResponse
2017-09-27 01:45:27 -05:00
{
2018-07-26 21:46:21 -05:00
/** @var Carbon $start */
2017-09-27 01:45:27 -05:00
$start = session('start', Carbon::now()->startOfMonth());
2018-07-26 21:46:21 -05:00
/** @var Carbon $end */
2018-08-06 12:14:30 -05:00
$end = session('end', Carbon::now()->endOfMonth());
2017-09-27 01:45:27 -05:00
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('box-bills');
if ($cache->has()) {
2018-03-10 13:30:09 -06:00
return response()->json($cache->get()); // @codeCoverageIgnore
2017-09-27 01:45:27 -05:00
}
/*
* Since both this method and the chart use the exact same data, we can suffice
* with calling the one method in the bill repository that will get this amount.
*/
$paidAmount = bcmul($repository->getBillsPaidInRange($start, $end), '-1');
$unpaidAmount = $repository->getBillsUnpaidInRange($start, $end); // will be a positive amount.
$currency = app('amount')->getDefaultCurrency();
$return = [
'paid' => app('amount')->formatAnything($currency, $paidAmount, false),
'unpaid' => app('amount')->formatAnything($currency, $unpaidAmount, false),
];
$cache->store($return);
2018-03-10 13:30:09 -06:00
return response()->json($return);
2017-09-27 01:45:27 -05:00
}
2018-07-08 05:08:53 -05:00
2017-09-27 01:45:27 -05:00
/**
2018-07-21 01:06:24 -05:00
* Total user net worth.
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2017-09-27 01:45:27 -05:00
*/
public function netWorth(): JsonResponse
2017-09-27 01:45:27 -05:00
{
2020-04-14 10:23:58 -05:00
$date = Carbon::now()->endOfDay();
2017-10-16 12:01:26 -05:00
// start and end in the future? use $end
2018-07-20 07:34:56 -05:00
if ($this->notInSessionRange($date)) {
/** @var Carbon $date */
$date = session('end', Carbon::now()->endOfMonth());
2017-10-16 12:01:26 -05:00
}
2018-07-20 07:34:56 -05:00
/** @var NetWorthInterface $netWorthHelper */
$netWorthHelper = app(NetWorthInterface::class);
$netWorthHelper->setUser(auth()->user());
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
2019-02-13 10:38:41 -06:00
$allAccounts = $accountRepository->getActiveAccountsByType(
[AccountType::DEFAULT, AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD]
);
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);
2017-09-27 01:45:27 -05:00
2018-01-13 11:40:28 -06:00
$return = [];
foreach ($netWorthSet as $index => $data) {
/** @var TransactionCurrency $currency */
2019-02-13 10:38:41 -06:00
$currency = $data['currency'];
$return[$currency->id] = app('amount')->formatAnything($currency, $data['balance'], false);
2018-01-13 11:40:28 -06:00
}
2017-09-27 01:45:27 -05:00
$return = [
2018-01-13 11:40:28 -06:00
'net_worths' => array_values($return),
2017-09-27 01:45:27 -05:00
];
2018-03-10 13:30:09 -06:00
return response()->json($return);
2017-09-27 01:45:27 -05:00
}
2018-07-20 07:34:56 -05:00
2017-11-08 02:05:10 -06:00
}