aboutsummaryrefslogtreecommitdiff
path: root/module/calp/entry-points/tidsrapport.scm
blob: a258cd73a63aaaff3bdcdc93c87d6d3e594adc14 (plain)
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
;;; Commentary:
;;; pdftk <filename.pdf> dump_data_fields to get field names (including checkbox values)
;;; Sample template file:
;; ((Text1 förnamn)
;;  (Text2 efternamn)
;;  (Text3 personnummer)
;;  (Text4 address)
;;  (Text5 postnummer)
;;  (Text6 postort)
;;  (Text7 medborgarskap)
;;  (Text8 telefonnummer)
;;  (Text9 mailaddress)
;;  (Text115 institution-enhet-kurs-program)
;;  (Text176 namnförtydligande)
;;  ("Check Box15" har-swedbank)

;;  (groups
;;    (group
;;      (prefix Text)
;;      (summary 16)
;;      (days (- 17 47))
;;      (sum 48))
;;    (group
;;      (prefix Text)
;;      (summary 49)
;;      (days (- 50 80))
;;      (sum 81))
;;    (group
;;      (prefix Text)
;;      (summary 82)
;;      (days (- 83 113))
;;      (sum 114))))

;;; Sample from data file
;; ((förnamn "Hugo"))

;;; Code:


(define-module (calp entry-points tidsrapport)
  :use-module (hnh util)
  :use-module (hnh util options)
  :use-module (ice-9 getopt-long)
  :use-module (calp translation)
  :use-module (sxml simple)
  :use-module (datetime)
  :use-module (srfi srfi-41)
  :use-module (srfi srfi-41 util)
  :use-module (srfi srfi-1)
  :use-module (vcomponent)
  :use-module (datetime)
  :use-module (vcomponent util instance)
  :use-module (vcomponent util instance methods)
  :use-module (hnh util)
  :use-module (ice-9 regex)
  :use-module (ice-9 popen)
  :export (main))


(define event-set
  (get-event-set global-event-object))

