by TeMPOraL · 7y ago · 0 comments
For instance, for my freelancing I track both tasks and time spent in Org Mode. I wrote myself a little bit of Elisp that, when invoked with the name of a customer, turns my Org Mode agenda into a table with line items for an invoice. I've been copying that into my invoice LaTeX template for a while, but recently I've been working on finishing the Elisp code: the end goal is to have a three-step process:
1) Press a key to generate a table with invoice line items and another with all other data that goes into the invoice.
2) Review the table, adjusting as necessary (e.g. rounding off stray minutes).
3) Press a key for Emacs to take these tables, fill a LaTeX template according to their contents, generate an invoice and file it in appropriate location.
At this point I have 1) and 2) fully working, and 3) missing only the "fill template" and "refile result" steps, which are rather trivial to add.
I'll release this publicly when I'm done. The current solution is somewhat specific to invoicing for services according to my country's regulations, but it shouldn't be too hard to make it universal (I'd love some help here, especially in form of examples of invoices for other countries).
Did I spend more time on it that I would if I used a web tool for invoices? Yes. Do I expect to ultimately get ahead on this solution? Maybe. Is it worth it? The interface (read: an org-mode document invoking some elisp) is already more fluid than any web tool I've ever seen, and I don't need to be on-line - so a huge YES!
As a non-Emacs user I do something similar with a bit of Bash scripting. It doesn't go as far as generating a ready to send invoice, but it will sum up the hours worked and provide a $ amount as output so I know how much to invoice someone.
It parses a log.md file I keep for each client.
The file looks like this, and the first number after the # is the hours worked.
---
# 4 2019/01/28
- Made something cool
- Helped XYZ do ABC
# 1 2019/02/11
- Foobar
- Yada yada yada
# 8.5 2019/02/12
- Lots of good stuff!
---
From here it's just a couple of piped Unix commands to generate the output I'm interested in.
You got me to thinking though. I wonder if I can generate a 100% ready to go PDF invoice. That would save a lot of tedious and error prone steps where I manually create each invoice based off a template in Google docs.
> I wonder if I can generate a 100% ready to go PDF invoice. That would save a lot of tedious and error prone steps where I manually create each invoice based off a template in Google docs.
This is my goal too. In parallel comment, I pasted an example table with line items. I wrote the code making that table some 9 months ago, and I've been retyping values into a LaTeX invoice ever since. I finally decided this is too annoying and error-prone to continue, and for the past couple weeks I've been slowly building up an end-to-end system, but with an explicit review step (I don't trust my own code that much).
As for time tracking format, the standard org-mode clocking feature appends a block under your TODO tasks that looks like this:
:LOGBOOK:
CLOCK: [2019-02-07 Thu 10:44]--[2019-02-07 Thu 13:37] => 2:53
CLOCK: [2019-02-06 Wed 14:27]--[2019-02-06 Wed 15:00] => 0:33
:END:
This block (called a "drawer") is automatically folded, so it doesn't clutter your org file as you work with it, but it's there in the plaintext. As I work, I mark a given TODO item as "active", either directly or via org-mode agenda, and when I'm done, I "clock out"; Emacs populates the :LOGBOOK: entries automatically. | Tag | Invoice line name | Hours | Rate | Net | VAT % | VAT amount | Total |
|-----------------+-----------------------+-------+-------+--------+-------+------------+--------|
| meeting | Meetings | 10 | 12.00 | 120.00 | NP | 0.00 | 120.00 |
| procrastinating | Important tasks | 20 | 13.00 | 260.00 | NP | 0.00 | 260.00 |
| hn | Other important tasks | 40 | 14.00 | 560.00 | NP | 0.00 | 560.00 |
| coding | Occasional code | 3 | 15.00 | 45.00 | NP | 0.00 | 45.00 |
|-----------------+-----------------------+-------+-------+--------+-------+------------+--------|
| Totals | - | 73 | - | 985.00 | - | 0.00 | 985.00 |
#+TBLFM: @>$3=vsum(@2..@-1)::@>$5=vsum(@2..@-1);%.2f::@>$7=vsum(@2..@-1);%.2f::@>$8=vsum(@2..@-1);%.2f::$5=$3*$4;%.2f::$7=if(typeof($6) < 10, $5*$6, 0);%.2f::$8=$7+$5;%.2f
The #+TBLFM: line at the bottom is org-mode spreadsheet functionality; it ensures I can modify the table text in-place, and just press C-c C-c to recalculate all values in it. I generate a similar table with data for invoices; some taken from my configuration, some computed (e.g. invoice number is generated by looking at filenames of previous invoices; sale date and service period are estimated from current date and the time range you specified when generating line items table, etc.).The code for generating above table is hand-crafted. While org-mode provides you a generator for agenda tables based on TODO item name - see [1], I had to write similar feature that aggregated time by org-mode tags. I used [2] as a reference, but essentially wrote my own implementation. Surprisingly, it turned out to be almost trivial.
Then, it's simply a matter of turning those two org-mode tables into an elisp list of keys and values. This I accomplish using a simple elisp that uses built-in Emacs calls:
(defun trc-invoicing--named-table-to-lisp (name)
"Convert a named org mode table in the current buffer into a list.
The function finds a table named `NAME', passes it through `ORG-TABLE-TO-LISP',
and return the result. Refer to the documentation of `ORG-TABLE-TO-LISP'
for info about result's shape."
(save-excursion
(goto-char (point-min))
(search-forward (format "#+NAME: %s" name))
(forward-line)
(org-table-to-lisp)))
Then it's a matter of populating a LaTeX template with extracted values, invoking pdflatex, and copying the resulting PDF in an appropriate place. That last part I yet have to finish.It's the biggest piece of elisp I wrote since Nyan Mode, but all in all, I find it surprisingly easy to code for Emacs. Availability of source code, extensive in-editor documentation and built-in source-level debugger make this process pretty pleasant.
--
[0] - Org Mode has a concept of "dynamic blocks". You type in #+BEGIN: blockname some params, and then using C-c C-c on it causes Org Mode to invoke an elisp function named org-dblock-write:blockname. The called function is then responsible for outputting contents of the dynamic block.
To be clear, the TBLFM there has a separate purpose. My implementation of [2] generates a table with appropriate shape and data items; TBLFM computes sums and products. This allows for manually modifying the table (e.g. adding a line item, rounding off hours, changing them to include tine you don't track in org mode) and having the sums and products recalculated.
(Also, for anyone scared by that TBLFM line noise, Emacs has a way to edit it in more readable form.)