Skip to content

A local-first personal finance app

Proportional distribute for split transaction (#7257)

Victor59839f83e31b
diff --git a/packages/desktop-client/src/components/transactions/TransactionsTable.tsx b/packages/desktop-client/src/components/transactions/TransactionsTable.tsx
index b7fa5789db..1b44bf0134 100644
--- a/packages/desktop-client/src/components/transactions/TransactionsTable.tsx
+++ b/packages/desktop-client/src/components/transactions/TransactionsTable.tsx
@@ -1205,7 +1205,7 @@ const Transaction = memo(function Transaction({
}
const id = setTimeout(() => {
- window.dispatchEvent(new Event('resize')); //Force popover to recalculate position
+ window.dispatchEvent(new Event('resize')); // Force popover to recalculate position
}, 1);
return () => clearTimeout(id);
}, [splitError, allTransactions]);
@@ -1964,7 +1964,6 @@ type TransactionErrorProps = {
onAddSplit: () => void;
onDistributeRemainder: () => void;
style?: CSSProperties;
- canDistributeRemainder: boolean;
};
function TransactionError({
@@ -1973,7 +1972,6 @@ function TransactionError({
onAddSplit,
onDistributeRemainder,
style,
- canDistributeRemainder,
}: TransactionErrorProps) {
switch (error.type) {
case 'SplitTransactionError':
@@ -2002,7 +2000,6 @@ function TransactionError({
style={{ marginLeft: 15 }}
onPress={onDistributeRemainder}
data-testid="distribute-split-button"
- isDisabled={!canDistributeRemainder}
>
<Trans>Distribute</Trans>
</Button>
@@ -2097,7 +2094,6 @@ function NewTransaction({
const childTransactions = transactions.filter(
t => t.parent_id === transactions[0].id,
);
- const emptyChildTransactions = childTransactions.filter(t => t.amount === 0);
const addButtonRef = useRef(null);
useProperFocus(addButtonRef, focusedField === 'add');
@@ -2188,7 +2184,6 @@ function NewTransaction({
onDistributeRemainder={() =>
onDistributeRemainder(transactions[0].id)
}
- canDistributeRemainder={emptyChildTransactions.length > 0}
/>
) : (
<Button
@@ -2402,9 +2397,6 @@ function TransactionTableInner({
const childTransactions = trans.is_parent
? props.transactionsByParent[trans.id]
: null;
- const emptyChildTransactions = props.transactionsByParent[
- (trans.is_parent ? trans.id : trans.parent_id) || ''
- ]?.filter(t => t.amount === 0);
// Get sibling count for child transactions (used for drag/drop)
const siblingCount =
@@ -2485,7 +2477,6 @@ function TransactionTableInner({
onDistributeRemainder={() =>
props.onDistributeRemainder(trans.id)
}
- canDistributeRemainder={emptyChildTransactions.length > 0}
/>
)
}
@@ -3298,37 +3289,93 @@ export const TransactionTable = forwardRef(
parentTransaction.amount -
siblingTransactions.reduce((acc, t) => acc + t.amount, 0);
- const amountPerTransaction = Math.floor(
- remainingAmount / emptyTransactions.length,
- );
- let remainingCents =
- remainingAmount - amountPerTransaction * emptyTransactions.length;
+ let amounts: number[] = [];
+ if (emptyTransactions.length > 0) {
+ const amountPerTransaction = Math.floor(
+ remainingAmount / emptyTransactions.length,
+ );
+ let remainingCents =
+ remainingAmount - amountPerTransaction * emptyTransactions.length;
- const amounts = new Array(emptyTransactions.length).fill(
- amountPerTransaction,
- );
+ amounts = new Array(emptyTransactions.length).fill(
+ amountPerTransaction,
+ );
- for (const [amountIndex] of amounts.entries()) {
- if (remainingCents === 0) break;
+ for (const [amountIndex] of amounts.entries()) {
+ if (remainingCents === 0) break;
- amounts[amountIndex] += 1;
- remainingCents--;
- }
+ amounts[amountIndex] += 1;
+ remainingCents--;
+ }
- if (isTemporaryId(id)) {
- newNavigator.onEdit(null);
- } else {
- tableNavigator.onEdit(null);
- }
+ if (isTemporaryId(id)) {
+ newNavigator.onEdit(null);
+ } else {
+ tableNavigator.onEdit(null);
+ }
- for (const [
- transactionIndex,
- transaction,
- ] of emptyTransactions.entries()) {
- await onSave({
- ...transaction,
- amount: amounts[transactionIndex],
- });
+ for (const [
+ transactionIndex,
+ transaction,
+ ] of emptyTransactions.entries()) {
+ await onSave({
+ ...transaction,
+ amount: amounts[transactionIndex],
+ });
+ }
+ } else if (
+ emptyTransactions.length === 0 &&
+ siblingTransactions.length > 0
+ ) {
+ const siblingTotal = siblingTransactions.reduce(
+ (acc, t) => acc + t.amount,
+ 0,
+ );
+ const siblingProportions = siblingTransactions.map(
+ t => t.amount / siblingTotal,
+ );
+
+ for (const [
+ transactionIndex,
+ transaction,
+ ] of siblingTransactions.entries()) {
+ amounts[transactionIndex] =
+ Math.floor(
+ siblingProportions[transactionIndex] * remainingAmount,
+ ) + transaction.amount;
+ }
+
+ let remainingCents =
+ parentTransaction.amount - amounts.reduce((acc, a) => acc + a, 0);
+
+ let amountIndex = 0;
+ while (remainingCents !== 0) {
+ amountIndex = amountIndex % amounts.length;
+ if (remainingCents > 0) {
+ amounts[amountIndex] += 1;
+ remainingCents--;
+ } else {
+ amounts[amountIndex] -= 1;
+ remainingCents++;
+ }
+ amountIndex++;
+ }
+
+ if (isTemporaryId(id)) {
+ newNavigator.onEdit(null);
+ } else {
+ tableNavigator.onEdit(null);
+ }
+
+ for (const [
+ transactionIndex,
+ transaction,
+ ] of siblingTransactions.entries()) {
+ await onSave({
+ ...transaction,
+ amount: amounts[transactionIndex],
+ });
+ }
}
},
[onSave],
diff --git a/packages/docs/docs-sidebar.js b/packages/docs/docs-sidebar.js
index f6a5243efb..5d288426ba 100644
--- a/packages/docs/docs-sidebar.js
+++ b/packages/docs/docs-sidebar.js
@@ -168,6 +168,7 @@ const sidebars = {
'transactions/payees',
'transactions/bulk-editing',
'transactions/tags',
+ 'transactions/split-transactions',
{
type: 'category',
label: 'Connecting Your Bank',
diff --git a/packages/docs/docs/transactions/split-transactions.md b/packages/docs/docs/transactions/split-transactions.md
new file mode 100644
index 0000000000..b064b98c09
--- /dev/null
+++ b/packages/docs/docs/transactions/split-transactions.md
@@ -0,0 +1,48 @@
+# Split Transactions
+
+Oftentimes a single transaction covers multiple categories. For example, a trip to the grocery store might include food, household supplies, and a gift card. Rather than creating separate transactions, you can split a single transaction into multiple child transactions, each assigned to its own category.
+
+## Creating a Split Transaction
+
+1. Click on the category of a transaction, and select the **Split Transaction** option. A new row will appear below the original transaction alongside a set of buttons for managing your split transaction.
+2. Add more splits of the original transaction as needed by clicking the **Add Split** button.
+3. Assign a category and amount to each split. The sum of the amounts of all child transactions must equal the amount of the parent transaction.
+
+![Image of buttons when managing split transactions](/img/split-transactions/split-transactions-buttons.png)
+
+## Distributing the Remainder
+
+The **Distribute** button works in two different ways depending on the state of the child transactions.
+
+### Even Distribution
+
+If at least one split is empty, clicking **Distribute** divides the remaining amount **evenly** among the empty splits.
+
+For example, if a $50.00 transaction has one split for $20.00 and two empty splits, clicking **Distribute** assigns $15.00 to each empty split.
+
+### Proportional Distribution
+
+If all splits already have amounts entered in, clicking **Distribute** divides the remaining amount **proportionally** based on the total of the child transactions. This can be useful when dividing taxes among different categories in a single transaction.
+
+For example, consider a $40.00 transaction split into two.
+
+| Split | Before | Proportion | Added | After |
+| --------- | ---------- | ---------- | ---------- | ---------- |
+| A | $20.00 | 2/3 | $6.66 | $26.66 |
+| B | $10.00 | 1/3 | $3.34 | $13.34 |
+| **Total** | **$30.00** | | **$10.00** | **$40.00** |
+
+Each split receives a share of the remainder proportional to its existing amount relative to the current total of the child transactions ($30.00).
+
+:::info
+When the remainder can't be divided evenly, the remaining cents are distributed one-by-one to the child transactions.
+:::
+
+## Unsplit Transactions
+
+Right-click any splits of the parent transaction and click **Unsplit 1 Transaction** to convert it into a regular transaction. You can also select and unsplit multiple rows at the same time. The total amount of the original transaction will be adjusted accordingly.
+
+:::tip
+Right-click the original transaction and unsplit it to convert all the splits into regular transactions.
+Alternatively, to convert a split transaction back into a regular transaction, delete all the splits.
+:::
diff --git a/packages/docs/static/img/split-transactions/split-transactions-buttons.png b/packages/docs/static/img/split-transactions/split-transactions-buttons.png
new file mode 100644
index 0000000000..cc1f4182bf
Binary files /dev/null and b/packages/docs/static/img/split-transactions/split-transactions-buttons.png differ
diff --git a/upcoming-release-notes/7257.md b/upcoming-release-notes/7257.md
new file mode 100644
index 0000000000..4fa33d86bc
--- /dev/null
+++ b/upcoming-release-notes/7257.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [victle]
+---
+
+Add option to distribute remaining amount proportionally