Add spent + earned info to category chart.

This commit is contained in:
James Cole
2025-08-22 09:15:13 +02:00
parent 63883c9a84
commit 7a1021dffc

View File

@@ -83,7 +83,7 @@ class CategoryController extends Controller
public function overview(SameDateRequest $request): JsonResponse public function overview(SameDateRequest $request): JsonResponse
{ {
/** @var Carbon $start */ /** @var Carbon $start */
$start = $this->parameters->get('start'); $start = $this->parameters->get('start');
/** @var Carbon $end */ /** @var Carbon $end */
$end = $this->parameters->get('end'); $end = $this->parameters->get('end');
@@ -94,25 +94,26 @@ class CategoryController extends Controller
// get journals for entire period: // get journals for entire period:
/** @var GroupCollectorInterface $collector */ /** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class); $collector = app(GroupCollectorInterface::class);
$collector->setRange($start, $end)->withAccountInformation(); $collector->setRange($start, $end)->withAccountInformation();
$collector->setXorAccounts($accounts)->withCategoryInformation(); $collector->setXorAccounts($accounts)->withCategoryInformation();
$collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value, TransactionTypeEnum::RECONCILIATION->value]); $collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value, TransactionTypeEnum::DEPOSIT->value]);
$journals = $collector->getExtractedJournals(); $journals = $collector->getExtractedJournals();
/** @var array $journal */ /** @var array $journal */
foreach ($journals as $journal) { foreach ($journals as $journal) {
// find journal: // find journal:
$journalCurrencyId = (int)$journal['currency_id']; $journalCurrencyId = (int)$journal['currency_id'];
$currency = $currencies[$journalCurrencyId] ?? $this->currencyRepos->find($journalCurrencyId); $type = $journal['transaction_type_type'];
$currencies[$journalCurrencyId] = $currency; $currency = $currencies[$journalCurrencyId] ?? $this->currencyRepos->find($journalCurrencyId);
$currencyId = (int)$currency->id; $currencies[$journalCurrencyId] = $currency;
$currencyName = (string)$currency->name; $currencyId = (int)$currency->id;
$currencyCode = (string)$currency->code; $currencyName = (string)$currency->name;
$currencySymbol = (string)$currency->symbol; $currencyCode = (string)$currency->code;
$currencyDecimalPlaces = (int)$currency->decimal_places; $currencySymbol = (string)$currency->symbol;
$amount = Steam::positive($journal['amount']); $currencyDecimalPlaces = (int)$currency->decimal_places;
$pcAmount = null; $amount = Steam::positive((string)$journal['amount']);
$pcAmount = null;
// overrule if necessary: // overrule if necessary:
if ($this->convertToPrimary && $journalCurrencyId === $this->primaryCurrency->id) { if ($this->convertToPrimary && $journalCurrencyId === $this->primaryCurrency->id) {
@@ -129,8 +130,8 @@ class CategoryController extends Controller
} }
$categoryName = $journal['category_name'] ?? (string)trans('firefly.no_category'); $categoryName = $journal['category_name'] ?? (string)trans('firefly.no_category');
$key = sprintf('%s-%s', $categoryName, $currencyCode); $key = sprintf('%s-%s', $categoryName, $currencyCode);
// create arrays // create arrays
$return[$key] ??= [ $return[$key] ??= [
'label' => $categoryName, 'label' => $categoryName,
@@ -150,23 +151,36 @@ class CategoryController extends Controller
'yAxisID' => 0, 'yAxisID' => 0,
'type' => 'bar', 'type' => 'bar',
'entries' => [ 'entries' => [
'spent' => '0', 'spent' => '0',
'earned' => '0',
], ],
'pc_entries' => [ 'pc_entries' => [
'spent' => '0', 'spent' => '0',
'earned' => '0',
], ],
]; ];
// add monies // add monies
$return[$key]['entries']['spent'] = bcadd($return[$key]['entries']['spent'], (string)$amount); // expenses to spent
if (null !== $pcAmount) { if (TransactionTypeEnum::WITHDRAWAL->value === $type) {
$return[$key]['pc_entries']['spent'] = bcadd($return[$key]['pc_entries']['spent'], (string)$pcAmount); $return[$key]['entries']['spent'] = bcadd($return[$key]['entries']['spent'], $amount);
if (null !== $pcAmount) {
$return[$key]['pc_entries']['spent'] = bcadd($return[$key]['pc_entries']['spent'], $pcAmount);
}
continue;
}
// positive amount = earned
if (TransactionTypeEnum::DEPOSIT->value === $type) {
$return[$key]['entries']['earned'] = bcadd($return[$key]['entries']['earned'], $amount);
if (null !== $pcAmount) {
$return[$key]['pc_entries']['earned'] = bcadd($return[$key]['pc_entries']['earned'], $pcAmount);
}
} }
} }
$return = array_values($return); $return = array_values($return);
// order by amount // order by amount
usort($return, static fn (array $a, array $b) => (float)$a['entries']['spent'] < (float)$b['entries']['spent'] ? 1 : -1); usort($return, static fn(array $a, array $b) => ((float)$a['entries']['spent'] + (float)$a['entries']['earned']) < ((float)$b['entries']['spent'] + (float)$b['entries']['earned']) ? 1 : -1);
return response()->json($this->clean($return)); return response()->json($this->clean($return));
} }