251 lines
7.3 KiB
Scheme
251 lines
7.3 KiB
Scheme
;;
|
|
;; notifications.scm
|
|
;;
|
|
;; Email notifications and reminders.
|
|
;;
|
|
;; ISC License
|
|
;;
|
|
;; Copyright 2023 Brmlab, z.s.
|
|
;; Dominik Pantůček <dominik.pantucek@trustica.cz>
|
|
;;
|
|
;; Permission to use, copy, modify, and/or distribute this software
|
|
;; for any purpose with or without fee is hereby granted, provided
|
|
;; that the above copyright notice and this permission notice appear
|
|
;; in all copies.
|
|
;;
|
|
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
;; WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
;; WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
;; AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
|
|
;; CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
;; OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
;; NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
;; CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
;;
|
|
|
|
(declare (unit notifications))
|
|
|
|
(module
|
|
notifications
|
|
(
|
|
make+print-reminder-email
|
|
make+send-reminder-email
|
|
|
|
make+print-summary-email
|
|
make+send-summary-email
|
|
)
|
|
|
|
(import scheme
|
|
(chicken base)
|
|
(chicken format)
|
|
(chicken string)
|
|
(chicken sort)
|
|
brmember
|
|
util-mail
|
|
util-bst-ldict
|
|
members-payments
|
|
util-format
|
|
brmember-format
|
|
configuration
|
|
util-time
|
|
members-fees
|
|
mbase
|
|
members-print
|
|
table
|
|
bank-account
|
|
logging)
|
|
|
|
;; Prints email to the console
|
|
(define (print-notification-email em)
|
|
(print "### From: " (ldict-ref em 'from (*email-from*)))
|
|
(print "### To: " (ldict-ref em 'to))
|
|
(print "### Subject: " (ldict-ref em 'subject))
|
|
(when (*notifications-cc*)
|
|
(print "### CC: " (*notifications-cc*)))
|
|
(let loop ((lines (ldict-ref em 'body)))
|
|
(when (not (null? lines))
|
|
(print (car lines))
|
|
(loop (cdr lines)))))
|
|
|
|
;; Sends notification email - the dictionary representation
|
|
(define (send-notification-email em)
|
|
(log-info "Sending ~A to ~A~A"
|
|
(ldict-ref em 'subject)
|
|
(ldict-ref em 'to)
|
|
(if (*mailto-override*)
|
|
(format " (overriden to ~A)" (*mailto-override*))
|
|
""))
|
|
(send-mail (ldict-ref em 'body)
|
|
#:from (*email-from*)
|
|
#:headers (ldict-ref em 'headers '())
|
|
#:to (ldict-ref em 'to)
|
|
#:subject (ldict-ref em 'subject)))
|
|
|
|
;; Creates reminder email body
|
|
(define (reminder-email-body mr)
|
|
(let ((C identity)
|
|
(M (lambda (s)
|
|
(brmember-format s mr)))
|
|
(F format)
|
|
(absdebt (format-amount (- (member-total-balance mr))))
|
|
(macc "2500079551/2010"))
|
|
(list
|
|
(M "Ahoj ~N,")
|
|
(C "podle evidence členů a bankovního účtu to vypadá, že máš nedoplatek")
|
|
(F "ve výši ~A Kč." absdebt)
|
|
(C "Zkontroluj, prosím, zda tvé platby členských příspěvků")
|
|
(M "s tvým variabilním symbolem ~I byly zaslány na")
|
|
(F "správný účet ~A." macc)
|
|
(C "Historii členských poplatků si po přihlášení na brmlab.cz můžeš zkontrolovat")
|
|
(C "také na adrese https://brmlab.cz/?do=payments")
|
|
(C "Pokud se domníváš, že někde vznikla chyba, neprodleně nás prosím kontaktuj.")
|
|
""
|
|
""
|
|
(M "Dear ~N,")
|
|
(C "according to the members database and bank account statements, it appears")
|
|
(F "your payment of ~A CZK is missing." absdebt)
|
|
(C "Please, check whether your membership fees were transfered")
|
|
(M "correctly with the variable symbol (identification) ~I to")
|
|
(F "the correct bank account ~A." macc)
|
|
(C "The member fees history can be seen at address https://brmlab.cz/?do=payments")
|
|
(C "(you may need to login to brmlab.cz first).")
|
|
(C "If you think there is an error in our evidence, please contact us ASAP.")
|
|
""
|
|
"--"
|
|
"Brmlab Hackerspace Members Database"
|
|
)))
|
|
|
|
;; Creates reminder email dictionary
|
|
(define (make-reminder-email mr)
|
|
(make-ldict
|
|
`((to . ,(brmember-info mr 'mail))
|
|
(subject . "Připomínka členských příspěvků / Membership fees reminder")
|
|
(body . ,(reminder-email-body mr))
|
|
(header .,(list (format "Content-Type: text/plain; charset=\"UTF-8\"")))
|
|
)))
|
|
|
|
;; Creates and prints reminder email for given member record
|
|
(define (make+print-reminder-email mr)
|
|
(print-notification-email
|
|
(make-reminder-email mr)))
|
|
|
|
;; Actually send emails
|
|
(define (make+send-reminder-email mr)
|
|
(let ((em (make-reminder-email mr)))
|
|
(send-notification-email em)))
|
|
|
|
;; Summary email of membership fees payments
|
|
(define (summary-email-body mb)
|
|
(let* ((mbs (members-summary mb))
|
|
(students (car mbs))
|
|
(full (cdr mbs))
|
|
(income (+ (* (lookup-member-fee 'normal) full)
|
|
(* (lookup-member-fee 'student) students)))
|
|
(income-lst
|
|
(list (format "Expected income: ~A CZK" income)
|
|
(format " ~A full members" full)
|
|
(format " ~A students" students)))
|
|
(unpaired (mbase-unpaired mb))
|
|
(unpaired-lst
|
|
(if (null? unpaired)
|
|
'()
|
|
(append
|
|
(list ""
|
|
"Unpaired transactions:")
|
|
(unpaired-table mb #:border-style 'ascii))))
|
|
(debtors (sort
|
|
(members-to-notify mb 1)
|
|
brmember<?))
|
|
(debtors-lst
|
|
(if (null? debtors)
|
|
'()
|
|
(append
|
|
(list ""
|
|
"Active debtors:")
|
|
(table->string-list
|
|
(cons (list "Id" "Member" "Type" "Balance" "Last payment")
|
|
(map (lambda (mr)
|
|
(list (brmember-id mr)
|
|
(brmember-nick mr)
|
|
(if (brmember-student? mr)
|
|
'student
|
|
'normal)
|
|
(format "\t~A" (+ 0.0 (member-total-balance mr)))
|
|
(let* ((payments (brmember-payments mr))
|
|
(tr (if (null? payments)
|
|
#f
|
|
(car (reverse payments)))))
|
|
(if tr
|
|
(bank-transaction-date tr)
|
|
"-"))
|
|
))
|
|
debtors))
|
|
#:border-style 'ascii
|
|
#:border '(((#:bottom #:right light) ... (#:bottom light))
|
|
((#:right light) ... none)
|
|
...)
|
|
))))
|
|
(boring (sort
|
|
(find-members-by-predicate
|
|
mb
|
|
(lambda (mr)
|
|
(and (brmember-active? mr)
|
|
(not (member-to-notify? mr)))))
|
|
brmember<?))
|
|
(boring-lst
|
|
(if (null? boring)
|
|
'()
|
|
(append
|
|
(list ""
|
|
"Other active members:")
|
|
(table->string-list
|
|
(cons (list "Id" "Member" "Type" "Balance" "Last payment")
|
|
(map (lambda (mr)
|
|
(list (brmember-id mr)
|
|
(brmember-nick mr)
|
|
(if (brmember-student? mr)
|
|
'student
|
|
'normal)
|
|
(format "\t~A" (member-total-balance mr))
|
|
(let* ((payments (brmember-payments mr))
|
|
(tr (if (null? payments)
|
|
#f
|
|
(car (reverse payments)))))
|
|
(if tr
|
|
(bank-transaction-date tr)
|
|
"-"))
|
|
))
|
|
boring))
|
|
#:border-style 'ascii
|
|
#:border '(((#:bottom #:right light) ... (#:bottom light))
|
|
((#:right light) ... none)
|
|
...)
|
|
)))))
|
|
(append income-lst
|
|
unpaired-lst
|
|
debtors-lst
|
|
boring-lst
|
|
(list ""
|
|
"--"
|
|
"Brmlab Hackerspace Members Database"
|
|
))))
|
|
|
|
;; Creates the summary email structure
|
|
(define (make-summary-email mb)
|
|
(make-ldict
|
|
`((to . ,(*summary-mailto*))
|
|
(subject . ,(format "Členské příspěvky ~A"
|
|
(today/iso)))
|
|
(body . ,(summary-email-body mb)))))
|
|
|
|
;; Just print to standard output
|
|
(define (make+print-summary-email mb)
|
|
(let ((em (make-summary-email mb)))
|
|
(print-notification-email em)))
|
|
|
|
;; Actually send emails
|
|
(define (make+send-summary-email mr)
|
|
(let ((em (make-summary-email mr)))
|
|
(send-notification-email em)))
|
|
|
|
)
|