2019-01-18 05:38:23 +01:00
|
|
|
<?php
|
2019-02-09 10:36:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AccountController.php
|
|
|
|
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-01-18 05:38:23 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Api\V1\Controllers\Chart;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Api\V1\Controllers\Controller;
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use FireflyIII\Models\AccountType;
|
2019-01-26 20:10:48 +01:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2019-01-18 05:38:23 +01:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2019-01-26 20:10:48 +01:00
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
2019-01-18 05:38:23 +01:00
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Http\Request;
|
2019-01-26 20:10:48 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2019-01-18 05:38:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AccountController
|
|
|
|
*/
|
|
|
|
class AccountController extends Controller
|
|
|
|
{
|
2019-01-26 20:10:48 +01:00
|
|
|
/** @var CurrencyRepositoryInterface */
|
|
|
|
private $currencyRepository;
|
2019-01-18 05:38:23 +01:00
|
|
|
/** @var AccountRepositoryInterface */
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AccountController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
$this->repository = app(AccountRepositoryInterface::class);
|
|
|
|
$this->repository->setUser($user);
|
|
|
|
|
2019-01-26 20:10:48 +01:00
|
|
|
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
|
|
|
|
$this->currencyRepository->setUser($user);
|
|
|
|
|
2019-01-18 05:38:23 +01:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-26 20:10:48 +01:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function expenseOverview(Request $request): JsonResponse
|
|
|
|
{
|
|
|
|
// parameters for chart:
|
|
|
|
$start = (string)$request->get('start');
|
|
|
|
$end = (string)$request->get('end');
|
|
|
|
if ('' === $start || '' === $end) {
|
|
|
|
throw new FireflyException('Start and end are mandatory parameters.');
|
|
|
|
}
|
2019-05-30 12:39:06 +02:00
|
|
|
/** @var Carbon $start */
|
2019-01-26 20:10:48 +01:00
|
|
|
$start = Carbon::createFromFormat('Y-m-d', $start);
|
|
|
|
$end = Carbon::createFromFormat('Y-m-d', $end);
|
|
|
|
$start->subDay();
|
|
|
|
|
|
|
|
// prep some vars:
|
|
|
|
$currencies = [];
|
|
|
|
$chartData = [];
|
|
|
|
$tempData = [];
|
|
|
|
|
|
|
|
// grab all accounts and names
|
|
|
|
$accounts = $this->repository->getAccountsByType([AccountType::EXPENSE]);
|
|
|
|
$accountNames = $this->extractNames($accounts);
|
|
|
|
$startBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $start);
|
|
|
|
$endBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $end);
|
|
|
|
|
|
|
|
// loop the end balances. This is an array for each account ($expenses)
|
|
|
|
foreach ($endBalances as $accountId => $expenses) {
|
|
|
|
$accountId = (int)$accountId;
|
|
|
|
// loop each expense entry (each entry can be a different currency).
|
|
|
|
foreach ($expenses as $currencyId => $endAmount) {
|
|
|
|
$currencyId = (int)$currencyId;
|
|
|
|
|
|
|
|
// see if there is an accompanying start amount.
|
|
|
|
// grab the difference and find the currency.
|
|
|
|
$startAmount = $startBalances[$accountId][$currencyId] ?? '0';
|
|
|
|
$diff = bcsub($endAmount, $startAmount);
|
|
|
|
$currencies[$currencyId] = $currencies[$currencyId] ?? $this->currencyRepository->findNull($currencyId);
|
|
|
|
if (0 !== bccomp($diff, '0')) {
|
|
|
|
// store the values in a temporary array.
|
|
|
|
$tempData[] = [
|
|
|
|
'name' => $accountNames[$accountId],
|
|
|
|
'difference' => $diff,
|
|
|
|
'diff_float' => (float)$diff,
|
|
|
|
'currency_id' => $currencyId,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort temp array by amount.
|
|
|
|
$amounts = array_column($tempData, 'diff_float');
|
|
|
|
array_multisort($amounts, SORT_DESC, $tempData);
|
|
|
|
|
|
|
|
// loop all found currencies and build the data array for the chart.
|
|
|
|
/**
|
2019-05-30 12:39:06 +02:00
|
|
|
* @var int $currencyId
|
2019-01-26 20:10:48 +01:00
|
|
|
* @var TransactionCurrency $currency
|
|
|
|
*/
|
|
|
|
foreach ($currencies as $currencyId => $currency) {
|
|
|
|
$currentSet = [
|
|
|
|
'label' => trans('firefly.box_spent_in_currency', ['currency' => $currency->symbol]),
|
|
|
|
'currency_id' => $currency->id,
|
|
|
|
'currency_code' => $currency->code,
|
|
|
|
'currency_symbol' => $currency->symbol,
|
|
|
|
'currency_decimal_places' => $currency->decimal_places,
|
|
|
|
'type' => 'bar', // line, area or bar
|
|
|
|
'yAxisID' => 0, // 0, 1, 2
|
|
|
|
'entries' => $this->expandNames($tempData),
|
|
|
|
];
|
|
|
|
$chartData[$currencyId] = $currentSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop temp data and place data in correct array:
|
|
|
|
foreach ($tempData as $entry) {
|
|
|
|
$currencyId = $entry['currency_id'];
|
|
|
|
$name = $entry['name'];
|
|
|
|
$chartData[$currencyId]['entries'][$name] = round($entry['difference'], $chartData[$currencyId]['currency_decimal_places']);
|
|
|
|
}
|
|
|
|
$chartData = array_values($chartData);
|
|
|
|
|
|
|
|
return response()->json($chartData);
|
|
|
|
}
|
|
|
|
|
2019-05-30 12:39:06 +02:00
|
|
|
/**
|
|
|
|
* Small helper function for the revenue and expense account charts.
|
|
|
|
* TODO should include Trait instead of doing this.
|
|
|
|
*
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function extractNames(Collection $accounts): array
|
|
|
|
{
|
|
|
|
$return = [];
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
$return[$account->id] = $account->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Small helper function for the revenue and expense account charts.
|
|
|
|
* TODO should include Trait instead of doing this.
|
|
|
|
*
|
|
|
|
* @param array $names
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function expandNames(array $names): array
|
|
|
|
{
|
|
|
|
$result = [];
|
|
|
|
foreach ($names as $entry) {
|
|
|
|
$result[$entry['name']] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-02-13 17:38:41 +01:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function overview(Request $request): JsonResponse
|
|
|
|
{
|
|
|
|
// parameters for chart:
|
|
|
|
$start = (string)$request->get('start');
|
|
|
|
$end = (string)$request->get('end');
|
|
|
|
if ('' === $start || '' === $end) {
|
|
|
|
throw new FireflyException('Start and end are mandatory parameters.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$start = Carbon::createFromFormat('Y-m-d', $start);
|
|
|
|
$end = Carbon::createFromFormat('Y-m-d', $end);
|
|
|
|
|
|
|
|
// user's preferences
|
|
|
|
$defaultSet = $this->repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray();
|
2019-05-30 12:39:06 +02:00
|
|
|
$frontPage = app('preferences')->get('frontPageAccounts', $defaultSet);
|
|
|
|
$default = app('amount')->getDefaultCurrency();
|
2019-05-30 12:31:19 +02:00
|
|
|
if (0 === count($frontPage->data)) {
|
2019-02-13 17:38:41 +01:00
|
|
|
$frontPage->data = $defaultSet;
|
|
|
|
$frontPage->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
// get accounts:
|
|
|
|
$accounts = $this->repository->getAccountsById($frontPage->data);
|
|
|
|
$chartData = [];
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
$currency = $this->repository->getAccountCurrency($account);
|
|
|
|
if (null === $currency) {
|
|
|
|
$currency = $default;
|
|
|
|
}
|
|
|
|
$currentSet = [
|
|
|
|
'label' => $account->name,
|
|
|
|
'currency_id' => $currency->id,
|
|
|
|
'currency_code' => $currency->code,
|
|
|
|
'currency_symbol' => $currency->symbol,
|
|
|
|
'currency_decimal_places' => $currency->decimal_places,
|
|
|
|
'type' => 'line', // line, area or bar
|
|
|
|
'yAxisID' => 0, // 0, 1, 2
|
|
|
|
'entries' => [],
|
|
|
|
];
|
2019-05-30 12:39:06 +02:00
|
|
|
/** @var Carbon $currentStart */
|
2019-02-13 17:38:41 +01:00
|
|
|
$currentStart = clone $start;
|
|
|
|
$range = app('steam')->balanceInRange($account, $start, clone $end);
|
|
|
|
$previous = round(array_values($range)[0], 12);
|
|
|
|
while ($currentStart <= $end) {
|
|
|
|
$format = $currentStart->format('Y-m-d');
|
|
|
|
$label = $currentStart->format('Y-m-d');
|
|
|
|
$balance = isset($range[$format]) ? round($range[$format], 12) : $previous;
|
|
|
|
$previous = $balance;
|
|
|
|
$currentStart->addDay();
|
|
|
|
$currentSet['entries'][$label] = $balance;
|
|
|
|
}
|
|
|
|
$chartData[] = $currentSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($chartData);
|
|
|
|
}
|
|
|
|
|
2019-01-26 20:10:48 +01:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function revenueOverview(Request $request): JsonResponse
|
|
|
|
{
|
|
|
|
// parameters for chart:
|
|
|
|
$start = (string)$request->get('start');
|
|
|
|
$end = (string)$request->get('end');
|
|
|
|
if ('' === $start || '' === $end) {
|
|
|
|
throw new FireflyException('Start and end are mandatory parameters.');
|
|
|
|
}
|
2019-05-30 12:39:06 +02:00
|
|
|
/** @var Carbon $start */
|
2019-01-26 20:10:48 +01:00
|
|
|
$start = Carbon::createFromFormat('Y-m-d', $start);
|
|
|
|
$end = Carbon::createFromFormat('Y-m-d', $end);
|
|
|
|
$start->subDay();
|
|
|
|
|
|
|
|
// prep some vars:
|
|
|
|
$currencies = [];
|
|
|
|
$chartData = [];
|
|
|
|
$tempData = [];
|
|
|
|
|
|
|
|
// grab all accounts and names
|
|
|
|
$accounts = $this->repository->getAccountsByType([AccountType::REVENUE]);
|
|
|
|
$accountNames = $this->extractNames($accounts);
|
|
|
|
$startBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $start);
|
|
|
|
$endBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $end);
|
|
|
|
|
|
|
|
// loop the end balances. This is an array for each account ($expenses)
|
|
|
|
foreach ($endBalances as $accountId => $expenses) {
|
|
|
|
$accountId = (int)$accountId;
|
|
|
|
// loop each expense entry (each entry can be a different currency).
|
|
|
|
foreach ($expenses as $currencyId => $endAmount) {
|
|
|
|
$currencyId = (int)$currencyId;
|
|
|
|
|
|
|
|
// see if there is an accompanying start amount.
|
|
|
|
// grab the difference and find the currency.
|
|
|
|
$startAmount = $startBalances[$accountId][$currencyId] ?? '0';
|
|
|
|
$diff = bcsub($endAmount, $startAmount);
|
|
|
|
$currencies[$currencyId] = $currencies[$currencyId] ?? $this->currencyRepository->findNull($currencyId);
|
|
|
|
if (0 !== bccomp($diff, '0')) {
|
|
|
|
// store the values in a temporary array.
|
|
|
|
$tempData[] = [
|
|
|
|
'name' => $accountNames[$accountId],
|
2019-02-13 17:38:41 +01:00
|
|
|
'difference' => bcmul($diff, '-1'),
|
2019-01-26 20:10:48 +01:00
|
|
|
'diff_float' => (float)$diff * -1,
|
|
|
|
'currency_id' => $currencyId,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort temp array by amount.
|
|
|
|
$amounts = array_column($tempData, 'diff_float');
|
|
|
|
array_multisort($amounts, SORT_DESC, $tempData);
|
|
|
|
|
|
|
|
// loop all found currencies and build the data array for the chart.
|
|
|
|
/**
|
2019-05-30 12:39:06 +02:00
|
|
|
* @var int $currencyId
|
2019-01-26 20:10:48 +01:00
|
|
|
* @var TransactionCurrency $currency
|
|
|
|
*/
|
|
|
|
foreach ($currencies as $currencyId => $currency) {
|
|
|
|
$currentSet = [
|
|
|
|
'label' => trans('firefly.box_earned_in_currency', ['currency' => $currency->symbol]),
|
|
|
|
'currency_id' => $currency->id,
|
|
|
|
'currency_code' => $currency->code,
|
|
|
|
'currency_symbol' => $currency->symbol,
|
|
|
|
'currency_decimal_places' => $currency->decimal_places,
|
|
|
|
'type' => 'bar', // line, area or bar
|
|
|
|
'yAxisID' => 0, // 0, 1, 2
|
|
|
|
'entries' => $this->expandNames($tempData),
|
|
|
|
];
|
|
|
|
$chartData[$currencyId] = $currentSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop temp data and place data in correct array:
|
|
|
|
foreach ($tempData as $entry) {
|
|
|
|
$currencyId = $entry['currency_id'];
|
|
|
|
$name = $entry['name'];
|
|
|
|
$chartData[$currencyId]['entries'][$name] = round($entry['difference'], $chartData[$currencyId]['currency_decimal_places']);
|
|
|
|
}
|
|
|
|
$chartData = array_values($chartData);
|
|
|
|
|
|
|
|
return response()->json($chartData);
|
|
|
|
}
|
|
|
|
|
2019-02-09 10:36:59 +01:00
|
|
|
}
|