Expand bill transformer.

This commit is contained in:
James Cole
2025-08-03 08:12:19 +02:00
parent 061c01da53
commit 0c7f04fb17
5 changed files with 65 additions and 49 deletions

View File

@@ -151,6 +151,7 @@ class ShowController extends Controller
$enrichment->setUser($admin); $enrichment->setUser($admin);
$enrichment->setStart($start); $enrichment->setStart($start);
$enrichment->setEnd($end); $enrichment->setEnd($end);
/** @var Bill $bill */
$bill = $enrichment->enrichSingle($bill); $bill = $enrichment->enrichSingle($bill);
/** @var BillTransformer $transformer */ /** @var BillTransformer $transformer */

View File

@@ -64,7 +64,7 @@ class AccountEnrichment implements EnrichmentInterface
private array $lastActivities; private array $lastActivities;
/** /**
* TODO Set primary currency using Amount::method, not through setter. * TODO The account enricher must do conversion from and to the primary currency.
*/ */
public function __construct() public function __construct()
{ {
@@ -78,10 +78,6 @@ class AccountEnrichment implements EnrichmentInterface
$this->lastActivities = []; $this->lastActivities = [];
$this->locations = []; $this->locations = [];
$this->primaryCurrency = Amount::getPrimaryCurrency(); $this->primaryCurrency = Amount::getPrimaryCurrency();
// $this->repository = app(AccountRepositoryInterface::class);
// $this->currencyRepository = app(CurrencyRepositoryInterface::class);
// $this->start = null;
// $this->end = null;
} }
#[Override] #[Override]

View File

@@ -80,7 +80,20 @@ class SubscriptionEnrichment implements EnrichmentInterface
'amount_min' => Steam::bcround($item->amount_min, $currency->decimal_places), 'amount_min' => Steam::bcround($item->amount_min, $currency->decimal_places),
'amount_max' => Steam::bcround($item->amount_max, $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), 'average' => Steam::bcround(bcdiv(bcadd($item->amount_min, $item->amount_max), '2'), $currency->decimal_places),
'pc_amount_min' => null,
'pc_amount_max' => null,
'pc_average' => null,
]; ];
if($this->convertToPrimary && $currency->id === $this->primaryCurrency->id) {
$amounts['pc_amount_min'] = $amounts['amount_min'];
$amounts['pc_amount_max'] = $amounts['amount_max'];
$amounts['pc_average'] = $amounts['average'];
}
if($this->convertToPrimary && $currency->id !== $this->primaryCurrency->id) {
$amounts['pc_amount_min'] = Steam::bcround($item->native_amount_min, $this->primaryCurrency->decimal_places);
$amounts['pc_amount_max'] = Steam::bcround($item->native_amount_max, $this->primaryCurrency->decimal_places);
$amounts['pc_average'] = Steam::bcround(bcdiv(bcadd($item->native_amount_min, $item->native_amount_max), '2'), $this->primaryCurrency->decimal_places);
}
// add object group if available // add object group if available
if (array_key_exists($id, $this->mappedObjects)) { if (array_key_exists($id, $this->mappedObjects)) {
@@ -95,16 +108,6 @@ class SubscriptionEnrichment implements EnrichmentInterface
$meta['notes'] = $notes[$item->id]; $meta['notes'] = $notes[$item->id];
} }
// Convert amounts to primary currency if needed
if ($this->convertToPrimary && $item->currency_id !== $this->primaryCurrency->id) {
Log::debug('Convert to primary currency');
$converter = new ExchangeRateConverter();
$amounts = [
'amount_min' => Steam::bcround($converter->convert($item->transactionCurrency, $this->primaryCurrency, today(), $item->amount_min), $this->primaryCurrency->decimal_places),
'amount_max' => Steam::bcround($converter->convert($item->transactionCurrency, $this->primaryCurrency, today(), $item->amount_max), $this->primaryCurrency->decimal_places),
];
$amounts['average'] = Steam::bcround(bcdiv(bcadd($amounts['amount_min'], $amounts['amount_max']), '2'), $this->primaryCurrency->decimal_places);
}
$item->amounts = $amounts; $item->amounts = $amounts;
$item->meta = $meta; $item->meta = $meta;

View File

