failing on non-existant column instead of silently ignoring it
[Evergreen.git] / Evergreen / src / support-scripts / eg_gen_overdue.pl
1 #!/usr/bin/perl
2 # ---------------------------------------------------------------
3 # Generates the overdue notices XML file
4 # ./eg_gen_overdue.pl <bootstap> 0
5 #               generates today's notices
6 # ./eg_gen_overdue.pl <bootstap> 1 0
7 #               generates notices for today - 1 and today
8 # ./eg_gen_overdue.pl <bootstap> 2 1 0  
9 # ./eg_gen_overdue.pl <bootstap> 3 2 1 0  etc...
10 # ---------------------------------------------------------------
11
12
13
14 use strict; use warnings;
15 require '../../../Open-ILS/src/support-scripts/oils_header.pl';
16 use vars qw/$logger $apputils/;
17 use Data::Dumper;
18 use OpenILS::Const qw/:const/;
19 use DateTime;
20 use Email::Send;
21 use DateTime::Format::ISO8601;
22 use OpenSRF::Utils qw/:datetime/;
23 use Unicode::Normalize;
24
25
26 my $bsconfig = shift || die "usage: $0 <bootstrap_config>\n";
27 my @goback = @ARGV;
28 @goback = (0) unless @goback;
29 osrf_connect($bsconfig);
30 my $e = OpenILS::Utils::CStoreEditor->new;
31
32 my $smtp = $ENV{EG_OVERDUE_SMTP_HOST};
33 my $mail_sender = $ENV{EG_OVERDUE_EMAIL_SENDER};
34
35 # ---------------------------------------------------------------
36 # Set up the email template
37 my $etmpl = $ENV{EG_OVERDUE_EMAIL_TEMPLATE};
38 print "Using email template: $etmpl\n";
39 open(F,"$etmpl");
40 my @etmpl = <F>;
41 close(F);
42 my $email_template = join('',@etmpl);
43 # ---------------------------------------------------------------
44
45
46
47 my @date = CORE::localtime;
48 my $sec  = $date[0];
49 my $min  = $date[1];
50 my $hour = $date[2];
51 my $day  = $date[3];
52 my $mon  = $date[4] + 1;
53 my $year = $date[5] + 1900;
54
55 my %USER_CACHE;
56 my %ORG_CACHE;
57
58
59 print <<XML;
60 <?xml version='1.0' encoding='UTF-8'?>
61 <file type="notice" date="$day/$mon/$year" time="$hour:$min:$sec">
62         <agency name="PINES">
63 XML
64
65 print_notices($_) for @goback;
66
67 print <<XML;
68         </agency>
69 </file>
70 XML
71
72
73 # -----------------------------------------------------------------------
74 # -----------------------------------------------------------------------
75
76
77 sub print_notices {
78         my $goback = shift || 0;
79
80         for my $day ( qw/ 7 14 30 / ) {
81                 my ($start, $end) = make_date_range($day + $goback);
82                 $logger->debug("OD_notice: process date range $start -> $end");
83
84                 my $circs = $e->search_action_circulation(
85                         [
86                                 {
87                                         stop_fines => undef,
88                                         due_date => { between => [ $start, $end ] }
89                                 },
90                                 {
91                                         order_by => { circ => 'usr, circ_lib' }
92                                 }
93                         ],
94                         { idlist => 1 }
95                 );
96
97                 process_circs( $circs, "${day}day" );
98         }
99 }
100
101
102 sub process_circs {
103         my $circs = shift;
104         my $range = shift;
105
106         return unless @$circs;
107
108         $logger->debug("OD_notice: processing range $range and circs @$circs");
109
110         my $org; 
111         my $patron;
112         my @current;
113
114         for my $circ (@$circs) {
115                 $circ = $e->retrieve_action_circulation($circ);
116
117                 if( !defined $org or 
118                                 $circ->circ_lib != $org  or $circ->usr ne $patron ) {
119                         $org = $circ->circ_lib;
120                         $patron = $circ->usr;
121                         print_notice( $range, \@current ) if @current;
122                         @current = ();
123                 }
124
125                 push( @current, $circ );
126         }
127
128         print_notice( $range, \@current );
129 }
130
131 sub make_date_range {
132         my $daysback = shift;
133
134         my $date = DateTime->from_epoch( 
135                 epoch => ( CORE::time - ($daysback * 24 * 60 * 60) ) );
136
137         $date->set_hour(0);
138         $date->set_minute(0);
139         $date->set_second(0);
140         my $start = "$date";
141
142         $date->set_hour(23);
143         $date->set_minute(59);
144         $date->set_second(59);
145
146         return ($start, "$date");
147 }
148
149
150 sub print_notice {
151         my( $range, $circs ) = @_;
152         return unless @$circs;
153         my $org = $circs->[0]->circ_lib;
154         my $usr = $circs->[0]->usr;
155         $logger->debug("OD_notice: printing $range user:$usr org:$org");
156
157         my @patron_data = fetch_patron_data($usr);
158         my @org_data = fetch_org_data($org);
159
160         my $email;
161
162         if( $email = $patron_data[0]->email 
163                 and $email =~ /.+\@.+/ 
164                 and ($range eq '7day' or $range eq '14day') ) {
165
166                         send_email($range, \@patron_data, \@org_data, $circs);
167
168         } else {
169
170                 print "\t\t<notice type='overdue' count='$range'>\n";
171                 print_patron_xml_chunk(@patron_data);
172                 print_org_xml_chunk(@org_data);
173                 print_circ_chunk($_) for @$circs;
174                 print "\t\t</notice>\n";
175         }
176 }
177
178
179 sub fetch_patron_data {
180         my $user_id = shift;
181
182         my $patron = $USER_CACHE{$user_id};
183
184         if( ! $patron ) {
185                 $logger->debug("OD_notice:   fetching patron $user_id");
186
187                 $patron = $e->retrieve_actor_user(
188                         [
189                                 $user_id,
190                                 {
191                                         flesh => 1,
192                                         flesh_fields => { 
193                                                 'au' => [qw/ card billing_address mailing_address /] 
194                                         }
195                                 }
196                         ]
197                 ) or return handle_event($e->event);
198
199                 $USER_CACHE{$user_id} = $patron;
200         }
201
202         my $bc = $patron->card->barcode;
203         my $fn = $patron->first_given_name;
204         my $mn = $patron->second_given_name;
205         my $ln = $patron->family_name;
206
207         my ( $s1, $s2, $city, $state, $zip );
208         my $baddr = $patron->billing_address || $patron->mailing_address;
209         if( $baddr ) {
210                 $s1             = $baddr->street1;
211                 $s2             = $baddr->street2;
212                 $city           = $baddr->city;
213                 $state  = $baddr->state;
214                 $zip            = $baddr->post_code;
215         }
216
217         $bc = entityize($bc);
218         $fn = entityize($fn);
219         $mn = entityize($mn);
220         $ln = entityize($ln);
221         $s1 = entityize($s1);
222         $s2 = entityize($s2);
223         $city  = entityize($city);
224         $state = entityize($state);
225         $zip     = entityize($zip);
226
227         return ( $patron, $bc, $fn, $mn, $ln, $s1, $s2, $city, $state, $zip );
228 }
229
230         
231 sub print_patron_xml_chunk {
232         my( $patron, $bc, $fn, $mn, $ln, $s1, $s2, $city, $state, $zip ) = @_;
233         print <<"       XML";
234                         <patron>
235                                 <id type="barcode">$bc</id>
236                                 <fullname>$fn $mn $ln</fullname>
237                                 <street1>$s1 $s2</street1>
238                                 <city_state_zip>$city, $state $zip</city_state_zip>
239                         </patron>
240         XML
241 }
242
243
244 sub fetch_org_data {
245         my $org_id = shift;
246
247         my $org = $ORG_CACHE{$org_id};
248
249         if( ! $org ) {
250                 $logger->debug("OD_notice:   fetching org $org_id");
251
252                 $org = $e->retrieve_actor_org_unit(
253                         [
254                                 $org_id,
255                                 {
256                                         flesh => 1, 
257                                         flesh_fields => 
258                                                 { aou => [ qw/billing_address mailing_address/ ] }
259                                 }
260                         ]
261                 ) or return handle_event($e->event);
262
263                 $ORG_CACHE{$org_id} = $org;
264         }
265
266         my $name = $org->name;
267         my $email = $org->email;
268
269         my( $phone, $s1, $s2, $city, $state, $zip );
270         my $baddr = $org->billing_address || $org->mailing_address;
271         if( $baddr ) {
272                 $s1             = $baddr->street1;
273                 $s2             = $baddr->street2;
274                 $city           = $baddr->city;
275                 $state  = $baddr->state;
276                 $zip            = $baddr->post_code;
277         }
278
279         $name  = entityize($name);
280         $phone = entityize($phone);
281         $s1      = entityize($s1);
282         $s2      = entityize($s2);
283         $city  = entityize($city);
284         $state = entityize($state);
285         $zip     = entityize($zip);
286         $email = entityize($email);
287
288         return ( $org, $name, $phone, $s1, $s2, $city, $state, $zip, $email );
289 }
290
291
292 sub print_org_xml_chunk {
293         my( $org, $name, $phone, $s1, $s2, $city, $state, $zip, $email ) = @_;
294         print <<"       XML";
295                         <library>
296                                 <libname>$name</libname>
297                                 <libphone>$phone</libphone>
298                                 <libstreet1>$s1 $s2</libstreet1>
299                                 <libcity_state_zip>$city, $state $zip</libcity_state_zip>
300                         </library>
301         XML
302 }
303
304
305 sub fetch_circ_data {
306         my $circ = shift;
307
308         my $title;
309         my $author;
310         my $cn;
311
312         my $d = $circ->due_date;
313         $d =~ s/[T ].*//og; # just for logging
314         $logger->debug("OD_notice:   processing circ ".$circ->id." $d");
315
316         my $due = DateTime::Format::ISO8601->new->parse_datetime(
317                 clense_ISO8601($circ->due_date));
318
319         my $day  = $due->day;
320         my $mon  = $due->month;
321         my $year = $due->year;
322
323         my $copy = $e->retrieve_asset_copy($circ->target_copy)
324                 or return handle_event($e->event);
325
326         my $bc = $copy->barcode;
327
328         if( $copy->call_number == OILS_PRECAT_CALL_NUMBER ) {
329                 $title = $copy->dummy_title || "";
330                 $author = $copy->dummy_author || "";
331
332         } else {
333
334                 my $volume = $e->retrieve_asset_call_number(
335                         [
336                                 $copy->call_number,
337                                 {
338                                         flesh => 1,
339                                         flesh_fields => {
340                                                 acn => [ qw/record/ ]
341                                         }
342                                 }
343                         ]
344                 ) or return handle_event($e->event);
345
346                 $cn = $volume->label;
347                 my $mods = $apputils->record_to_mvr($volume->record);
348                 if( $mods ) {
349                         $title = $mods->title || "";
350                         $author = $mods->author || "";
351                 }
352         }
353
354         $title = entityize($title);
355         $author = entityize($author);
356         $cn = entityize($cn);
357         $bc = entityize($bc);
358
359         return( $title, $author, $cn, $bc, $day, $mon, $year );
360 }
361
362
363 sub print_circ_chunk {
364         my $circ = shift;
365         my ( $title, $author, $cn, $bc, $day, $mon, $year ) = fetch_circ_data($circ);
366         print <<"       XML";
367                         <item>
368                                 <title>$title</title>
369                                 <author>$author</author>
370                                 <duedate>$day/$mon/$year</duedate>
371                                 <callno>$cn</callno>
372                                 <barcode>$bc</barcode>
373                         </item>
374         XML
375 }
376
377
378
379 sub send_email {
380         my( $range, $patron_data, $org_data, $circs ) = @_;
381         my( $org, $org_name, $org_phone, $org_s1, $org_s2, $org_city, $org_state, $org_zip, $org_email ) = @$org_data;
382         my( $patron, $bc, $fn, $mn, $ln, $user_s1, $user_s2, $user_city, $user_state, $user_zip ) = @$patron_data;
383
384         my $pemail = $patron_data->[0]->email;
385
386         my $tmpl = $email_template;
387         my @time = localtime;
388         my $year = $time[5] + 1900;
389         my $mon  = $time[4] + 1;
390         my $day  = $time[3];
391
392         my $r = ($range eq '7day') ? 7 : 14;
393
394         $org_email ||= $mail_sender;
395
396         $tmpl =~ s/\${EMAIL_RECIPIENT}/$pemail/;
397         $tmpl =~ s/\${EMAIL_REPLY_TO}/$org_email/o;
398         $tmpl =~ s/\${EMAIL_REPLY_TO}/$mail_sender/;
399    $tmpl =~ s/\${EMAIL_HEADERS}//;
400
401    $tmpl =~ s/\${RANGE}/$r/;
402    $tmpl =~ s/\${DATE}/$day\/$mon\/$year/;
403    $tmpl =~ s/\${FIRST_NAME}/$fn/;
404    $tmpl =~ s/\${MIDDLE_NAME}/$mn/;
405    $tmpl =~ s/\${LAST_NAME}/$ln/;
406
407         my ($itmpl) = $tmpl =~ /\${OVERDUE_ITEMS\[(.*)\]}/ms;
408
409         my $items = '';
410         for my $circ (@$circs) {
411                 my $circtmpl = $itmpl;
412                 my ( $title, $author, $cn, $bc, $due_day, $due_mon, $due_year ) = fetch_circ_data($circ);
413                 $circtmpl =~ s/\${TITLE}/$title/o;
414                 $circtmpl =~ s/\${AUTHOR}/$author/o;
415                 $circtmpl =~ s/\${CALL_NUMBER}/$cn/o;
416                 $circtmpl =~ s/\${DUE_DAY}/$due_day/o;
417                 $circtmpl =~ s/\${DUE_MONTH}/$due_mon/o;
418                 $circtmpl =~ s/\${DUE_YEAR}/$due_year/o;
419                 $circtmpl =~ s/\${ITEM_BARCODE}/$bc/o;
420                 $items .= "$circtmpl\n";
421         }
422
423         $tmpl =~ s/\${OVERDUE_ITEMS\[.*\]}/$items/ms;
424
425         my $org_addr = "$org_s1 $org_s2 $org_city, $org_state $org_zip";
426         $tmpl =~ s/\${ORG_NAME}/$org_name/o;
427         $tmpl =~ s/\${ORG_ADDRESS}/$org_addr/o;
428         $tmpl =~ s/\${ORG_PHONE}/$org_phone/o;
429
430         $logger->debug("OD_notice: sending email to $pemail: $tmpl");
431
432         my $sender = Email::Send->new({mailer => 'SMTP'});
433         $sender->mailer_args([Host => $smtp]);
434
435         my $stat;
436         #my $stat = $sender->send($tmpl);
437         
438         if( $stat and $stat->type eq 'success' ) {
439                 $logger->info("OD_notice:   successfully sent overdue email");
440         } else {
441                 $logger->warn("OD_notice:   unable to send hold overdue email: ".Dumper($stat));
442         }
443
444         $logger->info("OD_notice:   sending email to".$patron_data->[0]->email);
445 }
446
447 sub handle_event {
448         my $evt = shift;
449         warn "OD_notice: ".Dumper($evt) . "\n";
450         $logger->error("OD_notice: ".Dumper($evt));
451 }
452
453
454 sub entityize {
455         my $stuff = shift || return "";
456         $stuff =~ s/\</&lt;/og;
457         $stuff =~ s/\>/&gt;/og;
458         $stuff =~ s/\&/&amp;/og;
459         $stuff = NFC($stuff);
460         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
461         return $stuff;
462 }
463
464
465
466