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

@@ -44,7 +44,7 @@ class BillTransformer extends AbstractTransformer
{
private readonly BillDateCalculator $calculator;
private readonly bool $convertToNative;
private readonly TransactionCurrency $default;
private readonly TransactionCurrency $native;
private readonly BillRepositoryInterface $repository;
/**
@@ -54,7 +54,7 @@ class BillTransformer extends AbstractTransformer
{
$this->repository = app(BillRepositoryInterface::class);
$this->calculator = app(BillDateCalculator::class);
$this->default = Amount::getNativeCurrency();
$this->native = Amount::getNativeCurrency();
$this->convertToNative = Amount::convertToNative();
}
@@ -66,33 +66,19 @@ class BillTransformer extends AbstractTransformer
*/
public function transform(Bill $bill): array
{
$default = $this->parameters->get('defaultCurrency') ?? $this->default;
$paidData = $this->paidData($bill);
$lastPaidDate = $this->getLastPaidDate($paidData);
$start = $this->parameters->get('start') ?? today()->subYears(10);
$end = $this->parameters->get('end') ?? today()->addYears(10);
$payDates = $this->calculator->getPayDates($start, $end, $bill->date, $bill->repeat_freq, $bill->skip, $lastPaidDate);
$currency = $bill->transactionCurrency;
$notes = $this->repository->getNoteText($bill);
$notes = '' === $notes ? null : $notes;
$objectGroupId = null;
$objectGroupOrder = null;
$objectGroupTitle = null;
$paidData = $this->paidData($bill);
$lastPaidDate = $this->getLastPaidDate($paidData);
$start = $this->parameters->get('start') ?? today()->subYears(10);
$end = $this->parameters->get('end') ?? today()->addYears(10);
$payDates = $this->calculator->getPayDates($start, $end, $bill->date, $bill->repeat_freq, $bill->skip, $lastPaidDate);
$currency = $bill->transactionCurrency;
$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 = [];
$payDatesFormatted = [];
foreach ($paidData as $object) {
$date = Carbon::createFromFormat('!Y-m-d', $object['date'], config('app.timezone'));
$date = Carbon::createFromFormat('!Y-m-d', $object['date'], config('app.timezone'));
if (!$date instanceof Carbon) {
$date = today(config('app.timezone'));
}
@@ -101,24 +87,24 @@ class BillTransformer extends AbstractTransformer
}
foreach ($payDates as $string) {
$date = Carbon::createFromFormat('!Y-m-d', $string, config('app.timezone'));
$date = Carbon::createFromFormat('!Y-m-d', $string, config('app.timezone'));
if (!$date instanceof Carbon) {
$date = today(config('app.timezone'));
}
$payDatesFormatted[] = $date->toAtomString();
}
// next expected match
$nem = null;
$nemDate = null;
$nemDiff = trans('firefly.not_expected_period');
$firstPayDate = $payDates[0] ?? null;
$nem = null;
$nemDate = null;
$nemDiff = trans('firefly.not_expected_period');
$firstPayDate = $payDates[0] ?? null;
if (null !== $firstPayDate) {
$nemDate = Carbon::createFromFormat('!Y-m-d', $firstPayDate, config('app.timezone'));
if (!$nemDate instanceof Carbon) {
$nemDate = today(config('app.timezone'));
}
$nem = $nemDate->toAtomString();
$nem = $nemDate->toAtomString();
// nullify again when it's outside the current view range.
if (
@@ -139,7 +125,7 @@ class BillTransformer extends AbstractTransformer
$current = $payDatesFormatted[0] ?? null;
if (null !== $current && !$nemDate->isToday()) {
$temp2 = Carbon::createFromFormat('Y-m-d\TH:i:sP', $current);
$temp2 = Carbon::createFromFormat('Y-m-d\TH:i:sP', $current);
if (!$temp2 instanceof Carbon) {
$temp2 = today(config('app.timezone'));
}
@@ -149,43 +135,44 @@ class BillTransformer extends AbstractTransformer
}
return [
'id' => $bill->id,
'created_at' => $bill->created_at->toAtomString(),
'updated_at' => $bill->updated_at->toAtomString(),
'currency_id' => (string)$bill->transaction_currency_id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'native_currency_id' => null === $default ? null : (string)$default->id,
'native_currency_code' => $default?->code,
'native_currency_symbol' => $default?->symbol,
'native_currency_decimal_places' => $default?->decimal_places,
'name' => $bill->name,
'amount_min' => app('steam')->bcround($bill->amount_min, $currency->decimal_places),
'amount_max' => app('steam')->bcround($bill->amount_max, $currency->decimal_places),
'native_amount_min' => $this->convertToNative ? app('steam')->bcround($bill->native_amount_min, $default->decimal_places) : null,
'native_amount_max' => $this->convertToNative ? app('steam')->bcround($bill->native_amount_max, $default->decimal_places) : null,
'date' => $bill->date->toAtomString(),
'end_date' => $bill->end_date?->toAtomString(),
'extension_date' => $bill->extension_date?->toAtomString(),
'repeat_freq' => $bill->repeat_freq,
'skip' => $bill->skip,
'active' => $bill->active,
'order' => $bill->order,
'notes' => $notes,
'object_group_id' => null !== $objectGroupId ? (string)$objectGroupId : null,
'object_group_order' => $objectGroupOrder,
'object_group_title' => $objectGroupTitle,
'id' => $bill->id,
'created_at' => $bill->created_at->toAtomString(),
'updated_at' => $bill->updated_at->toAtomString(),
'currency_id' => (string)$bill->transaction_currency_id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'native_currency_id' => (string)$this->native->id,
'native_currency_code' => $this->native->code,
'native_currency_symbol' => $this->native->symbol,
'native_currency_decimal_places' => $this->native->decimal_places,
'name' => $bill->name,
'amount_min' => $bill->amounts['amount_min'],
'amount_max' => $bill->amounts['amount_max'],
'amount_avg' => $bill->amounts['average'],
'date' => $bill->date->toAtomString(),
'end_date' => $bill->end_date?->toAtomString(),
'extension_date' => $bill->extension_date?->toAtomString(),
'repeat_freq' => $bill->repeat_freq,
'skip' => $bill->skip,
'active' => $bill->active,
'order' => $bill->order,
'notes' => $bill->meta['notes'],
'object_group_id' => $bill->meta['object_group_id'],
'object_group_order' => $bill->meta['object_group_order'],
'object_group_title' => $bill->meta['object_group_title'],
// these fields need work:
'next_expected_match' => $nem,
'next_expected_match_diff' => $nemDiff,
'pay_dates' => $payDatesFormatted,
'paid_dates' => $paidDataFormatted,
'links' => [
// 'next_expected_match' => $nem,
// 'next_expected_match_diff' => $nemDiff,
// 'pay_dates' => $payDatesFormatted,
// 'paid_dates' => $paidDataFormatted,
'links' => [
[
'rel' => 'self',
'uri' => '/bills/'.$bill->id,
'uri' => '/bills/' . $bill->id,
],
],
];
@@ -207,13 +194,13 @@ class BillTransformer extends AbstractTransformer
// 2023-07-18 this particular date is used to search for the last paid date.
// 2023-07-18 the cloned $searchDate is used to grab the correct transactions.
/** @var Carbon $start */
$start = clone $this->parameters->get('start');
$searchStart = clone $start;
$start = clone $this->parameters->get('start');
$searchStart = clone $start;
$start->subDay();
/** @var Carbon $end */
$end = clone $this->parameters->get('end');
$searchEnd = clone $end;
$end = clone $this->parameters->get('end');
$searchEnd = clone $end;
// move the search dates to the start of the day.
$searchStart->startOfDay();
@@ -223,7 +210,7 @@ class BillTransformer extends AbstractTransformer
Log::debug(sprintf('Search parameters are: start: %s', $searchStart->format('Y-m-d')));
// Get from database when bill was paid.
$set = $this->repository->getPaidDatesInRange($bill, $searchStart, $searchEnd);
$set = $this->repository->getPaidDatesInRange($bill, $searchStart, $searchEnd);
Log::debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count()));
// Grab from array the most recent payment. If none exist, fall back to the start date and pretend *that* was the last paid date.
@@ -232,17 +219,17 @@ class BillTransformer extends AbstractTransformer
Log::debug(sprintf('Result of lastPaidDate is %s', $lastPaidDate->format('Y-m-d')));
// At this point the "next match" is exactly after the last time the bill was paid.
$result = [];
$result = [];
foreach ($set as $entry) {
$array = [
'transaction_group_id' => (string)$entry->transaction_group_id,
'transaction_journal_id' => (string)$entry->id,
'date' => $entry->date->format('Y-m-d'),
'date_object' => $entry->date,
'currency_id' => $entry->transaction_currency_id,
'currency_code' => $entry->transaction_currency_code,
'currency_decimal_places' => $entry->transaction_currency_decimal_places,
'amount' => Steam::bcround($entry->amount, $entry->transaction_currency_decimal_places),
$array = [
'transaction_group_id' => (string)$entry->transaction_group_id,
'transaction_journal_id' => (string)$entry->id,
'date' => $entry->date->format('Y-m-d'),
'date_object' => $entry->date,
'currency_id' => $entry->transaction_currency_id,
'currency_code' => $entry->transaction_currency_code,
'currency_decimal_places' => $entry->transaction_currency_decimal_places,
'amount' => Steam::bcround($entry->amount, $entry->transaction_currency_decimal_places),
];
if (null !== $entry->foreign_amount && null !== $entry->foreign_currency_code) {
$array['foreign_currency_id'] = $entry->foreign_currency_id;