send-reports.sh 2.44 KB
Newer Older
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
#!/bin/bash
# /cronjobs/send_reports.sh [email] [email]
#
# This cronjob runs saved reports, store them and sends result to emails

### Cronjobber til kemner
WHEN=`date +%F -d 'yesterday'`
REPORTDIR=/var/lib/state/reports
REPORTS=(
    "${WHEN}_rapport_over_innleverte_medier_med_purring.csv"
    "${WHEN}_rapport_medier_35_dager_over_forfall.csv"
    )
EMAILS=()

usage() {
  echo -e "\nUsage:\n$0 [-e|--email] \n"
  exit 1
}

send_reports() {
    local BOUNDARY="T/asfAY23523.34"
    for email in "${EMAILS[@]}"
    do
        echo "sending report to: ${email}"
        { printf "%s\n" \
            "Subject: Kemnerrapporter ${WHEN}" \
            "From: Deichman <noreply@deichman.no>" \
            "To: ${email}" \
            "Mime-Version: 1.0" \
            "Content-Type: multipart/mixed; boundary=\"$BOUNDARY\"" \
            \
            "--${BOUNDARY}" \
            "Content-Type: text/plain" \
            "Content-Disposition: inline" \
            "" \
            "Vedlagt ligger rapporter for ${WHEN}";

        for REPORT in "${REPORTS[@]}"; do
            printf "%s\n" "--${BOUNDARY}" \
            "Content-Type: text/csv" \
            "Content-Transfer-Encoding: base64" \
            "Content-Disposition: attachment; filename=\"${REPORT}\""\
            "";
            base64 ${REPORTDIR}/${REPORT}
            echo
        done
        printf '%s\n' "--${BOUNDARY}--"
        } | sendmail "${email}" -t -oi
    done
}

save_reports() {
    mkdir -p $REPORTDIR
    echo "saving reports to $REPORTDIR"
    # 28 - Erstatninger - Kategorier B,V,I som hadde 35 dager over forfall
    /koha/misc/cronjobs/runreport.pl --separator=";" --format=csv $STORE 28 > $REPORTDIR/${WHEN}_rapport_medier_35_dager_over_forfall.csv
    # 29 - Purregebyrer - Kategorier B,V,I som hadde 35 dager over forfall - append to previos report
    /koha/misc/cronjobs/runreport.pl --separator=";" --format=csv $STORE 29 | tail -n+2 >> $REPORTDIR/${WHEN}_rapport_medier_35_dager_over_forfall.csv
    # 45 - Rapport over innleverte medier som hadde status Regning eller Forlengst forfalt
    /koha/misc/cronjobs/runreport.pl --separator=";" --format=csv $STORE 45 > $REPORTDIR/${WHEN}_rapport_over_innleverte_medier_med_purring.csv
}

while [ "$1" != "" ]; do
    case "$1" in
      --email|-e)
        shift
        EMAILS+=("$1")
        ;;
      --store|-s)
        STORE="--store-results"
        ;;
      --help|-h)
        usage
        ;;
    esac
    shift
done

save_reports
send_reports