From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stefan Huchler Subject: Re: org-capture template to type in bills from shops in ledger format Date: Sat, 22 Jun 2019 16:14:40 +0200 Message-ID: <87imsxlnan.fsf@mail.de> References: <87woi4rgwf.fsf@mail.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:54242) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hegn4-0005br-DS for emacs-orgmode@gnu.org; Sat, 22 Jun 2019 10:14:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hegn3-00007Y-3d for emacs-orgmode@gnu.org; Sat, 22 Jun 2019 10:14:50 -0400 Received: from [195.159.176.226] (port=54878 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hegn2-00005y-SW for emacs-orgmode@gnu.org; Sat, 22 Jun 2019 10:14:49 -0400 Received: from list by blaine.gmane.org with local (Exim 4.89) (envelope-from ) id 1hegn0-0013a7-PD for emacs-orgmode@gnu.org; Sat, 22 Jun 2019 16:14:46 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org If somebody is interested I uploaded a reworked version of this there: https://github.com/spiderbit/org-capture-ledger-shopping Stefan Huchler writes: > I wrote this template to capture my bills from mostly one shop, but it > has support for multiple shops and the important feature is that it > suggests previous item names and remembers last prices, that gives you > lot's of autocompletion if you repetetivly buy often the same stuff over > and over again. > > #+begin_src emacs-lisp > %(let* ((default-directory (file-name-directory "%F")) > (map-file "shop-items.txt")) > (load-file "helper.el") > (require 'dash) > (let* ((shops (if (file-exists-p map-file) > (read-from-file map-file) '())) > (names (mapcar 'car shops)) > (shop-name (ido-completing-read "Shop: " names nil nil nil)) > (new-prices) (new-names) (new-amounts) > (products (assoc-default shop-name shops))) > (while (let* ((names (mapcar 'car products)) > (name (ido-completing-read "Name: " names)) > (amount (read-number "Amount: " 1)) > (item (alist-get name products)) > (item (if (stringp item) (string-to-number item) item)) > (price (read-number "Price: " (or item 2.00)))) > (setq new-names (append new-names (list name))) > (setq new-prices (append new-prices (list price))) > (setq new-amounts (append new-amounts (list amount))) > (y-or-n-p "More items? "))) > (let* ((-compare-fn (lambda (x y) (equal (car x) (car y)))) > (new-products (mapcar* 'cons new-names new-prices)) > (combined-products (-distinct (append new-products products))) > (combined-shops (-distinct (append `((,shop-name . ,combined-products)) shops))) > (format-string " expenses:food:%s \t\t%s St @ =€%s") > (format-function (lambda (name amount price) > (format format-string name amount price))) > (product-lines (mapcar* format-function new-names > new-amounts new-prices)) > (shopping-items (s-join "\n" product-lines)) > (total (reduce '+ (mapcar* '* new-amounts new-prices)))) > (print-to-file map-file combined-shops) > (concat (format " %(org-read-date nil nil) * %s\n%s" > shop-name shopping-items) > (format "\n assets:bank:chequing\t\t€-%s" > (read-string "Total: " (format "%.2f" total))))))) > #+end_src > > > Any thoughts? It's supposed to output into a org file with a embeded > ledger src block so you would have to check the alignment if you would > want to output to a normal ledger file. > > here are the 2 functions from helper.el: > > #+begin_src emacs-lisp > (defun print-to-file (filename data) > (with-temp-file filename > (let* ((print-length 5000)) > (prin1 data (current-buffer))))) > > (defun read-from-file (filename) > (with-temp-buffer > (insert-file-contents filename) > (cl-assert (eq (point) (point-min))) > (read (current-buffer)))) > #+end_src > > > Maybe that's useful for somebody else or somebody wants to suggest other > features or something. The only thing I would maybe consider to switch > is to use a json format for the shop-items.txt for the prices for easier > manual editing. > > that's how I set it up: > > #+begin_src emacs-lisp > ("s" "(S)hopping" plain > (file+function "path/finances.org" > (lambda () "" > (progn (org-babel-goto-named-src-block "balance") > (org-babel-goto-src-block-head)(forward-line)))) > "%[~/capture-templates/template-name]" > :jump-to-captured t > :empty-lines-after 1 > :immediate-finish nil) > #+end_src