Skip to content

A local-first personal finance app

feat(currency): add currency support to templates and goals (#5955)

Michael Süssemilch47c09ffcfa60
diff --git a/packages/desktop-client/src/components/budget/goals/editor/SimpleAutomation.tsx b/packages/desktop-client/src/components/budget/goals/editor/SimpleAutomation.tsx
index 6ac5002940..ca3a4ce0f2 100644
--- a/packages/desktop-client/src/components/budget/goals/editor/SimpleAutomation.tsx
+++ b/packages/desktop-client/src/components/budget/goals/editor/SimpleAutomation.tsx
@@ -1,6 +1,5 @@
import { useTranslation } from 'react-i18next';
-import { amountToInteger, integerToAmount } from 'loot-core/shared/util';
import type { SimpleTemplate } from 'loot-core/types/models/templates';
import {
@@ -9,7 +8,6 @@ import {
} from '@desktop-client/components/budget/goals/actions';
import { FormField, FormLabel } from '@desktop-client/components/forms';
import { AmountInput } from '@desktop-client/components/util/AmountInput';
-import { useFormat } from '@desktop-client/hooks/useFormat';
type SimpleAutomationProps = {
template: SimpleTemplate;
@@ -21,7 +19,6 @@ export const SimpleAutomation = ({
dispatch,
}: SimpleAutomationProps) => {
const { t } = useTranslation();
- const { currency } = useFormat();
return (
<FormField>
@@ -29,13 +26,13 @@ export const SimpleAutomation = ({
<AmountInput
id="amount-field"
key="amount-input"
- value={amountToInteger(template.monthly ?? 0, currency.decimalPlaces)}
+ value={template.monthly ?? 0}
zeroSign="+"
onUpdate={(value: number) =>
dispatch(
updateTemplate({
type: 'simple',
- monthly: integerToAmount(value, currency.decimalPlaces),
+ monthly: value,
}),
)
}
diff --git a/packages/desktop-client/src/components/budget/goals/editor/SimpleAutomationReadOnly.tsx b/packages/desktop-client/src/components/budget/goals/editor/SimpleAutomationReadOnly.tsx
index 39bfb4854f..98f5754f4a 100644
--- a/packages/desktop-client/src/components/budget/goals/editor/SimpleAutomationReadOnly.tsx
+++ b/packages/desktop-client/src/components/budget/goals/editor/SimpleAutomationReadOnly.tsx
@@ -1,6 +1,5 @@
import { Trans } from 'react-i18next';
-import { amountToInteger } from 'loot-core/shared/util';
import type { SimpleTemplate } from 'loot-core/types/models/templates';
import { useFormat } from '@desktop-client/hooks/useFormat';
@@ -17,10 +16,7 @@ export const SimpleAutomationReadOnly = ({
<Trans>
Budget{' '}
{{
- monthly: format(
- amountToInteger(template.monthly ?? 0, format.currency.decimalPlaces),
- 'financial',
- ),
+ monthly: format(template.monthly ?? 0, 'financial'),
}}{' '}
each month
</Trans>
diff --git a/packages/desktop-client/src/components/budget/goals/editor/WeekAutomation.tsx b/packages/desktop-client/src/components/budget/goals/editor/WeekAutomation.tsx
index 3ad8ab0d9b..4cb96474e4 100644
--- a/packages/desktop-client/src/components/budget/goals/editor/WeekAutomation.tsx
+++ b/packages/desktop-client/src/components/budget/goals/editor/WeekAutomation.tsx
@@ -1,6 +1,5 @@
import { useTranslation } from 'react-i18next';
-import { amountToInteger, integerToAmount } from 'loot-core/shared/util';
import type { PeriodicTemplate } from 'loot-core/types/models/templates';
import {
@@ -9,7 +8,6 @@ import {
} from '@desktop-client/components/budget/goals/actions';
import { FormField, FormLabel } from '@desktop-client/components/forms';
import { AmountInput } from '@desktop-client/components/util/AmountInput';
-import { useFormat } from '@desktop-client/hooks/useFormat';
type WeekAutomationProps = {
template: PeriodicTemplate;
@@ -18,7 +16,6 @@ type WeekAutomationProps = {
export const WeekAutomation = ({ template, dispatch }: WeekAutomationProps) => {
const { t } = useTranslation();
- const { currency } = useFormat();
return (
<FormField style={{ flex: 1 }}>
@@ -26,13 +23,13 @@ export const WeekAutomation = ({ template, dispatch }: WeekAutomationProps) => {
<AmountInput
id="amount-field"
key="amount-input"
- value={amountToInteger(template.amount ?? 0, currency.decimalPlaces)}
+ value={template.amount ?? 0}
zeroSign="+"
onUpdate={(value: number) =>
dispatch(
updateTemplate({
type: 'periodic',
- amount: integerToAmount(value, currency.decimalPlaces),
+ amount: value,
}),
)
}
diff --git a/packages/desktop-client/src/components/budget/goals/editor/WeekAutomationReadOnly.tsx b/packages/desktop-client/src/components/budget/goals/editor/WeekAutomationReadOnly.tsx
index 47323aa4c7..d2d5f4609f 100644
--- a/packages/desktop-client/src/components/budget/goals/editor/WeekAutomationReadOnly.tsx
+++ b/packages/desktop-client/src/components/budget/goals/editor/WeekAutomationReadOnly.tsx
@@ -1,6 +1,5 @@
import { Trans } from 'react-i18next';
-import { amountToInteger } from 'loot-core/shared/util';
import type { PeriodicTemplate } from 'loot-core/types/models/templates';
import { useFormat } from '@desktop-client/hooks/useFormat';
@@ -18,10 +17,7 @@ export const WeekAutomationReadOnly = ({
<Trans>
Budget{' '}
{{
- amount: format(
- amountToInteger(template.amount, format.currency.decimalPlaces),
- 'financial',
- ),
+ amount: format(template.amount, 'financial'),
}}{' '}
each week
</Trans>
diff --git a/packages/desktop-client/src/components/settings/Currency.tsx b/packages/desktop-client/src/components/settings/Currency.tsx
index fb556ecfdc..5144e6a6d3 100644
--- a/packages/desktop-client/src/components/settings/Currency.tsx
+++ b/packages/desktop-client/src/components/settings/Currency.tsx
@@ -37,7 +37,7 @@ export function CurrencySettings() {
['HKD', t('Hong Kong Dollar')],
['INR', t('Indian Rupee')],
['JMD', t('Jamaican Dollar')],
- // ['JPY', t('Japanese Yen')],
+ ['JPY', t('Japanese Yen')],
['LKR', t('Sri Lankan Rupee')],
['MDL', t('Moldovan Leu')],
['PHP', t('Philippine Peso')],
diff --git a/packages/loot-core/src/server/budget/category-template-context.test.ts b/packages/loot-core/src/server/budget/category-template-context.test.ts
index 41f24f3e0e..430bad6077 100644
--- a/packages/loot-core/src/server/budget/category-template-context.test.ts
+++ b/packages/loot-core/src/server/budget/category-template-context.test.ts
@@ -23,6 +23,29 @@ vi.mock('../aql', () => ({
aqlQuery: vi.fn(),
}));
+// Helper function to mock preferences (hideFraction and defaultCurrencyCode)
+function mockPreferences(
+ hideFraction: boolean = false,
+ currencyCode: string = 'USD',
+) {
+ vi.mocked(aql.aqlQuery).mockImplementation(async (query: unknown) => {
+ const queryStr = JSON.stringify(query);
+ if (queryStr.includes('hideFraction')) {
+ return {
+ data: [{ value: hideFraction ? 'true' : 'false' }],
+ dependencies: [],
+ };
+ }
+ if (queryStr.includes('defaultCurrencyCode')) {
+ return {
+ data: currencyCode ? [{ value: currencyCode }] : [],
+ dependencies: [],
+ };
+ }
+ return { data: [], dependencies: [] };
+ });
+}
+
// Test helper class to access constructor and methods
class TestCategoryTemplateContext extends CategoryTemplateContext {
public constructor(
@@ -31,27 +54,47 @@ class TestCategoryTemplateContext extends CategoryTemplateContext {
month: string,
fromLastMonth: number,
budgeted: number,
+ currencyCode: string = 'USD',
) {
- super(templates, category, month, fromLastMonth, budgeted);
+ super(templates, category, month, fromLastMonth, budgeted, currencyCode);
}
}
describe('CategoryTemplateContext', () => {
describe('runSimple', () => {
it('should return monthly amount when provided', () => {
+ const category: CategoryEntity = {
+ id: 'test',
+ name: 'Test Category',
+ group: 'test-group',
+ is_income: false,
+ };
const template: Template = {
type: 'simple',
monthly: 100,
directive: 'template',
priority: 1,
};
- const limit = 0;
- const result = CategoryTemplateContext.runSimple(template, limit);
+ const instance = new TestCategoryTemplateContext(
+ [],
+ category,
+ '2024-01',
+ 0,
+ 0,
+ );
+
+ const result = CategoryTemplateContext.runSimple(template, instance);
expect(result).toBe(amountToInteger(100));
});
it('should return limit when monthly is not provided', () => {
+ const category: CategoryEntity = {
+ id: 'test',
+ name: 'Test Category',
+ group: 'test-group',
+ is_income: false,
+ };
const template: Template = {
type: 'simple',
limit: { amount: 500, hold: false, period: 'monthly' },
@@ -59,8 +102,16 @@ describe('CategoryTemplateContext', () => {
priority: 1,
};
- const result = CategoryTemplateContext.runSimple(template, 500);
- expect(result).toBe(500);
+ const instance = new TestCategoryTemplateContext(
+ [template],
+ category,
+ '2024-01',
+ 0,
+ 0,
+ );
+
+ const result = CategoryTemplateContext.runSimple(template, instance);
+ expect(result).toBe(amountToInteger(500));
});
it('should handle weekly limit', async () => {
@@ -870,10 +921,7 @@ describe('CategoryTemplateContext', () => {
// Mock the sheet values needed for init
vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0); // lastMonthBalance
vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false); // carryover
- vi.mocked(aql.aqlQuery).mockResolvedValueOnce({
- data: [{ value: 'false' }],
- dependencies: [],
- });
+ mockPreferences(false, 'USD');
// Initialize the template
const instance = await CategoryTemplateContext.init(
@@ -937,10 +985,7 @@ describe('CategoryTemplateContext', () => {
// Mock the sheet values needed for init
vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0); // lastMonthBalance
vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false); // carryover
- vi.mocked(aql.aqlQuery).mockResolvedValueOnce({
- data: [{ value: 'false' }],
- dependencies: [],
- });
+ mockPreferences(false, 'USD');
// Initialize the template
const instance = await CategoryTemplateContext.init(
@@ -994,10 +1039,7 @@ describe('CategoryTemplateContext', () => {
// Mock the sheet values needed for init
vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0); // lastMonthBalance
vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false); // carryover
- vi.mocked(aql.aqlQuery).mockResolvedValueOnce({
- data: [{ value: 'false' }],
- dependencies: [],
- });
+ mockPreferences(false, 'USD');
// Initialize the template
const instance = await CategoryTemplateContext.init(
@@ -1056,10 +1098,7 @@ describe('CategoryTemplateContext', () => {
// Mock the sheet values needed for init
vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0); // lastMonthBalance
vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false); // carryover
- vi.mocked(aql.aqlQuery).mockResolvedValueOnce({
- data: [{ value: 'false' }],
- dependencies: [],
- });
+ mockPreferences(false, 'USD');
// Initialize the template
const instance = await CategoryTemplateContext.init(
@@ -1101,10 +1140,7 @@ describe('CategoryTemplateContext', () => {
// Mock the sheet values needed for init
vi.mocked(actions.getSheetValue).mockResolvedValueOnce(10000); // lastMonthBalance
vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false); // carryover
- vi.mocked(aql.aqlQuery).mockResolvedValueOnce({
- data: [{ value: 'false' }],
- dependencies: [],
- });
+ mockPreferences(false, 'USD');
// Initialize the template
const instance = await CategoryTemplateContext.init(
@@ -1148,10 +1184,7 @@ describe('CategoryTemplateContext', () => {
// Mock the sheet values needed for init
vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0); // lastMonthBalance
vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false); // carryover
- vi.mocked(aql.aqlQuery).mockResolvedValueOnce({
- data: [{ value: 'true' }],
- dependencies: [],
- });
+ mockPreferences(true, 'USD');
// Initialize the template
const instance = await CategoryTemplateContext.init(
@@ -1175,4 +1208,213 @@ describe('CategoryTemplateContext', () => {
expect(instance.isGoalOnly()).toBe(false); // Should not be goal only
});
});
+
+ describe('JPY currency', () => {
+ it('should handle simple template with JPY correctly', async () => {
+ const category: CategoryEntity = {
+ id: 'test',
+ name: 'Test Category',
+ group: 'test-group',
+ is_income: false,
+ };
+ const template: Template = {
+ type: 'simple',
+ monthly: 50,
+ directive: 'template',
+ priority: 1,
+ };
+
+ vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0);
+ vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false);
+ mockPreferences(true, 'JPY');
+
+ const instance = await CategoryTemplateContext.init(
+ [template],
+ category,
+ '2024-01',
+ 0,
+ );
+
+ await instance.runTemplatesForPriority(1, 100000, 100000);
+ const values = instance.getValues();
+
+ expect(values.budgeted).toBe(50);
+ });
+
+ it('should handle small amounts with JPY correctly', async () => {
+ const category: CategoryEntity = {
+ id: 'test',
+ name: 'Test Category',
+ group: 'test-group',
+ is_income: false,
+ };
+ const template: Template = {
+ type: 'simple',
+ monthly: 5,
+ directive: 'template',
+ priority: 1,
+ };
+
+ vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0);
+ vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false);
+ mockPreferences(true, 'JPY');
+
+ const instance = await CategoryTemplateContext.init(
+ [template],
+ category,
+ '2024-01',
+ 0,
+ );
+
+ await instance.runTemplatesForPriority(1, 100000, 100000);
+ const values = instance.getValues();
+
+ expect(values.budgeted).toBe(5);
+ });
+
+ it('should handle larger amounts with JPY correctly', async () => {
+ const category: CategoryEntity = {
+ id: 'test',
+ name: 'Test Category',
+ group: 'test-group',
+ is_income: false,
+ };
+ const template: Template = {
+ type: 'simple',
+ monthly: 250,
+ directive: 'template',
+ priority: 1,
+ };
+
+ vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0);
+ vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false);
+ mockPreferences(true, 'JPY');
+
+ const instance = await CategoryTemplateContext.init(
+ [template],
+ category,
+ '2024-01',
+ 0,
+ );
+
+ await instance.runTemplatesForPriority(1, 100000, 100000);
+ const values = instance.getValues();
+
+ expect(values.budgeted).toBe(250);
+ });
+
+ it('should handle weekly limit with JPY correctly', async () => {
+ const category: CategoryEntity = {
+ id: 'test',
+ name: 'Test Category',
+ group: 'test-group',
+ is_income: false,
+ };
+ const template: Template = {
+ type: 'simple',
+ limit: {
+ amount: 100,
+ hold: false,
+ period: 'weekly',
+ start: '2024-01-01',
+ },
+ directive: 'template',
+ priority: 1,
+ };
+
+ vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0);
+ vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false);
+ mockPreferences(true, 'JPY');
+
+ const instance = await CategoryTemplateContext.init(
+ [template],
+ category,
+ '2024-01',
+ 0,
+ );
+
+ const result = CategoryTemplateContext.runSimple(template, instance);
+
+ expect(result).toBeGreaterThanOrEqual(400);
+ expect(result).toBeLessThanOrEqual(500);
+ });
+
+ it('should handle periodic template with JPY correctly', async () => {
+ const category: CategoryEntity = {
+ id: 'test',
+ name: 'Test Category',
+ group: 'test-group',
+ is_income: false,
+ };
+ const template: Template = {
+ type: 'periodic',
+ amount: 1000,
+ period: { period: 'week', amount: 1 },
+ starting: '2024-01-01',
+ directive: 'template',
+ priority: 1,
+ };
+
+ vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0);
+ vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false);
+ mockPreferences(true, 'JPY');
+
+ const instance = await CategoryTemplateContext.init(
+ [template],
+ category,
+ '2024-01',
+ 0,
+ );
+
+ await instance.runTemplatesForPriority(1, 100000, 100000);
+ const values = instance.getValues();
+
+ expect(values.budgeted).toBeGreaterThan(3500);
+ expect(values.budgeted).toBeLessThan(5500);
+ });
+
+ it('should compare JPY vs USD for same template', async () => {
+ const category: CategoryEntity = {
+ id: 'test',
+ name: 'Test Category',
+ group: 'test-group',
+ is_income: false,
+ };
+ const template: Template = {
+ type: 'simple',
+ monthly: 100,
+ directive: 'template',
+ priority: 1,
+ };
+
+ vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0);
+ vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false);
+ mockPreferences(true, 'JPY');
+
+ const instanceJPY = await CategoryTemplateContext.init(
+ [template],
+ category,
+ '2024-01',
+ 0,
+ );
+ await instanceJPY.runTemplatesForPriority(1, 100000, 100000);
+ const valuesJPY = instanceJPY.getValues();
+
+ vi.mocked(actions.getSheetValue).mockResolvedValueOnce(0);
+ vi.mocked(actions.getSheetBoolean).mockResolvedValueOnce(false);
+ mockPreferences(false, 'USD');
+
+ const instanceUSD = await CategoryTemplateContext.init(
+ [template],
+ category,
+ '2024-01',
+ 0,
+ );
+ await instanceUSD.runTemplatesForPriority(1, 100000, 100000);
+ const valuesUSD = instanceUSD.getValues();
+
+ expect(valuesJPY.budgeted).toBe(100);
+ expect(valuesUSD.budgeted).toBe(10000);
+ });
+ });
});
diff --git a/packages/loot-core/src/server/budget/category-template-context.ts b/packages/loot-core/src/server/budget/category-template-context.ts
index 3e1701f30b..2af6a75937 100644
--- a/packages/loot-core/src/server/budget/category-template-context.ts
+++ b/packages/loot-core/src/server/budget/category-template-context.ts
@@ -1,4 +1,5 @@
// @ts-strict-ignore
+import { Currency, getCurrency } from 'loot-core/shared/currencies';
import { q } from 'loot-core/shared/query';
import * as monthUtils from '../../shared/months';
@@ -78,6 +79,12 @@ export class CategoryTemplateContext {
q('preferences').filter({ id: 'hideFraction' }).select('*'),
);
+ const currencyPref = await aqlQuery(
+ q('preferences').filter({ id: 'defaultCurrencyCode' }).select('*'),
+ );
+ const currencyCode =
+ currencyPref.data.length > 0 ? currencyPref.data[0].value : '';
+
// call the private constructor
return new CategoryTemplateContext(
templates,
@@ -85,6 +92,7 @@ export class CategoryTemplateContext {
month,
fromLastMonth,
budgeted,
+ currencyCode,
hideDecimal.data.length > 0
? hideDecimal.data[0].value === 'true'
: false,
@@ -146,10 +154,7 @@ export class CategoryTemplateContext {
let newBudget = 0;
switch (template.type) {
case 'simple': {
- newBudget = CategoryTemplateContext.runSimple(
- template,
- this.limitAmount,
- );
+ newBudget = CategoryTemplateContext.runSimple(template, this);
break;
}
case 'copy': {
@@ -301,6 +306,7 @@ export class CategoryTemplateContext {
private limitCheck = false;
private limitHold = false;
readonly previouslyBudgeted: number = 0;
+ private currency: Currency;
protected constructor(
templates: Template[],
@@ -308,12 +314,14 @@ export class CategoryTemplateContext {
month: string,
fromLastMonth: number,
budgeted: number,
+ currencyCode: string,
hideDecimal: boolean = false,
) {
this.category = category;
this.month = month;
this.fromLastMonth = fromLastMonth;
this.previouslyBudgeted = budgeted;
+ this.currency = getCurrency(currencyCode);
this.hideDecimal = hideDecimal;
// sort the template lines into regular template, goals, and remainder templates
if (templates) {
@@ -343,7 +351,10 @@ export class CategoryTemplateContext {
if (this.goals.length > 0) {
if (this.isGoalOnly()) this.toBudgetAmount = this.previouslyBudgeted;
this.isLongGoal = true;
- this.goalAmount = amountToInteger(this.goals[0].amount);
+ this.goalAmount = amountToInteger(
+ this.goals[0].amount,
+ this.currency.decimalPlaces,
+ );
return;
}
this.goalAmount = this.fullAmount;
@@ -454,11 +465,19 @@ export class CategoryTemplateContext {
monthUtils.addMonths(this.month, 1),
this.month,
);
- this.limitAmount += amountToInteger(limitDef.amount) * numDays;
+ this.limitAmount +=
+ amountToInteger(limitDef.amount, this.currency.decimalPlaces) *
+ numDays;
} else if (limitDef.period === 'weekly') {
+ if (!limitDef.start) {
+ throw new Error('Weekly limit requires a start date (YYYY-MM-DD)');
+ }
const nextMonth = monthUtils.nextMonth(this.month);
let week = limitDef.start;
- const baseLimit = amountToInteger(limitDef.amount);
+ const baseLimit = amountToInteger(
+ limitDef.amount,
+ this.currency.decimalPlaces,
+ );
while (week < nextMonth) {
if (week >= this.month) {
this.limitAmount += baseLimit;
@@ -466,7 +485,10 @@ export class CategoryTemplateContext {
week = monthUtils.addWeeks(week, 1);
}
} else if (limitDef.period === 'monthly') {
- this.limitAmount = amountToInteger(limitDef.amount);
+ this.limitAmount = amountToInteger(
+ limitDef.amount,
+ this.currency.decimalPlaces,
+ );
} else {
throw new Error('Invalid limit period. Check template syntax');
}
@@ -504,17 +526,26 @@ export class CategoryTemplateContext {
}
private removeFraction(amount: number): number {
- return amountToInteger(Math.round(integerToAmount(amount)));
+ return amountToInteger(
+ Math.round(integerToAmount(amount, this.currency.decimalPlaces)),
+ this.currency.decimalPlaces,
+ );
}
//-----------------------------------------------------------------------------
// Processor Functions
- static runSimple(template: SimpleTemplate, limit: number): number {
+ static runSimple(
+ template: SimpleTemplate,
+ templateContext: CategoryTemplateContext,
+ ): number {
if (template.monthly != null) {
- return amountToInteger(template.monthly);
+ return amountToInteger(
+ template.monthly,
+ templateContext.currency.decimalPlaces,
+ );
} else {
- return limit;
+ return templateContext.limitAmount;
}
}
@@ -536,7 +567,10 @@ export class CategoryTemplateContext {
templateContext: CategoryTemplateContext,
): number {
let toBudget = 0;
- const amount = amountToInteger(template.amount);
+ const amount = amountToInteger(
+ template.amount,
+ templateContext.currency.decimalPlaces,
+ );
const period = template.period.period;
const numPeriods = template.period.amount;
let date = template.starting;
@@ -637,7 +671,10 @@ export class CategoryTemplateContext {
toMonth,
templateContext.month,
);
- const target = amountToInteger(template.amount);
+ const target = amountToInteger(
+ template.amount,
+ templateContext.currency.decimalPlaces,
+ );
if (numMonths < 0) {
return 0;
} else {
@@ -740,17 +777,28 @@ export class CategoryTemplateContext {
// back interpolate what is needed in the short window
if (numMonths > shortNumMonths && period) {
amount = Math.round(
- (amountToInteger(template.amount) / period) *
+ (amountToInteger(
+ template.amount,
+ templateContext.currency.decimalPlaces,
+ ) /
+ period) *
(period - numMonths + shortNumMonths),
);
// fallback to this. This matches what the prior math accomplished, just more round about
} else if (numMonths > shortNumMonths) {
amount = Math.round(
- (amountToInteger(template.amount) / (numMonths + 1)) *
+ (amountToInteger(
+ template.amount,
+ templateContext.currency.decimalPlaces,
+ ) /
+ (numMonths + 1)) *
(shortNumMonths + 1),
);
} else {
- amount = amountToInteger(template.amount);
+ amount = amountToInteger(
+ template.amount,
+ templateContext.currency.decimalPlaces,
+ );
}
totalNeeded += amount;
}
diff --git a/packages/loot-core/src/server/budget/goal-template.ts b/packages/loot-core/src/server/budget/goal-template.ts
index 88cebb88bd..40f839c645 100644
--- a/packages/loot-core/src/server/budget/goal-template.ts
+++ b/packages/loot-core/src/server/budget/goal-template.ts
@@ -49,7 +49,7 @@ export async function applyTemplate({
}): Promise<Notification> {
await storeNoteTemplates();
const categoryTemplates = await getTemplates();
- const ret = await processTemplate(month, false, categoryTemplates);
+ const ret = await processTemplate(month, false, categoryTemplates, []);
return ret;
}
@@ -60,7 +60,7 @@ export async function overwriteTemplate({
}): Promise<Notification> {
await storeNoteTemplates();
const categoryTemplates = await getTemplates();
- const ret = await processTemplate(month, true, categoryTemplates);
+ const ret = await processTemplate(month, true, categoryTemplates, []);
return ret;
}
diff --git a/packages/loot-core/src/shared/currencies.ts b/packages/loot-core/src/shared/currencies.ts
index ee3d62cf7b..37473db5ee 100644
--- a/packages/loot-core/src/shared/currencies.ts
+++ b/packages/loot-core/src/shared/currencies.ts
@@ -37,7 +37,7 @@ export const currencies: Currency[] = [
{ code: 'HKD', name: 'Hong Kong Dollar', symbol: 'HK$', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
{ code: 'INR', name: 'Indian Rupee', symbol: '₹', decimalPlaces: 2, numberFormat: 'comma-dot-in', symbolFirst: true },
{ code: 'JMD', name: 'Jamaican Dollar', symbol: 'J$', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
- // { code: 'JPY', name: 'Japanese Yen', symbol: '¥', decimalPlaces: 0, numberFormat: 'comma-dot', symbolFirst: true },
+ { code: 'JPY', name: 'Japanese Yen', symbol: '¥', decimalPlaces: 0, numberFormat: 'comma-dot', symbolFirst: true },
{ code: 'LKR', name: 'Sri Lankan Rupee', symbol: 'Rs.', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
{ code: 'MDL', name: 'Moldovan Leu', symbol: 'L', decimalPlaces: 2, numberFormat: 'dot-comma', symbolFirst: false },
{ code: 'PHP', name: 'Philippine Peso', symbol: '₱', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
diff --git a/upcoming-release-notes/5955.md b/upcoming-release-notes/5955.md
new file mode 100644
index 0000000000..f12f938c74
--- /dev/null
+++ b/upcoming-release-notes/5955.md
@@ -0,0 +1,6 @@
+---
+category: Features
+authors: [misu-dev]
+---
+
+Adds currency support to templates and goals