mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-16 22:58:09 +00:00
Make some charts currency aware for #740
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user