A local-first personal finance app
Add an option to swap payee/memo when importing transaction form file (#7101)
Sylvercodef29c03173552
diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsxindex c41d0419a7..ecb67993d8 100644--- a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx@@ -1,6 +1,11 @@// @ts-strict-ignoreimport React, { useCallback, useEffect, useEffectEvent, useState } from 'react';-import type { ComponentProps } from 'react';+import type {+ ComponentProps,+ Dispatch,+ ReactNode,+ SetStateAction,+} from 'react';import { Trans, useTranslation } from 'react-i18next';import { Button, ButtonWithLoading } from '@actual-app/components/button';@@ -53,6 +58,28 @@ import { useDateFormat } from '@desktop-client/hooks/useDateFormat';import { useSyncedPrefs } from '@desktop-client/hooks/useSyncedPrefs';import { payeeQueries } from '@desktop-client/payees';+function CheckboxToggle({+ id,+ checked,+ onChange,+ children,+}: {+ id: string;+ checked: boolean;+ onChange: Dispatch<SetStateAction<boolean>>;+ children: ReactNode;+}) {+ return (+ <LabeledCheckbox+ id={id}+ checked={checked}+ onChange={() => onChange(prev => !prev)}+ >+ {children}+ </LabeledCheckbox>+ );+}+function getFileType(filepath: string): string {const m = filepath.match(/\.([^.]*)$/);if (!m) return 'ofx';@@ -213,6 +240,15 @@ export function ImportTransactionsModal({const [fallbackMissingPayeeToMemo, setFallbackMissingPayeeToMemo] = useState(String(prefs[`ofx-fallback-missing-payee-${accountId}`]) !== 'false',);+ const [ofxSwapPayeeAndMemo, setOfxSwapPayeeAndMemo] = useState(+ String(prefs[`ofx-swap-payee-memo-${accountId}`]) === 'true',+ );+ const [qifSwapPayeeAndMemo, setQifSwapPayeeAndMemo] = useState(+ String(prefs[`qif-swap-payee-memo-${accountId}`]) === 'true',+ );+ const [camtSwapPayeeAndMemo, setCamtSwapPayeeAndMemo] = useState(+ String(prefs[`camt-swap-payee-memo-${accountId}`]) === 'true',+ );const [parseDateFormat, setParseDateFormat] = useState<DateFormat | null>(null,@@ -413,6 +449,12 @@ export function ImportTransactionsModal({skipEndLines,fallbackMissingPayeeToMemo,importNotes,+ swapPayeeAndMemo: getSwapOption(+ fileType,+ ofxSwapPayeeAndMemo,+ qifSwapPayeeAndMemo,+ camtSwapPayeeAndMemo,+ ),});void parse(originalFileName, parseOptions);@@ -424,6 +466,9 @@ export function ImportTransactionsModal({skipEndLines,fallbackMissingPayeeToMemo,importNotes,+ ofxSwapPayeeAndMemo,+ qifSwapPayeeAndMemo,+ camtSwapPayeeAndMemo,parse,]);@@ -471,6 +516,12 @@ export function ImportTransactionsModal({skipEndLines,fallbackMissingPayeeToMemo,importNotes,+ swapPayeeAndMemo: getSwapOption(+ fileType,+ ofxSwapPayeeAndMemo,+ qifSwapPayeeAndMemo,+ camtSwapPayeeAndMemo,+ ),});void parse(res[0], parseOptions);@@ -625,6 +676,7 @@ export function ImportTransactionsModal({[`ofx-fallback-missing-payee-${accountId}`]: String(fallbackMissingPayeeToMemo,),+ [`ofx-swap-payee-memo-${accountId}`]: String(ofxSwapPayeeAndMemo),});}@@ -649,6 +701,18 @@ export function ImportTransactionsModal({});}+ if (filetype === 'qif') {+ savePrefs({+ [`qif-swap-payee-memo-${accountId}`]: String(qifSwapPayeeAndMemo),+ });+ }++ if (isCamtFile(filetype)) {+ savePrefs({+ [`camt-swap-payee-memo-${accountId}`]: String(camtSwapPayeeAndMemo),+ });+ }+importTransactions.mutate({accountId,@@ -949,39 +1013,62 @@ export function ImportTransactionsModal({)}{isOfxFile(filetype) && (- <LabeledCheckbox- id="form_fallback_missing_payee"- checked={fallbackMissingPayeeToMemo}- onChange={() => {- setFallbackMissingPayeeToMemo(state => !state);- }}- >- <Trans>Use Memo as a fallback for empty Payees</Trans>- </LabeledCheckbox>+ <>+ <CheckboxToggle+ id="form_fallback_missing_payee"+ checked={fallbackMissingPayeeToMemo}+ onChange={setFallbackMissingPayeeToMemo}+ >+ <Trans>Use Memo as a fallback for empty Payees</Trans>+ </CheckboxToggle>+ <CheckboxToggle+ id="form_ofx_swap_payee_memo"+ checked={ofxSwapPayeeAndMemo}+ onChange={setOfxSwapPayeeAndMemo}+ >+ <Trans>Swap Payee and Memo</Trans>+ </CheckboxToggle>+ </>)}{filetype !== 'csv' && (- <LabeledCheckbox+ <CheckboxToggleid="import_notes"checked={importNotes}- onChange={() => {- setImportNotes(!importNotes);- }}+ onChange={setImportNotes}><Trans>Import notes from file</Trans>- </LabeledCheckbox>+ </CheckboxToggle>+ )}++ {filetype === 'qif' && (+ <CheckboxToggle+ id="form_qif_swap_payee_memo"+ checked={qifSwapPayeeAndMemo}+ onChange={setQifSwapPayeeAndMemo}+ >+ <Trans>Swap Payee and Memo</Trans>+ </CheckboxToggle>+ )}++ {isCamtFile(filetype) && (+ <CheckboxToggle+ id="form_camt_swap_payee_memo"+ checked={camtSwapPayeeAndMemo}+ onChange={setCamtSwapPayeeAndMemo}+ >+ <Trans>Swap Payee and Memo</Trans>+ </CheckboxToggle>)}{(isOfxFile(filetype) || isCamtFile(filetype)) && (- <LabeledCheckbox+ <CheckboxToggleid="form_dont_reconcile"checked={reconcile}- onChange={() => {- setReconcile(!reconcile);- }}+ onChange={setReconcile}><Trans>Merge with existing transactions</Trans>- </LabeledCheckbox>+ </CheckboxToggle>)}{/*Import Options */}@@ -1079,33 +1166,27 @@ export function ImportTransactionsModal({style={{ width: 50 }}/></label>- <LabeledCheckbox+ <CheckboxToggleid="form_has_header"checked={hasHeaderRow}- onChange={() => {- setHasHeaderRow(!hasHeaderRow);- }}+ onChange={setHasHeaderRow}><Trans>File has header row</Trans>- </LabeledCheckbox>- <LabeledCheckbox+ </CheckboxToggle>+ <CheckboxToggleid="clear_on_import"checked={clearOnImport}- onChange={() => {- setClearOnImport(!clearOnImport);- }}+ onChange={setClearOnImport}><Trans>Clear transactions on import</Trans>- </LabeledCheckbox>- <LabeledCheckbox+ </CheckboxToggle>+ <CheckboxToggleid="form_dont_reconcile"checked={reconcile}- onChange={() => {- setReconcile(!reconcile);- }}+ onChange={setReconcile}><Trans>Merge with existing transactions</Trans>- </LabeledCheckbox>+ </CheckboxToggle></View>)}@@ -1113,15 +1194,13 @@ export function ImportTransactionsModal({<View style={{ marginRight: 10, gap: 5 }}><SectionLabel title={t('AMOUNT OPTIONS')} />- <LabeledCheckbox+ <CheckboxToggleid="form_flip"checked={flipAmount}- onChange={() => {- setFlipAmount(!flipAmount);- }}+ onChange={setFlipAmount}><Trans>Flip amount</Trans>- </LabeledCheckbox>+ </CheckboxToggle><MultiplierOptionmultiplierEnabled={multiplierEnabled}multiplierAmount={multiplierAmount}@@ -1205,17 +1284,43 @@ function getParseOptions(fileType: string, options: ParseFileOptions = {}) {return { delimiter, hasHeaderRow, skipStartLines, skipEndLines };}if (isOfxFile(fileType)) {- const { fallbackMissingPayeeToMemo, importNotes } = options;- return { fallbackMissingPayeeToMemo, importNotes };+ const { fallbackMissingPayeeToMemo, importNotes, swapPayeeAndMemo } =+ options;+ return { fallbackMissingPayeeToMemo, importNotes, swapPayeeAndMemo };+ }+ if (fileType === 'qif') {+ const { importNotes, swapPayeeAndMemo } = options;+ return { importNotes, swapPayeeAndMemo };}if (isCamtFile(fileType)) {- const { importNotes } = options;- return { importNotes };+ const { importNotes, swapPayeeAndMemo } = options;+ return { importNotes, swapPayeeAndMemo };}const { importNotes } = options;return { importNotes };}+function getSwapOption(+ fileType: string,+ ofxSwapPayeeAndMemo: boolean,+ qifSwapPayeeAndMemo: boolean,+ camtSwapPayeeAndMemo: boolean,+) {+ if (isOfxFile(fileType)) {+ return ofxSwapPayeeAndMemo;+ }++ if (fileType === 'qif') {+ return qifSwapPayeeAndMemo;+ }++ if (isCamtFile(fileType)) {+ return camtSwapPayeeAndMemo;+ }++ return false;+}+function isOfxFile(fileType: string) {return fileType === 'ofx' || fileType === 'qfx';}diff --git a/packages/loot-core/src/mocks/files/camt/camt.053.payee-memo.xml b/packages/loot-core/src/mocks/files/camt/camt.053.payee-memo.xmlnew file mode 100644index 0000000000..8255bef086--- /dev/null+++ b/packages/loot-core/src/mocks/files/camt/camt.053.payee-memo.xml@@ -0,0 +1,127 @@+<?xml version="1.0" encoding="UTF-8"?>+<Document xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.053.001.04 camt.053.001.04.xsd"+ xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.04"+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">++ <BkToCstmrStmt>+ <GrpHdr>+ <MsgId>053D2019-01-23T00:00:00.0N000000001</MsgId>+ <CreDtTm>2019-01-23T00:00:00.0+00:00</CreDtTm>+ </GrpHdr>+ <Stmt>+ <Id>TEST-PAYEE-MEMO</Id>+ <CreDtTm>2019-01-23T00:00:00.0+00:00</CreDtTm>+ <Acct>+ <Id>+ <IBAN>DE14740618130000033626</IBAN>+ </Id>+ <Ccy>EUR</Ccy>+ </Acct>++ <!-- 1: payee (Cdtr/Nm) AND notes (RmtInf/Ustrd) -->+ <Ntry>+ <Amt Ccy="EUR">10.00</Amt>+ <CdtDbtInd>DBIT</CdtDbtInd>+ <Sts>BOOK</Sts>+ <BookgDt>+ <Dt>2019-01-23</Dt>+ </BookgDt>+ <ValDt>+ <Dt>2019-01-23</Dt>+ </ValDt>+ <AcctSvcrRef>test-payee-and-notes</AcctSvcrRef>+ <BkTxCd />+ <NtryDtls>+ <TxDtls>+ <Refs>+ <EndToEndId>test-payee-and-notes</EndToEndId>+ </Refs>+ <RltdPties>+ <Cdtr>+ <Nm>Payee Name</Nm>+ </Cdtr>+ </RltdPties>+ <RmtInf>+ <Ustrd>Note Text</Ustrd>+ </RmtInf>+ </TxDtls>+ </NtryDtls>+ </Ntry>++ <!-- 2: payee only (Cdtr/Nm, no RmtInf) -->+ <Ntry>+ <Amt Ccy="EUR">20.00</Amt>+ <CdtDbtInd>DBIT</CdtDbtInd>+ <Sts>BOOK</Sts>+ <BookgDt>+ <Dt>2019-01-23</Dt>+ </BookgDt>+ <ValDt>+ <Dt>2019-01-23</Dt>+ </ValDt>+ <AcctSvcrRef>test-payee-only</AcctSvcrRef>+ <BkTxCd />+ <NtryDtls>+ <TxDtls>+ <Refs>+ <EndToEndId>test-payee-only</EndToEndId>+ </Refs>+ <RltdPties>+ <Cdtr>+ <Nm>Payee Name Only</Nm>+ </Cdtr>+ </RltdPties>+ </TxDtls>+ </NtryDtls>+ </Ntry>++ <!-- 3: notes only (RmtInf/Ustrd, no Cdtr/Nm) -->+ <Ntry>+ <Amt Ccy="EUR">30.00</Amt>+ <CdtDbtInd>DBIT</CdtDbtInd>+ <Sts>BOOK</Sts>+ <BookgDt>+ <Dt>2019-01-23</Dt>+ </BookgDt>+ <ValDt>+ <Dt>2019-01-23</Dt>+ </ValDt>+ <AcctSvcrRef>test-notes-only</AcctSvcrRef>+ <BkTxCd />+ <NtryDtls>+ <TxDtls>+ <Refs>+ <EndToEndId>test-notes-only</EndToEndId>+ </Refs>+ <RmtInf>+ <Ustrd>Note Only Text</Ustrd>+ </RmtInf>+ </TxDtls>+ </NtryDtls>+ </Ntry>++ <!-- 4: neither payee nor notes -->+ <Ntry>+ <Amt Ccy="EUR">40.00</Amt>+ <CdtDbtInd>DBIT</CdtDbtInd>+ <Sts>BOOK</Sts>+ <BookgDt>+ <Dt>2019-01-23</Dt>+ </BookgDt>+ <ValDt>+ <Dt>2019-01-23</Dt>+ </ValDt>+ <AcctSvcrRef>test-neither</AcctSvcrRef>+ <BkTxCd />+ <NtryDtls>+ <TxDtls>+ <Refs>+ <EndToEndId>test-neither</EndToEndId>+ </Refs>+ </TxDtls>+ </NtryDtls>+ </Ntry>++ </Stmt>+ </BkToCstmrStmt>+</Document>diff --git a/packages/loot-core/src/mocks/files/data-payee-memo.ofx b/packages/loot-core/src/mocks/files/data-payee-memo.ofxnew file mode 100644index 0000000000..85d56d414d--- /dev/null+++ b/packages/loot-core/src/mocks/files/data-payee-memo.ofx@@ -0,0 +1,75 @@+OFXHEADER:100+DATA:OFXSGML+VERSION:102+SECURITY:NONE+ENCODING:USASCII+CHARSET:1252+COMPRESSION:NONE+OLDFILEUID:NONE+NEWFILEUID:NONE++<OFX>+<SIGNONMSGSRSV1>+<SONRS>+<STATUS>+<CODE>0+<SEVERITY>INFO+</STATUS>+<DTSERVER>20190124212851.000[0:UTC]+<LANGUAGE>ENG+</SONRS>+</SIGNONMSGSRSV1>+<BANKMSGSRSV1>+<STMTTRNRS>+<TRNUID>0+<STATUS>+<CODE>0+<SEVERITY>INFO+</STATUS>+<STMTRS>+<CURDEF>USD+<BANKACCTFROM>+<BANKID>012345678+<ACCTID>123456789123+<ACCTTYPE>CHECKING+</BANKACCTFROM>+<BANKTRANLIST>+<DTSTART>20190119120000+<DTEND>20190124120000+<STMTTRN>+<TRNTYPE>DEBIT+<DTPOSTED>20190123120000+<TRNAMT>-10.00+<FITID>test-name-and-memo+<NAME>Payee Name+<MEMO>Memo Text+</STMTTRN>+<STMTTRN>+<TRNTYPE>DEBIT+<DTPOSTED>20190123120000+<TRNAMT>-20.00+<FITID>test-name-only+<NAME>Payee Name Only+</STMTTRN>+<STMTTRN>+<TRNTYPE>DEBIT+<DTPOSTED>20190123120000+<TRNAMT>-30.00+<FITID>test-memo-only+<MEMO>Memo Only Text+</STMTTRN>+<STMTTRN>+<TRNTYPE>DEBIT+<DTPOSTED>20190123120000+<TRNAMT>-40.00+<FITID>test-neither+</STMTTRN>+</BANKTRANLIST>+<LEDGERBAL>+<BALAMT>1000.00+<DTASOF>20190124212851+</LEDGERBAL>+</STMTRS>+</STMTTRNRS>+</BANKMSGSRSV1>+</OFX>diff --git a/packages/loot-core/src/mocks/files/data-payee-memo.qif b/packages/loot-core/src/mocks/files/data-payee-memo.qifnew file mode 100644index 0000000000..a91587e41b--- /dev/null+++ b/packages/loot-core/src/mocks/files/data-payee-memo.qif@@ -0,0 +1,17 @@+!Type:Bank+D01/23/2019+PPayee Name+MNote Text+T-10.00+^+D01/23/2019+PPayee Name Only+T-20.00+^+D01/23/2019+MNote Only Text+T-30.00+^+D01/23/2019+T-40.00+^diff --git a/packages/loot-core/src/server/transactions/import/parse-file.ts b/packages/loot-core/src/server/transactions/import/parse-file.tsindex 101056e5ce..6793e5751a 100644--- a/packages/loot-core/src/server/transactions/import/parse-file.ts+++ b/packages/loot-core/src/server/transactions/import/parse-file.ts@@ -68,6 +68,7 @@ export type ParseFileOptions = {hasHeaderRow?: boolean;delimiter?: string;fallbackMissingPayeeToMemo?: boolean;+ swapPayeeAndMemo?: boolean;skipStartLines?: number;skipEndLines?: number;importNotes?: boolean;@@ -172,16 +173,26 @@ async function parseQIF(return { errors, transactions: [] };}+ const swap = options.swapPayeeAndMemo;+return {errors: [],transactions: data.transactions- .map(trans => ({- amount: trans.amount != null ? looselyParseAmount(trans.amount) : null,- date: trans.date,- payee_name: trans.payee,- imported_payee: trans.payee,- notes: options.importNotes ? trans.memo || null : null,- }))+ .map(trans => {+ const payeeSource = swap ? trans.memo : trans.payee;+ const memoSource = swap ? trans.payee : trans.memo;+ const fallbackUsed = !payeeSource && swap;++ return {+ amount:+ trans.amount != null ? looselyParseAmount(trans.amount) : null,+ date: trans.date,+ payee_name: payeeSource || (fallbackUsed ? memoSource : null),+ imported_payee: payeeSource || (fallbackUsed ? memoSource : null),+ notes:+ options.importNotes && !fallbackUsed ? memoSource || null : null,+ };+ }).filter(trans => trans.date != null && trans.amount != null),};}@@ -207,6 +218,7 @@ async function parseOFX(// Banks don't always implement the OFX standard properly// If no payee is available try and fallback to memoconst useMemoFallback = options.fallbackMissingPayeeToMemo;+ const swap = options.swapPayeeAndMemo;return {errors,@@ -219,13 +231,17 @@ async function parseOFX(});}+ const payeeSource = swap ? trans.memo : trans.name;+ const memoSource = swap ? trans.name : trans.memo;+ const fallbackUsed = !payeeSource && useMemoFallback;+return {amount: parsedAmount || 0,imported_id: trans.fitId,date: trans.date,- payee_name: trans.name || (useMemoFallback ? trans.memo : null),- imported_payee: trans.name || (useMemoFallback ? trans.memo : null),- notes: options.importNotes ? trans.memo || null : null, //memo used for payee+ payee_name: payeeSource || (fallbackUsed ? memoSource : null),+ imported_payee: payeeSource || (fallbackUsed ? memoSource : null),+ notes: options.importNotes && !fallbackUsed ? memoSource || null : null,};}),};@@ -250,11 +266,21 @@ async function parseCAMT(return { errors };}+ const swap = options.swapPayeeAndMemo;+return {errors,- transactions: data.map(trans => ({- ...trans,- notes: options.importNotes ? trans.notes : null,- })),+ transactions: data.map(trans => {+ const payeeSource = swap ? trans.notes : trans.payee_name;+ const memoSource = swap ? trans.payee_name : trans.notes;+ const fallbackUsed = !payeeSource && swap;++ return {+ ...trans,+ payee_name: payeeSource || (fallbackUsed ? memoSource : null),+ imported_payee: payeeSource || (fallbackUsed ? memoSource : null),+ notes: options.importNotes && !fallbackUsed ? memoSource || null : null,+ };+ }),};}diff --git a/packages/loot-core/src/types/prefs.ts b/packages/loot-core/src/types/prefs.tsindex 1969d5f39e..0fa8f8e1df 100644--- a/packages/loot-core/src/types/prefs.ts+++ b/packages/loot-core/src/types/prefs.ts@@ -46,6 +46,9 @@ export type SyncedPrefs = Partial<| `sync-import-transactions-${string}`| `sync-update-dates-${string}`| `ofx-fallback-missing-payee-${string}`+ | `ofx-swap-payee-memo-${string}`+ | `qif-swap-payee-memo-${string}`+ | `camt-swap-payee-memo-${string}`| `flip-amount-${string}-${'csv' | 'qif'}`| `flags.${FeatureFlag}`| `learn-categories`,diff --git a/upcoming-release-notes/7101.md b/upcoming-release-notes/7101.mdnew file mode 100644index 0000000000..9308fb1ee2--- /dev/null+++ b/upcoming-release-notes/7101.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [sylvercode]+---++Add an option to swap payee/memo when importing transaction from file.