mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-17 23:28:20 +00:00
Make some charts currency aware for #740
This commit is contained in:
@@ -18,7 +18,6 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/** @noinspection NullPointerExceptionInspection */
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Http\Controllers\Chart;
|
||||
@@ -30,6 +29,7 @@ use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
@@ -54,24 +54,41 @@ class AccountController extends Controller
|
||||
/** @var GeneratorInterface Chart generation methods. */
|
||||
protected $generator;
|
||||
|
||||
/** @var AccountRepositoryInterface Account repository. */
|
||||
private $accountRepository;
|
||||
|
||||
/** @var CurrencyRepositoryInterface */
|
||||
private $currencyRepository;
|
||||
|
||||
/**
|
||||
* AccountController constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->generator = app(GeneratorInterface::class);
|
||||
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
$this->generator = app(GeneratorInterface::class);
|
||||
$this->accountRepository = app(AccountRepositoryInterface::class);
|
||||
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Shows the balances for all the user's expense accounts (on the front page).
|
||||
*
|
||||
* @param AccountRepositoryInterface $repository
|
||||
* This chart is (multi) currency aware.
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function expenseAccounts(AccountRepositoryInterface $repository): JsonResponse
|
||||
public function expenseAccounts(): JsonResponse
|
||||
{
|
||||
/** @var Carbon $start */
|
||||
$start = clone session('start', Carbon::now()->startOfMonth());
|
||||
@@ -86,49 +103,70 @@ class AccountController extends Controller
|
||||
}
|
||||
$start->subDay();
|
||||
|
||||
/** @var CurrencyRepositoryInterface $currencyRepos */
|
||||
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
||||
$currencies = [];
|
||||
$chartData = [
|
||||
[
|
||||
'label' => (string)trans('firefly.spent'),
|
||||
'type' => 'bar',
|
||||
'currency_symbol' => '¤',
|
||||
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
|
||||
'entries' => [],
|
||||
],
|
||||
];
|
||||
$accounts = $repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]);
|
||||
$accountNames = $this->extractNames($accounts);
|
||||
// prep some vars:
|
||||
$currencies = [];
|
||||
$chartData = [];
|
||||
$tempData = [];
|
||||
|
||||
// grab all accounts and names
|
||||
$accounts = $this->accountRepository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]);
|
||||
$accountNames = $this->extractNames($accounts);
|
||||
|
||||
// grab all balances
|
||||
$startBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $start);
|
||||
$endBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $end);
|
||||
$tempData = [];
|
||||
|
||||
|
||||
// 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;
|
||||
$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] ?? $currencyRepos->findNull($currencyId);
|
||||
$title = (string)trans(
|
||||
'firefly.account_in_currency', ['account' => $accountNames[$accountId], 'currency' => $currencies[$currencyId]->name]
|
||||
);
|
||||
$tempData[$title] = $diff;
|
||||
$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,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
arsort($tempData, SORT_NUMERIC);
|
||||
|
||||
foreach ($tempData as $label => $entry) {
|
||||
if (0 !== bccomp($entry, '0')) {
|
||||
$chartData[0]['entries'][$label] = $entry;
|
||||
}
|
||||
// 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.
|
||||
/**
|
||||
* @var int $currencyId
|
||||
* @var TransactionCurrency $currency
|
||||
*/
|
||||
foreach ($currencies as $currencyId => $currency) {
|
||||
$dataSet
|
||||
= [
|
||||
'label' => (string)trans('firefly.spent'),
|
||||
'type' => 'bar',
|
||||
'currency_symbol' => $currency->symbol,
|
||||
'entries' => $this->expandNames($tempData),
|
||||
];
|
||||
$chartData[$currencyId] = $dataSet;
|
||||
}
|
||||
if (1 === \count($currencies)) {
|
||||
$first = array_first($currencies);
|
||||
$chartData[0]['currency_symbol'] = $first->symbol;
|
||||
|
||||
// loop temp data and place data in correct array:
|
||||
foreach ($tempData as $entry) {
|
||||
$currencyId = $entry['currency_id'];
|
||||
$name = $entry['name'];
|
||||
$chartData[$currencyId]['entries'][$name] = $entry['difference'];
|
||||
}
|
||||
|
||||
$data = $this->generator->multiSet($chartData);
|
||||
$cache->store($data);
|
||||
|
||||
@@ -161,19 +199,31 @@ class AccountController extends Controller
|
||||
$transactions = $collector->getTransactions();
|
||||
$chartData = [];
|
||||
$result = [];
|
||||
|
||||
$budgetIds = [];
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
$jrnlBudgetId = (int)$transaction->transaction_journal_budget_id;
|
||||
$transBudgetId = (int)$transaction->transaction_budget_id;
|
||||
$budgetId = max($jrnlBudgetId, $transBudgetId);
|
||||
$result[$budgetId] = $result[$budgetId] ?? '0';
|
||||
$result[$budgetId] = bcadd($transaction->transaction_amount, $result[$budgetId]);
|
||||
$jrnlBudgetId = (int)$transaction->transaction_journal_budget_id;
|
||||
$transBudgetId = (int)$transaction->transaction_budget_id;
|
||||
$currencyName = $transaction->transaction_currency_name;
|
||||
$budgetId = max($jrnlBudgetId, $transBudgetId);
|
||||
$combi = $budgetId . $currencyName;
|
||||
$budgetIds[] = $budgetId;
|
||||
if (!isset($result[$combi])) {
|
||||
$result[$combi] = [
|
||||
'total' => '0',
|
||||
'budget_id' => $budgetId,
|
||||
'currency' => $currencyName,
|
||||
];
|
||||
}
|
||||
$result[$combi]['total'] = bcadd($transaction->transaction_amount, $result[$combi]['total']);
|
||||
}
|
||||
|
||||
$names = $this->getBudgetNames(array_keys($result));
|
||||
foreach ($result as $budgetId => $amount) {
|
||||
$chartData[$names[$budgetId]] = $amount;
|
||||
$names = $this->getBudgetNames($budgetIds);
|
||||
foreach ($result as $row) {
|
||||
$budgetId = $row['budget_id'];
|
||||
$name = $names[$budgetId];
|
||||
$label = (string)trans('firefly.name_in_currency', ['name' => $name, 'currency' => $row['currency']]);
|
||||
$chartData[$label] = $row['total'];
|
||||
}
|
||||
|
||||
$data = $this->generator->pieChart($chartData);
|
||||
@@ -225,18 +275,32 @@ class AccountController extends Controller
|
||||
$transactions = $collector->getTransactions();
|
||||
$result = [];
|
||||
$chartData = [];
|
||||
$categoryIds = [];
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
$jrnlCatId = (int)$transaction->transaction_journal_category_id;
|
||||
$transCatId = (int)$transaction->transaction_category_id;
|
||||
$categoryId = max($jrnlCatId, $transCatId);
|
||||
$result[$categoryId] = $result[$categoryId] ?? '0';
|
||||
$result[$categoryId] = bcadd($transaction->transaction_amount, $result[$categoryId]);
|
||||
$jrnlCatId = (int)$transaction->transaction_journal_category_id;
|
||||
$transCatId = (int)$transaction->transaction_category_id;
|
||||
$currencyName = $transaction->transaction_currency_name;
|
||||
$categoryId = max($jrnlCatId, $transCatId);
|
||||
$combi = $categoryId . $currencyName;
|
||||
$categoryIds[] = $categoryId;
|
||||
if (!isset($result[$combi])) {
|
||||
$result[$combi] = [
|
||||
'total' => '0',
|
||||
'category_id' => $categoryId,
|
||||
'currency' => $currencyName,
|
||||
];
|
||||
}
|
||||
$result[$combi]['total'] = bcadd($transaction->transaction_amount, $result[$combi]['total']);
|
||||
}
|
||||
|
||||
$names = $this->getCategoryNames(array_keys($result));
|
||||
foreach ($result as $categoryId => $amount) {
|
||||
$chartData[$names[$categoryId]] = $amount;
|
||||
|
||||
foreach ($result as $row) {
|
||||
$categoryId = $row['category_id'];
|
||||
$name = $names[$categoryId];
|
||||
$label = (string)trans('firefly.name_in_currency', ['name' => $name, 'currency' => $row['currency']]);
|
||||
$chartData[$label] = $row['total'];
|
||||
}
|
||||
|
||||
$data = $this->generator->pieChart($chartData);
|
||||
@@ -265,6 +329,8 @@ class AccountController extends Controller
|
||||
/**
|
||||
* Shows the balances for all the user's frontpage accounts.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param AccountRepositoryInterface $repository
|
||||
*
|
||||
* @return JsonResponse
|
||||
@@ -319,16 +385,28 @@ class AccountController extends Controller
|
||||
$chartData = [];
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
$jrnlCatId = (int)$transaction->transaction_journal_category_id;
|
||||
$transCatId = (int)$transaction->transaction_category_id;
|
||||
$categoryId = max($jrnlCatId, $transCatId);
|
||||
$result[$categoryId] = $result[$categoryId] ?? '0';
|
||||
$result[$categoryId] = bcadd($transaction->transaction_amount, $result[$categoryId]);
|
||||
$jrnlCatId = (int)$transaction->transaction_journal_category_id;
|
||||
$transCatId = (int)$transaction->transaction_category_id;
|
||||
$categoryId = max($jrnlCatId, $transCatId);
|
||||
$currencyName = $transaction->transaction_currency_name;
|
||||
$combi = $categoryId . $currencyName;
|
||||
$categoryIds[] = $categoryId;
|
||||
if (!isset($result[$combi])) {
|
||||
$result[$combi] = [
|
||||
'total' => '0',
|
||||
'category_id' => $categoryId,
|
||||
'currency' => $currencyName,
|
||||
];
|
||||
}
|
||||
$result[$combi]['total'] = bcadd($transaction->transaction_amount, $result[$combi]['total']);
|
||||
}
|
||||
|
||||
$names = $this->getCategoryNames(array_keys($result));
|
||||
foreach ($result as $categoryId => $amount) {
|
||||
$chartData[$names[$categoryId]] = $amount;
|
||||
foreach ($result as $row) {
|
||||
$categoryId = $row['category_id'];
|
||||
$name = $names[$categoryId];
|
||||
$label = (string)trans('firefly.name_in_currency', ['name' => $name, 'currency' => $row['currency']]);
|
||||
$chartData[$label] = $row['total'];
|
||||
}
|
||||
$data = $this->generator->pieChart($chartData);
|
||||
$cache->store($data);
|
||||
@@ -356,6 +434,8 @@ class AccountController extends Controller
|
||||
/**
|
||||
* Shows overview of account during a single period.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Account $account
|
||||
* @param Carbon $start
|
||||
*
|
||||
@@ -415,6 +495,8 @@ class AccountController extends Controller
|
||||
/**
|
||||
* Shows the balances for a given set of dates and accounts.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Collection $accounts
|
||||
@@ -430,41 +512,90 @@ class AccountController extends Controller
|
||||
/**
|
||||
* Shows the balances for all the user's revenue accounts.
|
||||
*
|
||||
* @param AccountRepositoryInterface $repository
|
||||
* This chart is multi-currency aware.
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function revenueAccounts(AccountRepositoryInterface $repository): JsonResponse
|
||||
public function revenueAccounts(): JsonResponse
|
||||
{
|
||||
$start = clone session('start', Carbon::now()->startOfMonth());
|
||||
$end = clone session('end', Carbon::now()->endOfMonth());
|
||||
$chartData = [];
|
||||
$cache = new CacheProperties;
|
||||
/** @var Carbon $start */
|
||||
$start = clone session('start', Carbon::now()->startOfMonth());
|
||||
/** @var Carbon $end */
|
||||
$end = clone session('end', Carbon::now()->endOfMonth());
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty($start);
|
||||
$cache->addProperty($end);
|
||||
$cache->addProperty('chart.account.revenue-accounts');
|
||||
if ($cache->has()) {
|
||||
return response()->json($cache->get()); // @codeCoverageIgnore
|
||||
//return response()->json($cache->get()); // @codeCoverageIgnore
|
||||
}
|
||||
$accounts = $repository->getAccountsByType([AccountType::REVENUE]);
|
||||
|
||||
$start->subDay();
|
||||
$startBalances = app('steam')->balancesByAccounts($accounts, $start);
|
||||
$endBalances = app('steam')->balancesByAccounts($accounts, $end);
|
||||
|
||||
foreach ($accounts as $account) {
|
||||
$id = $account->id;
|
||||
$startBalance = $startBalances[$id] ?? '0';
|
||||
$endBalance = $endBalances[$id] ?? '0';
|
||||
$diff = bcsub($endBalance, $startBalance);
|
||||
$diff = bcmul($diff, '-1');
|
||||
if (0 !== bccomp($diff, '0')) {
|
||||
$chartData[$account->name] = $diff;
|
||||
// prep some vars:
|
||||
$currencies = [];
|
||||
$chartData = [];
|
||||
$tempData = [];
|
||||
|
||||
// grab all accounts and names
|
||||
$accounts = $this->accountRepository->getAccountsByType([AccountType::REVENUE]);
|
||||
$accountNames = $this->extractNames($accounts);
|
||||
|
||||
// grab all balances
|
||||
$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,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
arsort($chartData);
|
||||
$data = $this->generator->singleSet((string)trans('firefly.earned'), $chartData);
|
||||
// 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.
|
||||
/**
|
||||
* @var int $currencyId
|
||||
* @var TransactionCurrency $currency
|
||||
*/
|
||||
foreach ($currencies as $currencyId => $currency) {
|
||||
$dataSet
|
||||
= [
|
||||
'label' => (string)trans('firefly.earned'),
|
||||
'type' => 'bar',
|
||||
'currency_symbol' => $currency->symbol,
|
||||
'entries' => $this->expandNames($tempData),
|
||||
];
|
||||
$chartData[$currencyId] = $dataSet;
|
||||
}
|
||||
|
||||
// loop temp data and place data in correct array:
|
||||
foreach ($tempData as $entry) {
|
||||
$currencyId = $entry['currency_id'];
|
||||
$name = $entry['name'];
|
||||
$chartData[$currencyId]['entries'][$name] = bcmul($entry['difference'], '-1');
|
||||
}
|
||||
|
||||
$data = $this->generator->multiSet($chartData);
|
||||
$cache->store($data);
|
||||
|
||||
return response()->json($data);
|
||||
@@ -535,6 +666,25 @@ class AccountController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Small helper function for the revenue and expense account charts.
|
||||
*
|
||||
* @param array $names
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function expandNames(array $names): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($names as $entry) {
|
||||
$result[$entry['name']] = 0;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Small helper function for the revenue and expense account charts.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
*
|
||||
* @return array
|
||||
@@ -549,36 +699,4 @@ class AccountController extends Controller
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method extracts the unique currency ID's from an array of balances.
|
||||
*
|
||||
* The given array is expected to be in this format:
|
||||
*
|
||||
* accountID1:
|
||||
* currencyID1: balance
|
||||
* currencyID2: balance
|
||||
* accountID2:
|
||||
* currencyID1: balance
|
||||
* currencyID2: balance
|
||||
*
|
||||
*
|
||||
* @param array $balances
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getCurrencyIDs(array $balances): array
|
||||
{
|
||||
$currencies = [];
|
||||
/**
|
||||
* @var int $accountId
|
||||
* @var array $info
|
||||
*/
|
||||
foreach ($balances as $accountId => $info) {
|
||||
$currencies = array_merge(array_keys($info));
|
||||
}
|
||||
|
||||
return array_unique($currencies);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -54,6 +54,8 @@ class BillController extends Controller
|
||||
/**
|
||||
* Shows all bills and whether or not they've been paid this month (pie chart).
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param BillRepositoryInterface $repository
|
||||
*
|
||||
* @return JsonResponse
|
||||
@@ -88,9 +90,11 @@ class BillController extends Controller
|
||||
* Shows overview for a single bill.
|
||||
*
|
||||
* @param TransactionCollectorInterface $collector
|
||||
* @param Bill $bill
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @return JsonResponse
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*/
|
||||
public function single(TransactionCollectorInterface $collector, Bill $bill): JsonResponse
|
||||
{
|
||||
@@ -109,9 +113,9 @@ class BillController extends Controller
|
||||
}
|
||||
);
|
||||
$chartData = [
|
||||
['type' => 'bar', 'label' => (string)trans('firefly.min-amount'),'currency_symbol' => $bill->transactionCurrency->symbol, 'entries' => []],
|
||||
['type' => 'bar', 'label' => (string)trans('firefly.max-amount'),'currency_symbol' => $bill->transactionCurrency->symbol, 'entries' => []],
|
||||
['type' => 'line', 'label' => (string)trans('firefly.journal-amount'),'currency_symbol' => $bill->transactionCurrency->symbol, 'entries' => []],
|
||||
['type' => 'bar', 'label' => (string)trans('firefly.min-amount'), 'currency_symbol' => $bill->transactionCurrency->symbol, 'entries' => []],
|
||||
['type' => 'bar', 'label' => (string)trans('firefly.max-amount'), 'currency_symbol' => $bill->transactionCurrency->symbol, 'entries' => []],
|
||||
['type' => 'line', 'label' => (string)trans('firefly.journal-amount'), 'currency_symbol' => $bill->transactionCurrency->symbol, 'entries' => []],
|
||||
];
|
||||
|
||||
/** @var Transaction $entry */
|
||||
|
||||
@@ -75,6 +75,8 @@ class BudgetController extends Controller
|
||||
/**
|
||||
* Shows overview of a single budget.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @return JsonResponse
|
||||
@@ -124,6 +126,8 @@ class BudgetController extends Controller
|
||||
/**
|
||||
* Shows the amount left in a specific budget limit.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Budget $budget
|
||||
* @param BudgetLimit $budgetLimit
|
||||
*
|
||||
@@ -171,6 +175,8 @@ class BudgetController extends Controller
|
||||
/**
|
||||
* Shows how much is spent per asset account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Budget $budget
|
||||
* @param BudgetLimit|null $budgetLimit
|
||||
*
|
||||
@@ -221,6 +227,8 @@ class BudgetController extends Controller
|
||||
/**
|
||||
* Shows how much is spent per category.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Budget $budget
|
||||
* @param BudgetLimit|null $budgetLimit
|
||||
*
|
||||
@@ -272,6 +280,8 @@ class BudgetController extends Controller
|
||||
/**
|
||||
* Shows how much is spent per expense account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Budget $budget
|
||||
* @param BudgetLimit|null $budgetLimit
|
||||
*
|
||||
@@ -323,6 +333,8 @@ class BudgetController extends Controller
|
||||
/**
|
||||
* Shows a budget list with spent/left/overspent.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @return JsonResponse
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
@@ -378,6 +390,8 @@ class BudgetController extends Controller
|
||||
/**
|
||||
* Shows a budget overview chart (spent and budgeted).
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
@@ -424,6 +438,8 @@ class BudgetController extends Controller
|
||||
/**
|
||||
* Shows a chart for transactions without a budget.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
|
||||
@@ -69,6 +69,8 @@ class BudgetReportController extends Controller
|
||||
/**
|
||||
* Chart that groups expenses by the account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $budgets
|
||||
* @param Carbon $start
|
||||
@@ -98,6 +100,8 @@ class BudgetReportController extends Controller
|
||||
/**
|
||||
* Chart that groups the expenses by budget.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $budgets
|
||||
* @param Carbon $start
|
||||
@@ -127,6 +131,8 @@ class BudgetReportController extends Controller
|
||||
/**
|
||||
* Main overview of a budget in the budget report.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $budgets
|
||||
* @param Carbon $start
|
||||
|
||||
@@ -27,8 +27,10 @@ use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -55,6 +57,8 @@ class CategoryController extends Controller
|
||||
/**
|
||||
* Show an overview for a category for all time, per month/week/year.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param CategoryRepositoryInterface $repository
|
||||
* @param AccountRepositoryInterface $accountRepository
|
||||
* @param Category $category
|
||||
@@ -131,35 +135,82 @@ class CategoryController extends Controller
|
||||
$cache->addProperty($end);
|
||||
$cache->addProperty('chart.category.frontpage');
|
||||
if ($cache->has()) {
|
||||
return response()->json($cache->get()); // @codeCoverageIgnore
|
||||
//return response()->json($cache->get()); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
// currency repos:
|
||||
/** @var CurrencyRepositoryInterface $currencyRepository */
|
||||
$currencyRepository = app(CurrencyRepositoryInterface::class);
|
||||
$currencies = [];
|
||||
|
||||
|
||||
$chartData = [];
|
||||
$tempData = [];
|
||||
$categories = $repository->getCategories();
|
||||
$accounts = $accountRepository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
|
||||
|
||||
/** @var Category $category */
|
||||
foreach ($categories as $category) {
|
||||
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $start, $end);
|
||||
if (bccomp($spent, '0') === -1) {
|
||||
$chartData[$category->name] = bcmul($spent, '-1');
|
||||
$spentArray = $repository->spentInPeriodPerCurrency(new Collection([$category]), $accounts, $start, $end);
|
||||
foreach ($spentArray as $currencyId => $spent) {
|
||||
if (bccomp($spent, '0') === -1) {
|
||||
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepository->findNull($currencyId);
|
||||
$tempData[] = [
|
||||
'name' => $category->name,
|
||||
'spent' => bcmul($spent, '-1'),
|
||||
'spent_float' => (float)bcmul($spent, '-1'),
|
||||
'currency_id' => $currencyId,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
// no category per currency:
|
||||
$noCategory = $repository->spentInPeriodPcWoCategory(new Collection, $start, $end);
|
||||
foreach ($noCategory as $currencyId => $spent) {
|
||||
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepository->findNull($currencyId);
|
||||
$tempData[] = [
|
||||
'name' => trans('firefly.no_category'),
|
||||
'spent' => bcmul($spent, '-1'),
|
||||
'spent_float' => (float)bcmul($spent, '-1'),
|
||||
'currency_id' => $currencyId,
|
||||
];
|
||||
}
|
||||
|
||||
$chartData[(string)trans('firefly.no_category')] = bcmul($repository->spentInPeriodWithoutCategory(new Collection, $start, $end), '-1');
|
||||
// sort temp array by amount.
|
||||
$amounts = array_column($tempData, 'spent_float');
|
||||
array_multisort($amounts, SORT_DESC, $tempData);
|
||||
|
||||
// sort
|
||||
arsort($chartData);
|
||||
|
||||
$data = $this->generator->singleSet((string)trans('firefly.spent'), $chartData);
|
||||
// loop all found currencies and build the data array for the chart.
|
||||
/**
|
||||
* @var int $currencyId
|
||||
* @var TransactionCurrency $currency
|
||||
*/
|
||||
foreach ($currencies as $currencyId => $currency) {
|
||||
$dataSet = [
|
||||
'label' => (string)trans('firefly.spent'),
|
||||
'type' => 'bar',
|
||||
'currency_symbol' => $currency->symbol,
|
||||
'entries' => $this->expandNames($tempData),
|
||||
];
|
||||
$chartData[$currencyId] = $dataSet;
|
||||
}
|
||||
// loop temp data and place data in correct array:
|
||||
foreach ($tempData as $entry) {
|
||||
$currencyId = $entry['currency_id'];
|
||||
$name = $entry['name'];
|
||||
$chartData[$currencyId]['entries'][$name] = $entry['spent'];
|
||||
}
|
||||
$data = $this->generator->multiSet($chartData);
|
||||
$cache->store($data);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
/**
|
||||
* Chart report.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Category $category
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
@@ -222,9 +273,13 @@ class CategoryController extends Controller
|
||||
}
|
||||
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
|
||||
/**
|
||||
* Chart for period for transactions without a category.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
@@ -286,6 +341,8 @@ class CategoryController extends Controller
|
||||
/**
|
||||
* Chart for a specific period.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Category $category
|
||||
* @param $date
|
||||
*
|
||||
@@ -301,10 +358,10 @@ class CategoryController extends Controller
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Chart for a specific period (start and end).
|
||||
*
|
||||
*
|
||||
* @param Category $category
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
@@ -371,4 +428,21 @@ class CategoryController extends Controller
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Small helper function for the revenue and expense account charts.
|
||||
*
|
||||
* @param array $names
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function expandNames(array $names): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($names as $entry) {
|
||||
$result[$entry['name']] = 0;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +64,8 @@ class CategoryReportController extends Controller
|
||||
/**
|
||||
* Chart for expenses grouped by expense account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
@@ -90,6 +92,8 @@ class CategoryReportController extends Controller
|
||||
/**
|
||||
* Chart for income grouped by revenue account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
@@ -119,6 +123,8 @@ class CategoryReportController extends Controller
|
||||
/**
|
||||
* Chart for expenses grouped by expense account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
@@ -148,6 +154,8 @@ class CategoryReportController extends Controller
|
||||
/**
|
||||
* Piechart for income grouped by account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
@@ -177,6 +185,8 @@ class CategoryReportController extends Controller
|
||||
/**
|
||||
* Main report category chart.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
|
||||
@@ -67,6 +67,8 @@ class ExpenseReportController extends Controller
|
||||
/**
|
||||
* Main chart that shows income and expense for a combination of expense/revenue accounts.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $expense
|
||||
* @param Carbon $start
|
||||
|
||||
@@ -55,6 +55,8 @@ class PiggyBankController extends Controller
|
||||
/**
|
||||
* Shows the piggy bank history.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param PiggyBankRepositoryInterface $repository
|
||||
* @param PiggyBank $piggyBank
|
||||
*
|
||||
|
||||
@@ -56,6 +56,8 @@ class ReportController extends Controller
|
||||
* This chart, by default, is shown on the multi-year and year report pages,
|
||||
* which means that giving it a 2 week "period" should be enough granularity.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
@@ -111,6 +113,8 @@ class ReportController extends Controller
|
||||
/**
|
||||
* Shows income and expense, debit/credit: operations.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
@@ -169,6 +173,8 @@ class ReportController extends Controller
|
||||
/**
|
||||
* Shows sum income and expense, debit/credit: operations.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
|
||||
@@ -56,6 +56,8 @@ class TagReportController extends Controller
|
||||
/**
|
||||
* Generate expenses for tags grouped on account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $tags
|
||||
* @param Carbon $start
|
||||
@@ -85,6 +87,8 @@ class TagReportController extends Controller
|
||||
/**
|
||||
* Generate income for tag grouped by account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $tags
|
||||
* @param Carbon $start
|
||||
@@ -114,6 +118,8 @@ class TagReportController extends Controller
|
||||
/**
|
||||
* Generate expense for tag grouped on budget.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $tags
|
||||
* @param Carbon $start
|
||||
@@ -142,6 +148,8 @@ class TagReportController extends Controller
|
||||
/**
|
||||
* Generate expense for tag grouped on category.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $tags
|
||||
* @param Carbon $start
|
||||
@@ -170,6 +178,8 @@ class TagReportController extends Controller
|
||||
/**
|
||||
* Generate main tag overview chart.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $tags
|
||||
* @param Carbon $start
|
||||
@@ -283,6 +293,8 @@ class TagReportController extends Controller
|
||||
/**
|
||||
* Show expense grouped by expense account.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $tags
|
||||
* @param Carbon $start
|
||||
@@ -312,6 +324,8 @@ class TagReportController extends Controller
|
||||
/**
|
||||
* Show income grouped by tag.
|
||||
*
|
||||
* TODO this chart is not multi-currency aware.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $tags
|
||||
* @param Carbon $start
|
||||
|
||||
Reference in New Issue
Block a user