From 12f91f7d86ab23db5ff0a7d287325c9d2f22fda9 Mon Sep 17 00:00:00 2001 From: wdpk <20182595+wdpk@users.noreply.github.com> Date: Thu, 18 Jul 2024 04:57:36 -1000 Subject: [PATCH] update split transaction handling for csv export (#2973) * Split transaction handling for csv export (#2973) * refactor, zero-amt parents and add split_amount change how child/parents are counted to remove dependence on `sort_order` being negative. for export, give parent transactions a 0 in the `amount` column, but add a new column with their `split_amount` for other programs to possibly import --------- Co-authored-by: Zeus\Herb <herb@win.dows> Co-authored-by: DJ Mountney <david.mountney@twkie.net> --- .../src/server/accounts/export-to-csv.ts | 45 ++++++++++++++----- upcoming-release-notes/2973.md | 6 +++ 2 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 upcoming-release-notes/2973.md diff --git a/packages/loot-core/src/server/accounts/export-to-csv.ts b/packages/loot-core/src/server/accounts/export-to-csv.ts index 6d9dd432b..7b8dda5af 100644 --- a/packages/loot-core/src/server/accounts/export-to-csv.ts +++ b/packages/loot-core/src/server/accounts/export-to-csv.ts @@ -66,6 +66,8 @@ export async function exportQueryToCSV(query) { { Payee: 'payee.name' }, { ParentId: 'parent_id' }, { IsParent: 'is_parent' }, + { IsChild: 'is_child' }, + { SortOrder: 'sort_order' }, { Notes: 'notes' }, { Category: 'category.name' }, { Amount: 'amount' }, @@ -75,25 +77,46 @@ export async function exportQueryToCSV(query) { .options({ splits: 'all' }), ); - const parentsPayees = new Map(); + // initialize a map to allow splits to have correct number of split from + const parentsChildCount: Map<number, number> = new Map(); + const childSplitOrder: Map<number, number> = new Map(); + + // find children, their order, and total # siblings for (const trans of transactions) { - if (trans.IsParent) { - parentsPayees.set(trans.Id, trans.Payee); + if (trans.IsChild) { + let childNumber = parentsChildCount.get(trans.ParentId) || 0; + childNumber++; + childSplitOrder.set(trans.Id, childNumber); + parentsChildCount.set(trans.ParentId, childNumber); } } - // filter out any parent transactions - const noParents = transactions.filter(t => !t.IsParent); - - // map final properties for export and grab the payee for splits from their parent transaction - const transactionsForExport = noParents.map(trans => { + // map final properties for export and grab the child count for splits from their parent transaction + const transactionsForExport = transactions.map(trans => { return { Account: trans.Account, Date: trans.Date, - Payee: trans.ParentId ? parentsPayees.get(trans.ParentId) : trans.Payee, - Notes: trans.Notes, + Payee: trans.Payee, + Notes: trans.IsParent + ? '(SPLIT INTO ' + + parentsChildCount.get(trans.Id) + + ') ' + + (trans.Notes || '') + : trans.IsChild + ? '(SPLIT ' + + childSplitOrder.get(trans.Id) + + ' OF ' + + parentsChildCount.get(trans.ParentId) + + ') ' + + (trans.Notes || '') + : trans.Notes, Category: trans.Category, - Amount: trans.Amount == null ? 0 : integerToAmount(trans.Amount), + Amount: trans.IsParent + ? 0 + : trans.Amount == null + ? 0 + : integerToAmount(trans.Amount), + Split_Amount: trans.IsParent ? integerToAmount(trans.Amount) : 0, Cleared: trans.Reconciled === true ? 'Reconciled' diff --git a/upcoming-release-notes/2973.md b/upcoming-release-notes/2973.md new file mode 100644 index 000000000..6729b8442 --- /dev/null +++ b/upcoming-release-notes/2973.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [wdpk] +--- + +Fix exporting split transactions to CSV by including top-line transactions and noting the split. -- GitLab