]> git.evergreen-ils.org Git - contrib/pines.git/blob - batch_void_fines.sh
add local git tar script
[contrib/pines.git] / batch_void_fines.sh
1 #!/bin/bash
2
3 # Copyright (C) 2014 Georgia Public Library Service
4 # Chris Sharp <csharp@georgialibraries.org>
5 #
6 # A program that takes a library system or branch name and a date or set of dates
7 # on the command line to apply a batch void for billings applied on that day.
8 # This is particularly useful if closed dates are set after the fine generator has 
9 # run for that day.
10 #
11 # This script assumes that you are on a server with direct access to your master
12 # database, and that you have set the credentials for that database in the .pgpass 
13 # file in your user's home directory. 
14
15 # See http://www.postgresql.org/docs/9.1/static/libpq-pgpass.html for more information.
16 #
17 # Usage Notes:
18 #
19 # You'll want to set the $EGUSER/$EGPASS variables to the username/password of an Evergreen
20 # user (e.g., your administrative user in Evergreen).  Adjust the $SRFSH variable to suit 
21 # your environment.  As of this writing, we're still installing Evergreen in /openils.
22 # The $VOID_NOTE variable should be something informative to your staff users as to why these
23 # bills were voided.  Our use case was closings due to inclement weather.
24 #
25 # The $BACKUP_SCHEMA variable should be set to a schema in your postgresql database that is
26 # for administrative use.  You should NOT use an existing Evergreen schema name (e.g. "action", 
27 # "actor", etc. for this purpose.  I use "csharp" for that in our case.
28 #
29 # My hope is that this can be re-implemented in Perl or something with actual OpenSRF bindings, 
30 # but my current skill level and available time prevented me from doing so now.
31
32 EGUSER="myuser"
33 EGPASS="mypass"
34 SRFSH="/openils/bin/srfsh"
35 DBHOST="mydbhost"
36 DBUSER="mydbuser"
37 VOID_NOTE="VOIDED BY SYSTEM STAFF - INCLEMENT WEATHER"
38 BACKUP_SCHEMA="myschema"
39
40 Usage() { echo "Usage: ./batch_void_fines.sh -d YYYY-MM-DD,YYYY-MM-DD,... -s systemname OR -b branchname." 
41 }
42
43 while getopts s:b:d:h OPTIONS
44 do  case "$OPTIONS" in
45     s)  SYSTEM="$OPTARG";;
46     b)  BRANCH="$OPTARG";;
47     d)  DATE="$OPTARG";;
48     h)  Usage ; exit 1;;
49     *)  Usage ; exit 2;;
50     esac
51 done
52
53 if [ -n "$SYSTEM" ] && [ -n "$BRANCH" ]; then
54     echo "Please only specify either system OR branch.  Not both."
55         Usage
56     exit 1;
57 fi
58
59 if [ -z "$DATE" ]; then
60         echo "Date is required."
61     Usage
62     exit 1;
63 fi
64
65 if [ ! $(echo $DATE | egrep '20[0-9][0-9]-[01][0-9]-[0-3][0-9](,20[0-9][0-9]-[01][0-9]-[0-3][0-9])?') ]; then
66         echo "Date must be in YYYY-MM-DD format.  Please check."
67         Usage
68         exit 1;
69 fi
70
71 if [ $(echo $DATE | grep ",") ]; then
72     SPLITDATE=`echo $DATE | sed "s/,/', '/g"`
73     DATE=$SPLITDATE
74 fi
75
76 if [ -n "$SYSTEM" ]; then
77         if [ $(echo $SYSTEM | grep "-") ]; then
78                 echo "System names do not have hyphens.  Did you mean -b?"
79                 Usage
80                 exit 1;
81         fi
82         BACKUP_TABLE="$BACKUP_SCHEMA.`echo $SYSTEM | tr '[:upper:]' '[:lower:]'`_batch_voided_fines_`echo $DATE | tr '-' '_' | sed "s/'//g" | sed "s/, /_/g"`"
83 else            
84         if [ ! $(echo $BRANCH | grep "-") ]; then
85         echo "Branch names have hyphens.  Did you mean -s?"
86         Usage
87         exit 1;
88     fi
89         BACKUP_TABLE="$BACKUP_SCHEMA.`echo $BRANCH | tr '[:upper:]' '[:lower:]' | tr '-' '_'`_batch_voided_fines_`echo $DATE | tr '-' '_' | sed "s/'//g" | sed "s/, /_/g"`"
90 fi
91 read -d '' SYSTEM_Q <<END_OF_Q
92 SELECT * INTO $BACKUP_TABLE
93 FROM money.billing mb
94 WHERE date(billing_ts) IN (
95     '$DATE'
96 ) AND voided = FALSE
97         AND EXISTS ( 
98     SELECT 1
99     FROM action.circulation ac
100     WHERE mb.xact = ac.id
101     AND ac.circ_lib IN (
102         SELECT id
103         FROM actor.org_unit
104         WHERE parent_ou IN (
105             SELECT id
106             FROM actor.org_unit
107             WHERE shortname = '$SYSTEM')));
108 END_OF_Q
109 read -d '' BRANCH_Q <<END_OF_Q
110 SELECT * INTO $BACKUP_TABLE
111 FROM money.billing mb
112 WHERE date(billing_ts) IN (
113     '$DATE'
114 ) AND voided = FALSE
115         AND EXISTS ( 
116     SELECT 1
117     FROM action.circulation ac
118     WHERE mb.xact = ac.id
119     AND ac.circ_lib IN (
120         SELECT id
121         FROM actor.org_unit
122         WHERE shortname = '$BRANCH'));
123 END_OF_Q
124
125 if [ -n "$SYSTEM" ]; then
126     echo "$SYSTEM_Q"
127         psql -U "$DBUSER" -h "$DBHOST" -c "$SYSTEM_Q"
128 fi
129
130 if [ -n "$BRANCH" ]; then
131     echo "$BRANCH_Q"
132         psql -U "$DBUSER" -h "$DBHOST" -c "$BRANCH_Q"
133 fi
134
135 read -d '' BILLS_SQL <<END_OF_Q
136 SELECT id FROM $BACKUP_TABLE;
137 END_OF_Q
138
139 read -d '' COUNT_SQL <<END_OF_Q
140 SELECT count(*)
141 FROM money.billing
142 WHERE NOT voided
143 AND id IN (
144         SELECT id
145         FROM $BACKUP_TABLE
146 );
147 END_OF_Q
148
149 read -d '' UPDATE_SQL <<END_OF_Q
150 BEGIN;
151 UPDATE money.billing
152 SET note = '$VOID_NOTE'
153 WHERE id IN (
154         SELECT id 
155         FROM $BACKUP_TABLE
156 );
157 COMMIT;
158 END_OF_Q
159
160 BILLS=`psql -A -t -U "$DBUSER" -h "$DBHOST" -c "$BILLS_SQL" | sed 's/^/"/g' | sed 's/$/", /g' | tr '\n' ' '`
161
162 AUTHTOKEN=`echo "login $EGUSER $EGPASS" | $SRFSH | grep "Login Session" | cut -d':' -f 2 | cut -d'.' -f1 | sed 's/ //g'`
163
164 SRFSH_COMMAND="request open-ils.circ open-ils.circ.money.billing.void \"$AUTHTOKEN\" $BILLS"
165
166 echo "$SRFSH_COMMAND" >> "${BACKUP_TABLE}_void.srfsh"
167
168 echo "$SRFSH_COMMAND" | $SRFSH
169
170 until [ $(psql -A -t -U "$DBUSER" -h "$DBHOST" -c "$COUNT_SQL") == "0" ]; do
171         echo "Waiting for srfsh command to complete..."
172         sleep 10
173 done
174
175 psql -U "$DBUSER" -h "$DBHOST" -c "$UPDATE_SQL"
176
177 echo "Update complete"