]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/ScriptBuilder.pm
updated hold permit to check correct lib, inserting location into script env
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / ScriptBuilder.pm
1 package OpenILS::Application::Circ::ScriptBuilder;
2 use strict; use warnings;
3 use OpenILS::Utils::ScriptRunner;
4 use OpenILS::Utils::CStoreEditor qw/:funcs/;
5 use OpenILS::Application::AppUtils;
6 use OpenILS::Application::Actor;
7 use OpenSRF::Utils::Logger qw/$logger/;
8 use OpenILS::Application::Circ::Holds;
9 use DateTime::Format::ISO8601;
10 use OpenSRF::Utils qw/:datetime/;
11 use Scalar::Util qw/weaken/;
12 my $U = "OpenILS::Application::AppUtils";
13 use Data::Dumper;
14
15 my $holdcode = "OpenILS::Application::Circ::Holds";
16
17 my $evt = "environment";
18 my %GROUP_SET;
19 my $GROUP_TREE;
20 my $ORG_TREE;
21 my @ORG_LIST;
22
23
24 # -----------------------------------------------------------------------
25 # Possible Args:
26 #  copy
27 #  copy_id
28 #  copy_barcode
29 #
30 #  patron
31 #  patron_id
32 #  patron_barcode
33 #
34 #  fetch_patron_circ_info - load info on items out, overdues, and fines.
35 #
36 #  _direct - this is a hash of key/value pairs to shove directly into the 
37 #  script runner.  Use this to cover data not covered by this module
38 # -----------------------------------------------------------------------
39 sub build {
40         my( $class, $args ) = @_;
41
42         my $evt;
43         my @evts;
44
45         my $editor = $$args{editor} || new_editor();
46
47         $args->{_direct} = {} unless $args->{_direct};
48         
49         $evt = fetch_bib_data($editor, $args);
50         push(@evts, $evt) if $evt;
51         $evt = fetch_user_data($editor, $args);
52         push(@evts, $evt) if $evt;
53
54         if(@evts) {
55                 my @e;
56                 push( @e, $_->{textcode} ) for @evts;
57                 $logger->info("script_builder: some events occurred: @e");
58                 $logger->debug("script_builder: some events occurred: " . Dumper(\@evts));
59                 $args->{_events} = \@evts;
60         }
61
62         return build_runner($editor, $args);
63 }
64
65
66 sub build_runner {
67         my $editor      = shift;
68         my $ctx         = shift;
69
70         my $runner = OpenILS::Utils::ScriptRunner->new;
71
72         my $gt = $GROUP_TREE;
73         $runner->insert( "$evt.groupTree",      $gt, 1);
74
75
76         $runner->insert( "$evt.patron",         $ctx->{patron}, 1);
77         $runner->insert( "$evt.copy",                   $ctx->{copy}, 1);
78         $runner->insert( "$evt.volume",         $ctx->{volume}, 1);
79         $runner->insert( "$evt.title",          $ctx->{title}, 1);
80         $runner->insert( "$evt.requestor",      $ctx->{requestor}, 1);
81         $runner->insert( "$evt.location",       
82                 $editor->retrieve_actor_org_unit($ctx->{requestor}->ws_ou), 1);
83
84         $runner->insert( "$evt.patronItemsOut", $ctx->{patronItemsOut}, 1 );
85         $runner->insert( "$evt.patronOverdueCount", $ctx->{patronOverdue}, 1 );
86         $runner->insert( "$evt.patronFines", $ctx->{patronFines}, 1 );
87
88         $runner->insert("$evt.$_", $ctx->{_direct}->{$_}, 1) for keys %{$ctx->{_direct}};
89
90         insert_org_methods( $editor, $runner );
91         insert_copy_methods( $editor, $ctx, $runner );
92
93         return $runner;
94 }
95
96 sub fetch_bib_data {
97         my $e = shift;
98         my $ctx = shift;
99
100         if(!$ctx->{copy}) {
101
102                 my $flesh = { flesh => 1, flesh_fields => { acp => [ 'location', 'status', 'circ_lib' ] } };
103
104                 if($ctx->{copy_id}) {
105                         $ctx->{copy} = $e->retrieve_asset_copy(
106                                 [$ctx->{copy_id}, $flesh ]) or return $e->event;
107
108                 } elsif( $ctx->{copy_barcode} ) {
109
110                         $ctx->{copy} = $e->search_asset_copy(
111                                 [{barcode => $ctx->{copy_barcode}}, $flesh ])->[0]
112                                 or return $e->event;
113                 }
114         }
115
116         return undef unless my $copy = $ctx->{copy};
117
118         $copy->location($e->retrieve_asset_copy_location($copy->location))
119                 unless( ref $copy->location );
120
121         $copy->status($e->retrieve_config_copy_status($copy->status))
122                 unless( ref $copy->status );
123
124         $copy->circ_lib( 
125                 $e->retrieve_actor_org_unit($copy->circ_lib)) 
126                 unless ref $copy->circ_lib;
127
128         $ctx->{volume} = $e->retrieve_asset_call_number(
129                 $ctx->{copy}->call_number) or return $e->event;
130
131         $ctx->{title} = $e->retrieve_biblio_record_entry(
132                 $ctx->{volume}->record) or return $e->event;
133
134         $copy->age_protect(
135                 $e->retrieve_config_rules_age_hold_protect($copy->age_protect))
136                 if $ctx->{flesh_age_protect} and $copy->age_protect;
137
138         return undef;
139 }
140
141
142
143 sub fetch_user_data {
144         my( $e, $ctx ) = @_;
145         
146         if(!$ctx->{patron}) {
147
148                 if( $ctx->{patron_id} ) {
149                         $ctx->{patron} = $e->retrieve_actor_user($ctx->{patron_id});
150
151                 } elsif( $ctx->{patron_barcode} ) {
152
153                         my $card = $e->search_actor_card( 
154                                 { barcode => $ctx->{patron_barcode} } )->[0] or return $e->event;
155
156                         $ctx->{patron} = $e->search_actor_user( 
157                                 { card => $card->id })->[0] or return $e->event;
158
159                 } elsif( $ctx->{fetch_patron_by_circ_copy} ) {
160
161                         if( my $copy = $ctx->{copy} ) {
162                                 my $circs = $e->search_action_circulation(
163                                         { target_copy => $copy->id, stop_fines_time => undef });
164
165                                 if( my $circ = $circs->[0] ) {
166                                         $ctx->{patron} = $e->retrieve_actor_user($circ->usr)
167                                                 or return $e->event;
168                                 }
169                         }
170                 }
171         }
172
173         return undef unless my $patron = $ctx->{patron};
174
175         return OpenILS::Event->new('PATRON_INACTIVE')
176                 unless $U->is_true($patron->active);
177
178         $patron->card($e->retrieve_actor_card($patron->card));
179
180         return OpenILS::Event->new('PATRON_CARD_INACTIVE')
181                 unless $U->is_true($patron->card->active);
182
183         my $expire = DateTime::Format::ISO8601->new->parse_datetime(
184                 clense_ISO8601($patron->expire_date));
185
186         return OpenILS::Event->new('PATRON_ACCOUNT_EXPIRED')
187                 if( CORE::time > $expire->epoch ) ;
188
189         $patron->home_ou( 
190                 $e->retrieve_actor_org_unit($patron->home_ou) ) 
191                 unless ref $patron->home_ou;
192
193         $patron->home_ou->ou_type(
194                 $patron->home_ou->ou_type->id) 
195                 if ref $patron->home_ou->ou_type;
196
197         if(!%GROUP_SET) {
198                 $GROUP_TREE = $e->search_permission_grp_tree(
199                         [
200                                 { parent => undef }, 
201                                 { 
202                                         flesh => 100,
203                                         flesh_fields => { pgt => ['children'] }
204                                 } 
205                         ]
206                 )->[0];
207
208                 flatten_groups($GROUP_TREE);
209         }
210
211         $patron->profile( $GROUP_SET{$patron->profile} )
212                 unless ref $patron->profile;
213
214
215         $ctx->{requestor} = $ctx->{requestor} || $e->requestor;
216
217         # this could alter the requestor object within the editor..
218         #if( my $req = $ctx->{requestor} ) {
219         #       $req->home_ou( $e->retrieve_actor_org_unit($requestor->home_ou) );      
220         #       $req->ws_ou( $e->retrieve_actor_org_unit($requestor->ws_ou) );  
221         #}
222
223         if( $ctx->{fetch_patron_circ_info} ) {
224
225                 my $circ_counts = 
226                         OpenILS::Application::Actor::_checked_out(1, $e, $patron->id);
227
228                 $ctx->{patronOverdue} = $circ_counts->{overdue} || 0;
229                 $ctx->{patronItemsOut} = $ctx->{patronOverdue} + $circ_counts->{out};
230
231                 # Grab the fines
232                 my $fxacts = $e->search_money_open_billable_transaction_summary(
233                         { usr => $patron->id, balance_owed => { "!=" => 0 } });
234
235                 my $fines = 0;
236                 $fines += $_->balance_owed for @$fxacts;
237                 $ctx->{patronFines} = $fines;
238
239                 $logger->debug("script_builder: patron fines determined to be $fines");
240                 $logger->debug("script_builder: patron overdue count is " . $ctx->{patronOverdue});
241         }
242
243         return undef;
244 }
245
246
247 sub flatten_groups {
248         my $tree = shift;
249         return undef unless $tree;
250         $GROUP_SET{$tree->id} = $tree;
251         if( $tree->children ) {
252                 flatten_groups($_) for @{$tree->children};
253         }
254 }
255
256 sub flatten_org_tree {
257         my $tree = shift;
258         return undef unless $tree;
259         push( @ORG_LIST, $tree );
260         if( $tree->children ) {
261                 flatten_org_tree($_) for @{$tree->children};
262         }
263 }
264
265
266
267 sub insert_org_methods {
268         my ( $editor, $runner ) = @_;
269
270         if(!$ORG_TREE) {
271                 $ORG_TREE = $editor->search_actor_org_unit(
272                         [
273                                 {"parent_ou" => undef },
274                                 {
275                                         flesh                           => 2,
276                                         flesh_fields    => { aou =>  ['children'] },
277                                         order_by                        => { aou => 'name'}
278                                 }
279                         ]
280                 )->[0];
281                 flatten_org_tree($ORG_TREE);
282         }
283
284         my $r = $runner;
285         weaken($r);
286
287         $r->insert(__OILS_FUNC_isOrgDescendent  => 
288                 sub {
289                         my( $write_key, $sname, $id ) = @_;
290                         my ($parent)    = grep { $_->shortname eq $sname } @ORG_LIST;
291                         my ($child)             = grep { $_->id == $id } @ORG_LIST;
292                         my $val = is_org_descendent( $parent, $child );
293                         $logger->debug("script_builder: is_org_desc returned val $val, writing to $write_key");
294                         $r->insert($write_key, $val, 1) if $val; # Needs testing, was dying before
295                         return $val;
296                 }
297         );
298
299 }
300
301
302 sub is_org_descendent {
303         my( $parent, $child ) = @_;
304         return 0 unless $parent and $child;
305         $logger->debug("script_builder: is_org_desc checking parent=".$parent->id.", child=".$child->id);
306         do {
307                 return 0 unless defined $child->parent_ou;
308                 return 1 if $parent->id == $child->id;
309         } while( ($child) = grep { $_->id == $child->parent_ou } @ORG_LIST );
310         return 0;
311 }
312
313 sub insert_copy_methods {
314         my( $e, $ctx,  $runner ) = @_;
315         if( my $copy = $ctx->{copy} ) {
316                 $runner->insert_method( 'environment.copy', '__OILS_FUNC_fetch_best_hold', sub {
317                                 my $key = shift;
318                                 $logger->debug("script_builder: searching for permitted hold for copy ".$copy->barcode);
319                                 my ($hold) = $holdcode->find_nearest_permitted_hold(
320                                         OpenSRF::AppSession->create('open-ils.storage'), $copy, $e->requestor );
321                                 $runner->insert( $key, $hold, 1 );
322                         }
323                 );
324         }
325 }
326
327
328
329
330
331 1;