org-invoice.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. ;;; org-invoice.el --- Help manage client invoices in OrgMode
  2. ;;
  3. ;; Copyright (C) 2008 pmade inc. (Peter Jones pjones@pmade.com)
  4. ;;
  5. ;; Permission is hereby granted, free of charge, to any person obtaining
  6. ;; a copy of this software and associated documentation files (the
  7. ;; "Software"), to deal in the Software without restriction, including
  8. ;; without limitation the rights to use, copy, modify, merge, publish,
  9. ;; distribute, sublicense, and/or sell copies of the Software, and to
  10. ;; permit persons to whom the Software is furnished to do so, subject to
  11. ;; the following conditions:
  12. ;;
  13. ;; The above copyright notice and this permission notice shall be
  14. ;; included in all copies or substantial portions of the Software.
  15. ;;
  16. ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. ;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. ;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. ;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. ;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. ;;
  24. ;; Commentary:
  25. ;;
  26. ;; Building on top of the terrific OrgMode, org-invoice tries to
  27. ;; provide functionality for managing invoices. Currently, it does
  28. ;; this by implementing an OrgMode dynamic block where invoice
  29. ;; information is aggregated so that it can be exported.
  30. ;;
  31. ;; It also provides a library of functions that can be used to collect
  32. ;; this invoice information and use it in other ways, such as
  33. ;; submitting it to on-line invoicing tools.
  34. ;;
  35. ;; I'm already working on an elisp package to submit this invoice data
  36. ;; to the FreshBooks on-line accounting tool.
  37. ;;
  38. ;; Usage:
  39. ;;
  40. ;; In your ~/.emacs:
  41. ;; (autoload 'org-invoice-report "org-invoice")
  42. ;; (autoload 'org-dblock-write:invoice "org-invoice")
  43. ;;
  44. ;; See the documentation in the following functions:
  45. ;;
  46. ;; `org-invoice-report'
  47. ;; `org-dblock-write:invoice'
  48. ;;
  49. ;; Latest version:
  50. ;;
  51. ;; git clone git://pmade.com/elisp
  52. (eval-when-compile
  53. (require 'cl)
  54. (require 'org))
  55. (defgroup org-invoice nil
  56. "OrgMode Invoice Helper"
  57. :tag "Org-Invoice" :group 'org)
  58. (defcustom org-invoice-long-date-format "%A, %B %d, %Y"
  59. "The format string for long dates."
  60. :type 'string :group 'org-invoice)
  61. (defcustom org-invoice-strip-ts t
  62. "Remove org timestamps that appear in headings."
  63. :type 'boolean :group 'org-invoice)
  64. (defcustom org-invoice-default-level 2
  65. "The heading level at which a new invoice starts. This value
  66. is used if you don't specify a scope option to the invoice block,
  67. and when other invoice helpers are trying to find the heading
  68. that starts an invoice.
  69. The default is 2, assuming that you structure your invoices so
  70. that they fall under a single heading like below:
  71. * Invoices
  72. ** This is invoice number 1...
  73. ** This is invoice number 2...
  74. If you don't structure your invoices using those conventions,
  75. change this setting to the number that corresponds to the heading
  76. at which an invoice begins."
  77. :type 'integer :group 'org-invoice)
  78. (defcustom org-invoice-start-hook nil
  79. "Hook called when org-invoice is about to collect data from an
  80. invoice heading. When this hook is called, point will be on the
  81. heading where the invoice begins.
  82. When called, `org-invoice-current-invoice' will be set to the
  83. alist that represents the info for this invoice."
  84. :type 'hook :group 'org-invoice)
  85. (defcustom org-invoice-heading-hook nil
  86. "Hook called when org-invoice is collecting data from a
  87. heading. You can use this hook to add additional information to
  88. the alist that represents the heading.
  89. When this hook is called, point will be on the current heading
  90. being processed, and `org-invoice-current-item' will contain the
  91. alist for the current heading.
  92. This hook is called repeatedly for each invoice item processed."
  93. :type 'hook :group 'org-invoice)
  94. (defvar org-invoice-current-invoice nil
  95. "Information about the current invoice.")
  96. (defvar org-invoice-current-item nil
  97. "Information about the current invoice item.")
  98. (defvar org-invoice-table-params nil
  99. "The table parameters currently being used.")
  100. (defvar org-invoice-total-time nil
  101. "The total invoice time for the summary line.")
  102. (defvar org-invoice-total-price nil
  103. "The total invoice price for the summary line.")
  104. (defconst org-invoice-version "1.0.0"
  105. "The org-invoice version number.")
  106. (defun org-invoice-goto-tree (&optional tree)
  107. "Move point to the heading that represents the head of the
  108. current invoice. The heading level will be taken from
  109. `org-invoice-default-level' unless tree is set to a string that
  110. looks like tree2, where the level is 2."
  111. (let ((level org-invoice-default-level))
  112. (save-match-data
  113. (when (and tree (string-match "^tree\\([0-9]+\\)$" tree))
  114. (setq level (string-to-number (match-string 1 tree)))))
  115. (org-back-to-heading)
  116. (while (and (> (org-reduced-level (org-outline-level)) level)
  117. (org-up-heading-safe)))))
  118. (defun org-invoice-heading-info ()
  119. "Return invoice information from the current heading."
  120. (let ((title (org-no-properties (org-get-heading t)))
  121. (date (org-entry-get nil "TIMESTAMP" 'selective))
  122. (work (org-entry-get nil "WORK" nil))
  123. (rate (or (org-entry-get nil "RATE" t) "0"))
  124. (level (org-outline-level))
  125. raw-date long-date)
  126. (unless date (setq date (org-entry-get nil "TIMESTAMP_IA" 'selective)))
  127. (unless date (setq date (org-entry-get nil "TIMESTAMP" t)))
  128. (unless date (setq date (org-entry-get nil "TIMESTAMP_IA" t)))
  129. (unless work (setq work (org-entry-get nil "CLOCKSUM" nil)))
  130. (unless work (setq work "00:00"))
  131. (when date
  132. (setq raw-date (apply 'encode-time (org-parse-time-string date)))
  133. (setq long-date (format-time-string org-invoice-long-date-format raw-date)))
  134. (when (and org-invoice-strip-ts (string-match org-ts-regexp-both title))
  135. (setq title (replace-match "" nil nil title)))
  136. (when (string-match "^[ \t]+" title)
  137. (setq title (replace-match "" nil nil title)))
  138. (when (string-match "[ \t]+$" title)
  139. (setq title (replace-match "" nil nil title)))
  140. (setq work (org-hh:mm-string-to-minutes work))
  141. (setq rate (string-to-number rate))
  142. (setq org-invoice-current-item (list (cons 'title title)
  143. (cons 'date date)
  144. (cons 'raw-date raw-date)
  145. (cons 'long-date long-date)
  146. (cons 'work work)
  147. (cons 'rate rate)
  148. (cons 'level level)
  149. (cons 'price (* rate (/ work 60.0)))))
  150. (run-hook-with-args 'org-invoice-heading-hook)
  151. org-invoice-current-item))
  152. (defun org-invoice-level-min-max (ls)
  153. "Return a list where the car is the min level, and the cdr the max."
  154. (let ((max 0) min level)
  155. (dolist (info ls)
  156. (when (cdr (assoc 'date info))
  157. (setq level (cdr (assoc 'level info)))
  158. (when (or (not min) (< level min)) (setq min level))
  159. (when (> level max) (setq max level))))
  160. (cons (or min 0) max)))
  161. (defun org-invoice-collapse-list (ls)
  162. "Reorganize the given list by dates."
  163. (let ((min-max (org-invoice-level-min-max ls)) new)
  164. (dolist (info ls)
  165. (let* ((date (cdr (assoc 'date info)))
  166. (work (cdr (assoc 'work info)))
  167. (price (cdr (assoc 'price info)))
  168. (long-date (cdr (assoc 'long-date info)))
  169. (level (cdr (assoc 'level info)))
  170. (bucket (cdr (assoc date new))))
  171. (if (and (/= (car min-max) (cdr min-max))
  172. (= (car min-max) level)
  173. (= work 0) (not bucket) date)
  174. (progn
  175. (setq info (assq-delete-all 'work info))
  176. (push (cons 'total-work 0) info)
  177. (push (cons date (list info)) new)
  178. (setq bucket (cdr (assoc date new))))
  179. (when (and date (not bucket))
  180. (setq bucket (list (list (cons 'date date)
  181. (cons 'title long-date)
  182. (cons 'total-work 0)
  183. (cons 'price 0))))
  184. (push (cons date bucket) new)
  185. (setq bucket (cdr (assoc date new))))
  186. (when (and date bucket)
  187. (setcdr (assoc 'total-work (car bucket))
  188. (+ work (cdr (assoc 'total-work (car bucket)))))
  189. (setcdr (assoc 'price (car bucket))
  190. (+ price (cdr (assoc 'price (car bucket)))))
  191. (nconc bucket (list info))))))
  192. (nreverse new)))
  193. (defun org-invoice-info-to-table (info)
  194. "Create a single org table row from the given info alist."
  195. (let ((title (cdr (assoc 'title info)))
  196. (total (cdr (assoc 'total-work info)))
  197. (work (cdr (assoc 'work info)))
  198. (price (cdr (assoc 'price info)))
  199. (with-price (plist-get org-invoice-table-params :price)))
  200. (unless total
  201. (setq
  202. org-invoice-total-time (+ org-invoice-total-time work)
  203. org-invoice-total-price (+ org-invoice-total-price price)))
  204. (setq total (and total (org-minutes-to-hh:mm-string total)))
  205. (setq work (and work (org-minutes-to-hh:mm-string work)))
  206. (insert-before-markers
  207. (concat "|" title
  208. (cond
  209. (total (concat "|" total))
  210. (work (concat "|" work)))
  211. (and with-price price (concat "|" (format "%.2f" price)))
  212. "|" "\n"))))
  213. (defun org-invoice-list-to-table (ls)
  214. "Convert a list of heading info to an org table"
  215. (let ((with-price (plist-get org-invoice-table-params :price))
  216. (with-summary (plist-get org-invoice-table-params :summary))
  217. (with-header (plist-get org-invoice-table-params :headers))
  218. (org-invoice-total-time 0)
  219. (org-invoice-total-price 0))
  220. (insert-before-markers
  221. (concat "| Task / Date | Time" (and with-price "| Price") "|\n"))
  222. (dolist (info ls)
  223. (insert-before-markers "|-\n")
  224. (mapc 'org-invoice-info-to-table (if with-header (cdr info) (cdr (cdr info)))))
  225. (when with-summary
  226. (insert-before-markers
  227. (concat "|-\n|Total:|"
  228. (org-minutes-to-hh:mm-string org-invoice-total-time)
  229. (and with-price (concat "|" (format "%.2f" org-invoice-total-price)))
  230. "|\n")))))
  231. (defun org-invoice-collect-invoice-data ()
  232. "Collect all the invoice data from the current OrgMode tree and
  233. return it. Before you call this function, move point to the
  234. heading that begins the invoice data, usually using the
  235. `org-invoice-goto-tree' function."
  236. (let ((org-invoice-current-invoice
  237. (list (cons 'point (point)) (cons 'buffer (current-buffer))))
  238. (org-invoice-current-item nil))
  239. (save-restriction
  240. (org-narrow-to-subtree)
  241. (org-clock-sum)
  242. (run-hook-with-args 'org-invoice-start-hook)
  243. (cons org-invoice-current-invoice
  244. (org-invoice-collapse-list
  245. (org-map-entries 'org-invoice-heading-info t 'tree 'archive))))))
  246. (defun org-dblock-write:invoice (params)
  247. "Function called by OrgMode to write the invoice dblock. To
  248. create an invoice dblock you can use the `org-invoice-report'
  249. function.
  250. The following parameters can be given to the invoice block (for
  251. information about dblock parameters, please see the Org manual):
  252. :scope Allows you to override the `org-invoice-default-level'
  253. variable. The only supported values right now are ones
  254. that look like :tree1, :tree2, etc.
  255. :prices Set to nil to turn off the price column.
  256. :headers Set to nil to turn off the group headers.
  257. :summary Set to nil to turn off the final summary line."
  258. (let ((scope (plist-get params :scope))
  259. (org-invoice-table-params params)
  260. (zone (move-marker (make-marker) (point)))
  261. table)
  262. (unless scope (setq scope 'default))
  263. (unless (plist-member params :price) (plist-put params :price t))
  264. (unless (plist-member params :summary) (plist-put params :summary t))
  265. (unless (plist-member params :headers) (plist-put params :headers t))
  266. (save-excursion
  267. (cond
  268. ((eq scope 'tree) (org-invoice-goto-tree "tree1"))
  269. ((eq scope 'default) (org-invoice-goto-tree))
  270. ((symbolp scope) (org-invoice-goto-tree (symbol-name scope))))
  271. (setq table (org-invoice-collect-invoice-data))
  272. (goto-char zone)
  273. (org-invoice-list-to-table (cdr table))
  274. (goto-char zone)
  275. (org-table-align)
  276. (move-marker zone nil))))
  277. (defun org-invoice-in-report-p ()
  278. "Check to see if point is inside an invoice report."
  279. (let ((pos (point)) start)
  280. (save-excursion
  281. (end-of-line 1)
  282. (and (re-search-backward "^#\\+BEGIN:[ \t]+invoice" nil t)
  283. (setq start (match-beginning 0))
  284. (re-search-forward "^#\\+END:.*" nil t)
  285. (>= (match-end 0) pos)
  286. start))))
  287. (defun org-invoice-report (&optional jump)
  288. "Create or update an invoice dblock report. If point is inside
  289. an existing invoice report, the report is updated. If point
  290. isn't inside an invoice report, a new report is created.
  291. When called with a prefix argument, move to the first invoice
  292. report after point and update it.
  293. For information about various settings for the invoice report,
  294. see the `org-dblock-write:invoice' function documentation.
  295. An invoice report is created by reading a heading tree and
  296. collecting information from various properties. It is assumed
  297. that all invoices start at a second level heading, but this can
  298. be configured using the `org-invoice-default-level' variable.
  299. Here is an example, where all invoices fall under the first-level
  300. heading Invoices:
  301. * Invoices
  302. ** Client Foo (Jan 01 - Jan 15)
  303. *** [2008-01-01 Tue] Built New Server for Production
  304. *** [2008-01-02 Wed] Meeting with Team to Design New System
  305. ** Client Bar (Jan 01 - Jan 15)
  306. *** [2008-01-01 Tue] Searched for Widgets on Google
  307. *** [2008-01-02 Wed] Billed You for Taking a Nap
  308. In this layout, invoices begin at level two, and invoice
  309. items (tasks) are at level three. You'll notice that each level
  310. three heading starts with an inactive timestamp. The timestamp
  311. can actually go anywhere you want, either in the heading, or in
  312. the text under the heading. But you must have a timestamp
  313. somewhere so that the invoice report can group your items by
  314. date.
  315. Properties are used to collect various bits of information for
  316. the invoice. All properties can be set on the invoice item
  317. headings, or anywhere in the tree. The invoice report will scan
  318. up the tree looking for each of the properties.
  319. Properties used:
  320. CLOCKSUM: You can use the Org clock-in and clock-out commands to
  321. create a CLOCKSUM property. Also see WORK.
  322. WORK: An alternative to the CLOCKSUM property. This property
  323. should contain the amount of work that went into this
  324. invoice item formatted as HH:MM (e.g. 01:30).
  325. RATE: Used to calculate the total price for an invoice item.
  326. Should be the price per hour that you charge (e.g. 45.00).
  327. It might make more sense to place this property higher in
  328. the hierarchy than on the invoice item headings.
  329. Using this information, a report is generated that details the
  330. items grouped by days. For each day you will be able to see the
  331. total number of hours worked, the total price, and the items
  332. worked on.
  333. You can place the invoice report anywhere in the tree you want.
  334. I place mine under a third-level heading like so:
  335. * Invoices
  336. ** An Invoice Header
  337. *** [2008-11-25 Tue] An Invoice Item
  338. *** Invoice Report
  339. #+BEGIN: invoice
  340. #+END:"
  341. (interactive "P")
  342. (let ((report (org-invoice-in-report-p)))
  343. (when (and (not report) jump)
  344. (when (re-search-forward "^#\\+BEGIN:[ \t]+invoice" nil t)
  345. (org-show-entry)
  346. (beginning-of-line)
  347. (setq report (point))))
  348. (if report (goto-char report)
  349. (org-create-dblock (list :name "invoice")))
  350. (org-update-dblock)))
  351. (provide 'org-invoice)