]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/generate_circ_notices.pl
don't re-fetch the user and circ lib on a batch. use editor for initial query (good...
[Evergreen.git] / Open-ILS / src / support-scripts / generate_circ_notices.pl
1 #!/usr/bin/perl
2 # ---------------------------------------------------------------
3 # Copyright (C) 2008  Georgia Public Library Service
4 # Bill Erickson <erickson@esilibrary.com>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #  ---------------------------------------------------------------
16 use strict; use warnings;
17 require 'oils_header.pl';
18 use vars qw/$logger/;
19 use DateTime;
20 use Data::Dumper;
21 use Email::Send;
22 use Getopt::Long;
23 use Unicode::Normalize;
24 use DateTime::Format::ISO8601;
25 use OpenSRF::Utils qw/:datetime/;
26 use OpenSRF::Utils::JSON;
27 use OpenSRF::Utils::SettingsClient;
28 use OpenSRF::AppSession;
29 use OpenILS::Const qw/:const/;
30 use OpenILS::Application::AppUtils;
31 use OpenILS::Const qw/:const/;
32 my $U = 'OpenILS::Application::AppUtils';
33
34 my $settings = undef;
35 my $e = OpenILS::Utils::CStoreEditor->new;
36
37 my ($osrf_config, $send_email, $gen_day_intervals, $days_back) = 
38     ('/openils/conf/opensrf_core.xml', 1, 0, 0); 
39
40 GetOptions(
41     'osrf_osrf_config=s' => \$osrf_config,
42     'send-emails' => \$send_email,
43     'generate-day-intervals' => \$gen_day_intervals,
44     'days-back=s' => \$days_back,
45 );
46
47 sub help {
48     print <<HELP;
49         --config <config_file>
50         
51         --send-emails If set, generate email notices
52
53         --generate-day-intervals If set, notices which have a notify_interval of >= 1 day will be processed.
54
55         --days-back <days_back_comma_separted>  This is used to set the effective run date of the script.
56             This is useful if you don't want to generate notices on certain days.  For example, if you don't 
57             generate notices on the weekend, you would run this script on weekdays and set --days-back to 
58             0,1,2 when it's run on Monday to capture any notices from Saturday and Sunday. 
59 HELP
60 }
61
62
63 sub main {
64     osrf_connect($osrf_config);
65     $settings = OpenSRF::Utils::SettingsClient->new;
66
67     my $smtp_server = $settings->config_value(notifications => 'smtp_server');
68     my $sender_address = $settings->config_value(notifications => 'sender_address');
69     my $od_sender_addr = $settings->config_value(notifications => overdue => 'sender_address') || $sender_address;
70     my $pd_sender_addr = $settings->config_value(notifications => predue => 'sender_address') || $sender_address;
71     my $overdue_notices = $settings->config_value(notifications => overdue => 'notice');
72     my $predue_notices = $settings->config_value(notifications => predue => 'notice');
73
74     $overdue_notices = [$overdue_notices] unless ref $overdue_notices eq 'ARRAY'; 
75     $predue_notices = [$predue_notices] unless ref $predue_notices eq 'ARRAY'; 
76
77     my @overdues = sort { 
78         OpenSRF::Utils->interval_to_seconds($a->{notify_interval}) <=> 
79         OpenSRF::Utils->interval_to_seconds($b->{notify_interval}) } @$overdue_notices;
80
81     my @predues = sort { 
82         OpenSRF::Utils->interval_to_seconds($a->{notify_interval}) <=> 
83         OpenSRF::Utils->interval_to_seconds($b->{notify_interval}) } @$predue_notices;
84
85     generate_notice_set($_, $od_sender_addr, 'overdue') for @overdues;
86     generate_notice_set($_, $pd_sender_addr, 'predue') for @predues;
87 }
88
89
90 sub generate_notice_set {
91     my($notice, $smtp_sender, $type) = @_;
92
93     my $notify_interval = OpenSRF::Utils->interval_to_seconds($notice->{notify_interval});
94     $notify_interval = -$notify_interval if $type eq 'overdue';
95
96     my ($start_date, $end_date) = make_date_range(-$days_back + $notify_interval);
97
98     $logger->info("notice: retrieving circs with due date in range $start_date -> $end_date");
99
100     my $QUERY = {
101         select => {
102             circ => ['id']
103         }, 
104         from => 'circ', 
105         where => {
106             '+circ' => {
107                 checkin_time => undef, 
108                 '-or' => [
109                     {stop_fines => ["LOST","LONGOVERDUE","CLAIMSRETURNED"]},
110                     {stop_fines => undef}
111                 ],
112                                 due_date => {between => [$start_date, $end_date]},
113             }
114         }
115     };
116
117     # if a circ duration is defined for this type of notice
118     if(my $durs = $settings->{circ_duration_range}) {
119         $QUERY->{where}->{'+circ'}->{duration} = {between => [$durs->{from}, $durs->{to}]};
120     }
121
122     my $circs = $e->json_query($QUERY, {timeout => 18000, substream => 1});
123     process_circs($notice, $type, map {$_->{id}} @$circs);
124 }
125
126
127 sub process_circs {
128     my $notice = shift;
129     my $type = shift;
130     my @circs = @_;
131
132         return unless @circs;
133
134         $logger->info("notice: processing $type notices with notify interval ". 
135         $notice->{notify_interval}."  and ".scalar(@circs)." circs");
136
137         my $org; 
138         my $patron;
139         my @current;
140
141         my $x = 0;
142         for my $circ (@circs) {
143                 $circ = $e->retrieve_action_circulation($circ);
144
145                 if( !defined $org or 
146                                 $circ->circ_lib != $org  or $circ->usr ne $patron ) {
147                         $org = $circ->circ_lib;
148                         $patron = $circ->usr;
149                         generate_notice($notice, @current) if @current;
150                         @current = ();
151                 }
152
153                 push(@current, $circ);
154                 $x++;
155         }
156
157         $logger->info("notice: processed $x circs");
158         generate_notice($notice, @current);
159 }
160
161 my %ORG_CACHE;
162
163 sub generate_notice {
164     my $notice = shift;
165     my @circs = @_;
166
167         my $circ_lib_id = $circs[0]->circ_lib;
168         my $usr_id = $circs[0]->usr;
169         $logger->debug("notice: printing user:$usr_id circ_lib:$circ_lib_id");
170
171     my $usr = $e->retrieve_actor_user([
172         $usr_id,
173         {   flesh => 1,
174             flesh_fields => {
175                 au => [qw/card billing_address mailing_address/] 
176             }
177         }
178     ]);
179
180     my $circ_lib = $ORG_CACHE{$circ_lib_id} ||
181         $e->retrieve_actor_org_unit([
182             $circ_lib_id,
183             {   flesh => 1,
184                 flesh_fields => {
185                     aou => [qw/billing_address mailing_address/],
186                 }
187             }
188         ]);
189     $ORG_CACHE{$circ_lib_id} = $circ_lib;
190
191     my $circ_objs = $e->search_action_circulation([
192         {id => [map {$_->id} @circs]},
193         {   flesh => 2,
194             flesh_fields => {
195                 circ => [q/target_copy/],
196                 acp => ['call_number'],
197                 acn => ['record'],
198             }
199         }
200     ]);
201
202     $_->circ_lib($circ_lib) for @$circ_objs;
203     $_->usr($usr) for @$circ_objs;
204
205     print $_->circ_lib->shortname . ' : ' . $_->usr->usrname . 
206         ' : ' .  $_->target_copy->barcode . "\n" for @$circ_objs;
207 }
208
209 =head
210         if( $circ->copy->call_number->id == OILS_PRECAT_CALL_NUMBER ) {
211                 $data->{title} = $copy->dummy_title || '';
212                 $data->{author} = $copy->dummy_author || '';
213     } else {
214         $data->{mvr} = $U->record_to_mvr($copy->call_number->record);
215                 $data->{title} = $data->{mvr}->title || '';
216                 $data->{author} = $data->{mvr}->author || '';
217     }
218 =cut
219
220 sub make_date_range {
221         my $offset = shift;
222     #my $is_day_precision = shift; # window?
223
224         my $epoch = CORE::time + $offset;
225         my $date = DateTime->from_epoch(epoch => $epoch, time_zone => 'local');
226
227         $date->set_hour(0);
228         $date->set_minute(0);
229         $date->set_second(0);
230         my $start = "$date";
231         
232         $date->set_hour(23);
233         $date->set_minute(59);
234         $date->set_second(59);
235
236         return ($start, "$date");
237 }
238
239 main();