[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/csv2ledger c5b7b0766e 024/190: Rewrite `c2l-compose-entry`
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/csv2ledger c5b7b0766e 024/190: Rewrite `c2l-compose-entry` and `c2l-csv-line-to-ledger`. |
Date: |
Sun, 2 Jun 2024 15:59:42 -0400 (EDT) |
branch: elpa/csv2ledger
commit c5b7b0766e622e7088c0394b764ffa6800938c57
Author: Joost Kremers <joostkremers@fastmail.fm>
Commit: Joost Kremers <joostkremers@fastmail.fm>
Rewrite `c2l-compose-entry` and `c2l-csv-line-to-ledger`.
Adapt both functions to the new user options, which provide more
customisability.
---
csv2ledger.el | 66 +++++++++++++++++++++++++++++++++--------------------------
1 file changed, 37 insertions(+), 29 deletions(-)
diff --git a/csv2ledger.el b/csv2ledger.el
index 179933ad63..245daddf54 100644
--- a/csv2ledger.el
+++ b/csv2ledger.el
@@ -145,7 +145,6 @@ returns a match wins."
(defvar c2l--accounts nil "List of ledger accounts, mainly used for
completion.")
(defvar c2l--compiled-matcher-regexes nil "Alist of accounts and their
matchers.")
-(defun c2l-compose-entry (date title amount &optional description from to)
(defun c2l-convert-little-endian-to-iso8601-date (date)
"Convert DATE from a little-endian format to an ISO 8601 format.
DATE should be a string representing a date of the form
@@ -172,22 +171,35 @@ returned, otherwise the payee is returned."
sender
payee)))
+(defun c2l-compose-entry (items &optional from to)
"Create a ledger entry.
-DATE, TITLE, AMOUNT are used to create the entry. DESCRIPTION,
-if non-nil, is added as a comment, preceded by \"Desc:\". FROM
-is the account where the money comes from, TO the account to
+ITEMS is an alist containing (key . value) pairs that should be
+included in the entry. It should contain values for the keys
+`date', `payee', `sender' and `amount'. ITEMS may also contain a
+value for `description'. If it is present, it is added as a
+comment, preceded by \"Desc:\".
+
+FROM is the account where the money comes from, TO the account to
which it goes. Note that if AMOUNT is negative, these roles are
-reversed."
- ;; TODO Fix doc string
+reversed. FROM and TO default to `c2l-fallback-account' and
+`c2l-base-account', respectively."
+ (or from (setq from c2l-fallback-account))
(or to (setq to c2l-base-account))
- (setq amount (c2l-parse-amount amount))
- (let ((width ledger-post-amount-alignment-column))
- (concat (c2l-parse-date date) " * " title "\n"
- (if description (format " ; Desc: %s\n" description) "")
- (format " %s\n" (or from c2l-fallback-account))
- (format " %s " to)
- (make-string (- width 4 (length to) 2 (length amount)) ?\s)
- amount "\n")))
+ (let* ((width ledger-post-amount-alignment-column)
+ (parsed-items (mapcar (lambda (item)
+ (let ((field (car item))
+ (value (cdr item)))
+ (cons field
+ (funcall (alist-get field
c2l-field-parse-functions #'identity) value))))
+ items))
+ (title (funcall c2l-title-function parsed-items)))
+ (let-alist parsed-items
+ (concat .date (if .valuation (format "=%s " .valuation) "") (if
c2l-auto-reconcile " *" "") " " title "\n"
+ (if (and .description (not (string-empty-p .description)))
(format " ; Desc: %s\n" .description) "")
+ (format " %s\n" from)
+ (format " %s " to)
+ (make-string (- width 4 (length to) 2 (length .amount)) ?\s)
+ .amount "\n"))))
(defun c2l-read-accounts (file)
"Read list of accounts from FILE."
@@ -241,21 +253,17 @@ for that account."
(defun c2l-csv-line-to-ledger (row)
"Convert ROW to a ledger entry.
-ROW is a list of strings and should have the following elements:
-
-Date Valuation Type Description Sender Payee Amount Balance
-
-Valuation and Balance are ignored, the other elements are used to
-create the ledger entry."
- (pcase-let* ((`(,date ,_ ,type ,description ,sender ,payee ,amount ,_) row)
- (title (if (and (stringp c2l-account-holder)
- (string-match-p c2l-account-holder payee))
- sender
- payee))
- (account (or (seq-some #'c2l-match-account (list description
payee type sender))
- (completing-read (format "Account for transaction
%s, %s «%.75s» " title amount description) c2l--accounts nil t)))
- (entry (c2l-compose-entry date title amount description
account)))
- entry))
+ROW contains the data of the entry as a list of strings. The
+strings are interpreted according to the template in
+`c2l-csv-columns'."
+ (let* ((fields (--remove (eq (car it) '_) (-zip-pair c2l-csv-columns row)))
+ (account (or (-some #'c2l-match-account (mapcar #'cdr (--filter (memq
(car it) c2l-title-match-fields) fields)))
+ (completing-read (format "Account for transaction %s, %s
«%.75s» "
+ (funcall c2l-title-function
fields)
+ (alist-get 'amount fields)
+ (alist-get 'description fields))
+ c2l--accounts nil t))))
+ (c2l-compose-entry fields account)))
;;;###autoload
(defun c2l-csv-entry-as-kill ()
- [nongnu] elpa/csv2ledger 3b203a66e1 118/190: Improve code layout., (continued)
- [nongnu] elpa/csv2ledger 3b203a66e1 118/190: Improve code layout., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 64da760597 119/190: Update copyright year., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 98ab50c9a7 185/190: New command c2l-read-account-matchers, ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger e744d0706f 186/190: Update version number., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 188ddacde7 057/190: Rename target account to balancing account., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger a1b0dd35e1 053/190: Update README., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 88c394d5bb 066/190: Rename c2l-payee-or-sender to c2l-title-is-payee-or-sender., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger e8a7609736 050/190: Add user option `c2l-alignment-column`., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 0af0aa3de2 021/190: Rename `c2l-parse-date` and update doc string., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 900eef1457 017/190: Move definition of `c2l-account-matchers-file`., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger c5b7b0766e 024/190: Rewrite `c2l-compose-entry` and `c2l-csv-line-to-ledger`.,
ELPA Syncer <=
- [nongnu] elpa/csv2ledger 6597b8cd22 003/190: Initial commit of csv2ledger.el, ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 1ce1fe5297 005/190: Remove BSD license from header, point to GNU license., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger f19910d17b 099/190: Add option c2l-entry-function., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger aa55c01cc1 106/190: New user option c2l-transaction-modify-functions., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger d1910389f1 100/190: Rename c2l-title-is-payee-or-sender to c2l-payee-or-sender., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 7cc0cd1a0a 029/190: Warn if trying to read a file that cannot be found., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 59ba841805 086/190: Guard against empty payee and sender in c2l-title-is-payee-or-sender, ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 6076653a0f 084/190: Rename local variable in c2l--csv-line-to-ledger., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger af1129653d 178/190: Make auto-clearing of transactions with effective date optional., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 66d5a39b99 116/190: Replace point-at-{bol|eol} with pos-{bol|eol}., ELPA Syncer, 2024/06/03