Skip to content

Firefly III: a personal finances manager

Expand bill transformer.

James Cole0c7f04fb1767
diff --git a/app/Http/Controllers/Bill/ShowController.php b/app/Http/Controllers/Bill/ShowController.php
index 554ecf3a73..52703f2b10 100644
--- a/app/Http/Controllers/Bill/ShowController.php
+++ b/app/Http/Controllers/Bill/ShowController.php
@@ -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 */
diff --git a/app/Support/JsonApi/Enrichments/AccountEnrichment.php b/app/Support/JsonApi/Enrichments/AccountEnrichment.php
index fb1c5566b7..f34e740077 100644
--- a/app/Support/JsonApi/Enrichments/AccountEnrichment.php
+++ b/app/Support/JsonApi/Enrichments/AccountEnrichment.php
@@ -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]
diff --git a/app/Support/JsonApi/Enrichments/SubscriptionEnrichment.php b/app/Support/JsonApi/Enrichments/SubscriptionEnrichment.php
index 3255ba869a..6077ac05c8 100644
--- a/app/Support/JsonApi/Enrichments/SubscriptionEnrichment.php
+++ b/app/Support/JsonApi/Enrichments/SubscriptionEnrichment.php
@@ -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;
diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php
index 65b3a564fe..e3131e661e 100644
--- a/app/Transformers/AccountTransformer.php
+++ b/app/Transformers/AccountTransformer.php
@@ -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) {
diff --git a/app/Transformers/BillTransformer.php b/app/Transformers/BillTransformer.php
index 88834c1d45..5cff9045d9 100644
--- a/app/Transformers/BillTransformer.php
+++ b/app/Transformers/BillTransformer.php
@@ -52,49 +52,58 @@ 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,
-
- 'primary_currency_id' => (string)$this->primary->id,
+ '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,
+ 'currency_decimal_places' => $currency->decimal_places,
+
+ 'primary_currency_id' => (string) $this->primary->id,
'primary_currency_code' => $this->primary->code,
'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'],
- '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'],
-
-
- 'paid_dates' => $bill->meta['paid_dates'],
- 'pay_dates' => $bill->meta['pay_dates'],
- 'next_expected_match' => $bill->meta['nem']?->toAtomString(),
- 'next_expected_match_diff' => $bill->meta['nem_diff'],
+ '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(),
+ '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'],
+
+
+ 'paid_dates' => $bill->meta['paid_dates'],
+ 'pay_dates' => $bill->meta['pay_dates'],
+ 'next_expected_match' => $bill->meta['nem']?->toAtomString(),
+ 'next_expected_match_diff' => $bill->meta['nem_diff'],
// these fields need work:
// 'next_expected_match' => $nem,
// 'next_expected_match_diff' => $nemDiff,
// 'pay_dates' => $payDatesFormatted,
- 'links' => [
+ 'links' => [
[
'rel' => 'self',
- 'uri' => '/bills/'.$bill->id,
+ 'uri' => '/bills/' . $bill->id,
],
],
];