emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/csv2ledger c558f2bf40 176/190: Add alternative account cre


From: ELPA Syncer
Subject: [nongnu] elpa/csv2ledger c558f2bf40 176/190: Add alternative account creation function c2l-create-account-ask-matcher
Date: Sun, 2 Jun 2024 15:59:56 -0400 (EDT)

branch: elpa/csv2ledger
commit c558f2bf408f539ff16873259b20d69408aa5332
Author: Joost Kremers <joostkremers@fastmail.com>
Commit: Joost Kremers <joostkremers@fastmail.com>

    Add alternative account creation function c2l-create-account-ask-matcher
---
 README.md     |  4 ++++
 csv2ledger.el | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/README.md b/README.md
index 4b2ee1849f..827404ad1a 100644
--- a/README.md
+++ b/README.md
@@ -229,6 +229,10 @@ Another important point to note is that the amount in the 
`amount` field must be
 
 The third function in `c2l-transaction-modify-functions` is 
`c2l-create-account`. This is the function that checks the fields of the 
transaction against the account matchers, and if one is not found, uses 
`c2l-fallback-account` or asks the user. If you wish to use a different method 
to set the account, you can replace this function with a custom one. It needs 
to add an `account` field to the transaction, but there are no restrictions on 
how the account is determined.
 
+`csv2ledger` provides an alternative function for creating an account: 
`c2l-create-account-ask-matcher`. It also checks the transaction against the 
account matchers, but if no match is found, `c2l-fallback-account` is not used. 
Instead, you are asked for an account and also whether you want to add a 
matcher for it. If you say yes, you are asked to provide a string to match 
against. The new matcher is added to your account matchers file and is also 
immediately available for further transa [...]
+
+If you want to use this function, remove `c2l-create-account` from 
`c2l-transaction-modify-functions` and replace it with 
`c2l-create-account-ask-matcher` to it.
+
 
 ### Creating the entry ###
 
diff --git a/csv2ledger.el b/csv2ledger.el
index 3cdda3224f..8ae49e91af 100644
--- a/csv2ledger.el
+++ b/csv2ledger.el
@@ -345,6 +345,46 @@ method yields an account, ask the user."
     (push (cons 'account account) transaction)
     transaction))
 
+(defun c2l-create-account-ask-matcher (transaction)
+  "Get the balancing account for TRANSACTION.
+First check if the account matchers provide a balancing account.
+If not, ask the user for an account and ask if they want to
+create an account matcher for it."
+  (let ((account (seq-some #'c2l--match-account
+                           (mapcar #'cdr
+                                   (seq-filter (lambda (e) (memq (car e) 
c2l-target-match-fields))
+                                               transaction)))))
+    (when (not account)
+      (setq account (completing-read (format "Account for transaction %s, %s 
«%.75s» "
+                                             (alist-get 'title transaction 
"Unknown payee")
+                                             (alist-get 'amount transaction 
"0.00")
+                                             (alist-get 'description 
transaction "?"))
+                                     c2l--accounts))
+      (if (y-or-n-p "Add a matcher for this account? ")
+          ;; If the user changes their mind, we still want to continue
+          ;; processing the transaction, so we capture quit.
+          (condition-case nil
+              (let ((matcher (read-string (format "Matcher for «%s»: " 
account))))
+                (when (and matcher (not (string-empty-p matcher)))
+                  (c2l-add-matcher matcher account c2l-account-matchers-file)
+                  (push (cons (regexp-opt (list matcher)) account) 
c2l-matcher-regexps)))
+            (quit nil))))
+    (push (cons 'account account) transaction)
+    transaction))
+
+(defun c2l-add-matcher (matcher account file)
+  "Add MATCHER to FILE.
+MATCHER will be linked to ACCOUNT."
+  (if (not (and (stringp file)
+                (file-writable-p file)))
+      (user-error "[Csv2Ledger] Cannot write to account matchers file `%s'" 
file)
+    (with-temp-buffer
+      (insert-file-contents file)
+      (goto-char (point-max))
+      (ensure-empty-lines 0)            ; Make sure we're on a new, empty line.
+      (insert (format "%s\t%s\n" matcher account))
+      (write-region (point-min) (point-max) file))))
+
 (defun c2l-compose-entry (transaction)
   "Create a ledger entry.
 TRANSACTION is an alist containing (key . value) pairs that will



reply via email to

[Prev in Thread] Current Thread [Next in Thread]