Skip to content

Firefly III: a personal finances manager

Merge branch 'develop' into adminlte4

James Cole8ce760a0bf95
diff --cc app/Api/V2/Controllers/Chart/CategoryController.php
index 28899d80a4,bf167b3bad..443fad2eab
--- a/app/Api/V2/Controllers/Chart/CategoryController.php
+++ b/app/Api/V2/Controllers/Chart/CategoryController.php
@@@ -62,7 -67,72 +67,71 @@@ class CategoryController extends Contro
* @return JsonResponse
* @throws FireflyException
*/
- public function dashboard(DateRequest $request): JsonResponse {}
+ public function dashboard(DateRequest $request): JsonResponse
+ {
+ $params = $request->getAll();
+ /** @var Carbon $start */
+ $start = $params['start'];
+ /** @var Carbon $end */
+ $end = $params['end'];
+ $accounts = $this->accountRepos->getAccountsByType([AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::ASSET, AccountType::DEFAULT]);
+ $default = app('amount')->getDefaultCurrency();
+ $converter = new ExchangeRateConverter();
+ $currencies = [];
+ $return = [];
+ // get journals for entire period:
+ /** @var GroupCollectorInterface $collector */
+ $collector = app(GroupCollectorInterface::class);
+ $collector->setRange($start, $end)->withAccountInformation();
+ $collector->setXorAccounts($accounts)->withCategoryInformation();
+ $collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::RECONCILIATION]);
+ $journals = $collector->getExtractedJournals();
+
+ /** @var array $journal */
+ foreach ($journals as $journal) {
+ $currencyId = (int)$journal['currency_id'];
+ $currency = $currencies[$currencyId] ?? $this->currencyRepos->find($currencyId);
+ $currencies[$currencyId] = $currency;
+ $categoryName = null === $journal['category_name'] ? (string)trans('firefly.no_category') : $journal['category_name'];
+ $amount = app('steam')->positive($journal['amount']);
+ $nativeAmount = $converter->convert($default, $currency, $journal['date'], $amount);
+ $key = sprintf('%s-%s', $categoryName, $currency->code);
+ if ((int)$journal['foreign_currency_id'] === (int)$default->id) {
+ $nativeAmount = app('steam')->positive($journal['foreign_amount']);
+ }
+ // create arrays
+ $return[$key] = $return[$key] ?? [
+ 'label' => $categoryName,
+ 'currency_id' => (int)$currency->id,
+ 'currency_code' => $currency->code,
+ 'currency_name' => $currency->name,
+ 'currency_symbol' => $currency->symbol,
+ 'currency_decimal_places' => (int)$currency->decimal_places,
+ 'native_id' => (int)$default->id,
+ 'native_code' => $default->code,
+ 'native_name' => $default->name,
+ 'native_symbol' => $default->symbol,
+ 'native_decimal_places' => (int)$default->decimal_places,
+ 'period' => null,
+ 'start' => $start->toAtomString(),
+ 'end' => $end->toAtomString(),
+ 'amount' => '0',
+ 'native_amount' => '0',
+ ];
+
+
+ // add monies
+ $return[$key]['amount'] = bcadd($return[$key]['amount'], $amount);
+ $return[$key]['native_amount'] = bcadd($return[$key]['native_amount'], $nativeAmount);
+ }
+ $return = array_values($return);
+
+ // order by native amount
+ usort($return, function (array $a, array $b) {
+ return (float)$a['native_amount'] < (float)$b['native_amount'] ? 1 : -1;
+ });
+ return response()->json($this->clean($return));
+ }
-
}