org-invoice.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. (defgroup org-invoice nil
  58. "OrgMode Invoice Helper"
  59. :tag "Org-Invoice" :group 'org)
  60. (defcustom org-invoice-long-date-format "%A, %B %d, %Y"
  61. "The format string for long dates."
  62. :type 'string :group 'org-invoice)
  63. (defcustom org-invoice-strip-ts t
  64. "Remove org timestamps that appear in headings."
  65. :type 'boolean :group 'org-invoice)
  66. (defcustom org-invoice-default-level 2
  67. "The heading level at which a new invoice starts. This value
  68. is used if you don't specify a scope option to the invoice block,
  69. and when other invoice helpers are trying to find the heading
  70. that starts an invoice.
  71. The default is 2, assuming that you structure your invoices so
  72. that they fall under a single heading like below:
  73. * Invoices
  74. ** This is invoice number 1...
  75. ** This is invoice number 2...
  76. If you don't structure your invoices using those conventions,
  77. change this setting to the number that corresponds to the heading
  78. at which an invoice begins."
  79. :type 'integer :group 'org-invoice)
  80. (defcustom org-invoice-start-hook nil
  81. "Hook called when org-invoice is about to collect data from an
  82. invoice heading. When this hook is called, point will be on the
  83. heading where the invoice begins.
  84. When called, `org-invoice-current-invoice' will be set to the
  85. alist that represents the info for this invoice."
  86. :type 'hook :group 'org-invoice)
  87. (defcustom org-invoice-heading-hook nil
  88. "Hook called when org-invoice is collecting data from a
  89. heading. You can use this hook to add additional information to
  90. the alist that represents the heading.
  91. When this hook is called, point will be on the current heading
  92. being processed, and `org-invoice-current-item' will contain the
  93. alist for the current heading.
  94. This hook is called repeatedly for each invoice item processed."
  95. :type 'hook :group 'org-invoice)
  96. (defvar org-invoice-current-invoice nil
  97. "Information about the current invoice.")
  98. (defvar org-invoice-current-item nil
  99. "Information about the current invoice item.")
  100. (defvar org-invoice-table-params nil
  101. "The table parameters currently being used.")
  102. (defvar org-invoice-total-time nil
  103. "The total invoice time for the summary line.")
  104. (defvar org-invoice-total-price nil
  105. "The total invoice price for the summary line.")
  106. (defconst org-invoice-version "1.0.0"
  107. "The org-invoice version number.")
  108. (defun org-invoice-goto-tree (&optional tree)
  109. "Move point to the heading that represents the head of the
  110. current invoice. The heading level will be taken from
  111. `org-invoice-default-level' unless tree is set to a string that
  112. looks like tree2, where the level is 2."
  113. (let ((level org-invoice-default-level))
  114. (save-match-data
  115. (when (and tree (string-match "^tree\\([0-9]+\\)$" tree))
  116. (setq level (string-to-number (match-string 1 tree)))))
  117. (org-back-to-heading)
  118. (while (and (> (org-reduced-level (org-outline-level)) level)
  119. (org-up-heading-safe)))))
  120. (defun org-invoice-heading-info ()
  121. "Return invoice information from the current heading."
  122. (let ((title (org-no-properties (org-get-heading t)))
  123. (date (org-entry-get nil "TIMESTAMP" 'selective))
  124. (work (org-entry-get nil "WORK" nil))
  125. (rate (or (org-entry-get nil "RATE" t) "0"))
  126. (level (org-outline-level))
  127. raw-date long-date)
  128. (unless date (setq date (org-entry-get nil "TIMESTAMP_IA" 'selective)))
  129. (unless date (setq date (org-entry-get nil "TIMESTAMP" t)))
  130. (unless date (setq date (org-entry-get nil "TIMESTAMP_IA" t)))
  131. (unless work (setq work (org-entry-get nil "CLOCKSUM" nil)))
  132. (unless work (setq work "00:00"))
  133. (when date
  134. (setq raw-date (apply 'encode-time (org-parse-time-string date)))
  135. (setq long-date (format-time-string org-invoice-long-date-format raw-date)))
  136. (when (and org-invoice-strip-ts (string-match org-ts-regexp-both title))
  137. (setq title (replace-match "" nil nil title)))
  138. (when (string-match "^[ \t]+" title)
  139. (setq title (replace-match "" nil nil title)))
  140. (when (string-match "[ \t]+$" title)
  141. (setq title (replace-match "" nil nil title)))
  142. (setq work (org-hh:mm-string-to-minutes work))
  143. (setq rate (string-to-number rate))
  144. (setq org-invoice-current-item (list (cons 'title title)
  145. (cons 'date date)
  146. (cons 'raw-date raw-date)
  147. (cons 'long-date long-date)
  148. (cons 'work work)
  149. (cons 'rate rate)
  150. (cons 'level level)
  151. (cons 'price (* rate (/ work 60.0)))))
  152. (run-hook-with-args 'org-invoice-heading-hook)
  153. org-invoice-current-item))
  154. (defun org-invoice-level-min-max (ls)
  155. "Return a list where the car is the min level, and the cdr the max."
  156. (let ((max 0) min level)
  157. (dolist (info ls)
  158. (when (cdr (assoc 'date info))
  159. (setq level (cdr (assoc 'level info)))
  160. (when (or (not min) (< level min)) (setq min level))
  161. (when (> level max) (setq max level))))
  162. (cons (or min 0) max)))
  163. (defun org-invoice-collapse-list (ls)
  164. "Reorganize the given list by dates."
  165. (let ((min-max (org-invoice-level-min-max ls)) new)
  166. (dolist (info ls)
  167. (let* ((date (cdr (assoc 'date info)))
  168. (work (cdr (assoc 'work info)))
  169. (price (cdr (assoc 'price info)))
  170. (long-date (cdr (assoc 'long-date info)))
  171. (level (cdr (assoc 'level info)))
  172. (bucket (cdr (assoc date new))))
  173. (if (and (/= (car min-max) (cdr min-max))
  174. (= (car min-max) level)
  175. (= work 0) (not bucket) date)
  176. (progn
  177. (setq info (assq-delete-all 'work info))
  178. (push (cons 'total-work 0) info)
  179. (push (cons date (list info)) new)
  180. (setq bucket (cdr (assoc date new))))
  181. (when (and date (not bucket))
  182. (setq bucket (list (list (cons 'date date)
  183. (cons 'title long-date)
  184. (cons 'total-work 0)
  185. (cons 'price 0))))
  186. (push (cons date bucket) new)
  187. (setq bucket (cdr (assoc date new))))
  188. (when (and date bucket)
  189. (setcdr (assoc 'total-work (car bucket))
  190. (+ work (cdr (assoc 'total-work (car bucket)))))
  191. (setcdr (assoc 'price (car bucket))
  192. (+ price (cdr (assoc 'price (car bucket)))))
  193. (nconc bucket (list info))))))
  194. (nreverse new)))
  195. (defun org-invoice-info-to-table (info)
  196. "Create a single org table row from the given info alist."
  197. (let ((title (cdr (assoc 'title info)))
  198. (total (cdr (assoc 'total-work info)))
  199. (work (cdr (assoc 'work info)))
  200. (price (cdr (assoc 'price info)))
  201. (with-price (plist-get org-invoice-table-params :price)))
  202. (unless total
  203. (setq
  204. org-invoice-total-time (+ org-invoice-total-time work)
  205. org-invoice-total-price (+ org-invoice-total-price price)))
  206. (setq total (and total (org-minutes-to-clocksum-string total)))
  207. (setq work (and work (org-minutes-to-clocksum-string work)))
  208. (insert-before-markers
  209. (concat "|" title
  210. (cond
  211. (total (concat "|" total))
  212. (work (concat "|" work)))
  213. (and with-price price (concat "|" (format "%.2f" price)))
  214. "|" "\n"))))
  215. (defun org-invoice-list-to-table (ls)
  216. "Convert a list of heading info to an org table"
  217. (let ((with-price (plist-get org-invoice-table-params :price))
  218. (with-summary (plist-get org-invoice-table-params :summary))
  219. (with-header (plist-get org-invoice-table-params :headers))
  220. (org-invoice-total-time 0)
  221. (org-invoice-total-price 0))
  222. (insert-before-markers
  223. (concat "| Task / Date | Time" (and with-price "| Price") "|\n"))
  224. (dolist (info ls)
  225. (insert-before-markers "|-\n")
  226. (mapc 'org-invoice-info-to-table (if with-header (cdr info) (cdr (cdr info)))))
  227. (when with-summary
  228. (insert-before-markers
  229. (concat "|-\n|Total:|"
  230. (org-minutes-to-clocksum-string org-invoice-total-time)
  231. (and with-price (concat "|" (format "%.2f" org-invoice-total-price)))
  232. "|\n")))))
  233. (defun org-invoice-collect-invoice-data ()
  234. "Collect all the invoice data from the current OrgMode tree and
  235. return it. Before you call this function, move point to the
  236. heading that begins the invoice data, usually using the
  237. `org-invoice-goto-tree' function."
  238. (let ((org-invoice-current-invoice
  239. (list (cons 'point (point)) (cons 'buffer (current-buffer))))
  240. (org-invoice-current-item nil))
  241. (save-restriction
  242. (org-narrow-to-subtree)
  243. (org-clock-sum)
  244. (run-hook-with-args 'org-invoice-start-hook)
  245. (cons org-invoice-current-invoice
  246. (org-invoice-collapse-list
  247. (org-map-entries 'org-invoice-heading-info t 'tree 'archive))))))
  248. (defun org-dblock-write:invoice (params)
  249. "Function called by OrgMode to write the invoice dblock. To
  250. create an invoice dblock you can use the `org-invoice-report'
  251. function.
  252. The following parameters can be given to the invoice block (for
  253. information about dblock parameters, please see the Org manual):
  254. :scope Allows you to override the `org-invoice-default-level'
  255. variable. The only supported values right now are ones
  256. that look like :tree1, :tree2, etc.
  257. :prices Set to nil to turn off the price column.
  258. :headers Set to nil to turn off the group headers.
  259. :summary Set to nil to turn off the final summary line."
  260. (let ((scope (plist-get params :scope))
  261. (org-invoice-table-params params)
  262. (zone (point-marker))
  263. table)
  264. (unless scope (setq scope 'default))
  265. (unless (plist-member params :price) (plist-put params :price t))
  266. (unless (plist-member params :summary) (plist-put params :summary t))
  267. (unless (plist-member params :headers) (plist-put params :headers t))
  268. (save-excursion
  269. (cond
  270. ((eq scope 'tree) (org-invoice-goto-tree "tree1"))
  271. ((eq scope 'default) (org-invoice-goto-tree))
  272. ((symbolp scope) (org-invoice-goto-tree (symbol-name scope))))
  273. (setq table (org-invoice-collect-invoice-data))
  274. (goto-char zone)
  275. (org-invoice-list-to-table (cdr table))
  276. (goto-char zone)
  277. (org-table-align)
  278. (move-marker zone nil))))
  279. (defun org-invoice-in-report-p ()
  280. "Check to see if point is inside an invoice report."
  281. (let ((pos (point)) start)
  282. (save-excursion
  283. (end-of-line 1)
  284. (and (re-search-backward "^#\\+BEGIN:[ \t]+invoice" nil t)
  285. (setq start (match-beginning 0))
  286. (re-search-forward "^#\\+END:.*" nil t)
  287. (>= (match-end 0) pos)
  288. start))))
  289. (defun org-invoice-report (&optional jump)
  290. "Create or update an invoice dblock report. If point is inside
  291. an existing invoice report, the report is updated. If point
  292. isn't inside an invoice report, a new report is created.
  293. When called with a prefix argument, move to the first invoice
  294. report after point and update it.
  295. For information about various settings for the invoice report,
  296. see the `org-dblock-write:invoice' function documentation.
  297. An invoice report is created by reading a heading tree and
  298. collecting information from various properties. It is assumed
  299. that all invoices start at a second level heading, but this can
  300. be configured using the `org-invoice-default-level' variable.
  301. Here is an example, where all invoices fall under the first-level
  302. heading Invoices:
  303. * Invoices
  304. ** Client Foo (Jan 01 - Jan 15)
  305. *** [2008-01-01 Tue] Built New Server for Production
  306. *** [2008-01-02 Wed] Meeting with Team to Design New System
  307. ** Client Bar (Jan 01 - Jan 15)
  308. *** [2008-01-01 Tue] Searched for Widgets on Google
  309. *** [2008-01-02 Wed] Billed You for Taking a Nap
  310. In this layout, invoices begin at level two, and invoice
  311. items (tasks) are at level three. You'll notice that each level
  312. three heading starts with an inactive timestamp. The timestamp
  313. can actually go anywhere you want, either in the heading, or in
  314. the text under the heading. But you must have a timestamp
  315. somewhere so that the invoice report can group your items by
  316. date.
  317. Properties are used to collect various bits of information for
  318. the invoice. All properties can be set on the invoice item
  319. headings, or anywhere in the tree. The invoice report will scan
  320. up the tree looking for each of the properties.
  321. Properties used:
  322. CLOCKSUM: You can use the Org clock-in and clock-out commands to
  323. create a CLOCKSUM property. Also see WORK.
  324. WORK: An alternative to the CLOCKSUM property. This property
  325. should contain the amount of work that went into this
  326. invoice item formatted as HH:MM (e.g. 01:30).
  327. RATE: Used to calculate the total price for an invoice item.
  328. Should be the price per hour that you charge (e.g. 45.00).
  329. It might make more sense to place this property higher in
  330. the hierarchy than on the invoice item headings.
  331. Using this information, a report is generated that details the
  332. items grouped by days. For each day you will be able to see the
  333. total number of hours worked, the total price, and the items
  334. worked on.
  335. You can place the invoice report anywhere in the tree you want.
  336. I place mine under a third-level heading like so:
  337. * Invoices
  338. ** An Invoice Header
  339. *** [2008-11-25 Tue] An Invoice Item
  340. *** Invoice Report
  341. #+BEGIN: invoice
  342. #+END:"
  343. (interactive "P")
  344. (let ((report (org-invoice-in-report-p)))
  345. (when (and (not report) jump)
  346. (when (re-search-forward "^#\\+BEGIN:[ \t]+invoice" nil t)
  347. (org-show-entry)
  348. (beginning-of-line)
  349. (setq report (point))))
  350. (if report (goto-char report)
  351. (org-create-dblock (list :name "invoice")))
  352. (org-update-dblock)))
  353. (provide 'org-invoice)