summaryrefslogtreecommitdiff
path: root/contrib/raw/ledger-matching.el
blob: 36006a695629c4a9990cba6c40a6c6a8a714e8d3 (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
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
;; This library is intended to allow me to view a receipt on one panel, and tie it to ledger transactions in another

(require 'ledger-report)

(defgroup ledger-matching nil
  "Ledger image matching")

(defcustom ledger-matching-sourcedir "~/AdamsInfoServ/BusinessDocuments/Ledger/Incoming"
  "Source directory for images to process, ie: the incoming queue of images."
  :group 'ledger-matching)

(defcustom ledger-matching-destdir "~/AdamsInfoServ/BusinessDocuments/Ledger/AdamsRussell/Receipts"
  "Destination directory for images when matched, will still have a project directory appended to it."
  :group 'ledger-matching)

(defcustom ledger-matching-relative-receipt-dir "Receipts"
  "Relative directory root for destination images used in Ledger entries, will have the project directory appended and receipt filename."
  :group 'ledger-matching)

(defcustom ledger-matching-convert-binary "/usr/bin/convert"
  "Path to the Imagemagick convert command."
  :group 'ledger-matching)

(defcustom ledger-matching-scale 50
  "Scaling parameter to Imagemagick's convert to resize an image for viewing."
  :group 'ledger-matching)

(defcustom ledger-matching-rotation 0
  "Rotation parameter to Imagemagick's convert to rotate an image for viewing. Images on disk should always be upright for reading."
  :group 'ledger-matching)


(defconst ledger-matching-image-buffer "*Receipt*"
  "Buffer name we load images into. Created if it doesn't exist, and persists across image loads.")


(defvar ledger-matching-project "Internal"
  "The directory appended to the destination for the project code where receipts will be stored.")

(defvar ledger-matching-image-offset 0
  "The index of the current file from the SORTED source directory contents.")

(defvar ledger-matching-image-name nil
  "The filename only of the current image.")


(defun ledger-matching-display-image (image-filename)
  "Resize the image and load it into our viewing buffer."

  ;; Create our viewing buffer if needed, and set it. Do NOT switch,
  ;; this buffer isn't the primary. Let the user leave it where they
  ;; place it.
  (unless (get-buffer ledger-matching-image-buffer)
    (get-buffer-create ledger-matching-image-buffer))
  (set-buffer ledger-matching-image-buffer)
  (erase-buffer)
  (goto-char (point-min))
  (insert-string image-filename "\n")

  ;; Convert the source to the temporary dest applying resizing and rotation
  (let* ((source (expand-file-name image-filename ledger-matching-sourcedir))
         (dest (make-temp-file "ledger-matching-" nil ".jpg"))
         (result (call-process ledger-matching-convert-binary nil (get-buffer "*Messages*") nil
                               source
                               "-scale" (concat (number-to-string ledger-matching-scale) "%")
                               "-rotate" (number-to-string ledger-matching-rotation)
                               dest)))

    (if (/= 0 result)

        ;; Bomb out if the convert fails
        (message "Error running convert, see *Messages* buffer for details.")

      ;; Insert scaled image into the viewing buffer, replacing
      ;; current contents Temp buffer is to force sync reading into
      ;; memory of the jpeg due to async race condition with display
      ;; and file deletion
      (let ((image (create-image (with-temp-buffer
                                   (insert-file-contents-literally dest)
                                   (string-as-unibyte (buffer-string)))
                                 'jpeg t)))
        (insert-image image)
        (goto-char (point-min))

        ;; Redisplay is required to prevent a race condition between displaying the image and the deletion. Apparently its async.
        ;; Either redisplay or the above string method work, both together can't hurt.
        (redisplay)
        ))

    ;; Delete our temporary file
    (delete-file dest)))



(defun ledger-matching-update-current-image ()
  "Grab the image from the source directory by offset and display"

  (let* ((file-listing (directory-files ledger-matching-sourcedir nil "\.jpg$" nil))
         (len (safe-length file-listing)))

    ;; Ensure our offset doesn't exceed the file list
    (cond ((= len 0)
           (message "No files found in source directory."))

          ((< len 0)
           (message "Error, list of files should never be negative. Epic fail."))

          ((>= ledger-matching-image-offset len)
           (message "Hit end of list. Last image.")
           (setq ledger-matching-image-offset (1- len)))

          ((< ledger-matching-image-offset 0)
           (message "Beginning of list. First image.")
           (setq ledger-matching-image-offset 0)))

    ;; Get the name for the offset
    (setq ledger-matching-image-name (nth ledger-matching-image-offset file-listing))

    (ledger-matching-display-image ledger-matching-image-name)))



(defun ledger-matching-image-offset-adjust (amount)
  "Incr/decr the offset and update the receipt buffer."

  (setq ledger-matching-image-offset (+ ledger-matching-image-offset amount))
  (ledger-matching-update-current-image))



(defun ledger-receipt-matching ()
  "Open the receipt buffer and start with the first image."
  (interactive)
  (setq ledger-matching-image-offset 0)
  (ledger-matching-update-current-image))



(defun ledger-matching-tie-receipt-to-txn ()
  (interactive)
  (save-selected-window
    (ledger-report-visit-source)

    ;; Assumes we're in a narrowed buffer with ONLY this txn
    (backward-paragraph)
    (beginning-of-line)

    ;; ;; Update the ER and Project while I'm there
    ;; (save-excursion
    ;;   (search-forward "; ER:")
    ;;   (kill-line nil)
    ;;   (insert " " *ledger-expense-shortcut-ER*))
    ;; Just do the project for now.
    (save-excursion
      (search-forward "; PROJECT:")
      (kill-line nil)
      (insert " " *ledger-expense-shortcut-Proj*))

    ;; Goto the receipt line, unless their isn't one then add one
    (unless (search-forward "RECEIPT:" nil t)

      ;; Still at date line if that failed
      (next-line)
      (newline)
      (insert-string "    ; RECEIPT:"))

    ;; Point immediately after : on tag

    ;; Check for existing jpg file
    (if (search-forward ".jpg" (line-end-position) t)

        ;; if present make it a comma delimited list
        (insert-string ",")

      ;; otherwise just add a space to pad
      (insert-string " "))

    ;; Add our relative filename as the value of the RECEIPT tag
    (insert-string (concat ledger-matching-relative-receipt-dir "/"
                           ledger-matching-project "/"
                           ledger-matching-image-name))

    ;; Create the destination project dir if it doesn't exist.
    (let ((full-destination (concat ledger-matching-destdir "/" ledger-matching-project )))
      (unless (file-accessible-directory-p full-destination)
        (make-directory full-destination t)))

    ;; Rename the file from the source directory to its permanent home
    (rename-file (concat ledger-matching-sourcedir "/"
                         ledger-matching-image-name)
                 (concat ledger-matching-destdir "/"
                         ledger-matching-project "/"
                         ledger-matching-image-name))

    ;; Update the receipt screen
    (ledger-matching-update-current-image)

    (message "Filed %s to project %s" ledger-matching-image-name ledger-matching-project)))



(defun ledger-receipt-skip ()
  "Move the current image to the Skip directory because its not relevant."

  (rename-file (concat ledger-matching-sourcedir "/"
                       ledger-matching-image-name)
               (concat ledger-matching-sourcedir "/Skip/"
                       ledger-matching-image-name))

  ;; Update the receipt screen at the same offset
  (ledger-matching-update-current-image))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Items below are speed entry macros, and should eventually migrate to their own file.

(defvar *ledger-expense-shortcut-ER*
  "Current expense report number, just last four digits (ie: 1234 results in AISER1234).")

(defvar *ledger-expense-shortcut-split-ER*
  "Split (ie: internal) expense report number, just last four digits (ie: 1234 results in AISER1234).")

(defvar *ledger-expense-shortcut-Proj* ""
  "Current export report project code (ie: AGIL1292)")

(defun ledger-expense-shortcut-ER-format-specifier () *ledger-expense-shortcut-ER*)

(defun ledger-expense-shortcut-Project-format-specifier () *ledger-expense-shortcut-Proj*)

(defun ledger-expense-shortcut-setup (ER Split Proj)
  "Sets the variables expanded into the transaction."
  (interactive "MER Number (ER or IN and 4 digit number only): \nMSplit ER Number (ER or IN and 4 digit number only): \nMProject: ")
  (setq *ledger-expense-shortcut-ER*
        (concatenate 'string "AIS" ER))
  (setq *ledger-expense-shortcut-split-ER*
        (concatenate 'string "AIS" Split))
  (setq *ledger-expense-shortcut-Proj* Proj)
  (setq ledger-matching-project Proj)
  (message "Set Proj to %s and ER to %s, split to %s"
           *ledger-expense-shortcut-Proj*
           *ledger-expense-shortcut-ER*
           *ledger-expense-shortcut-split-ER*))

(defun ledger-expense-shortcut ()
  "Updates the ER and Project metadata with the current values of the shortcut variables."
  (interactive)
  (when (eq major-mode 'ledger-mode)
    (if (or (eql *ledger-expense-shortcut-ER* "")
            (eql *ledger-expense-shortcut-Proj* ""))
        (message "Run ledger-expense-shortcut-setup first.")
      (save-excursion
        (search-forward "; ER:")
        (kill-line nil)
        (insert " " *ledger-expense-shortcut-ER*))
      (save-excursion
        (search-forward "; PROJECT:")
        (kill-line nil)
        (insert " " *ledger-expense-shortcut-Proj*)))))

(defun ledger-expense-split ()
  "Splits the current transaction between internal and projects."
  (interactive)
  (when (eq major-mode 'ledger-mode) ; I made this local now, should only trigger in ldg-mode
    (save-excursion
      (end-of-line)
      (re-search-backward "^[0-9]\\{4\\}/")
      (re-search-forward "^ +Dest:Projects")
      (move-beginning-of-line nil)
      (let ((begin (point))
            (end (re-search-forward "^$")))
        (goto-char end)
        (insert (buffer-substring begin end))
        (goto-char end)
        (re-search-forward "^    Dest:Projects")
        (replace-match "    Dest:Internal")
        (re-search-forward "; ER: +[A-Za-z0-9]+")
        (replace-match (concat "; ER: " *ledger-expense-shortcut-split-ER*)  t)
        (when (re-search-forward "; CATEGORY: Meals" (save-excursion (re-search-forward "^$")) t)
          (replace-match "; CATEGORY: Travel" t))))
    (re-search-backward "^[0-9]\\{4\\}/")
    (re-search-forward "^ +Dest:Projects")
    (insert-string "                                     $") ))

(defun ledger-expense-internal ()
  "Makes the expense an internal one."
  (interactive)
  (when (eq major-mode 'ledger-mode) ; I made this local now, should only trigger in ldg-mode
    (save-excursion
      (end-of-line)
      (re-search-backward "^[0-9]\\{4\\}/")
      (let ((begin (point))
            (end (save-excursion (re-search-forward "^$"))))
        (when (re-search-forward "^    Dest:Projects" end t)
          (replace-match "    Dest:Internal") )
        (when (re-search-forward "; CATEGORY: Meals" (save-excursion (re-search-forward "^$")) t)
          (replace-match "; CATEGORY: Travel" t))))))

(defun ledger-expense-personal ()
  "Makes the expense an personal one, eliminating metadata and receipts."
  (interactive)
  (when (eq major-mode 'ledger-mode) ; I made this local now, should only trigger in ldg-mode
    (save-excursion
      (end-of-line)
      (re-search-backward "^[0-9]\\{4\\}/")
      (let ((begin (point))
            (end (save-excursion (re-search-forward "^$"))))
        (when (re-search-forward "^    Dest:Projects" end t)
          (replace-match "    Other:Personal"))
        (goto-char begin)
        (save-excursion
          (when (re-search-forward "^ +; ER:" end t)
            (beginning-of-line)
            (kill-line 1)))
        (save-excursion
          (when (re-search-forward "^ +; PROJECT:" end t)
            (beginning-of-line)
            (kill-line 1)))
        (save-excursion
          (when (re-search-forward "^ +; CATEGORY:" end t)
            (beginning-of-line)
            (kill-line 1)))
        (save-excursion
          (when (re-search-forward "^ +; RECEIPT:" end t)
            (beginning-of-line)
            (kill-line 1)))
        (ledger-toggle-current-entry)))))

(defun ledger-expense-show-receipt ()
  "Uses the Receipt buffer to show the receipt of the txn we're on."
  (when (eq major-mode 'ledger-mode) ; I made this local now, should only trigger in ldg-mode
    (save-excursion
      (end-of-line)
      (re-search-backward "^[0-9]\\{4\\}/")
      (let ((begin (point))
            (end (save-excursion (re-search-forward "^$"))))
        (save-excursion
          (when (re-search-forward "^\\( +; RECEIPT: +\\)\\([^,]+?.jpg\\).*$" end t)
            (ledger-matching-display-image
             (concat "/home/adamsrl/AdamsInfoServ/BusinessDocuments/Ledger/AdamsRussell/"
                     (match-string 2))) ))))))


(provide 'ledger-matching)