]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/PermitHold.pm
5f69de9dff433f069a618c0e1276a95f990b335d
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / PermitHold.pm
1 package OpenILS::Utils::PermitHold;
2 use strict; use warnings;
3 use Data::Dumper;
4 use OpenSRF::Utils;
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);
11 use OpenILS::Event;
12 my $U   = "OpenILS::Application::AppUtils";
13
14 my $script;                     # - the permit script
15 my $script_libs;        # - extra script libs
16
17 # mental note:  open-ils.storage.biblio.record_entry.ranged_tree
18
19
20 # params within a hash are: copy, patron, 
21 # requestor, request_lib, title, title_descriptor
22 sub permit_copy_hold {
23         my $params      = shift;
24         my @allevents;
25
26         my $ctx = {
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,
34                 _direct => {
35                         requestLib      => $$params{request_lib},
36                         pickupLib       => $$params{pickup_lib},
37                 }
38         };
39
40         my $runner = OpenILS::Application::Circ::ScriptBuilder->build($ctx);
41
42         my $ets = $ctx->{_events};
43
44         # --------------------------------------------------------------
45         # Strip the expired event since holds are still allowed to be
46         # captured on expired patrons.  
47         # --------------------------------------------------------------
48         if( $ets and @$ets ) {
49                 $ets = [ grep { $_->{textcode} ne 'PATRON_ACCOUNT_EXPIRED' } @$ets ];
50         } else { $ets = []; }
51
52         if( @$ets ) {
53                 push( @allevents, @$ets);
54
55                 # --------------------------------------------------------------
56                 # If scriptbuilder returned any events, then the script context
57                 # is undefined and should not be used
58                 # --------------------------------------------------------------
59
60         } else {
61
62                 # check the various holdable flags
63                 push( @allevents, OpenILS::Event->new('ITEM_NOT_HOLDABLE') )
64                         unless $U->is_true($ctx->{copy}->holdable);
65         
66                 push( @allevents, OpenILS::Event->new('ITEM_NOT_HOLDABLE') )
67                         unless $U->is_true($ctx->{copy}->location->holdable);
68         
69                 push( @allevents, OpenILS::Event->new('ITEM_NOT_HOLDABLE') )
70                         unless $U->is_true($ctx->{copy}->status->holdable);
71         
72       my $evt;
73
74       # grab the data safely
75       my $rlib = ref($$params{request_lib}) ? $$params{request_lib}->id : $$params{request_lib};
76       my $olib = ref($ctx->{volume}) ? $ctx->{volume}->owning_lib : -1;
77       my $rid  = ref($ctx->{requestor}) ? $ctx->{requestor}->id : -2;
78                 my $pid  = ($params->{patron}) ? $params->{patron}->id : $params->{patron_id};
79
80       if( ($rid ne $pid) and ($olib eq $rlib) ) {
81          $logger->info("Item owning lib $olib is the same as the request lib.  No age_protection will be checked");
82       } else {
83          $logger->info("item owning lib = $olib, request lib = $rlib, requestor=$rid, patron=$pid. checking age_protection");
84                    $evt = check_age_protect($ctx->{patron}, $ctx->{copy});
85                    push( @allevents, $evt ) if $evt;
86       }
87         
88                 $logger->debug("Running permit_copy_hold on copy " . $$params{copy}->id);
89         
90                 load_scripts($runner);
91                 my $result = $runner->run or 
92                         throw OpenSRF::EX::ERROR ("Hold Copy Permit Script Died: $@");
93
94                 # --------------------------------------------------------------
95                 # Extract and uniquify the event list
96                 # --------------------------------------------------------------
97                 my $events = $result->{events};
98                 $logger->debug("circ_permit_hold for user $pid returned events: [@$events]");
99         
100                 push( @allevents, OpenILS::Event->new($_)) for @$events;
101         }
102
103         my %hash = map { ($_->{ilsevent} => $_) } @allevents;
104         @allevents = values %hash;
105
106         $runner->cleanup;
107
108         return \@allevents if $$params{show_event_list};
109         return 1 unless @allevents;
110         return 0;
111 }
112
113
114 sub load_scripts {
115         my $runner = shift;
116
117         if(!$script) {
118                 my $conf = OpenSRF::Utils::SettingsClient->new;
119                 my @pfx = ( "apps", "open-ils.circ","app_settings" );
120                 my $libs        = $conf->config_value(@pfx, 'script_path');
121                 $script = $conf->config_value(@pfx, 'scripts', 'circ_permit_hold');
122                 $script_libs = (ref($libs)) ? $libs : [$libs];
123         }
124
125         $runner->add_path($_) for(@$script_libs);
126         $runner->load($script);
127 }
128
129
130 sub check_age_protect {
131         my( $patron, $copy ) = @_;
132
133         return undef unless $copy and $copy->age_protect and $patron;
134
135         my $hou = (ref $patron->home_ou) ? $patron->home_ou->id : $patron->home_ou;
136
137         my $prox = $U->storagereq(
138                 'open-ils.storage.asset.copy.proximity', $copy->id, $hou );
139
140         # If this copy is within the appropriate proximity, 
141         # age protect does not apply
142         return undef if $prox <= $copy->age_protect->prox;
143
144         my $protection_list = $U->storagereq(
145                 'open-ils.storage.direct.config.rules.age_hold_protect.search_where.atomic', 
146                 { age  => { '>=' => $copy->age_protect->age  },
147                   prox => { '>=' => $copy->age_protect->prox },
148                 },
149                 { order_by => 'age' }
150         );
151
152         # Now, now many seconds old is this copy
153         my $create_date = DateTime::Format::ISO8601
154                 ->new
155                 ->parse_datetime( OpenSRF::Utils::clense_ISO8601($copy->create_date) )
156                 ->epoch;
157
158         my $age = time - $create_date;
159
160         for my $protection ( @$protection_list ) {
161
162                 $logger->info("analyzing age protect ".$protection->name);
163
164                 # age protect does not apply if within the proximity
165                 last if $prox <= $protection->prox;
166
167                 # How many seconds old does the copy have to be to escape age protection
168                 my $interval = OpenSRF::Utils::interval_to_seconds($protection->age);
169
170                 $logger->info("age_protect interval=$interval, create_date=$create_date, age=$age");
171
172                 if( $interval > $age ) { 
173                         # if age of the item is less than the protection interval, 
174                         # the item falls within the age protect range
175                         $logger->info("age_protect prevents copy from having a hold placed on it: ".$copy->id);
176                         return OpenILS::Event->new('ITEM_AGE_PROTECTED', copy => $copy->id );
177                 }
178         }
179                 
180         return undef;
181 }
182
183 23;