Add conversion

This commit is contained in:
James Cole
2025-07-31 18:16:54 +02:00
parent 4dd77303f7
commit 33bde854ec
2 changed files with 21 additions and 0 deletions

View File

@@ -90,6 +90,7 @@ class SubscriptionEnrichment implements EnrichmentInterface
// Convert amounts to native currency if needed
if ($this->convertToNative && $item->currency_id !== $this->nativeCurrency->id) {
Log::debug('Convert to native currency');
$converter = new ExchangeRateConverter();
$amounts = [
'amount_min' => Steam::bcround($converter->convert($item->transactionCurrency, $this->nativeCurrency, today(), $item->amount_min), $this->nativeCurrency->decimal_places),
@@ -235,6 +236,7 @@ class SubscriptionEnrichment implements EnrichmentInterface
Log::debug(sprintf('Count %d entries in set', $set->count()));
// for each bill, do a loop.
$converter = new ExchangeRateConverter();
/** @var Bill $subscription */
foreach ($this->collection as $subscription) {
// Grab from array the most recent payment. If none exist, fall back to the start date and pretend *that* was the last paid date.
@@ -265,6 +267,13 @@ class SubscriptionEnrichment implements EnrichmentInterface
$array['foreign_currency_decimal_places'] = $entry->foreign_currency_decimal_places;
$array['foreign_amount'] = Steam::bcround($entry->foreign_amount, $entry->foreign_currency_decimal_places);
}
if($this->convertToNative) {
$array['amount'] = $converter->convert($entry->transactionCurrency, $this->nativeCurrency, $entry->date, $entry->amount);
$array['currency_id'] = $this->nativeCurrency->id;
$array['currency_code'] = $this->nativeCurrency->code;
$array['currency_decimal_places'] = $this->nativeCurrency->decimal_places;
}
$result[] = $array;
}
@@ -334,6 +343,10 @@ class SubscriptionEnrichment implements EnrichmentInterface
private function collectPayDates(): void
{
if(null === $this->start || null === $this->end) {
Log::debug('Parameters are NULL, set empty array');
return;
}
/** @var Bill $subscription */
foreach ($this->collection as $subscription) {
$id = (int)$subscription->id;

View File

@@ -30,6 +30,7 @@ import i18next from "i18next";
let afterPromises = false;
let apiData = [];
let subscriptionData = {};
let convertToNative = false;
function addObjectGroupInfo(data) {
let objectGroupId = parseInt(data.object_group_id);
@@ -70,6 +71,11 @@ function parseBillInfo(data) {
pay_dates: parsePayDates(data.attributes.pay_dates),
paid: data.attributes.paid_dates.length > 0,
};
if(convertToNative) {
result.currency_code = data.attributes.native_currency_code;
}
// set variables
result.expected_amount = formatMoney(result.amount, result.currency_code);
result.expected_times = i18next.t('firefly.subscr_expected_x_times', {
@@ -214,6 +220,7 @@ export default () => ({
['@convert-to-native.window'](event){
console.log('I heard that! (dashboard/subscriptions)');
this.convertToNative = event.detail;
convertToNative = event.detail;
this.startSubscriptions();
}
},
@@ -308,6 +315,7 @@ export default () => ({
init() {
Promise.all([getVariable('convert_to_native', false)]).then((values) => {
this.convertToNative = values[0];
convertToNative = values[0];
afterPromises = true;
if (false === this.loading) {