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

291 lines
11 KiB
PHP
Raw Normal View History

2017-09-27 01:45:27 -05:00
<?php
/**
* BoxController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-09-27 01:45:27 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
2018-03-09 23:49:03 -06:00
use Amount;
2017-09-27 01:45:27 -05:00
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
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;
2018-03-09 23:49:03 -06:00
use FireflyIII\Models\Transaction;
2017-09-27 01:45:27 -05:00
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
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;
2018-07-08 05:28:42 -05:00
use Illuminate\Http\JsonResponse;
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
{
2018-07-08 05:08:53 -05:00
2017-09-27 01:45:27 -05:00
/**
* @param BudgetRepositoryInterface $repository
2017-09-27 01:45:27 -05:00
*
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 available(BudgetRepositoryInterface $repository): JsonResponse
2017-09-27 01:45:27 -05:00
{
$start = session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
$today = new Carbon;
$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
}
// get available amount
$currency = app('amount')->getDefaultCurrency();
$available = $repository->getAvailableBudget($currency, $start, $end);
// get spent amount:
$budgets = $repository->getActiveBudgets();
$budgetInformation = $repository->collectBudgetInformation($budgets, $start, $end);
2018-03-25 03:17:07 -05:00
$spent = (string)array_sum(array_column($budgetInformation, 'spent'));
2017-09-27 01:45:27 -05:00
$left = bcadd($available, $spent);
2018-03-25 03:17:07 -05:00
$days = $today->diffInDays($end) + 1;
$perDay = '0';
$text = (string)trans('firefly.left_to_spend');
$overspent = false;
2017-09-27 01:45:27 -05:00
if (bccomp($left, '0') === -1) {
2018-03-25 03:17:07 -05:00
$text = (string)trans('firefly.overspent');
$overspent = true;
2017-09-27 01:45:27 -05:00
}
2018-03-25 03:17:07 -05:00
if (0 !== $days && bccomp($left, '0') > -1) {
$perDay = bcdiv($left, (string)$days);
2017-09-27 01:45:27 -05:00
}
$return = [
2018-03-25 03:17:07 -05:00
'perDay' => app('amount')->formatAnything($currency, $perDay, false),
'left' => app('amount')->formatAnything($currency, $left, false),
'text' => $text,
'overspent' => $overspent,
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-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.
2017-09-27 01:45:27 -05:00
$start = session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('box-balance');
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
}
2018-03-09 23:49:03 -06:00
// prep some arrays:
$incomes = [];
$expenses = [];
$sums = [];
2017-09-27 01:45:27 -05:00
2018-03-09 23:49:03 -06:00
// collect income of user:
2017-09-27 01:45:27 -05:00
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($start, $end)
->setTypes([TransactionType::DEPOSIT])
->withOpposingAccount();
2018-03-09 23:49:03 -06:00
$set = $collector->getJournals();
/** @var Transaction $transaction */
foreach ($set as $transaction) {
2018-04-02 08:10:40 -05:00
$currencyId = (int)$transaction->transaction_currency_id;
2018-03-09 23:49:03 -06:00
$incomes[$currencyId] = $incomes[$currencyId] ?? '0';
$incomes[$currencyId] = bcadd($incomes[$currencyId], $transaction->transaction_amount);
$sums[$currencyId] = $sums[$currencyId] ?? '0';
$sums[$currencyId] = bcadd($sums[$currencyId], $transaction->transaction_amount);
}
2017-09-27 01:45:27 -05:00
2018-03-09 23:49:03 -06:00
// collect expenses
2017-09-27 01:45:27 -05:00
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAllAssetAccounts()->setRange($start, $end)
->setTypes([TransactionType::WITHDRAWAL])
->withOpposingAccount();
2018-03-09 23:49:03 -06:00
$set = $collector->getJournals();
/** @var Transaction $transaction */
foreach ($set as $transaction) {
2018-03-25 03:17:07 -05:00
$currencyId = (int)$transaction->transaction_currency_id;
2018-03-09 23:49:03 -06:00
$expenses[$currencyId] = $expenses[$currencyId] ?? '0';
$expenses[$currencyId] = bcadd($expenses[$currencyId], $transaction->transaction_amount);
$sums[$currencyId] = $sums[$currencyId] ?? '0';
$sums[$currencyId] = bcadd($sums[$currencyId], $transaction->transaction_amount);
}
// format amounts:
foreach ($sums as $currencyId => $amount) {
$currency = $repository->findNull($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-07-08 00:59:58 -05:00
if (0 === \count($sums)) {
2018-06-17 00:45:10 -05:00
$currency = app('amount')->getDefaultCurrency();
$sums[$currency->id] = Amount::formatAnything($currency, '0', false);
$incomes[$currency->id] = Amount::formatAnything($currency, '0', false);
$expenses[$currency->id] = Amount::formatAnything($currency, '0', false);
}
2017-09-27 01:45:27 -05:00
$response = [
2018-03-09 23:49:03 -06:00
'incomes' => $incomes,
'expenses' => $expenses,
'sums' => $sums,
2018-04-27 23:23:13 -05:00
'size' => \count($sums),
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
/**
* @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
{
$start = session('start', Carbon::now()->startOfMonth());
$end = session('end', Carbon::now()->endOfMonth());
$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-03-11 10:24:07 -05:00
* @param AccountRepositoryInterface $repository
*
* @param CurrencyRepositoryInterface $currencyRepos
2017-09-27 01:45:27 -05:00
*
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 netWorth(AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepos): JsonResponse
2017-09-27 01:45:27 -05:00
{
2017-10-16 12:01:26 -05:00
$date = new Carbon(date('Y-m-d')); // needed so its per day.
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
// start and end in the future? use $end
if ($start->greaterThanOrEqualTo($date) && $end->greaterThanOrEqualTo($date)) {
$date = $end;
}
// start and end in the past? use $end
if ($start->lessThanOrEqualTo($date) && $end->lessThanOrEqualTo($date)) {
$date = $end;
}
// start in the past, end in the future? use $date
2017-09-27 01:45:27 -05:00
$cache = new CacheProperties;
2017-10-16 12:01:26 -05:00
$cache->addProperty($date);
2017-09-27 01:45:27 -05:00
$cache->addProperty('box-net-worth');
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
}
2018-01-13 11:40:28 -06:00
$netWorth = [];
2018-03-07 13:27:10 -06:00
$accounts = $repository->getActiveAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
2017-09-27 01:45:27 -05:00
$currency = app('amount')->getDefaultCurrency();
2017-10-16 12:01:26 -05:00
$balances = app('steam')->balancesByAccounts($accounts, $date);
2018-01-13 11:40:28 -06:00
/** @var Account $account */
foreach ($accounts as $account) {
2018-03-30 15:40:20 -05:00
$accountCurrency = null;
2018-01-13 11:40:28 -06:00
$balance = $balances[$account->id] ?? '0';
2018-03-25 03:17:07 -05:00
$currencyId = (int)$repository->getMetaValue($account, 'currency_id');
2018-07-08 00:59:58 -05:00
if (0 !== $currencyId) {
$accountCurrency = $currencyRepos->findNull($currencyId);
2018-01-13 11:40:28 -06:00
}
2018-03-30 15:40:20 -05:00
if (null === $accountCurrency) {
$accountCurrency = $currency;
}
// if the account is a credit card, subtract the virtual balance from the balance,
// to better reflect that this is not money that is actually "yours".
$role = (string)$repository->getMetaValue($account, 'accountRole');
$virtualBalance = (string)$account->virtual_balance;
2018-07-08 00:59:58 -05:00
if ('ccAsset' === $role && '' !== $virtualBalance && (float)$virtualBalance > 0) {
$balance = bcsub($balance, $virtualBalance);
}
2018-01-13 11:40:28 -06:00
if (!isset($netWorth[$accountCurrency->id])) {
$netWorth[$accountCurrency->id]['currency'] = $accountCurrency;
$netWorth[$accountCurrency->id]['sum'] = '0';
}
$netWorth[$accountCurrency->id]['sum'] = bcadd($netWorth[$accountCurrency->id]['sum'], $balance);
2017-09-27 01:45:27 -05:00
}
2018-01-13 11:40:28 -06:00
$return = [];
foreach ($netWorth as $currencyId => $data) {
$return[$currencyId] = app('amount')->formatAnything($data['currency'], $data['sum'], false);
}
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
];
$cache->store($return);
2018-03-10 13:30:09 -06:00
return response()->json($return);
2017-09-27 01:45:27 -05:00
}
2017-11-08 02:05:10 -06:00
}