Start improving bill overview.

This commit is contained in:
James Cole
2025-07-30 20:35:28 +02:00
parent 671ff95f22
commit a7973190c2
6 changed files with 254 additions and 91 deletions

View File

@@ -28,7 +28,9 @@ use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Support\JsonApi\Enrichments\SubscriptionEnrichment;
use FireflyIII\Transformers\BillTransformer; use FireflyIII\Transformers\BillTransformer;
use FireflyIII\User;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
use League\Fractal\Pagination\IlluminatePaginatorAdapter; use League\Fractal\Pagination\IlluminatePaginatorAdapter;
@@ -76,6 +78,15 @@ class ShowController extends Controller
$bills = $bills->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); $bills = $bills->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
$paginator = new LengthAwarePaginator($bills, $count, $pageSize, $this->parameters->get('page')); $paginator = new LengthAwarePaginator($bills, $count, $pageSize, $this->parameters->get('page'));
// enrich
/** @var User $admin */
$admin = auth()->user();
$enrichment = new SubscriptionEnrichment();
$enrichment->setUser($admin);
$enrichment->setConvertToNative($this->convertToNative);
$enrichment->setNative($this->nativeCurrency);
$bills = $enrichment->enrich($bills);
/** @var BillTransformer $transformer */ /** @var BillTransformer $transformer */
$transformer = app(BillTransformer::class); $transformer = app(BillTransformer::class);
$transformer->setParameters($this->parameters); $transformer->setParameters($this->parameters);

View File

@@ -0,0 +1,157 @@
<?php
namespace FireflyIII\Support\JsonApi\Enrichments;
use FireflyIII\Models\Account;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Note;
use FireflyIII\Models\ObjectGroup;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\UserGroup;
use FireflyIII\Support\Facades\Steam;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class SubscriptionEnrichment implements EnrichmentInterface
{
private User $user;
private UserGroup $userGroup;
private Collection $collection;
private bool $convertToNative = false;
private array $subscriptionIds = [];
private array $objectGroups = [];
private array $mappedObjects = [];
private array $notes = [];
private TransactionCurrency $nativeCurrency;
public function enrich(Collection $collection): Collection
{
$this->collection = $collection;
$this->collectSubscriptionIds();
$this->collectNotes();
$this->collectObjectGroups();
$notes = $this->notes;
$objectGroups = $this->objectGroups;
$this->collection = $this->collection->map(function (Bill $item) use ($notes, $objectGroups) {
$id = (int) $item->id;
$currency = $item->transactionCurrency;
$meta = [
'notes' => null,
'object_group_id' => null,
'object_group_title' => null,
'object_group_order' => null,
];
$amounts = [
'amount_min' => Steam::bcround($item->amount_min, $currency->decimal_places),
'amount_max' => Steam::bcround($item->amount_max, $currency->decimal_places),
'average' => Steam::bcround(bcdiv(bcadd($item->amount_min, $item->amount_max), '2'), $currency->decimal_places),
];
// add object group if available
if (array_key_exists($id, $this->mappedObjects)) {
$key = $this->mappedObjects[$id];
$meta['object_group_id'] = $objectGroups[$key]['id'];
$meta['object_group_title'] = $objectGroups[$key]['title'];
$meta['object_group_order'] = $objectGroups[$key]['order'];
}
// Add notes if available.
if (array_key_exists($item->id, $notes)) {
$meta['notes'] = $notes[$item->id];
}
// Convert amounts to native currency if needed
if ($this->convertToNative && $item->currency_id !== $this->nativeCurrency->id) {
$converter = new ExchangeRateConverter();
$amounts = [
'amount_min' => Steam::bcround($converter->convert($item->transactionCurrency, $this->nativeCurrency, today(), $item->amount_min), $this->nativeCurrency->decimal_places),
'amount_max' => Steam::bcround($converter->convert($item->transactionCurrency, $this->nativeCurrency, today(), $item->amount_max), $this->nativeCurrency->decimal_places),
];
$amounts['average'] =Steam::bcround(bcdiv(bcadd($amounts['amount_min'], $amounts['amount_max']), '2'), $this->nativeCurrency->decimal_places);
}
$item->amounts = $amounts;
$item->meta = $meta;
return $item;
});
return $collection;
}
public function enrichSingle(Model|array $model): array|Model
{
Log::debug(__METHOD__);
$collection = new Collection([$model]);
$collection = $this->enrich($collection);
return $collection->first();
}
private function collectNotes(): void
{
$notes = Note::query()->whereIn('noteable_id', $this->subscriptionIds)
->whereNotNull('notes.text')
->where('notes.text', '!=', '')
->where('noteable_type', Bill::class)->get(['notes.noteable_id', 'notes.text'])->toArray()
;
foreach ($notes as $note) {
$this->notes[(int) $note['noteable_id']] = (string) $note['text'];
}
Log::debug(sprintf('Enrich with %d note(s)', count($this->notes)));
}
public function setUser(User $user): void
{
$this->user = $user;
$this->userGroup = $user->userGroup;
}
public function setUserGroup(UserGroup $userGroup): void
{
$this->userGroup = $userGroup;
}
public function setConvertToNative(bool $convertToNative): void
{
$this->convertToNative = $convertToNative;
}
public function setNative(TransactionCurrency $nativeCurrency): void
{
$this->nativeCurrency = $nativeCurrency;
}
private function collectSubscriptionIds(): void
{
/** @var Bill $bill */
foreach ($this->collection as $bill) {
$this->subscriptionIds[] = (int) $bill->id;
}
$this->subscriptionIds = array_unique($this->subscriptionIds);
}
private function collectObjectGroups(): void
{
$set = DB::table('object_groupables')
->whereIn('object_groupable_id', $this->subscriptionIds)
->where('object_groupable_type', Bill::class)
->get(['object_groupable_id','object_group_id']);
$ids = array_unique($set->pluck('object_group_id')->toArray());
foreach($set as $entry) {
$this->mappedObjects[(int)$entry->object_groupable_id] = (int)$entry->object_group_id;
}
$groups = ObjectGroup::whereIn('id', $ids)->get(['id', 'title','order'])->toArray();
foreach($groups as $group) {
$group['id'] = (int) $group['id'];
$group['order'] = (int) $group['order'];
$this->objectGroups[(int)$group['id']] = $group;
}
}
}

