org-invoice.el 16 KB

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