@@ -103,18 +103,19 @@ class AccountTransformer extends AbstractTransformer
$finalBalance = Steam::finalAccountBalance($account, $date, $this->primary, $this->convertToPrimary); $finalBalance = Steam::finalAccountBalance($account, $date, $this->primary, $this->convertToPrimary);
Log::debug(sprintf('Call finalAccountBalance(%s) with date/time "%s"', var_export($this->convertToPrimary, true), $date->toIso8601String()), $finalBalance); Log::debug(sprintf('Call finalAccountBalance(%s) with date/time "%s"', var_export($this->convertToPrimary, true), $date->toIso8601String()), $finalBalance);
// set some pc_ default values to NULL:
$pcCurrentBalance = null;
$pcOpeningBalance = null;
$pcVirtualBalance = null;
$pcDebtAmount = null;
// collect current balances: // collect current balances:
$currentBalance = Steam::bcround($finalBalance[$currency->code] ?? '0', $currency->decimal_places); $currentBalance = Steam::bcround($finalBalance[$currency->code] ?? '0', $currency->decimal_places);
$openingBalance = Steam::bcround($openingBalance ?? '0', $currency->decimal_places); $openingBalance = Steam::bcround($openingBalance ?? '0', $currency->decimal_places);
$virtualBalance = Steam::bcround($account->virtual_balance ?? '0', $currency->decimal_places); $virtualBalance = Steam::bcround($account->virtual_balance ?? '0', $currency->decimal_places);
$debtAmount = $account->meta['current_debt'] ?? null; $debtAmount = $account->meta['current_debt'] ?? null;
// TODO this currency conversion must not be happening here.
// set some pc_ default values to NULL:
$pcCurrentBalance = null;
$pcOpeningBalance = null;
$pcVirtualBalance = null;
$pcDebtAmount = null;
// convert to primary currency if needed: // convert to primary currency if needed:
if ($this->convertToPrimary && $currency->id !== $this->primary->id) { if ($this->convertToPrimary && $currency->id !== $this->primary->id) {
Log::debug(sprintf('Convert to primary, from %s to %s', $currency->code, $this->primary->code)); Log::debug(sprintf('Convert to primary, from %s to %s', $currency->code, $this->primary->code));
@@ -124,6 +125,12 @@ class AccountTransformer extends AbstractTransformer
$pcVirtualBalance = $converter->convert($currency, $this->primary, $date, $virtualBalance); $pcVirtualBalance = $converter->convert($currency, $this->primary, $date, $virtualBalance);
$pcDebtAmount = null === $debtAmount ? null : $converter->convert($currency, $this->primary, $date, $debtAmount); $pcDebtAmount = null === $debtAmount ? null : $converter->convert($currency, $this->primary, $date, $debtAmount);
} }
if ($this->convertToPrimary && $currency->id === $this->primary->id) {
$pcCurrentBalance = $currentBalance;
$pcOpeningBalance = $openingBalance;
$pcVirtualBalance = $virtualBalance;
$pcDebtAmount = $debtAmount;
}
// set opening balance(s) to NULL if the date is null // set opening balance(s) to NULL if the date is null
if (null === $openingBalanceDate) { if (null === $openingBalanceDate) {

View File

@@ -52,49 +52,58 @@ class BillTransformer extends AbstractTransformer
return [ return [
'id' => $bill->id, 'id' => $bill->id,
'created_at' => $bill->created_at->toAtomString(), 'created_at' => $bill->created_at->toAtomString(),
'updated_at' => $bill->updated_at->toAtomString(), 'updated_at' => $bill->updated_at->toAtomString(),
'currency_id' => (string)$bill->transaction_currency_id, 'name' => $bill->name,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'primary_currency_id' => (string)$this->primary->id, // currencies according to 6.3.0
'currency_id' => (string) $bill->transaction_currency_id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'primary_currency_id' => (string) $this->primary->id,
'primary_currency_code' => $this->primary->code, 'primary_currency_code' => $this->primary->code,
'primary_currency_symbol' => $this->primary->symbol, 'primary_currency_symbol' => $this->primary->symbol,
'primary_currency_decimal_places' => $this->primary->decimal_places, 'primary_currency_decimal_places' => $this->primary->decimal_places,
'name' => $bill->name, // amounts according to 6.3.0
'amount_min' => $bill->amounts['amount_min'], 'amount_min' => $bill->amounts['amount_min'],
'amount_max' => $bill->amounts['amount_max'], 'pc_amount_min' => $bill->amounts['pc_amount_min'],
'amount_avg' => $bill->amounts['average'],
'date' => $bill->date->toAtomString(), 'amount_max' => $bill->amounts['amount_max'],
'end_date' => $bill->end_date?->toAtomString(), 'pc_amount_max' => $bill->amounts['pc_amount_max'],
'extension_date' => $bill->extension_date?->toAtomString(),
'repeat_freq' => $bill->repeat_freq, 'amount_avg' => $bill->amounts['average'],
'skip' => $bill->skip, 'pc_amount_avg' => $bill->amounts['pc_average'],
'active' => $bill->active,
'order' => $bill->order, 'date' => $bill->date->toAtomString(),
'notes' => $bill->meta['notes'], 'end_date' => $bill->end_date?->toAtomString(),
'object_group_id' => $bill->meta['object_group_id'], 'extension_date' => $bill->extension_date?->toAtomString(),
'object_group_order' => $bill->meta['object_group_order'], 'repeat_freq' => $bill->repeat_freq,
'object_group_title' => $bill->meta['object_group_title'], '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'],
'paid_dates' => $bill->meta['paid_dates'], 'paid_dates' => $bill->meta['paid_dates'],
'pay_dates' => $bill->meta['pay_dates'], 'pay_dates' => $bill->meta['pay_dates'],
'next_expected_match' => $bill->meta['nem']?->toAtomString(), 'next_expected_match' => $bill->meta['nem']?->toAtomString(),
'next_expected_match_diff' => $bill->meta['nem_diff'], 'next_expected_match_diff' => $bill->meta['nem_diff'],
// 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,
'links' => [ 'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/bills/'.$bill->id, 'uri' => '/bills/' . $bill->id,
], ],
], ],
]; ];