-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtimesheet.el
More file actions
1215 lines (1127 loc) · 43.1 KB
/
timesheet.el
File metadata and controls
1215 lines (1127 loc) · 43.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; timesheet.el --- Timesheet management add-on for org-mode
;; Copyright © 2014-2022 Informatique, Inc.
;; Author: Tom Marble
;; URL: https://github.com/tmarble/timesheet.el
;; Version: 0.5.0
;; Created: 2022-10-04
;; Keywords: org timesheet
;; Package-Requires: ((s "1") (org "9"))
;; This file is NOT part of GNU Emacs.
;;; Commentary:
;; Debian-depends: emacs24 make gawk sed git tar rubber texlive-latex-extra texlive-fonts-recommended texlive-fonts-extra evince
;;
;; This library adds timetracking and invoice generation to org-mode
;; and relies heavily on
;; org clocking http://orgmode.org/org.html#Clocking-work-time
;; and TODO items http://orgmode.org/org.html#TODO-Items
;; and org spreadsheets http://orgmode.org/org.html#The-spreadsheet
;;
;; This library attempts to conform to packaging conventions:
;; https://www.gnu.org/software/emacs/manual/html_node/elisp/Packaging.html
;; Bugs, enhancements welcome!
;;; Usage
;; Ensure TEXINPUTS is set to (in your ~/.bashrc)
;; export TEXINPUTS=.:$HOME/.emacs.d/elpa/auctex-11.87.4/latex:
;;
;; Start by creating an example client...
;; M-x timesheet-example
;; You will be viewing the buffer yoyodyne.org that already has some example
;; time entries... Create an invoice with
;; M-x timesheet-invoice-this
;;
;; Next steps...
;; - customize your name (in defs.tex) and logo (in logo.pdf).
;; - update some time entries.
;;
;; Example key bindings
;; see example.emacs.d/foo/bindings.el
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Code:
(require 's)
(require 'org)
(require 'org-clock)
(require 'org-table)
;; vars
;; timesheet-version should match the Version comment above
(defconst timesheet-version "0.5.0")
(defconst timesheet-path (file-name-directory (or load-file-name (buffer-file-name))))
(defun timesheet-home-dir ()
"Return the user's HOME directory."
(file-name-as-directory (expand-file-name "~")))
(defconst timesheet-currencies
'(("USD" . "$")
("GBP" . "£")
("EUR" . "€")))
;; customizations
(defgroup timesheet nil
"Timesheet functions"
:tag "Timesheet"
:group 'org)
(defcustom timesheet-invoice-number 100
"Next invoice number."
:type '(integer)
:group 'timesheet)
(defcustom timesheet-invoice-script
(expand-file-name "timesheet-invoice" (expand-file-name "bin" timesheet-path))
"Script to create a timesheet invoice."
:type 'string
:group 'timesheet)
(defcustom timesheet-company-dir
(expand-file-name "Timesheet" (timesheet-home-dir))
"Parent directory for timesheet invoices."
:type 'string
:group 'timesheet)
(defcustom timesheet-always-recalculate-times nil
"When non-nil, recalculate all times before creating invoices.
Typically the user is expected to run `timesheet-calc-today' at
the end of each day, creating the corresponding entry under the
\"Timesheet\" heading. If this option is non-nil, all day
entries will be updated before creating an invoice. This may
slow down invoice creation."
:type 'boolean
:group 'timesheet)
;; get the next invoice number (and increment the counter)
;; if the argument is given.. set the next invoice number
;;;###autoload
(defun timesheet-next-invoice (&optional invoice)
"Get next invoice number (following `timesheet-invoice-number' or INVOICE if present)."
(interactive)
(let* ((next-invoice (if invoice invoice timesheet-invoice-number))
(arg (list 'timesheet-invoice-number (1+ next-invoice))))
(custom-set-variables arg)
(custom-save-all)
next-invoice))
;; debug functions
;; these will probably be removed from a future version...
(defvar timesheet-debug-msg-delay 1)
;;;###autoload
(defun timesheet-debug-msg (&rest msgs)
"Display some debug MSGS."
(interactive)
(message (apply 'concat msgs))
(redisplay t)
(sleep-for timesheet-debug-msg-delay))
;;;###autoload
(defun timesheet-debug-time (time &rest msgs)
"Display TIME with some debug MSGS."
(interactive)
(let ((at-time (if time time (current-time))))
(apply 'timesheet-debug-msg (cons (format-time-string "%Y-%m-%d %a %H:%M:%S" at-time) msgs))))
;;;###autoload
(defun timesheet-debug-time-cal (time-cal &rest msgs)
"Display TIME-CAL with some debug MSGS."
(interactive)
(apply 'timesheet-debug-time (cons (apply 'encode-time time-cal) msgs)))
;; functions
(defun timesheet-get-file-property (property)
"Return the value of the file PROPERTY (or nil if not found)"
(let ((regex (concat "^#\\+PROPERTY: " property)))
(save-excursion
(beginning-of-line 1)
(when (re-search-forward regex nil t)
(substring
(org-element-property :value (org-element-at-point))
(1+ (length property)))))))
(defun timesheet-get-currency-symbol (currency)
"Return the currency symbol for CURRENCY (or nil if not found)"
(cdr (assoc-string currency timesheet-currencies)))
(defun timesheet-template-files ()
"Return a list of pathnames for timesheet template files."
(let ((share-dir (expand-file-name "share" timesheet-path)))
(directory-files share-dir t (concat "\\." (regexp-opt '("tex" "pdf" "org")) "\\'"))))
(defun timesheet-round-time-down (tl)
"Round the time in TL (timelist format) to the previous quarter hour (default).
The constant roundmin can be set to any value between 1 and 60 (default 15)."
(let* ((s (car tl))
(m (nth 1 tl))
(h (nth 2 tl))
(rest (nthcdr 3 tl))
(roundmin-str (org-table-get-constant "roundmin"))
(roundmin (if (string= roundmin-str "#UNDEFINED_NAME") 15
(string-to-number roundmin-str))))
(cons s
(cons (* (/ m roundmin) roundmin)
(cons h rest)))))
(defun timesheet-round-time-up (tl)
"Round the time in TL (timelist format) do the next quarter hour."
(let* ((s (car tl))
(m (nth 1 tl))
(h (nth 2 tl))
(rest (nthcdr 3 tl))
(roundmin-str (org-table-get-constant "roundmin"))
(roundmin (if (string= roundmin-str "#UNDEFINED_NAME") 15
(string-to-number roundmin-str)))
(md (nth 1 (timesheet-round-time-down tl)))
(mu (* (min (/ 60 roundmin) ;; most chunks possible is 4 * 15 = 60
(floor (/ (+ m (floor (* 0.67 roundmin)))
roundmin))) ;; within 5 min of 15 min boundary
roundmin)) ; 15 min chunks
tup)
(if (> mu md)
(setq m mu)
(setq m md))
(when (= m 60)
(setq m 0)
(setq h (+ h 1))) ;; BUG! overflow days!
(setq tup (cons s (cons m (cons h rest))))
tup))
;; timesheet-get-heading-path should return
;; ("Tasks" "ProjectName" "GoalName" "TaskName")
(defun timesheet-get-heading-path ()
"Return the full heading path."
;; NOTE: for a brief moment a version of org worked with simply (org-get-outline-path)
(append (org-get-outline-path) (list (nth 4 (org-heading-components)))))
;;;###autoload
(defun timesheet-clock-update-timeclock (&optional withpath)
"If this is a CLOCK line, update /round it and return t.
Otherwise, return nil. Optionally using WITHPATH."
(interactive)
(save-excursion
(beginning-of-line 1)
(skip-chars-forward " \t")
(when (looking-at org-clock-string)
(let ((re (concat "[ \t]*" org-clock-string
" *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
"\\([ \t]*=>.*\\)?\\)?"))
ts te h m s neg dts dte ets ete secs fh)
(cond
((not (looking-at re))
nil)
((not (match-end 2))
(when (and (equal (marker-buffer org-clock-marker) (current-buffer))
(> org-clock-marker (point))
(<= org-clock-marker (point-at-eol)))
;; The clock is running here
(setq org-clock-start-time
(apply 'encode-time
(org-parse-time-string (match-string 1))))
(org-clock-update-mode-line)))
(t
(and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
(end-of-line 1)
(setq ts (match-string 1)
te (match-string 3))
(setq dts (timesheet-round-time-down (org-parse-time-string ts))
dte (timesheet-round-time-up (org-parse-time-string te)))
(setq ets (apply 'encode-time dts)
ete (apply 'encode-time dte))
(setq ts (format-time-string "%Y-%m-%d %a %H:%M" ets)
te (format-time-string "%Y-%m-%d %a %H:%M" ete))
(setq s (- (org-float-time ete)
(org-float-time ets))
neg (< s 0)
s (abs s)
secs s
fh (/ secs 3600.0)
h (floor (/ s 3600))
s (- s (* 3600 h))
m (floor (/ s 60))
s (- s (* 60 s)))
(insert " => " (format "%s -- %s @ %5.2f" ts te fh))
(if withpath
(list ets ete fh (timesheet-get-heading-path))
(list ets ete fh))))))))
(defun timesheet-same-day-p (t1 t2)
"Return true of T1 and T2 (timelist format) are on the same day."
(let* ((dt1 (decode-time t1))
(dt2 (decode-time t2)))
(and (= (nth 5 dt1) (nth 5 dt2))
(= (nth 4 dt1) (nth 4 dt2))
(= (nth 3 dt1) (nth 3 dt2)))))
(defun timesheet-midnight (day-time)
"Round DAY-TIME to midnight on that day."
(let* ((day-time-cal (decode-time day-time))
(day-cal (append '(0 0 0)
(nthcdr 3 day-time-cal)))
(day (apply 'encode-time day-cal)))
day))
(defun timesheet-add-days (time days)
"Offset TIME by a positive (or negative) number of DAYS."
(let* ((day (* 60 60 24))
(d (abs days))
(time-func (if (< days 0) 'time-subtract 'time-add))
(time2 (funcall time-func time (seconds-to-time (* d day)))))
time2))
;;;###autoload
(defun timesheet-today ()
"Date for calculating timesheet: today."
(interactive)
(let* ((now (current-time))
(today (timesheet-midnight now)))
today))
;;;###autoload
(defun timesheet-yesterday ()
"Date for calculating timesheet: yesterday."
(interactive)
(timesheet-add-days (timesheet-today) -1))
;;;###autoload
(defun timesheet-at-point ()
"Date for calculating timesheet: current clock line."
(interactive)
(save-excursion
(beginning-of-line 1)
(skip-chars-forward " \t")
(when (looking-at org-clock-string)
(let ((re (concat "[ \t]*" org-clock-string
" *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
"\\([ \t]*=>.*\\)?\\)?")))
(when (looking-at re)
(timesheet-midnight (apply 'encode-time (org-parse-time-string (match-string 1)))))))))
(defun timesheet-heading ()
"Goto (or create) the Timesheet heading."
(let* ((timesheet (org-find-exact-headline-in-buffer "Timesheet" nil t)))
(when timesheet
(goto-char timesheet))
(unless timesheet
(message "adding a Timesheet...")
(goto-char (point-max))
(insert "\n* Timesheet"))
(beginning-of-line)))
;; BUG: on creating the month heading for a new month... it ends up AFTER
;; the summary (it should be before!)
(defun timesheet-heading-month (day)
"Goto (or create) the Timesheet/month heading for DAY."
(timesheet-heading)
(let* ((yyyy-mm (format-time-string "%Y-%m" day))
(m-head (org-goto-first-child))
found
firstmonth)
(unless m-head ;; no months have been entered yet
(setq firstmonth t)
(end-of-line)
(org-insert-subheading t))
(while m-head ;; checking for a matching month
(let* ((m (nth 4 (org-heading-components)))
(prev (point)))
(if (string= m yyyy-mm) ;; found it!
(progn
(setq m-head nil)
(setq found t))
(setq m-head (org-get-next-sibling)))
(unless m-head ;; we have no children
(goto-char prev)
(end-of-line))))
(unless found
(unless firstmonth
(org-insert-heading-after-current))
(insert yyyy-mm)))
(beginning-of-line))
;; FIX: insert in order
(defun timesheet-heading-day (day)
"Goto (or create) the Timesheet/month/day heading for DAY."
(timesheet-heading-month day)
;; (timesheet-debug-msg "timesheet-heading-day BEGIN")
;; (timesheet-debug-msg "timesheet-heading-day BEGIN 2")
;; (timesheet-debug-msg "timesheet-heading-day BEGIN 3")
(let* ((dday (format-time-string "%Y-%m-%d %a" day))
(d-head (org-goto-first-child))
found
firstday)
;; (timesheet-debug-msg "looking for " dday)
(unless d-head ;; no days yet
(setq firstday t)
(end-of-line)
(org-insert-subheading t))
(while d-head
(let* ((d (nth 4 (org-heading-components)))
(prev (point)))
;; (timesheet-debug-msg "checking " d)
(if (s-starts-with? dday d)
(progn
;; (timesheet-debug-msg "FOUND it")
(setq d-head nil)
(setq found t))
(setq d-head (org-get-next-sibling)))
(unless d-head ;; no more kids
;; (timesheet-debug-msg "no more kids")
(goto-char prev)
(end-of-line))))
(unless found
;; (timesheet-debug-msg "NOT found")
(unless firstday
(org-insert-heading-after-current))
(insert dday))))
(defun timesheet-cmp-task (atask btask)
"Compare two tasks and return t if ATASK < BTASK."
(let* ((apath (car atask))
(bpath (car btask))
(alen (length apath))
(blen (length bpath))
cmp)
(if (> alen blen)
(setq cmp t)
(when (= alen blen)
;; return true of the components of apath are lexicographically after bpath
(while apath
(let* ((a (pop apath))
(b (pop bpath)))
(cond ((string< b a)
(setq cmp t)
(setq apath nil)) ;; we know a < b, stop
((string= b a)
nil) ; keep comparing along apath
(t
(setq apath nil))) ;; we know a > b, stop
))
))
cmp))
(defun timesheet-paths-same-p (a b)
"Return t if paths A and B are the same."
(when (and (listp a)
(listp b)
(= (length a) (length b)))
(let* ((cmp t)
(a0 (pop a))
(b0 (pop b)))
(while a0
(if (string= a0 b0)
(progn
(setq a0 (pop a))
(setq b0 (pop b)))
(progn
(setq a0 nil)
(setq cmp nil))))
cmp)))
(defun timesheet-rollup-times (clocks)
"Sort and rollup CLOCKS."
(let* (clock
task
tasks
rtasks
(day-hours 0.0)
(project-hours 0.0)
(goal-hours 0.0)
(task-hours 0.0)
prev-task-path
prev-goal-path
prev-project-path)
(dolist (clock clocks)
(setq task (list (cdr (nth 3 clock)) (car clock) (nth 1 clock) (nth 2 clock)))
(push task tasks))
;; sort tasks by path: longest, descending
(setq rtasks (sort tasks 'timesheet-cmp-task))
;; go through.. when path changes, make roll up entry (path start stop total-hours)
(setq tasks nil)
(dolist (task rtasks)
(let* ((task-path (car task))
(goal-path (butlast task-path))
(project-path (butlast goal-path))
(hours (nth 3 task))
(p (length task-path)))
;; if the path is not the same, then make rollup for previous
(unless (timesheet-paths-same-p task-path prev-task-path)
(unless (= task-hours 0.0)
(push (list prev-task-path nil nil task-hours) tasks)
(setq task-hours 0.0))
(unless (timesheet-paths-same-p goal-path prev-goal-path)
(unless (= goal-hours 0.0)
(push (list prev-goal-path nil nil goal-hours) tasks)
(setq goal-hours 0.0))
(unless (timesheet-paths-same-p project-path prev-project-path)
(unless (= project-hours 0.0)
(push (list prev-project-path nil nil project-hours) tasks)
(setq project-hours 0.0)))))
;; add this leaf task
(push task tasks)
;; add totals
(when (> p 0)
(when (> p 1)
(when (> p 2)
(setq task-hours (+ task-hours hours)))
(setq goal-hours (+ goal-hours hours)))
(setq project-hours (+ project-hours hours)))
(setq day-hours (+ day-hours hours))
(setq prev-task-path task-path)
(setq prev-goal-path goal-path)
(setq prev-project-path project-path)
))
(unless (= task-hours 0.0)
(push (list prev-task-path nil nil task-hours) tasks))
(unless (= goal-hours 0.0)
(push (list prev-goal-path nil nil goal-hours) tasks))
(unless (= project-hours 0.0)
(push (list prev-project-path nil nil project-hours) tasks))
(push (list nil nil nil day-hours) tasks)
tasks))
(defun timesheet-calc (day)
"Calculate timesheet for the given DAY."
(let* ((clocks (timesheet-clocks day (timesheet-add-days day 1)))
(tasks (timesheet-rollup-times clocks))
(day-hours (nth 3 (pop tasks)))
task
prevpath)
(timesheet-heading-day day)
;; delete contents of heading
(org-cut-subtree)
;; insert updated data for the day
(insert (format "*** %s = %3.2f hours\n" (format-time-string "%Y-%m-%d %a" day) day-hours))
(forward-line -1)
(end-of-line)
(while (setq task (pop tasks))
(let* ((path (car task))
(p (length path))
(start (nth 1 task))
(stop (nth 2 task))
(hours (nth 3 task)))
(unless start ;; do NOT print clock entries
(insert "\n***")
(while (> p 0)
(insert "*")
(setq p (1- p)))
(insert (format " %s = %3.2f hours" (car (last path)) hours))
)
(setq prevpath path)))))
(defun timesheet-clocks (start-time end-time)
"Return a list of clocks for the time interval given by START-TIME and END-TIME."
(save-excursion
(save-restriction
(let (day-times sehp)
(goto-char (point-max))
(beginning-of-line 1)
(while (not (bobp))
(setq sehp (timesheet-clock-update-timeclock t)) ;; start end hours path
(when (and sehp
(listp sehp)
(time-less-p start-time (car sehp))
(time-less-p (car sehp) end-time))
(push sehp day-times))
(beginning-of-line 0))
day-times))))
;;;###autoload
(defun timesheet-calc-today ()
"Calculate timesheet for today."
(interactive)
(timesheet-calc (timesheet-today)))
;;;###autoload
(defun timesheet-calc-yesterday ()
"Calculate timesheet for yesterday."
(interactive)
(timesheet-calc (timesheet-yesterday)))
;;;###autoload
(defun timesheet-calc-at-point ()
"Calculate timesheet for the date on this line."
(interactive)
(let ((day (timesheet-at-point)))
(if day
(timesheet-calc day)
(message (concat "no " org-clock-string " at point!")))))
(defun timesheet-calc-all ()
"Calculate timesheet for all dates in the buffer."
(let (seen day)
(save-excursion
(goto-char (point-max))
(while (null (bobp))
(when (and (timesheet-clock-update-timeclock)
(null (member (setq day (timesheet-at-point)) seen)))
(timesheet-calc-at-point)
;; This "seen" guard provides a bit of efficiency, but every
;; call to `timesheet-calc-at-point' is still running over
;; every clock line in the buffer.
(push day seen))
(beginning-of-line 0)))))
;;;###autoload
(defun timesheet-weekly-at-point ()
"Calculate week for the date on this line."
(interactive)
(let ((day (timesheet-at-point)))
(if day
(timesheet-weekly (timesheet-week-time day))
(message (concat "no " org-clock-string " at point!")))))
(defun timesheet-cmp-string-lists (asl bsl)
"Compare two string lists and return t if ASL < BSL."
(let ((i 0)
(cmp 0)) ;; -1 <, 0 =, 1 >
(while (and (= cmp 0) (> (length asl) i) (> (length bsl) i))
(let ((a (nth i asl))
(b (nth i bsl)))
(if (string< a b)
(setq cmp -1)
(if (not (string= a b))
(setq cmp 1)))
)
(setq i (1+ i)))
(when (and (= cmp 0) (< (length asl) (length bsl)))
(setq cmp -1))
(= cmp -1)))
(defun timesheet-project-times ()
"Get list of project times for the given week."
(save-excursion
(save-restriction
(timesheet-heading)
(when (or timesheet-always-recalculate-times
;; Timesheet subtree is empty.
(= (line-number-at-pos)
(save-excursion
(org-end-of-subtree)
(line-number-at-pos))))
(timesheet-calc-all))
(let (path project-times)
(org-map-tree
(lambda ()
(setq path (timesheet-get-heading-path))
(when (and (= (length path) 4)
(null (member path project-times)))
(push (cdr path) project-times))))
(sort project-times 'timesheet-cmp-string-lists)))))
(defun timesheet-goto-weekly ()
"Goto (or create) the Weekly heading."
(let* ((weekly (org-find-exact-headline-in-buffer "Weekly" nil t)))
(when weekly
(goto-char weekly))
(unless weekly
(message "adding a Weekly...")
(goto-char (point-max))
(insert "\n* Weekly"))
(beginning-of-line)))
;; FIX: insert in order
(defun timesheet-heading-week (week &optional delete-existing-week)
"Goto (or create) the Timesheet WEEK heading.
If DELETE-EXISTING-WEEK is set then the old heading is removed."
(timesheet-goto-weekly)
(recenter-top-bottom 1)
(let* ((week-sunday (timesheet-add-days week 6))
(week-label (format "%s week #%d: %s - %s"
(format-time-string "%Y" week)
(timesheet-week-number week)
(format-time-string "%B %d" week)
(format-time-string "%B %d" week-sunday)))
(w-head (org-goto-first-child))
firstweek
found)
(end-of-line)
(unless w-head
(setq firstweek t))
(while w-head
(let* ((w (nth 4 (org-heading-components)))
(prev (point)))
(if (string= w week-label)
(progn
(setq w-head nil)
(setq found t))
(setq w-head (org-get-next-sibling)))
(unless w-head ;; we have no children
(goto-char prev)
(end-of-line))
)
)
(when (and found delete-existing-week)
(org-insert-heading)
(org-cut-subtree)
(forward-line -1))
(unless found
(org-insert-heading-after-current)
(insert week-label)
(when firstweek
(org-demote-subtree)))
(beginning-of-line)
week-label))
;;;###autoload
(defun timesheet-table-goto (top col row)
"In the table given at TOP move to a position COL ROW."
(interactive)
(goto-char top)
(forward-line row)
(org-table-goto-column col))
(defun timesheet-weekly (week)
"Calculate weekly timesheet for the given WEEK."
(let* ((all-project-times (timesheet-project-times))
(week-label (timesheet-heading-week week t))
project-times ;; day project hours
dates-cols ;; date to column alist
projects
project-rows ;; project to row alist
table-top) ;; point at the beginning of the table
(end-of-line)
(insert "\n")
;; setup dates-cols alist
(dotimes (i 7)
(let* ((day (timesheet-add-days week i)))
;; (when (= i 6)
;; (setq week-sunday day))
(push (cons (format-time-string "%Y-%m-%d" day) (+ i 2)) dates-cols)))
(dolist (pt all-project-times)
(let* ((day-total (nth 1 pt)) ;; 2013-06-11 Tue = 6.50 hours
(project-total (nth 2 pt)) ;; SuperProject = 6.50 hours
day
project
hours)
(when (string-match "^\\([0-9\-]+\\) ... = \\([0-9\.]+\\) hours" day-total)
(setq day (match-string 1 day-total)) ;; don't use match 2
(when (string-match "^\\(.+\\) = \\([0-9\.]+\\) hours" project-total)
(setq project (match-string 1 project-total))
(setq hours (match-string 2 project-total))))
(when (and day project (assoc day dates-cols))
(unless (assoc project project-rows)
(push (cons project 0) project-rows))
(push (list project day hours) project-times))
))
;; now begin to construct the table
(setq table-top (point))
(insert "#+BEGIN: columnview :hlines 1 :id global\n")
(insert "| /Project/ | Mon | Tue | Wed | Thu | Fri | Sat | Sun | /Total/ |\n")
(insert "|-----------+-----+-----+-----+-----+-----+-----+-----+---------|\n")
(insert "| | | | | | | | | |\n")
(insert "|-----------+-----+-----+-----+-----+-----+-----+-----+---------|\n")
(insert "| /Daily/ | | | | | | | | |\n")
(insert "#+TBLFM: @2$9..@II$9 = vsum($2..$8);%.2f :: @>$2..@>$9 = '(format \"%3.2f\" (apply '+ '(@II..@III)));N\n")
(insert "#+END:")
;; sort by project
(dolist (p project-rows)
(push (car p) projects))
(setq projects (sort projects 'string<)) ;; sort is destructive :(
(setq project-rows nil)
(let* ((row 3))
(dolist (p projects)
(push (cons p row) project-rows)
(when (> row 3)
(timesheet-table-goto table-top 1 (1- row))
(org-table-insert-row t))
(timesheet-table-goto table-top 1 row)
(insert p)
(setq row (1+ row))))
(goto-char table-top)
(dolist (pt project-times)
(timesheet-table-goto table-top
(cdr (assoc (nth 1 pt) dates-cols))
(cdr (assoc (car pt) project-rows)))
(insert (nth 2 pt)))
;; compute formulae in table
(org-table-iterate)
(org-table-align)))
(defun timesheet-week-time (time)
"Round TIME to beginning of the week."
(let* ((time-cal (decode-time time))
(dow (nth 6 time-cal)) ;; 0 == Sunday
(day (* 60 60 24))) ; in seconds
(when (= dow 0)
(setq dow 7))
(timesheet-midnight (time-subtract time (seconds-to-time (* day (1- dow)))))))
(defun timesheet-week-number (time)
"Calculate the ISO week number for this TIME."
(let* ((w-str (format-time-string "%W" time))
(w (string-to-number w-str)))
(1+ w)))
;;;###autoload
(defun timesheet-this-week ()
"Date for calculating timesheet: today."
(interactive)
(let* ((now (current-time))
(week (timesheet-week-time now)))
week))
;;;###autoload
(defun timesheet-last-week ()
"Date for calculating timesheet: yesterday."
(interactive)
(let* ((this (timesheet-this-week))
(day (* 60 60 24)) ; in seconds
(last (time-subtract this (seconds-to-time (* 7 day)))))
last))
;;;###autoload
(defun timesheet-weekly-this ()
"Calculate timesheet this week."
(interactive)
(timesheet-weekly (timesheet-this-week)))
;;;###autoload
(defun timesheet-weekly-last ()
"Calculate timesheet last week."
(interactive)
(timesheet-weekly (timesheet-last-week)))
;; NOTE: this is not handled particularly well... and it needs a better user interface
;; (defun timesheet-overlap (day)
;; "calculate timesheet overlap for today"
;; (interactive)
;; (let* ((clocks (timesheet-clocks day))
;; (sorted-clocks (sort clocks (lambda (a b) (time-less-p (car a) (car b)))))
;; times overlap
;; last-start last-end
;; start end
;; path
;; )
;; (goto-char (point-max))
;; (insert "\n* Overlap\n")
;; (dolist (sehp sorted-clocks)
;; (setq start (car sehp))
;; (setq end (nth 1 sehp))
;; (setq path (nth 3 sehp))
;; (cond ((and last-end (time-less-p start last-end)) ;; overlap
;; (setq overlap t)
;; )
;; ((and last-start (timesheet-same-day-p start last-start)) ;; same day
;; nil
;; )
;; (last-start ;; new day (save previous day)
;; (setq overlap nil)))
;; (when overlap
;; (insert (format "OVERLAP at %s in %s\n"
;; (format-time-string "%Y-%m-%d %a %H:%M" end)
;; path)))
;; (setq last-start start)
;; (setq last-end end)
;; )
;; (message "check overlap")
;; ))
;; (defun timesheet-overlap-today ()
;; "calculate timesheet for today"
;; (interactive)
;; (timesheet-overlap (timesheet-today)))
;; (defun timesheet-overlap-yesterday ()
;; "calculate timesheet for yesterday"
;; (interactive)
;; (timesheet-overlap (timesheet-yesterday)))
(defun timesheet-currency (v)
"Return currency value for V."
(let* ((currency (timesheet-get-file-property "Currency"))
(currency-symbol (timesheet-get-currency-symbol (or currency "USD")))
(fv (format "%s%3.2f" currency-symbol (or v 0)))
(len (length fv)))
(cond ((>= v 1000000.00)
(concat (substring fv 0 (- len 9)) ","
(substring fv (- len 9) (- len 6)) ","
(substring fv (- len 6))))
((>= v 1000.00)
(concat (substring fv 0 (- len 6)) ","
(substring fv (- len 6))))
(t
fv))))
(defun timesheet-month-time (&optional time)
"Round `current-time' (or TIME if given) to beginning of the month."
(let* ((at-time (if time time (current-time)))
(time-cal (decode-time at-time))
(first-cal (list (car time-cal)
(nth 1 time-cal)
(nth 2 time-cal)
1 ;; first day of month
(nth 4 time-cal)
(nth 5 time-cal))))
(timesheet-midnight (apply 'encode-time first-cal))))
;;;###autoload
(defun timesheet-this-month ()
"Date for calculating timesheet: this month."
(interactive)
(timesheet-month-time))
(defun timesheet-days-in-month (year month)
"How many days in the month given by YEAR MONTH."
(let* ((jason '(0 31 28 31 30 31 30 31 31 30 31 30 31))
(days (nth month jason)))
(if (and (= month 2) (date-leap-year-p year))
(1+ days)
days)))
;;;###autoload
(defun timesheet-last-month ()
"Date for calculating timesheet: last month."
(interactive)
(let* ((this-month (timesheet-this-month))
(secs-per-day (* 60 60 24))) ; in seconds
(timesheet-month-time (time-subtract this-month (seconds-to-time secs-per-day)))))
;;;###autoload
(defun timesheet-last-day-in-month (&optional time)
"Return the date for the last day in this month.
Current month or month for TIME if present."
(interactive)
(let* ((at-time (if time time (current-time)))
(time-cal (decode-time at-time))
(month (nth 4 time-cal))
(year (nth 5 time-cal))
(last-cal (list 0 ;(car time-cal)
0 ; (nth 1 time-cal)
0 ; (nth 2 time-cal)
(timesheet-days-in-month year month) ;; last day of month
month
year)))
(apply 'encode-time last-cal)))
;;;###autoload
(defun timesheet-first-day-next-month (&optional time)
"Return the date for the first day in the next month.
Current month or month for TIME if present."
(interactive)
(let* ((last-day (timesheet-last-day-in-month time))
(secs-per-day (* 60 60 24))) ; in seconds
(timesheet-month-time (time-add last-day (seconds-to-time secs-per-day)))))
;;;###autoload
(defun timesheet-invoice-this ()
"Calculate invoice this month."
(interactive)
(timesheet-invoice (timesheet-this-month)))
;;;###autoload
(defun timesheet-invoice-at-point ()
"Calculate invoice at point (a CLOCK line)."
(interactive)
(let ((day (timesheet-at-point)))
(if day
(timesheet-invoice day)
(message (concat "no " org-clock-string " at point!")))))
;;;###autoload
(defun timesheet-invoice-last ()
"Calculate invoice last month."
(interactive)
(timesheet-invoice (timesheet-last-month)))
;; MUST leave point at end of line so inserting subtrees works as expected
;;;###autoload
(defun timesheet-goto-invoices ()
"Goto (or create) the Invoices heading."
(interactive)
(let* ((invoices (org-find-exact-headline-in-buffer "Invoices" nil t)))
(when invoices
(goto-char invoices))
(unless invoices
(message "adding a Invoices heading...")
(goto-char (point-max))
(insert "\n* Invoices\n")
(forward-line -1))
(end-of-line)))
;;;###autoload
(defun timesheet-goto-invoice (month)
"Goto the invoice for the MONTH."
(interactive)
(timesheet-goto-invoices) ;; 2013-06 Invoice #430
(let* ((yyyy-mm (format-time-string "%Y-%m" month))
(m-head (org-goto-first-child))
invoice
found
firstinvoice)
(unless m-head
(setq firstinvoice t))
(while m-head
(let* ((ym (nth 4 (org-heading-components)))
(prev (point)))
(if (s-starts-with? yyyy-mm ym)
(progn
(setq m-head nil)
(setq found t))
(setq m-head (org-get-next-sibling)))
(unless m-head ;; we have no children
(goto-char prev)
(end-of-line))))
(when found
(when (org-goto-first-child)
(let ((invoice-str (org-entry-get nil "Invoice")))
(when (stringp invoice-str)
(setq invoice (string-to-number invoice-str))))
(outline-up-heading 1))
(end-of-line)
(insert "\n** old")
(org-cut-subtree)