Skip to content

A local-first personal finance app

Allow entering amount when covering overspent/overbudgeted (#5988)

Joel Jeremy Marquez0043577695ba
diff --git a/packages/desktop-client/src/budget/budgetSlice.ts b/packages/desktop-client/src/budget/budgetSlice.ts
index 70bce78f59..b932bf16d0 100644
--- a/packages/desktop-client/src/budget/budgetSlice.ts
+++ b/packages/desktop-client/src/budget/budgetSlice.ts
@@ -3,6 +3,7 @@ import { t } from 'i18next';
import memoizeOne from 'memoize-one';
import { send } from 'loot-core/platform/client/fetch';
+import { type IntegerAmount } from 'loot-core/shared/util';
import {
type CategoryEntity,
type CategoryGroupEntity,
@@ -333,6 +334,7 @@ type ApplyBudgetActionPayload =
args: {
to: CategoryEntity['id'];
from: CategoryEntity['id'];
+ amount?: IntegerAmount;
};
}
| {
@@ -348,6 +350,7 @@ type ApplyBudgetActionPayload =
month: string;
args: {
category: CategoryEntity['id'];
+ amount?: IntegerAmount;
};
}
| {
@@ -495,6 +498,7 @@ export const applyBudgetAction = createAppAsyncThunk(
month,
to: args.to,
from: args.from,
+ amount: args.amount,
});
break;
case 'transfer-available':
@@ -508,6 +512,7 @@ export const applyBudgetAction = createAppAsyncThunk(
await send('budget/cover-overbudgeted', {
month,
category: args.category,
+ amount: args.amount,
});
break;
case 'transfer-category':
diff --git a/packages/desktop-client/src/components/budget/envelope/BalanceMovementMenu.tsx b/packages/desktop-client/src/components/budget/envelope/BalanceMovementMenu.tsx
index 769776ca0c..f4233ca4fb 100644
--- a/packages/desktop-client/src/components/budget/envelope/BalanceMovementMenu.tsx
+++ b/packages/desktop-client/src/components/budget/envelope/BalanceMovementMenu.tsx
@@ -71,11 +71,13 @@ export function BalanceMovementMenu({
{menu === 'cover' && (
<CoverMenu
categoryId={categoryId}
+ initialAmount={catBalance}
onClose={onClose}
- onSubmit={fromCategoryId => {
+ onSubmit={(amount, fromCategoryId) => {
onBudgetAction(month, 'cover-overspending', {
to: categoryId,
from: fromCategoryId,
+ amount,
});
}}
/>
diff --git a/packages/desktop-client/src/components/budget/envelope/CoverMenu.tsx b/packages/desktop-client/src/components/budget/envelope/CoverMenu.tsx
index fc1a1bdb46..e0fafbda3c 100644
--- a/packages/desktop-client/src/components/budget/envelope/CoverMenu.tsx
+++ b/packages/desktop-client/src/components/budget/envelope/CoverMenu.tsx
@@ -4,8 +4,15 @@ import { Trans, useTranslation } from 'react-i18next';
import { Button } from '@actual-app/components/button';
import { InitialFocus } from '@actual-app/components/initial-focus';
+import { Input } from '@actual-app/components/input';
import { View } from '@actual-app/components/view';
+import { evalArithmetic } from 'loot-core/shared/arithmetic';
+import {
+ amountToInteger,
+ integerToCurrency,
+ type IntegerAmount,
+} from 'loot-core/shared/util';
import { type CategoryEntity } from 'loot-core/types/models';
import { CategoryAutocomplete } from '@desktop-client/components/autocomplete/CategoryAutocomplete';
@@ -17,13 +24,15 @@ import { useCategories } from '@desktop-client/hooks/useCategories';
type CoverMenuProps = {
showToBeBudgeted?: boolean;
+ initialAmount?: IntegerAmount | null;
categoryId?: CategoryEntity['id'];
- onSubmit: (categoryId: CategoryEntity['id']) => void;
+ onSubmit: (amount: IntegerAmount, categoryId: CategoryEntity['id']) => void;
onClose: () => void;
};
export function CoverMenu({
showToBeBudgeted = true,
+ initialAmount = 0,
categoryId,
onSubmit,
onClose,
@@ -44,9 +53,13 @@ export function CoverMenu({
: categoryGroups;
}, [categoryId, showToBeBudgeted, originalCategoryGroups]);
+ const _initialAmount = integerToCurrency(Math.abs(initialAmount ?? 0));
+ const [amount, setAmount] = useState<string | null>(null);
+
function _onSubmit() {
- if (fromCategoryId) {
- onSubmit(fromCategoryId);
+ const parsedAmount = evalArithmetic(amount || '');
+ if (parsedAmount && fromCategoryId) {
+ onSubmit(amountToInteger(parsedAmount), fromCategoryId);
}
onClose();
}
@@ -60,26 +73,27 @@ export function CoverMenu({
>
<View style={{ padding: 10 }}>
<View style={{ marginBottom: 5 }}>
- <Trans>Cover from a category:</Trans>
+ <Trans>Cover this amount:</Trans>
+ </View>
+ <View>
+ <InitialFocus>
+ <Input defaultValue={_initialAmount} onUpdate={setAmount} />
+ </InitialFocus>
+ </View>
+ <View style={{ margin: '10px 0 5px 0' }}>
+ <Trans>From:</Trans>
</View>
- <InitialFocus<HTMLInputElement>>
- {node => (
- <CategoryAutocomplete
- categoryGroups={filteredCategoryGroups}
- value={null}
- openOnFocus={true}
- onSelect={(id: string | undefined) =>
- setFromCategoryId(id || null)
- }
- inputProps={{
- ref: node,
- placeholder: t('(none)'),
- }}
- showHiddenCategories={false}
- />
- )}
- </InitialFocus>
+ <CategoryAutocomplete
+ categoryGroups={filteredCategoryGroups}
+ value={null}
+ openOnFocus={true}
+ onSelect={(id: string | undefined) => setFromCategoryId(id || null)}
+ inputProps={{
+ placeholder: t('(none)'),
+ }}
+ showHiddenCategories={false}
+ />
<View
style={{
diff --git a/packages/desktop-client/src/components/budget/envelope/TransferMenu.tsx b/packages/desktop-client/src/components/budget/envelope/TransferMenu.tsx
index f83eaa922b..2595f4053b 100644
--- a/packages/desktop-client/src/components/budget/envelope/TransferMenu.tsx
+++ b/packages/desktop-client/src/components/budget/envelope/TransferMenu.tsx
@@ -8,7 +8,11 @@ import { Input } from '@actual-app/components/input';
import { View } from '@actual-app/components/view';
import { evalArithmetic } from 'loot-core/shared/arithmetic';
-import { integerToCurrency, amountToInteger } from 'loot-core/shared/util';
+import {
+ integerToCurrency,
+ amountToInteger,
+ type IntegerAmount,
+} from 'loot-core/shared/util';
import { type CategoryEntity } from 'loot-core/types/models';
import { CategoryAutocomplete } from '@desktop-client/components/autocomplete/CategoryAutocomplete';
@@ -20,9 +24,9 @@ import { useCategories } from '@desktop-client/hooks/useCategories';
type TransferMenuProps = {
categoryId?: CategoryEntity['id'];
- initialAmount?: number;
+ initialAmount?: IntegerAmount | null;
showToBeBudgeted?: boolean;
- onSubmit: (amount: number, categoryId: CategoryEntity['id']) => void;
+ onSubmit: (amount: IntegerAmount, categoryId: CategoryEntity['id']) => void;
onClose: () => void;
};
@@ -46,14 +50,14 @@ export function TransferMenu({
: categoryGroups;
}, [originalCategoryGroups, categoryId, showToBeBudgeted]);
- const _initialAmount = integerToCurrency(Math.max(initialAmount, 0));
+ const _initialAmount = integerToCurrency(initialAmount ?? 0);
const [amount, setAmount] = useState<string | null>(null);
const [toCategoryId, setToCategoryId] = useState<string | null>(null);
- const _onSubmit = (newAmount: string | null, categoryId: string | null) => {
- const parsedAmount = evalArithmetic(newAmount || '');
- if (parsedAmount && categoryId) {
- onSubmit?.(amountToInteger(parsedAmount), categoryId);
+ const _onSubmit = () => {
+ const parsedAmount = evalArithmetic(amount || '');
+ if (parsedAmount && toCategoryId) {
+ onSubmit(amountToInteger(parsedAmount), toCategoryId);
}
onClose();
@@ -63,7 +67,7 @@ export function TransferMenu({
<Form
onSubmit={e => {
e.preventDefault();
- _onSubmit(amount, toCategoryId);
+ _onSubmit();
}}
>
<View style={{ padding: 10 }}>
@@ -75,7 +79,9 @@ export function TransferMenu({
<Input defaultValue={_initialAmount} onUpdate={setAmount} />
</InitialFocus>
</View>
- <View style={{ margin: '10px 0 5px 0' }}>To:</View>
+ <View style={{ margin: '10px 0 5px 0' }}>
+ <Trans>To:</Trans>
+ </View>
<CategoryAutocomplete
categoryGroups={filteredCategoryGroups}
diff --git a/packages/desktop-client/src/components/budget/envelope/budgetsummary/ToBudget.tsx b/packages/desktop-client/src/components/budget/envelope/budgetsummary/ToBudget.tsx
index df903976ca..b2e4756550 100644
--- a/packages/desktop-client/src/components/budget/envelope/budgetsummary/ToBudget.tsx
+++ b/packages/desktop-client/src/components/budget/envelope/budgetsummary/ToBudget.tsx
@@ -116,7 +116,7 @@ export function ToBudget({
)}
{menuStep === 'transfer' && (
<TransferMenu
- initialAmount={availableValue ?? undefined}
+ initialAmount={availableValue}
onClose={() => setMenuOpen(false)}
onSubmit={(amount, categoryId) => {
onBudgetAction(month, 'transfer-available', {
@@ -129,10 +129,12 @@ export function ToBudget({
{menuStep === 'cover' && (
<CoverMenu
showToBeBudgeted={false}
+ initialAmount={availableValue}
onClose={() => setMenuOpen(false)}
- onSubmit={categoryId => {
+ onSubmit={(amount, categoryId) => {
onBudgetAction(month, 'cover-overbudgeted', {
category: categoryId,
+ amount,
});
}}
/>
diff --git a/packages/desktop-client/src/components/mobile/budget/BudgetPage.tsx b/packages/desktop-client/src/components/mobile/budget/BudgetPage.tsx
index bf43befb86..52e8c98400 100644
--- a/packages/desktop-client/src/components/mobile/budget/BudgetPage.tsx
+++ b/packages/desktop-client/src/components/mobile/budget/BudgetPage.tsx
@@ -748,10 +748,12 @@ function OverbudgetedBanner({ month, onBudgetAction, ...props }) {
options: {
title: t('Cover overbudgeted'),
month,
+ amount: toBudgetAmount,
showToBeBudgeted: false,
- onSubmit: categoryId => {
+ onSubmit: (amount, categoryId) => {
onBudgetAction(month, 'cover-overbudgeted', {
category: categoryId,
+ amount,
});
showUndoNotification({
message: t('Covered overbudgeted from {{categoryName}}', {
@@ -770,6 +772,7 @@ function OverbudgetedBanner({ month, onBudgetAction, ...props }) {
onBudgetAction,
showUndoNotification,
t,
+ toBudgetAmount,
]);
if (!toBudgetAmount || toBudgetAmount >= 0) {
@@ -824,8 +827,11 @@ function OverspendingBanner({ month, onBudgetAction, budgetType, ...props }) {
const dispatch = useDispatch();
const format = useFormat();
- const { categories: overspentCategories, totalAmount: totalOverspending } =
- useOverspentCategories({ month });
+ const {
+ categories: overspentCategories,
+ amountsByCategory,
+ totalAmount: totalOverspending,
+ } = useOverspentCategories({ month });
const categoryGroupsToShow = useMemo(
() =>
@@ -850,11 +856,13 @@ function OverspendingBanner({ month, onBudgetAction, budgetType, ...props }) {
options: {
title: category.name,
month,
+ amount: amountsByCategory.get(category.id),
categoryId: category.id,
- onSubmit: fromCategoryId => {
+ onSubmit: (amount, fromCategoryId) => {
onBudgetAction(month, 'cover-overspending', {
to: category.id,
from: fromCategoryId,
+ amount,
});
showUndoNotification({
message: t(
@@ -874,7 +882,15 @@ function OverspendingBanner({ month, onBudgetAction, budgetType, ...props }) {
}),
);
},
- [categoriesById, dispatch, month, onBudgetAction, showUndoNotification, t],
+ [
+ amountsByCategory,
+ categoriesById,
+ dispatch,
+ month,
+ onBudgetAction,
+ showUndoNotification,
+ t,
+ ],
);
const onOpenCategorySelectionModal = useCallback(() => {
diff --git a/packages/desktop-client/src/components/mobile/budget/ExpenseCategoryListItem.tsx b/packages/desktop-client/src/components/mobile/budget/ExpenseCategoryListItem.tsx
index 088c27fbe2..254739a4c4 100644
--- a/packages/desktop-client/src/components/mobile/budget/ExpenseCategoryListItem.tsx
+++ b/packages/desktop-client/src/components/mobile/budget/ExpenseCategoryListItem.tsx
@@ -311,17 +311,20 @@ export function ExpenseCategoryListItem({
options: {
title: category.name,
month,
+ amount: catBalance,
categoryId: category.id,
- onSubmit: fromCategoryId => {
+ onSubmit: (amount, fromCategoryId) => {
onBudgetAction(month, 'cover-overspending', {
to: category.id,
from: fromCategoryId,
+ amount,
});
dispatch(collapseModals({ rootModalName: balanceMenuModalName }));
showUndoNotification({
message: t(
- `Covered {{toCategoryName}} overspending from {{fromCategoryName}}.`,
+ `Covered {{amount}} {{toCategoryName}} overspending from {{fromCategoryName}}.`,
{
+ amount: integerToCurrency(amount),
toCategoryName: category.name,
fromCategoryName: categoriesById[fromCategoryId].name,
},
@@ -336,6 +339,7 @@ export function ExpenseCategoryListItem({
category,
dispatch,
month,
+ catBalance,
onBudgetAction,
balanceMenuModalName,
showUndoNotification,
diff --git a/packages/desktop-client/src/components/modals/CoverModal.tsx b/packages/desktop-client/src/components/modals/CoverModal.tsx
index 77d6c79b19..2fbf6a7239 100644
--- a/packages/desktop-client/src/components/modals/CoverModal.tsx
+++ b/packages/desktop-client/src/components/modals/CoverModal.tsx
@@ -6,6 +6,8 @@ import { InitialFocus } from '@actual-app/components/initial-focus';
import { styles } from '@actual-app/components/styles';
import { View } from '@actual-app/components/view';
+import { type IntegerAmount } from 'loot-core/shared/util';
+
import {
addToBeBudgetedGroup,
removeCategoriesFromGroups,
@@ -19,6 +21,7 @@ import {
FieldLabel,
TapField,
} from '@desktop-client/components/mobile/MobileForms';
+import { AmountInput } from '@desktop-client/components/util/AmountInput';
import { useCategories } from '@desktop-client/hooks/useCategories';
import { useInitialMount } from '@desktop-client/hooks/useInitialMount';
import {
@@ -31,6 +34,7 @@ type CoverModalProps = Extract<ModalType, { name: 'cover' }>['options'];
export function CoverModal({
title,
+ amount: initialAmount,
categoryId,
month,
showToBeBudgeted = true,
@@ -73,14 +77,17 @@ export function CoverModal({
);
}, [categoryGroups, dispatch, month]);
- const _onSubmit = (categoryId: string | null) => {
- if (categoryId) {
- onSubmit?.(categoryId);
+ const fromCategory = categories.find(c => c.id === fromCategoryId);
+ const [amount, setAmount] = useState<IntegerAmount>(
+ Math.abs(initialAmount ?? 0),
+ );
+
+ const _onSubmit = () => {
+ if (amount && fromCategoryId) {
+ onSubmit(amount, fromCategoryId);
}
};
- const fromCategory = categories.find(c => c.id === fromCategoryId);
-
const isInitialMount = useInitialMount();
useEffect(() => {
if (isInitialMount) {
@@ -97,16 +104,33 @@ export function CoverModal({
rightContent={<ModalCloseButton onPress={close} />}
/>
<View>
- <FieldLabel title={t('Cover from a category:')} />
+ <FieldLabel title={t('Cover this amount:')} />
<InitialFocus>
- <TapField
- autoFocus
- value={fromCategory?.name}
- onPress={openCategoryModal}
+ <AmountInput
+ value={amount}
+ autoDecimals={true}
+ style={{
+ marginLeft: styles.mobileEditingPadding,
+ marginRight: styles.mobileEditingPadding,
+ }}
+ inputStyle={{
+ height: styles.mobileMinHeight,
+ }}
+ onUpdate={setAmount}
+ onEnter={() => {
+ if (!fromCategoryId) {
+ openCategoryModal();
+ }
+ }}
/>
</InitialFocus>
</View>
+ <View>
+ <FieldLabel title={t('From:')} />
+ <TapField value={fromCategory?.name} onPress={openCategoryModal} />
+ </View>
+
<View
style={{
justifyContent: 'center',
@@ -122,7 +146,7 @@ export function CoverModal({
marginRight: styles.mobileEditingPadding,
}}
onPress={() => {
- _onSubmit(fromCategoryId);
+ _onSubmit();
close();
}}
>
diff --git a/packages/desktop-client/src/components/modals/EnvelopeBudgetSummaryModal.tsx b/packages/desktop-client/src/components/modals/EnvelopeBudgetSummaryModal.tsx
index 44b0043aa1..c694c2bce0 100644
--- a/packages/desktop-client/src/components/modals/EnvelopeBudgetSummaryModal.tsx
+++ b/packages/desktop-client/src/components/modals/EnvelopeBudgetSummaryModal.tsx
@@ -88,9 +88,11 @@ export function EnvelopeBudgetSummaryModal({
title: t('Cover overbudgeted'),
month,
showToBeBudgeted: false,
- onSubmit: categoryId => {
+ amount: sheetValue,
+ onSubmit: (amount, categoryId) => {
onBudgetAction(month, 'cover-overbudgeted', {
category: categoryId,
+ amount,
});
dispatch(collapseModals({ rootModalName: 'cover' }));
showUndoNotification({
diff --git a/packages/desktop-client/src/components/modals/TransferModal.tsx b/packages/desktop-client/src/components/modals/TransferModal.tsx
index b9e0e6aa71..bfc30a1e41 100644
--- a/packages/desktop-client/src/components/modals/TransferModal.tsx
+++ b/packages/desktop-client/src/components/modals/TransferModal.tsx
@@ -6,6 +6,8 @@ import { InitialFocus } from '@actual-app/components/initial-focus';
import { styles } from '@actual-app/components/styles';
import { View } from '@actual-app/components/view';
+import { type IntegerAmount } from 'loot-core/shared/util';
+
import {
addToBeBudgetedGroup,
removeCategoriesFromGroups,
@@ -55,7 +57,7 @@ export function TransferModal({
return [filteredCategoryGroups, filteredCategories];
}, [categoryId, originalCategoryGroups, showToBeBudgeted]);
- const [amount, setAmount] = useState<number>(initialAmount);
+ const [amount, setAmount] = useState<IntegerAmount>(initialAmount);
const [toCategoryId, setToCategoryId] = useState<string | null>(null);
const dispatch = useDispatch();
diff --git a/packages/desktop-client/src/components/util/AmountInput.tsx b/packages/desktop-client/src/components/util/AmountInput.tsx
index 0df36a379d..9836cdc214 100644
--- a/packages/desktop-client/src/components/util/AmountInput.tsx
+++ b/packages/desktop-client/src/components/util/AmountInput.tsx
@@ -18,20 +18,25 @@ import { theme } from '@actual-app/components/theme';
import { View } from '@actual-app/components/view';
import { css, cx } from '@emotion/css';
+import { type IntegerAmount } from 'loot-core/shared/util';
+
import { useFormat } from '@desktop-client/hooks/useFormat';
import { useMergedRefs } from '@desktop-client/hooks/useMergedRefs';
type AmountInputProps = {
id?: string;
ref?: Ref<HTMLInputElement>;
- value: number;
+ value: IntegerAmount;
zeroSign?: '-' | '+';
sign?: '-' | '+';
onChangeValue?: (value: string) => void;
onFocus?: FocusEventHandler<HTMLInputElement>;
onBlur?: FocusEventHandler<HTMLInputElement>;
- onEnter?: (event: KeyboardEvent<HTMLInputElement>, amount?: number) => void;
- onUpdate?: (amount: number) => void;
+ onEnter?: (
+ event: KeyboardEvent<HTMLInputElement>,
+ amount?: IntegerAmount,
+ ) => void;
+ onUpdate?: (amount: IntegerAmount) => void;
style?: CSSProperties;
inputStyle?: CSSProperties;
inputClassName?: string;
diff --git a/packages/desktop-client/src/modals/modalsSlice.ts b/packages/desktop-client/src/modals/modalsSlice.ts
index d1aaa9c3a4..43b6a3378b 100644
--- a/packages/desktop-client/src/modals/modalsSlice.ts
+++ b/packages/desktop-client/src/modals/modalsSlice.ts
@@ -1,6 +1,7 @@
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';
import { send } from 'loot-core/platform/client/fetch';
+import { type IntegerAmount } from 'loot-core/shared/util';
import { type File } from 'loot-core/types/file';
import {
type AccountEntity,
@@ -411,10 +412,13 @@ export type Modal =
name: 'transfer';
options: {
title: string;
+ amount: IntegerAmount;
categoryId?: CategoryEntity['id'];
month: string;
- amount: number;
- onSubmit: (amount: number, toCategoryId: CategoryEntity['id']) => void;
+ onSubmit: (
+ amount: IntegerAmount,
+ toCategoryId: CategoryEntity['id'],
+ ) => void;
showToBeBudgeted?: boolean;
};
}
@@ -422,10 +426,14 @@ export type Modal =
name: 'cover';
options: {
title: string;
+ amount?: IntegerAmount | null;
categoryId?: CategoryEntity['id'];
month: string;
showToBeBudgeted?: boolean;
- onSubmit: (fromCategoryId: CategoryEntity['id']) => void;
+ onSubmit: (
+ amount: IntegerAmount,
+ fromCategoryId: CategoryEntity['id'],
+ ) => void;
};
}
| {
diff --git a/packages/loot-core/src/server/budget/actions.ts b/packages/loot-core/src/server/budget/actions.ts
index 00361b6296..7306da0eb9 100644
--- a/packages/loot-core/src/server/budget/actions.ts
+++ b/packages/loot-core/src/server/budget/actions.ts
@@ -3,7 +3,11 @@
import * as asyncStorage from '../../platform/server/asyncStorage';
import { getLocale } from '../../shared/locale';
import * as monthUtils from '../../shared/months';
-import { integerToCurrency, safeNumber } from '../../shared/util';
+import {
+ IntegerAmount,
+ integerToCurrency,
+ safeNumber,
+} from '../../shared/util';
import { CategoryEntity } from '../../types/models';
import * as db from '../db';
import * as sheet from '../sheet';
@@ -432,45 +436,53 @@ export async function coverOverspending({
month,
to,
from,
+ amount,
}: {
month: string;
to: CategoryEntity['id'] | 'to-budget';
from: CategoryEntity['id'] | 'to-budget' | 'overbudgeted';
+ amount?: IntegerAmount;
}): Promise<void> {
const sheetName = monthUtils.sheetForMonth(month);
const toBudgeted = await getSheetValue(sheetName, 'budget-' + to);
- const leftover = await getSheetValue(sheetName, 'leftover-' + to);
const leftoverFrom = await getSheetValue(
sheetName,
from === 'to-budget' ? 'to-budget' : 'leftover-' + from,
);
- if (leftover >= 0 || leftoverFrom <= 0) {
+ // Cover provided amount (can be partial) or full overspending amount.
+ const amountToCover = amount
+ ? // Covering in the app provides a positive amount to cover so we invert it here
+ -amount
+ : await getSheetValue(sheetName, 'leftover-' + to);
+
+ if (amountToCover >= 0 || leftoverFrom <= 0) {
return;
}
- const amountCovered = Math.min(-leftover, leftoverFrom);
-
- // If we are covering it from the to be budgeted amount, ignore this
- if (from !== 'to-budget') {
- const fromBudgeted = await getSheetValue(sheetName, 'budget-' + from);
- await setBudget({
- category: from,
- month,
- amount: fromBudgeted - amountCovered,
- });
- }
+ // Don't go over the leftover amount of the covering category
+ const coverableAmount = Math.min(Math.abs(amountToCover), leftoverFrom);
await batchMessages(async () => {
+ // If we are covering it from the to be budgeted amount, ignore this
+ if (from !== 'to-budget') {
+ const fromBudgeted = await getSheetValue(sheetName, 'budget-' + from);
+ await setBudget({
+ category: from,
+ month,
+ amount: fromBudgeted - coverableAmount,
+ });
+ }
+
await setBudget({
category: to,
month,
- amount: toBudgeted + amountCovered,
+ amount: toBudgeted + coverableAmount,
});
await addMovementNotes({
month,
- amount: amountCovered,
+ amount: coverableAmount,
to,
from,
});
@@ -497,21 +509,38 @@ export async function transferAvailable({
export async function coverOverbudgeted({
month,
category,
+ amount,
}: {
month: string;
category: string;
+ amount?: IntegerAmount;
}): Promise<void> {
const sheetName = monthUtils.sheetForMonth(month);
- const toBudget = await getSheetValue(sheetName, 'to-budget');
-
const categoryBudget = await getSheetValue(sheetName, 'budget-' + category);
+ // Cover provided amount (can be partial) or full overbudgeted amount.
+ const amountToCover = amount
+ ? // Covering in the app provides a positive amount to cover so we invert it here
+ -amount
+ : await getSheetValue(sheetName, 'to-budget');
+
+ if (amountToCover >= 0 || categoryBudget <= 0) {
+ return;
+ }
+
+ // Don't allow the budget of the covering category to go negative.
+ const coverableAmount = Math.min(Math.abs(amountToCover), categoryBudget);
+
await batchMessages(async () => {
- await setBudget({ category, month, amount: categoryBudget + toBudget });
+ await setBudget({
+ category,
+ month,
+ amount: categoryBudget - coverableAmount,
+ });
await addMovementNotes({
month,
- amount: -toBudget,
+ amount: coverableAmount,
from: category,
to: 'overbudgeted',
});
diff --git a/upcoming-release-notes/5988.md b/upcoming-release-notes/5988.md
new file mode 100644
index 0000000000..3ea9e14335
--- /dev/null
+++ b/upcoming-release-notes/5988.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [joel-jeremy]
+---
+
+Allow entering amount when covering overspent/overbudgeted