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 # ---------------------------------------------------------------
14 use strict; use warnings;
15 require '../../../Open-ILS/src/support-scripts/oils_header.pl';
16 use vars qw/$logger $apputils/;
18 use OpenILS::Const qw/:const/;
19 use OpenILS::Application::AppUtils;
22 use DateTime::Format::ISO8601;
23 use OpenSRF::Utils qw/:datetime/;
24 use OpenSRF::Utils::JSON;
25 use Unicode::Normalize;
26 use OpenILS::Const qw/:const/;
28 my $U = 'OpenILS::Application::AppUtils';
32 my $bsconfig = shift || die "usage: $0 <bootstrap_config>\n";
34 @goback = (0) unless @goback;
35 osrf_connect($bsconfig);
36 my $e = OpenILS::Utils::CStoreEditor->new;
38 my $smtp = $ENV{EG_OVERDUE_SMTP_HOST};
39 my $mail_sender = $ENV{EG_OVERDUE_EMAIL_SENDER};
41 # ---------------------------------------------------------------
42 # Set up the email template
43 my $etmpl = $ENV{EG_OVERDUE_EMAIL_TEMPLATE};
45 if( open(F,"$etmpl") ) {
47 $email_template = join('',@etmpl);
50 # ---------------------------------------------------------------
54 my @date = CORE::localtime;
59 my $mon = $date[4] + 1;
60 my $year = $date[5] + 1900;
67 <?xml version='1.0' encoding='UTF-8'?>
68 <file type="notice" date="$mon/$day/$year" time="$hour:$min:$sec">
72 print_notices($_) for @goback;
80 # -----------------------------------------------------------------------
81 # -----------------------------------------------------------------------
85 my $goback = shift || 0;
87 for my $day ( qw/ 7 14 30 / ) {
88 my ($start, $end) = make_date_range($day + $goback);
90 $logger->info("OD_notice: process date range $start -> $end");
94 checkin_time => undef,
95 due_date => { between => [ $start, $end ] },
97 { order_by => { circ => 'usr, circ_lib' } }
99 my $circs = $e->search_action_circulation($query, {idlist=>1});
101 process_circs( $circs, "${day}day" );
110 return unless @$circs;
112 $logger->info("OD_notice: processing range $range and ".scalar(@$circs)." potential circs");
119 for my $circ (@$circs) {
120 $circ = $e->retrieve_action_circulation($circ);
123 $circ->circ_lib != $org or $circ->usr ne $patron ) {
124 $org = $circ->circ_lib;
125 $patron = $circ->usr;
126 print_notice( $range, \@current ) if @current;
130 push( @current, $circ );
134 $logger->info("OD_notice: processed $x circs");
135 print_notice( $range, \@current );
138 sub make_date_range {
139 my $daysback = shift;
141 my $epoch = CORE::time - ($daysback * 24 * 60 * 60);
142 my $date = DateTime->from_epoch( epoch => $epoch, time_zone => 'local');
145 $date->set_minute(0);
146 $date->set_second(0);
150 $date->set_minute(59);
151 $date->set_second(59);
153 return ($start, "$date");
158 my( $range, $circs ) = @_;
159 return unless @$circs;
161 my $s1 = scalar(@$circs);
163 # we don't charge for lost or claimsreturned
167 $_->stop_fines ne OILS_STOP_FINES_LOST and
168 $_->stop_fines ne OILS_STOP_FINES_CLAIMSRETURNED
173 return unless @$circs;
175 my $s2 = $s1 - scalar(@$circs);
176 $logger->info("OD_notice: dropped $s2 lost/CR from processing...") if $s2;
178 my $org = $circs->[0]->circ_lib;
179 my $usr = $circs->[0]->usr;
180 $logger->debug("OD_notice: printing $range user:$usr org:$org");
182 my @patron_data = fetch_patron_data($usr);
183 my @org_data = fetch_org_data($org);
185 return unless (@patron_data and @org_data);
189 if( $email = $patron_data[0]->email
190 and $email =~ /.+\@.+/
191 and ($range eq '7day' or $range eq '14day') ) {
193 send_email($range, \@patron_data, \@org_data, $circs);
197 if( $patron_data[9] ) {
199 print "\t\t<notice type='overdue' count='$range'>\n";
200 print_patron_xml_chunk(@patron_data);
201 print_org_xml_chunk(@org_data);
202 print_circ_chunk($_) for @$circs;
203 print "\t\t</notice>\n";
206 # There is no zip, therefore no address.
207 $logger->warn("OD_notice: unable to send mail notification for $usr due to lack of valid address");
213 sub fetch_patron_data {
216 my $patron = $USER_CACHE{$user_id};
219 $logger->debug("OD_notice: fetching patron $user_id");
221 $patron = $e->retrieve_actor_user(
227 'au' => [qw/ card billing_address mailing_address /]
231 ) or return handle_event($e->event);
233 $USER_CACHE{$user_id} = $patron;
236 my $bc = $patron->card->barcode;
237 my $fn = $patron->first_given_name;
238 my $mn = $patron->second_given_name;
239 my $ln = $patron->family_name;
241 my ( $s1, $s2, $city, $state, $zip );
243 my $baddr = $patron->mailing_address;
244 unless( $baddr and $U->is_true($baddr->valid) ) {
245 $baddr = $patron->billing_address;
246 $baddr = undef unless( $baddr and $U->is_true($baddr->valid) );
250 $s1 = $baddr->street1;
251 $s2 = $baddr->street2;
252 $city = $baddr->city;
253 $state = $baddr->state;
254 $zip = $baddr->post_code;
257 $bc = entityize($bc);
258 $fn = entityize($fn);
259 $mn = entityize($mn);
260 $ln = entityize($ln);
261 $s1 = entityize($s1);
262 $s2 = entityize($s2);
263 $city = entityize($city);
264 $state = entityize($state);
265 $zip = entityize($zip);
267 return ( $patron, $bc, $fn, $mn, $ln, $s1, $s2, $city, $state, $zip );
271 sub print_patron_xml_chunk {
272 my( $patron, $bc, $fn, $mn, $ln, $s1, $s2, $city, $state, $zip ) = @_;
273 my $pid = $patron->id;
276 <id type="barcode">$bc</id>
277 <fullname>$fn $mn $ln</fullname>
278 <street1>$s1 $s2</street1>
279 <city_state_zip>$city, $state $zip</city_state_zip>
280 <sys_id>$pid</sys_id>
289 my $org = $ORG_CACHE{$org_id};
292 $logger->debug("OD_notice: fetching org $org_id");
294 $org = $e->retrieve_actor_org_unit(
300 { aou => [ qw/billing_address mailing_address/ ] }
303 ) or return handle_event($e->event);
305 $ORG_CACHE{$org_id} = $org;
308 my $name = $org->name;
309 my $phone = $org->phone;
310 my $email = $org->email;
313 my( $s1, $s2, $city, $state, $zip );
314 my $baddr = $org->billing_address || $org->mailing_address;
316 $s1 = $baddr->street1;
317 $s2 = $baddr->street2;
318 $city = $baddr->city;
319 $state = $baddr->state;
320 $zip = $baddr->post_code;
323 $name = entityize($name);
324 $phone = entityize($phone);
325 $s1 = entityize($s1);
326 $s2 = entityize($s2);
327 $city = entityize($city);
328 $state = entityize($state);
329 $zip = entityize($zip);
330 $email = entityize($email);
332 return ( $org, $name, $phone, $s1, $s2, $city, $state, $zip, $email );
336 sub print_org_xml_chunk {
337 my( $org, $name, $phone, $s1, $s2, $city, $state, $zip, $email ) = @_;
340 <libname>$name</libname>
341 <libphone>$phone</libphone>
342 <libstreet1>$s1 $s2</libstreet1>
343 <libcity_state_zip>$city, $state $zip</libcity_state_zip>
349 sub fetch_circ_data {
356 my $d = $circ->due_date;
357 $d =~ s/[T ].*//og; # just for logging
358 $logger->debug("OD_notice: processing circ ".$circ->id." $d");
360 my $due = DateTime::Format::ISO8601->new->parse_datetime(
361 clense_ISO8601($circ->due_date));
364 my $mon = $due->month;
365 my $year = $due->year;
367 my $copy = $e->retrieve_asset_copy($circ->target_copy)
368 or return handle_event($e->event);
370 my $bc = $copy->barcode;
372 if( $copy->call_number == OILS_PRECAT_CALL_NUMBER ) {
373 $title = $copy->dummy_title || "";
374 $author = $copy->dummy_author || "";
378 my $volume = $e->retrieve_asset_call_number(
384 acn => [ qw/record/ ]
388 ) or return handle_event($e->event);
390 $cn = $volume->label;
391 my $mods = $apputils->record_to_mvr($volume->record);
393 $title = $mods->title || "";
394 $author = $mods->author || "";
398 $title = entityize($title);
399 $author = entityize($author);
400 $cn = entityize($cn);
401 $bc = entityize($bc);
403 return( $title, $author, $cn, $bc, $day, $mon, $year );
407 sub print_circ_chunk {
409 my ( $title, $author, $cn, $bc, $day, $mon, $year ) = fetch_circ_data($circ);
413 <title>$title</title>
414 <author>$author</author>
415 <duedate>$mon/$day/$year</duedate>
417 <barcode>$bc</barcode>
418 <circ_id>$cid</circ_id>
426 my( $range, $patron_data, $org_data, $circs ) = @_;
427 my( $org, $org_name, $org_phone, $org_s1, $org_s2, $org_city, $org_state, $org_zip, $org_email ) = @$org_data;
428 my( $patron, $bc, $fn, $mn, $ln, $user_s1, $user_s2, $user_city, $user_state, $user_zip ) = @$patron_data;
430 return unless $SEND_EMAILS;
432 my $pemail = $patron_data->[0]->email;
434 my $tmpl = $email_template;
435 my @time = localtime;
436 my $year = $time[5] + 1900;
437 my $mon = $time[4] + 1;
440 my $r = ($range eq '7day') ? 7 : 14;
442 # - default to the global sender for the errors-to header
443 my $errors_to = $mail_sender;
445 # if they have an org setting for errors-to, use that as the errors-to address
446 if( my $set = $e->search_actor_org_unit_setting(
447 { name => 'org.bounced_emails', org_unit => $org->id } )->[0] ) {
449 my $bemail = OpenSRF::Utils::JSON->JSON2perl($set->value);
450 $errors_to = $bemail if $bemail;
454 $tmpl =~ s/\${EMAIL_RECIPIENT}/$pemail/;
455 $tmpl =~ s/\${EMAIL_SENDER}/$mail_sender/o;
456 $tmpl =~ s/\${EMAIL_REPLY_TO}/$mail_sender/;
457 $tmpl =~ s/\${EMAIL_ERRORS_TO}/$errors_to/;
458 $tmpl =~ s/\${EMAIL_HEADERS}//; # - we have no additional headers to add
460 $tmpl =~ s/\${RANGE}/$r/;
461 $tmpl =~ s/\${DATE}/$mon\/$day\/$year/;
462 $tmpl =~ s/\${FIRST_NAME}/$fn/;
463 $tmpl =~ s/\${MIDDLE_NAME}/$mn/;
464 $tmpl =~ s/\${LAST_NAME}/$ln/;
466 my ($itmpl) = $tmpl =~ /\${OVERDUE_ITEMS\[(.*)\]}/ms;
469 for my $circ (@$circs) {
470 my $circtmpl = $itmpl;
471 my ( $title, $author, $cn, $bc, $due_day, $due_mon, $due_year ) = fetch_circ_data($circ);
472 $circtmpl =~ s/\${TITLE}/$title/o;
473 $circtmpl =~ s/\${AUTHOR}/$author/o;
474 $circtmpl =~ s/\${CALL_NUMBER}/$cn/o;
475 $circtmpl =~ s/\${DUE_DAY}/$due_day/o;
476 $circtmpl =~ s/\${DUE_MONTH}/$due_mon/o;
477 $circtmpl =~ s/\${DUE_YEAR}/$due_year/o;
478 $circtmpl =~ s/\${ITEM_BARCODE}/$bc/o;
479 $items .= "$circtmpl\n";
482 $tmpl =~ s/\${OVERDUE_ITEMS\[.*\]}/$items/ms;
484 my $org_addr = "$org_s1 $org_s2 $org_city, $org_state $org_zip";
485 $tmpl =~ s/\${ORG_NAME}/$org_name/o;
486 $tmpl =~ s/\${ORG_ADDRESS}/$org_addr/o;
487 $tmpl =~ s/\${ORG_PHONE}/$org_phone/o;
489 $logger->debug("OD_notice: sending email to $pemail: $tmpl");
491 my $sender = Email::Send->new({mailer => 'SMTP'});
492 $sender->mailer_args([Host => $smtp]);
494 my $stat = $sender->send($tmpl);
496 if( $stat and $stat->type eq 'success' ) {
497 $logger->info("OD_notice: successfully sent overdue email");
499 $logger->warn("OD_notice: unable to send hold overdue email: ".Dumper($stat));
502 $logger->info("OD_notice: sending email to".$patron_data->[0]->email);
507 warn "OD_notice: ".Dumper($evt) . "\n";
508 $logger->error("OD_notice: ".Dumper($evt));
514 my $stuff = shift || return "";
515 $stuff =~ s/\</</og;
516 $stuff =~ s/\>/>/og;
517 $stuff =~ s/\&/&/og;
518 $stuff = NFC($stuff);
519 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;