]> 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 BACKUP_SCHEMA="myschema"
38
39 Usage() { echo "Usage: ./batch_void_fines.sh -d YYYY-MM-DD,YYYY-MM-DD,... -s systemname OR -b branchname." 
40 }
41
42 while getopts s:b:d:n:h OPTIONS
43 do  case "$OPTIONS" in
44     s)  SYSTEM="$OPTARG";;
45     b)  BRANCH="$OPTARG";;
46     d)  DATE="$OPTARG";;
47     n)  VOID_NOTE="$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 [ -n "$VOID_NOTE" ]; then
60 VOID_NOTE="VOIDED BY PINES STAFF BY REQUEST FROM LIBRARY/SYSTEM"
61 fi
62
63 if [ -z "$DATE" ]; then
64         echo "Date is required."
65     Usage
66     exit 1;
67 fi
68
69 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
70         echo "Date must be in YYYY-MM-DD format.  Please check."
71         Usage
72         exit 1;
73 fi
74
75 if [ $(echo $DATE | grep ",") ]; then
76     SPLITDATE=`echo $DATE | sed "s/,/', '/g"`
77     DATE=$SPLITDATE
78 fi
79
80 if [ -n "$SYSTEM" ]; then
81         if [ $(echo $SYSTEM | grep "-") ]; then
82                 echo "System names do not have hyphens.  Did you mean -b?"
83                 Usage
84                 exit 1;
85         fi
86         BACKUP_TABLE="$BACKUP_SCHEMA.`echo $SYSTEM | tr '[:upper:]' '[:lower:]'`_batch_voided_fines_`echo $DATE | tr '-' '_' | sed "s/'//g" | sed "s/, /_/g"`"
87 else            
88         if [ ! $(echo $BRANCH | grep "-") ]; then
89         echo "Branch names have hyphens.  Did you mean -s?"
90         Usage
91         exit 1;
92     fi
93         BACKUP_TABLE="$BACKUP_SCHEMA.`echo $BRANCH | tr '[:upper:]' '[:lower:]' | tr '-' '_'`_batch_voided_fines_`echo $DATE | tr '-' '_' | sed "s/'//g" | sed "s/, /_/g"`"
94 fi
95 read -d '' SYSTEM_Q <<END_OF_Q
96 SELECT * INTO $BACKUP_TABLE
97 FROM money.billing mb
98 WHERE btype = 1
99 AND date(billing_ts) IN (
100     '$DATE'
101 ) AND voided = FALSE
102         AND EXISTS ( 
103     SELECT 1
104     FROM action.circulation ac
105     WHERE mb.xact = ac.id
106     AND ac.circ_lib IN (
107         SELECT id
108         FROM actor.org_unit
109         WHERE parent_ou IN (
110             SELECT id
111             FROM actor.org_unit
112             WHERE shortname = '$SYSTEM')));
113 END_OF_Q
114 read -d '' BRANCH_Q <<END_OF_Q
115 SELECT * INTO $BACKUP_TABLE
116 FROM money.billing mb
117 WHERE btype = 1 
118 AND date(billing_ts) IN (
119     '$DATE'
120 ) AND voided = FALSE
121         AND EXISTS ( 
122     SELECT 1
123     FROM action.circulation ac
124     WHERE mb.xact = ac.id
125     AND ac.circ_lib IN (
126         SELECT id
127         FROM actor.org_unit
128         WHERE shortname = '$BRANCH'));
129 END_OF_Q
130
131 if [ -n "$SYSTEM" ]; then
132     echo "$SYSTEM_Q"
133         psql -U "$DBUSER" -h "$DBHOST" -c "$SYSTEM_Q"
134 fi
135
136 if [ -n "$BRANCH" ]; then
137     echo "$BRANCH_Q"
138         psql -U "$DBUSER" -h "$DBHOST" -c "$BRANCH_Q"
139 fi
140
141 read -d '' BILLS_SQL <<END_OF_Q
142 SELECT id FROM $BACKUP_TABLE;
143 END_OF_Q
144
145 read -d '' COUNT_SQL <<END_OF_Q
146 SELECT count(*)
147 FROM money.billing
148 WHERE NOT voided
149 AND id IN (
150         SELECT id
151         FROM $BACKUP_TABLE
152 );
153 END_OF_Q
154
155 read -d '' UPDATE_SQL <<END_OF_Q
156 BEGIN;
157 UPDATE money.billing
158 SET note = '$VOID_NOTE'
159 WHERE id IN (
160         SELECT id 
161         FROM $BACKUP_TABLE
162 );
163 COMMIT;
164 END_OF_Q
165
166 BILLS=`psql -A -t -U "$DBUSER" -h "$DBHOST" -c "$BILLS_SQL" | sed 's/^/"/g' | sed 's/$/", /g' | tr '\n' ' '`
167
168 AUTHTOKEN=`echo "login $EGUSER $EGPASS" | $SRFSH | grep "Login Session" | cut -d':' -f 2 | cut -d'.' -f1 | sed 's/ //g'`
169
170 SRFSH_COMMAND="request open-ils.circ open-ils.circ.money.billing.void \"$AUTHTOKEN\" $BILLS"
171
172 echo "$SRFSH_COMMAND" >> "${BACKUP_TABLE}_void.srfsh"
173
174 echo "$SRFSH_COMMAND" | $SRFSH
175
176 until [ $(psql -A -t -U "$DBUSER" -h "$DBHOST" -c "$COUNT_SQL") == "0" ]; do
177         echo "Waiting for srfsh command to complete..."
178         sleep 10
179 done
180
181 psql -U "$DBUSER" -h "$DBHOST" -c "$UPDATE_SQL"
182
183 echo "Update complete"