View File

@@ -44,7 +44,7 @@ class BillTransformer extends AbstractTransformer
{ {
private readonly BillDateCalculator $calculator; private readonly BillDateCalculator $calculator;
private readonly bool $convertToNative; private readonly bool $convertToNative;
private readonly TransactionCurrency $default; private readonly TransactionCurrency $native;
private readonly BillRepositoryInterface $repository; private readonly BillRepositoryInterface $repository;
/** /**
@@ -54,7 +54,7 @@ class BillTransformer extends AbstractTransformer
{ {
$this->repository = app(BillRepositoryInterface::class); $this->repository = app(BillRepositoryInterface::class);
$this->calculator = app(BillDateCalculator::class); $this->calculator = app(BillDateCalculator::class);
$this->default = Amount::getNativeCurrency(); $this->native = Amount::getNativeCurrency();
$this->convertToNative = Amount::convertToNative(); $this->convertToNative = Amount::convertToNative();
} }
@@ -66,28 +66,14 @@ class BillTransformer extends AbstractTransformer
*/ */
public function transform(Bill $bill): array public function transform(Bill $bill): array
{ {
$default = $this->parameters->get('defaultCurrency') ?? $this->default;
$paidData = $this->paidData($bill); $paidData = $this->paidData($bill);
$lastPaidDate = $this->getLastPaidDate($paidData); $lastPaidDate = $this->getLastPaidDate($paidData);
$start = $this->parameters->get('start') ?? today()->subYears(10); $start = $this->parameters->get('start') ?? today()->subYears(10);
$end = $this->parameters->get('end') ?? today()->addYears(10); $end = $this->parameters->get('end') ?? today()->addYears(10);
$payDates = $this->calculator->getPayDates($start, $end, $bill->date, $bill->repeat_freq, $bill->skip, $lastPaidDate); $payDates = $this->calculator->getPayDates($start, $end, $bill->date, $bill->repeat_freq, $bill->skip, $lastPaidDate);
$currency = $bill->transactionCurrency; $currency = $bill->transactionCurrency;
$notes = $this->repository->getNoteText($bill);
$notes = '' === $notes ? null : $notes;
$objectGroupId = null;
$objectGroupOrder = null;
$objectGroupTitle = null;
$this->repository->setUser($bill->user); $this->repository->setUser($bill->user);
/** @var null|ObjectGroup $objectGroup */
$objectGroup = $bill->objectGroups->first();
if (null !== $objectGroup) {
$objectGroupId = $objectGroup->id;
$objectGroupOrder = $objectGroup->order;
$objectGroupTitle = $objectGroup->title;
}
$paidDataFormatted = []; $paidDataFormatted = [];
$payDatesFormatted = []; $payDatesFormatted = [];
@@ -156,15 +142,16 @@ class BillTransformer extends AbstractTransformer
'currency_code' => $currency->code, 'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol, 'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places, 'currency_decimal_places' => $currency->decimal_places,
'native_currency_id' => null === $default ? null : (string)$default->id,
'native_currency_code' => $default?->code, 'native_currency_id' => (string)$this->native->id,
'native_currency_symbol' => $default?->symbol, 'native_currency_code' => $this->native->code,
'native_currency_decimal_places' => $default?->decimal_places, 'native_currency_symbol' => $this->native->symbol,
'native_currency_decimal_places' => $this->native->decimal_places,
'name' => $bill->name, 'name' => $bill->name,
'amount_min' => app('steam')->bcround($bill->amount_min, $currency->decimal_places), 'amount_min' => $bill->amounts['amount_min'],
'amount_max' => app('steam')->bcround($bill->amount_max, $currency->decimal_places), 'amount_max' => $bill->amounts['amount_max'],
'native_amount_min' => $this->convertToNative ? app('steam')->bcround($bill->native_amount_min, $default->decimal_places) : null, 'amount_avg' => $bill->amounts['average'],
'native_amount_max' => $this->convertToNative ? app('steam')->bcround($bill->native_amount_max, $default->decimal_places) : null,
'date' => $bill->date->toAtomString(), 'date' => $bill->date->toAtomString(),
'end_date' => $bill->end_date?->toAtomString(), 'end_date' => $bill->end_date?->toAtomString(),
'extension_date' => $bill->extension_date?->toAtomString(), 'extension_date' => $bill->extension_date?->toAtomString(),
@@ -172,16 +159,16 @@ class BillTransformer extends AbstractTransformer
'skip' => $bill->skip, 'skip' => $bill->skip,
'active' => $bill->active, 'active' => $bill->active,
'order' => $bill->order, 'order' => $bill->order,
'notes' => $notes, 'notes' => $bill->meta['notes'],
'object_group_id' => null !== $objectGroupId ? (string)$objectGroupId : null, 'object_group_id' => $bill->meta['object_group_id'],
'object_group_order' => $objectGroupOrder, 'object_group_order' => $bill->meta['object_group_order'],
'object_group_title' => $objectGroupTitle, 'object_group_title' => $bill->meta['object_group_title'],
// these fields need work: // these fields need work:
'next_expected_match' => $nem, // 'next_expected_match' => $nem,
'next_expected_match_diff' => $nemDiff, // 'next_expected_match_diff' => $nemDiff,
'pay_dates' => $payDatesFormatted, // 'pay_dates' => $payDatesFormatted,
'paid_dates' => $paidDataFormatted, // 'paid_dates' => $paidDataFormatted,
'links' => [ 'links' => [
[ [
'rel' => 'self', 'rel' => 'self',

View File

@@ -163,14 +163,10 @@ function downloadSubscriptions(params) {
currency_code: bill.currency_code, currency_code: bill.currency_code,
paid: 0, paid: 0,
unpaid: 0, unpaid: 0,
// native_currency_code: bill.native_currency_code,
// native_paid: 0,
//native_unpaid: 0,
}; };
} }
subscriptionData[objectGroupId].payment_info[bill.currency_code].unpaid += totalAmount; subscriptionData[objectGroupId].payment_info[bill.currency_code].unpaid += totalAmount;
//subscriptionData[objectGroupId].payment_info[bill.currency_code].native_unpaid += totalNativeAmount;
} }
if (current.attributes.paid_dates.length > 0) { if (current.attributes.paid_dates.length > 0) {
@@ -178,8 +174,6 @@ function downloadSubscriptions(params) {
if (current.attributes.paid_dates.hasOwnProperty(ii)) { if (current.attributes.paid_dates.hasOwnProperty(ii)) {
// bill is paid! // bill is paid!
// since bill is paid, 3 possible currencies: // since bill is paid, 3 possible currencies:
// native, currency, foreign currency.
// foreign currency amount (converted to native or not) will be ignored.
let currentJournal = current.attributes.paid_dates[ii]; let currentJournal = current.attributes.paid_dates[ii];
// new array for the currency // new array for the currency
if (!subscriptionData[objectGroupId].payment_info.hasOwnProperty(currentJournal.currency_code)) { if (!subscriptionData[objectGroupId].payment_info.hasOwnProperty(currentJournal.currency_code)) {
@@ -187,15 +181,10 @@ function downloadSubscriptions(params) {
currency_code: bill.currency_code, currency_code: bill.currency_code,
paid: 0, paid: 0,
unpaid: 0, unpaid: 0,
// native_currency_code: bill.native_currency_code,
// native_paid: 0,
//native_unpaid: 0,
}; };
} }
const amount = parseFloat(currentJournal.amount) * -1; const amount = parseFloat(currentJournal.amount) * -1;
// const nativeAmount = parseFloat(currentJournal.native_amount) * -1;
subscriptionData[objectGroupId].payment_info[currentJournal.currency_code].paid += amount; subscriptionData[objectGroupId].payment_info[currentJournal.currency_code].paid += amount;
// subscriptionData[objectGroupId].payment_info[currentJournal.currency_code].native_paid += nativeAmount;
} }
} }
} }
@@ -221,6 +210,14 @@ export default () => ({
formatMoney(amount, currencyCode) { formatMoney(amount, currencyCode) {
return formatMoney(amount, currencyCode); return formatMoney(amount, currencyCode);
}, },
eventListeners: {
['@convert-to-native.window'](event){
console.log('I heard that! (dashboard/subscriptions)');
this.convertToNative = event.detail;
this.startSubscriptions();
}
},
startSubscriptions() { startSubscriptions() {
this.loading = true; this.loading = true;
let start = new Date(window.store.get('start')); let start = new Date(window.store.get('start'));

View File

@@ -1865,6 +1865,8 @@ return [
// bills: // bills:
'left_to_pay_lc' => 'left to pay', 'left_to_pay_lc' => 'left to pay',
'less_than_expected' => 'less than expected',
'more_than_expected' => 'more than expected',
'skip_help_text' => 'Use the skip field to create bi-monthly (skip = 1) or other custom intervals.', 'skip_help_text' => 'Use the skip field to create bi-monthly (skip = 1) or other custom intervals.',
'subscription' => 'Subscription', 'subscription' => 'Subscription',
'not_expected_period' => 'Not expected this period', 'not_expected_period' => 'Not expected this period',

View File

@@ -1,4 +1,4 @@
<div class="col" x-data="subscriptions"> <div class="col" x-data="subscriptions" x-bind="eventListeners">
<template x-for="group in subscriptions"> <template x-for="group in subscriptions">
<div class="card mb-2"> <div class="card mb-2">
<div class="card-header"> <div class="card-header">
@@ -62,7 +62,16 @@
<template x-for="transaction in bill.transactions"> <template x-for="transaction in bill.transactions">
<li> <li>
<span :title="transaction.amount" x-text="transaction.amount"></span> <span :title="transaction.amount" x-text="transaction.amount"></span>
(<span title="Less or more than expected." x-text="transaction.percentage"></span>%) <template x-if="transaction.percentage < 0">
<span>
(<span :title="transaction.percentage + '% {{ __("firefly.less_than_expected") }}'" x-text="transaction.percentage"></span>%)
</span>
</template>
<template x-if="transaction.percentage > 0">
<span>
(<span :title="transaction.percentage + '% {{ __("firefly.more_than_expected") }}'" x-text="transaction.percentage"></span>%)
</span>
</template>
</li> </li>
</template> </template>
</ul> </ul>