emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ross Patterson <me@rpatterson.net>
To: emacs-orgmode@gnu.org
Subject: [PATCH] Add a flat clocktable formatter useful for tables whose CSV export can be imported into accounting/invoiving tools such as Zoho Invoicing.
Date: Thu, 27 Jun 2013 21:20:20 -0700	[thread overview]
Message-ID: <874nci7tob.fsf@rpatterson.net> (raw)


---
 lisp/org-clock.el |   77 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 8ac215e..bad653e 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -325,6 +325,14 @@ play with them."
   :group 'org-clocktable
   :type 'plist)
 
+(defcustom org-clock-clocktable-flat-columns
+  '("Invoice Date" "Invoice Number" "Customer Name"
+    "Item Name" "Item Desc" "Quantity" "Item Price")
+  "Columns for the org-clocktable-write-flat clocktable formatter.
+Default value matches the ZOHO CSV import format."
+  :group 'org-clocktable
+  :type '(repeat string))
+
 (defcustom org-clock-idle-time nil
   "When non-nil, resolve open clocks if the user is idle more than X minutes."
   :group 'org-clock
@@ -2546,6 +2554,75 @@ from the dynamic block definition."
       (org-table-delete-column))
     total-time))
 
+(defun org-clocktable-write-flat (ipos tables params)
+  "Write out a flat clock table at position IPOS in the current
+buffer.  Useful to render a table which can be exported to CSV and
+then imported into accounting/invoiving tools such as Zoho Invoicing.
+TABLES is a list of tables with clocking data as produced by
+`org-clock-get-table-data'.
+
+PARAMS is the parameter property list obtained from the dynamic block
+definition.  Several parameters are used beyond those used by the
+clocktable dynamic block:
+    :customer A string to be inserted into the 'Customer Name' column
+    :price A number to be inserted into the 'Item Price' column as a rate
+    :number A string to be inserter into the Invoice Number column
+    :columns A list which overrides the default CSV columns from
+             `org-clock-clocktable-flat-columns'
+
+Several other columns are calculated automatically:
+    Item Name: The path to the parent headline
+    Item Desc: The headline
+    Quantity: The clocktime.  Useful with the `;t' formatter to get a
+              decimal billable time.
+"
+  (let ((date (format-time-string "%Y-%m-%d"))
+	(customer (plist-get params :customer))
+	(number (plist-get params :number))
+	(price (plist-get params :price))
+	(columns (or (plist-get params :columns)
+		     org-clock-clocktable-flat-columns)))
+    (insert "\n|")
+    (dolist (column columns)
+      (insert column "|"))
+    (insert "\n|-\n")
+    (dolist (table tables)
+      (let ((rows (nth 2 table))
+	    (parents (list "")))
+	(dotimes (row-idx (length rows))
+	  (let ((row (nth row-idx rows)))
+	    (if (listp row)
+		(progn
+		  (dotimes (parent-idx
+			    (- (nth 0 (nth (- row-idx 1) rows)) (nth 0 row)))
+		    (setcdr (last parents 2) nil))
+		  (if (and (< row-idx (- (length rows) 1))
+			   (< (nth 0 row) (nth 0 (nth (+ row-idx 1) rows))))
+		      (nconc parents (list (cadr row)))
+		    (insert "|")
+		    (dolist (column columns)
+		      (cond
+		       ((equal column "Invoice Date") (insert date))
+		       ((equal column "Invoice Number") (insert number))
+		       ((equal column "Customer Name") (insert customer))
+		       ((equal column "Item Name")
+			(dotimes (parent-idx (length parents))
+			  (insert (nth parent-idx parents))
+			  (if (not (or (= parent-idx 0)
+				       (= parent-idx (- (length parents) 1))))
+			      (insert "/"))))
+		       ((equal column "Item Desc") (insert (cadr row)))
+		       ((equal column "Quantity")
+			(insert (org-minutes-to-hh:mm-string (nth 3 row))))
+		       ((equal column "Item Price")
+			(insert (format "%s" price))))
+		      (insert "|"))
+		    (insert "\n"))))))))
+    (insert "#+TBLFM: " (plist-get params :formula))
+    (org-ctrl-c-ctrl-c)
+    (goto-char ipos)
+    (skip-chars-forward "^|")))
+
 (defun org-clocktable-indent-string (level)
   (if (= level 1)
       ""
-- 
1.7.9.5

             reply	other threads:[~2013-06-28  4:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-28  4:20 Ross Patterson [this message]
2013-06-28  7:03 ` [PATCH] Add a flat clocktable formatter useful for tables whose CSV export can be imported into accounting/invoiving tools such as Zoho Invoicing Bastien
2013-06-29  2:09   ` Ross Patterson
2013-06-30 20:25     ` Bastien

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=874nci7tob.fsf@rpatterson.net \
    --to=me@rpatterson.net \
    --cc=emacs-orgmode@gnu.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).