emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Alan Schmitt <alan.schmitt-o/5/jSaJEHk+NdeTPqioyti2O/JbrIOy@public.gmane.org>
To: Erik Hetzner <egh-r1v5srsr4wc@public.gmane.org>
Cc: ledger-cli-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org,
	Org Mode <emacs-orgmode-mXXj517/zsQ@public.gmane.org>
Subject: Re: Weaving a budget with Org & ledger
Date: Fri, 18 Apr 2014 14:55:24 +0200	[thread overview]
Message-ID: <m2eh0u4x1f.fsf@polytechnique.org> (raw)
In-Reply-To: <87k3cp9i2a.wl%egh-r1v5srsr4wc@public.gmane.org> (Erik Hetzner's message of "Thu, 20 Feb 2014 20:59:57 -0800")

Hi,

On 2014-02-21 05:59, Erik Hetzner <egh-r1v5srsr4wc@public.gmane.org> writes:

> Users of ledger and Org may be interested in this tutorial on how I
> manage an envelope style budget with those two excellent tools.
>
>   http://orgmode.org/worg/org-tutorials/weaving-a-budget.html

Thanks to this nice description, I migrated my finances to ledger since
the beginning of March. I've since written a couple extensions to track
expenses, what remains in the envelopes (that I call "buckets"), and to
generate monthly budgets. They make extensive use of the babel features
of org mode (all the following should be in an orgmode file).

First, I wrote a function that does an external call to ledger and
returns the last value. It's used to get a balance for a given period.
The `bucketp' boolean indicates whether we want information about the
bucket (Bucket:Expenses:Foo) or the expenses themselves (Expenses:Foo).

--8<---------------cut here---------------start------------->8---
#+name: call_ledger
#+begin_src emacs-lisp :var lcmd="bal" :var bucket="Quotidien" :var period=() :var bucketp=()
  (let* ((name (org-babel-trim bucket))
         (bname (concat "'^" (when bucketp "Bucket:") "Expenses:" name "'"))
         (ledger "ledger -f ~/Documents/Org/mescomptes.ledger")
         (parg (when period (concat " -p '" period "'")))
         (cutcmd "tail -1 | cut -d ' ' -f 2")
         (cmd 
          (concat ledger " -J " parg " " lcmd " " bname " | " cutcmd))
         (res (org-babel-trim (shell-command-to-string cmd))))
    (if (equal res "") 0 res))
#+end_src
--8<---------------cut here---------------end--------------->8---

Second, a function to get an average spending since a given date.
I compute a monthly average, based on the numbers of days since the
starting date.

--8<---------------cut here---------------start------------->8---
#+name: monthly_average_since
#+begin_src emacs-lisp :var start-date="2014-03-01" :var amount=100
  (let ((nbdays (- (time-to-days (current-time))
                   (time-to-days (org-read-date nil t start-date)))))
    (calc-eval "round($ / (12 * ($$ / 365.25)), 2)" nil amount nbdays))
#+end_src

#+name: ledger_average
#+begin_src emacs-lisp :var b="Quotidien" :var sd="2014-03-01"
  (let ((a (org-sbe call_ledger (bucket (eval b)))))
    (org-sbe monthly_average_since (amount (eval a)) (start-date (eval sd))))
#+end_src
--8<---------------cut here---------------end--------------->8---

Using these functions, I can write a table where I input the bucket
names and the monthly budget I want (first two columns). The table
formula then computes the remaining amount in the buckets, the actual
spending for this month, the spending for last month, and the average
spending since march 2014 (last four columns).

#+name: budget
| Bucket                   | Planned | Remaining | This Month | Last Month | Average |
|--------------------------+---------+-----------+------------+------------+---------|
| Foo                      |    1200 |      18.7 |    1196.44 |    1196.44 | 1549.64 |
| Bar                      |      85 |    254.63 |          0 |       17.4 |   11.27 |
|--------------------------+---------+-----------+------------+------------+---------|
| Total                    |         |           |            |            |         |
#+TBLFM: @>$2..@>$6=vsum(@2..@-1)::@2$3..@>>$3='(org-sbe call_ledger (bucket (concat "\"" $1 "\"")) (bucketp (eval t)))::@2$4..@>>$4='(org-sbe call_ledger (bucket (concat "\"" $1 "\"")) (period "\"this month\""))::@2$5..@>>$5='(org-sbe call_ledger (bucket (concat "\"" $1 "\"")) (period "\"last month\""))::@2$6..@>>$6='(org-sbe ledger_average (b (concat "\"" $1 "\"")))

Using this, I can track my expenses, see how much I've still to spend,
and study the consistency between the actual spending and the budget.

I then use the table to create my monthly filling of buckets with the
function (the "Bucket:Unallocated:EUR" is where I put my income, before
it's allocated to some bucket):

--8<---------------cut here---------------start------------->8---
#+name: ledger_budget
#+BEGIN_SRC emacs-lisp :results output :var table=budget :var year=2014 :var month=04
  (princ (format "%d-%02d-01 * Budget %d %02d\n" year month year month))
  (mapcar
   (lambda (tuple)
     (princ (format "    Bucket:Expenses:%s    %d €\n" (car tuple) (cadr tuple))))
   (butlast (cdr table) 1))
  (princ "    Bucket:Unallocated:EUR\n")
#+END_SRC
--8<---------------cut here---------------end--------------->8---

Then I can evaluate the following call line, which generates the budget
that I copy and paste to my ledger file.

--8<---------------cut here---------------start------------->8---
#+call: ledger_budget(year=2014, month=04) :wrap src ledger

#+results:
#+BEGIN_src ledger
2014-04-01 * Budget 2014 04
    Bucket:Expenses:Foo    1200 €
    Bucket:Expenses:Bar    85 €
    Bucket:Unallocated:EUR
#+END_src
--8<---------------cut here---------------end--------------->8---

I hope this will be useful to someone, and I want to seize the occasion
to thank everyone on the ledger and orgmode list for putting up with the
many questions I had when I was writing this. I'll gladly take any
suggestion for improving all this.

Best,

Alan

-- 

--- 
You received this message because you are subscribed to the Google Groups "Ledger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ledger-cli+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/d/optout.

      parent reply	other threads:[~2014-04-18 12:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-21  4:59 Weaving a budget with Org & ledger Erik Hetzner
2014-02-21  7:56 ` Alan Schmitt
     [not found] ` <87k3cp9i2a.wl%egh-r1v5srsr4wc@public.gmane.org>
2014-04-18 12:55   ` Alan Schmitt [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2eh0u4x1f.fsf@polytechnique.org \
    --to=alan.schmitt-o/5/jsajehk+ndetpqioyti2o/jbrioy@public.gmane.org \
    --cc=egh-r1v5srsr4wc@public.gmane.org \
    --cc=emacs-orgmode-mXXj517/zsQ@public.gmane.org \
    --cc=ledger-cli-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).