(define (get-worked-hours summary-search month year)

  (define instances
   (group-by (compose day as-date (extract 'DTSTART))
             (stream->list
              ((@ (vcomponent util search) execute-query)
               (lambda (e)
                 (define d (as-datetime (prop e 'DTSTART)))
                 (define s (date year: year month: month day: 1))

                 (and (string=? summary-search (prop e 'SUMMARY))
                      (datetime<=? (datetime date: s) d)
                      (datetime<=? d (datetime date: (date+ s (date month: 1))))))
               event-set
               ))))

  (define by-day (make-vector 31 0))

  (define (exactify n)
    (if (= n (round n))
        (inexact->exact n)
        n))

  (for-each (lambda (group)
              (define day (car group))
              (vector-set! by-day day
                           (exactify
                            (apply +
                                   (map (lambda (e)
                                          (time->decimal-hour
                                           (as-time
                                            (datetime-difference (prop e 'DTEND)
                                                                 (prop e 'DTSTART)))))
                                        (cdr group))))))
            instances)



  (vector->list by-day))

(define (build-alist work-hours fields)
  (filter-map
    (lambda (f n)
      (if (= 0 n)
        #f
        (list (string->symbol f)
              n)))
    fields
    work-hours))



(define (fill-from-alist template-list data-list)
  (filter-map (lambda (pair)
                (cond ((assoc-ref data-list (cadr pair))
                       => (lambda (it) (cons (car pair) it)))
                      (else #f)))
              template-list))



;; 
;;        [ 1, 31]
;; Text16 [17, 47] Text48
;; Text49 [50, 80] Text81
;; Text82 [83, 113] Text114
;; 
(define (format-field key value)
  (format #f "~%<<~%/T (~a)~%/V (~a)~%>>"
          key value))

(define prefix-string
  "%FDF-1.2
%âãÏÓ
1 0 obj

<<
/FDF 
<<
/Fields [")

(define post-string
  "]
>>
>>
endobj

trailer

<<
/Root 1 0 R
>>
%%EOF")


(define (generate-fdf report)
  (string-append prefix-string
                 (string-join (map (lambda (pair)
                                     (apply format-field pair))
                                   report))
                 post-string)
)

(define opt-spec
  `((pdf (value #t)
         (description ,(G_ "Input pdf file")))
    (output (single-char #\o) (value optional)
            (description ,(G_ "Output file")))

    (data (value optional)
          (description ,(G_ "Static data to fill fields with"))
          )
    (template (value optional)
              (description ,(xml->sxml (G_ "<group>Map between real field names and human readable names.<br/>
If data is given, but not trans, then data is assumed to be in a correct format</group>"))))
    (search (value #t)
            (description
             ,(G_ "Search term for dynamic filling. Supports basic globbing")))))

(define (parse-search str)
  (cond [(string-match "\\{(.*)\\}" str)
         => (lambda (m)
              (map (lambda (option)
                     (string-replace str option
                                     (match:start m)
                                     (match:end m)))
                   (string-split (match:substring m 1) #\,)))]
        [else (list str)]))

(define (main args)
  (define opts (getopt-long args (getopt-opt opt-spec)))

  (define input-pdf (option-ref opts 'pdf #f))
  (define output-pdf (or (option-ref opts 'output #f)
                         (and input-pdf
                              (string-append (dirname input-pdf)
                                             "/" (basename input-pdf ".pdf")
                                             "-output.pdf"))))

  (define data (option-ref opts 'data #f))
  (define template
    (call-with-input-file
     (or (option-ref opts 'template #f)
         (error (G_ "Template required")))
     read))

  (define prepared-data

    (cond ((and template data)
           (fill-from-alist template
                            (call-with-input-file data read)))
          (data (call-with-input-file data read))
          (template '())
          (else '())))

  (define search (parse-search (option-ref opts 'search #f)))

  ;; month year
  (define rem (map string->number (option-ref opts '() '())))
  ;; TODO warn when length(search) > 3 (number of rows in pdf)

  (define auto-filled
    (concatenate
     (map (lambda (group search-term)
            (define prefix (->string (car (or (assoc-ref group 'prefix) (list (symbol))))))
            (define summary
              (string-append prefix (->string (car (assoc-ref group 'summary)))))
            (define sum (string-append prefix (->string (car (assoc-ref group 'sum)))))
            (define days
              (let ((days (assoc-ref group 'days)))
                (cond ((not (list? days))
                       (error (G_ "Needs list, not pair")))
                      ((null? days)
                       (error (G_ "Need more days")))
                      ((and (list? (car days)) (eqv? '- (caar days)))
                       (map (lambda (s) (string-append prefix (->string s)))
                            (iota (1+ (- (list-ref (car days) 2)
                                         (list-ref (car days) 1)))
                                  (list-ref (car days) 1))))
                      ;; TODO case where cadr is a list, instead of cdr is the list?
                      (else
                       (map (lambda (s) (string-append prefix (->string s)))
                            days)))))

            (define work-hours (apply get-worked-hours search-term rem))
            `((,summary ,(format #f "~a ~a" (locale-month (car rem)) search-term))
              ,@(build-alist work-hours days)
              (,sum ,(apply + work-hours))))
          (or (assoc-ref template 'groups)
              (error (G_ "Groups required in template")))
          search)))

  (define report
    (append
     prepared-data
     auto-filled))


  (if input-pdf
      (let ((port (open-pipe* OPEN_WRITE
                         "pdftk" input-pdf "fill_form" "-"
                         "output" output-pdf)))
        (set-port-encoding! port "ISO-8859-1")
        (display (generate-fdf report) port)
        (newline port)
        ;; (put-bytevector port (generate-fdf report))
        (close-pipe port))
      (display (generate-fdf report))))