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->setStart($start);
$enrichment->setEnd($end);
/** @var Bill $bill */
$bill = $enrichment->enrichSingle($bill);
/** @var BillTransformer $transformer */

View File

@@ -64,7 +64,7 @@ class AccountEnrichment implements EnrichmentInterface
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()
{
@@ -78,10 +78,6 @@ class AccountEnrichment implements EnrichmentInterface
$this->lastActivities = [];
$this->locations = [];
$this->primaryCurrency = Amount::getPrimaryCurrency();
// $this->repository = app(AccountRepositoryInterface::class);
// $this->currencyRepository = app(CurrencyRepositoryInterface::class);
// $this->start = null;
// $this->end = null;
}
#[Override]

View File

@@ -80,7 +80,20 @@ class SubscriptionEnrichment implements EnrichmentInterface
'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),
'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
if (array_key_exists($id, $this->mappedObjects)) {
@@ -95,16 +108,6 @@ class SubscriptionEnrichment implements EnrichmentInterface
$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->meta = $meta;

View File

@@ -103,18 +103,19 @@ class AccountTransformer extends AbstractTransformer
$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);
// set some pc_ default values to NULL:
$pcCurrentBalance = null;
$pcOpeningBalance = null;
$pcVirtualBalance = null;
$pcDebtAmount = null;
// collect current balances:
$currentBalance = Steam::bcround($finalBalance[$currency->code] ?? '0', $currency->decimal_places);
$openingBalance = Steam::bcround($openingBalance ?? '0', $currency->decimal_places);
$virtualBalance = Steam::bcround($account->virtual_balance ?? '0', $currency->decimal_places);
$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:
if ($this->convertToPrimary && $currency->id !== $this->primary->id) {
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);
$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
if (null === $openingBalanceDate) {

View File

@@ -55,6 +55,9 @@ class BillTransformer extends AbstractTransformer
'id' => $bill->id,
'created_at' => $bill->created_at->toAtomString(),
'updated_at' => $bill->updated_at->toAtomString(),
'name' => $bill->name,
// currencies according to 6.3.0
'currency_id' => (string) $bill->transaction_currency_id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
@@ -65,10 +68,16 @@ class BillTransformer extends AbstractTransformer
'primary_currency_symbol' => $this->primary->symbol,
'primary_currency_decimal_places' => $this->primary->decimal_places,
'name' => $bill->name,
// amounts according to 6.3.0
'amount_min' => $bill->amounts['amount_min'],
'pc_amount_min' => $bill->amounts['pc_amount_min'],
'amount_max' => $bill->amounts['amount_max'],
'pc_amount_max' => $bill->amounts['pc_amount_max'],
'amount_avg' => $bill->amounts['average'],
'pc_amount_avg' => $bill->amounts['pc_average'],
'date' => $bill->date->toAtomString(),
'end_date' => $bill->end_date?->toAtomString(),
'extension_date' => $bill->extension_date?->toAtomString(),