1 package OpenILS::Utils::PermitHold;
2 use strict; use warnings;
5 use OpenSRF::Utils::SettingsClient;
6 use OpenILS::Utils::ScriptRunner;
7 use OpenILS::Application::AppUtils;
8 use DateTime::Format::ISO8601;
9 use OpenILS::Application::Circ::ScriptBuilder;
10 use OpenSRF::Utils::Logger qw(:logger);
12 my $U = "OpenILS::Application::AppUtils";
14 my $script; # - the permit script
15 my $script_libs; # - extra script libs
17 # mental note: open-ils.storage.biblio.record_entry.ranged_tree
20 # params within a hash are: copy, patron,
21 # requestor, request_lib, title, title_descriptor
22 sub permit_copy_hold {
27 patron_id => $$params{patron_id},
28 patron => $$params{patron},
29 copy => $$params{copy},
30 requestor => $$params{requestor},
31 title => $$params{title},
32 volume => $$params{volume},
33 flesh_age_protect => 1,
35 requestLib => $$params{request_lib},
36 pickupLib => $$params{pickup_lib},
40 my $runner = OpenILS::Application::Circ::ScriptBuilder->build($ctx);
42 my $evt = check_age_protect($ctx->{patron}, $ctx->{copy});
43 push( @allevents, $evt ) if $evt;
45 $logger->debug("Running permit_copy_hold on copy " . $$params{copy}->id);
47 load_scripts($runner);
48 my $result = $runner->run or
49 throw OpenSRF::EX::ERROR ("Hold Copy Permit Script Died: $@");
53 # --------------------------------------------------------------
54 # Extract and uniquify the event list
55 # --------------------------------------------------------------
56 my $events = $result->{events};
57 my $pid = ($params->{patron}) ? $params->{patron}->id : $params->{patron_id};
58 $logger->debug("circ_permit_hold for user $pid returned events: [@$events]");
60 push( @allevents, OpenILS::Event->new($_)) for @$events;
61 my %hash = map { ($_->{ilsevent} => $_) } @allevents;
62 @allevents = values %hash;
64 return \@allevents if $$params{show_event_list};
65 return 1 unless @allevents;
74 my $conf = OpenSRF::Utils::SettingsClient->new;
75 my @pfx = ( "apps", "open-ils.circ","app_settings" );
76 my $libs = $conf->config_value(@pfx, 'script_path');
77 $script = $conf->config_value(@pfx, 'scripts', 'circ_permit_hold');
78 $script_libs = (ref($libs)) ? $libs : [$libs];
81 $runner->add_path($_) for(@$script_libs);
82 $runner->load($script);
86 sub check_age_protect {
87 my( $patron, $copy ) = @_;
89 return undef unless $copy->age_protect;
91 my $prox = $U->storagereq(
92 'open-ils.storage.asset.copy.proximity',
93 $copy->id, $patron->home_ou->id );
95 # If this copy is within the appropriate proximity,
96 # age protect does not apply
97 return undef if $prox <= $copy->age_protect->prox;
99 # How many seconds old does the copy have to be to escape age protection
100 my $interval = OpenSRF::Utils::interval_to_seconds($copy->age_protect->age);
101 my $start_date = time - $interval;
103 # Now, now many seconds old is this copy
104 my $dparser = DateTime::Format::ISO8601->new;
105 my $create_date = $dparser->parse_datetime(
106 OpenSRF::Utils::clense_ISO8601($copy->create_date));
107 my $age = $create_date->epoch;
109 $logger->debug("age_protect create_date = $create_date : age=$age, start_date=$start_date");
111 unless( $start_date < $age ) {
112 $logger->info("age_protect prevents copy from having a hold placed on it: ".$copy->id);
113 return OpenILS::Event->new('ITEM_AGE_PROTECTED', copy => $copy->id );