]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/ScriptBuilder.pm
added age-protect fleshing, removed some old code
[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 Scalar::Util qw/weaken/;
10 my $U = "OpenILS::Application::AppUtils";
11 use Data::Dumper;
12
13 my $holdcode = "OpenILS::Application::Circ::Holds";
14
15 my $evt = "environment";
16 my @COPY_STATUSES;
17 my @COPY_LOCATIONS;
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         #weaken($gt); # just to be safe
74         $runner->insert( "$evt.groupTree",      $gt, 1);
75
76
77         $runner->insert( "$evt.patron",         $ctx->{patron}, 1);
78         $runner->insert( "$evt.copy",                   $ctx->{copy}, 1);
79         $runner->insert( "$evt.volume",         $ctx->{volume}, 1);
80         $runner->insert( "$evt.title",          $ctx->{title}, 1);
81         $runner->insert( "$evt.requestor",      $ctx->{requestor}, 1);
82
83         $runner->insert( "$evt.patronItemsOut", $ctx->{patronItemsOut}, 1 );
84         $runner->insert( "$evt.patronOverdueCount", $ctx->{patronOverdue}, 1 );
85         $runner->insert( "$evt.patronFines", $ctx->{patronFines}, 1 );
86
87         $runner->insert("$evt.$_", $ctx->{_direct}->{$_}, 1) for keys %{$ctx->{_direct}};
88
89         insert_org_methods( $editor, $runner );
90         insert_copy_methods( $editor, $ctx, $runner );
91
92         return $runner;
93 }
94
95 sub fetch_bib_data {
96         my $e = shift;
97         my $ctx = shift;
98
99         if(!$ctx->{copy}) {
100
101                 if($ctx->{copy_id}) {
102                         $ctx->{copy} = $e->retrieve_asset_copy($ctx->{copy_id})
103                                 or return $e->event;
104
105                 } elsif( $ctx->{copy_barcode} ) {
106
107                         my $cps = $e->search_asset_copy({barcode => $ctx->{copy_barcode}});
108                         return $e->event unless @$cps;
109                         $ctx->{copy} = $$cps[0];
110                 }
111         }
112
113         return undef unless my $copy = $ctx->{copy};
114
115         # --------------------------------------------------------------------
116         # Fetch/Cache the copy status and location objects
117         # --------------------------------------------------------------------
118         if(!@COPY_STATUSES) {
119                 my $s = $e->retrieve_all_config_copy_status();
120                 @COPY_STATUSES = @$s;
121                 $s = $e->retrieve_all_asset_copy_location();
122                 @COPY_LOCATIONS = @$s;
123         }
124
125         # Flesh the status and location
126         $copy->status( 
127                 grep { $_->id == $copy->status } @COPY_STATUSES ) 
128                 unless ref $copy->status;
129
130         $copy->location( 
131                 grep { $_->id == $copy->location } @COPY_LOCATIONS ) 
132                 unless ref $copy->location;
133
134         $copy->circ_lib( 
135                 $e->retrieve_actor_org_unit($copy->circ_lib)) 
136                 unless ref $copy->circ_lib;
137
138         $ctx->{volume} = $e->retrieve_asset_call_number(
139                 $ctx->{copy}->call_number) or return $e->event;
140
141         $ctx->{title} = $e->retrieve_biblio_record_entry(
142                 $ctx->{volume}->record) or return $e->event;
143
144         $copy->age_protect(
145                 $e->retrieve_config_rules_age_hold_protect($copy->age_protect))
146                 if $ctx->{flesh_age_protect} and $copy->age_protect;
147
148         return undef;
149 }
150
151
152
153 sub fetch_user_data {
154         my( $e, $ctx ) = @_;
155         
156         if(!$ctx->{patron}) {
157
158                 if( $ctx->{patron_id} ) {
159                         $ctx->{patron} = $e->retrieve_actor_user($ctx->{patron_id});
160
161                 } elsif( $ctx->{patron_barcode} ) {
162
163                         my $card = $e->search_actor_card( 
164                                 { barcode => $ctx->{patron_barcode} } ) or return $e->event;
165
166                         $ctx->{patron} = $e->search_actor_user( 
167                                 { card => $card->[0]->id }) or return $e->event;
168                         $ctx->{patron} = $ctx->{patron}->[0];
169
170                 } elsif( $ctx->{fetch_patron_by_circ_copy} ) {
171
172                         if( my $copy = $ctx->{copy} ) {
173                                 my $circs = $e->search_action_circulation(
174                                         { target_copy => $copy->id, stop_fines_time => undef });
175
176                                 if( my $circ = $circs->[0] ) {
177                                         $ctx->{patron} = $e->retrieve_actor_user($circ->usr)
178                                                 or return $e->event;
179                                 }
180                         }
181                 }
182         }
183
184         return undef unless my $patron = $ctx->{patron};
185
186         return OpenILS::Event->new('PATRON_INACTIVE')
187                 unless $U->is_true($patron->active);
188
189         $patron->card($e->retrieve_actor_card($patron->card));
190
191         return OpenILS::Event->new('PATRON_CARD_INACTIVE')
192                 unless $U->is_true($patron->card->active);
193
194         $patron->home_ou( 
195                 $e->retrieve_actor_org_unit($patron->home_ou) ) 
196                 unless ref $patron->home_ou;
197
198         $patron->home_ou->ou_type(
199                 $patron->home_ou->ou_type->id) 
200                 if ref $patron->home_ou->ou_type;
201
202         if(!%GROUP_SET) {
203                 $GROUP_TREE = $e->search_permission_grp_tree(
204                         [
205                                 { parent => undef }, 
206                                 { 
207                                         flesh => 100,
208                                         flesh_fields => { pgt => ['children'] }
209                                 } 
210                         ]
211                 )->[0];
212
213                 flatten_groups($GROUP_TREE);
214         }
215
216         $patron->profile( $GROUP_SET{$patron->profile} )
217                 unless ref $patron->profile;
218
219
220         $ctx->{requestor} = $ctx->{requestor} || $e->requestor;
221
222         # this could alter the requestor object within the editor..
223         #if( my $req = $ctx->{requestor} ) {
224         #       $req->home_ou( $e->retrieve_actor_org_unit($requestor->home_ou) );      
225         #       $req->ws_ou( $e->retrieve_actor_org_unit($requestor->ws_ou) );  
226         #}
227
228         if( $ctx->{fetch_patron_circ_info} ) {
229
230                 my $circ_counts = 
231                         OpenILS::Application::Actor::_checked_out(1, $e, $patron->id);
232
233                 $ctx->{patronOverdue} = $circ_counts->{overdue} || 0;
234                 $ctx->{patronItemsOut} = $ctx->{patronOverdue} + $circ_counts->{out};
235
236                 # Grab the fines
237                 my $fxacts = $e->search_money_open_billable_transaction_summary(
238                         { usr => $patron->id, balance_owed => { ">" => 0 } });
239
240                 my $fines = 0;
241                 $fines += $_->balance_owed for @$fxacts;
242                 $ctx->{patronFines} = $fines;
243
244                 $logger->debug("script_builder: patron fines determined to be $fines");
245                 $logger->debug("script_builder: patron overdue count is " . $ctx->{patronOverdue});
246         }
247
248         return undef;
249 }
250
251
252 sub flatten_groups {
253         my $tree = shift;
254         return undef unless $tree;
255         $GROUP_SET{$tree->id} = $tree;
256         if( $tree->children ) {
257                 flatten_groups($_) for @{$tree->children};
258         }
259 }
260
261 sub flatten_org_tree {
262         my $tree = shift;
263         return undef unless $tree;
264         push( @ORG_LIST, $tree );
265         if( $tree->children ) {
266                 flatten_org_tree($_) for @{$tree->children};
267         }
268 }
269
270
271
272 sub insert_org_methods {
273         my ( $editor, $runner ) = @_;
274
275         if(!$ORG_TREE) {
276                 $ORG_TREE = $editor->search_actor_org_unit(
277                         [
278                                 {"parent_ou" => undef },
279                                 {
280                                         flesh                           => 2,
281                                         flesh_fields    => { aou =>  ['children'] },
282                                         order_by                        => { aou => 'name'}
283                                 }
284                         ]
285                 )->[0];
286                 flatten_org_tree($ORG_TREE);
287         }
288
289         my $r = $runner;
290         weaken($r);
291
292         $r->insert(__OILS_FUNC_isOrgDescendent  => 
293                 sub {
294                         my( $write_key, $sname, $id ) = @_;
295                         my ($parent)    = grep { $_->shortname eq $sname } @ORG_LIST;
296                         my ($child)             = grep { $_->id == $id } @ORG_LIST;
297                         my $val = is_org_descendent( $parent, $child );
298                         $logger->debug("script_builder: is_org_desc returned val $val, writing to $write_key");
299                         $r->insert($write_key, $val, 1) if $val; # Needs testing, was dying before
300                         return $val;
301                 }
302         );
303
304 }
305
306
307 sub is_org_descendent {
308         my( $parent, $child ) = @_;
309         return 0 unless $parent and $child;
310         $logger->debug("script_builder: is_org_desc checking parent=".$parent->id.", child=".$child->id);
311         do {
312                 return 1 if $parent->id == $child->id;
313         } while( ($child) = grep { $_->id == $child->parent_ou } @ORG_LIST );
314         return 0;
315 }
316
317 sub insert_copy_methods {
318         my( $e, $ctx,  $runner ) = @_;
319         if( my $copy = $ctx->{copy} ) {
320                 $runner->insert_method( 'environment.copy', '__OILS_FUNC_fetch_best_hold', sub {
321                                 my $key = shift;
322                                 $logger->debug("script_builder: searching for permitted hold for copy ".$copy->barcode);
323                                 my ($hold) = $holdcode->find_nearest_permitted_hold(
324                                         OpenSRF::AppSession->create('open-ils.storage'), $copy, $e->requestor );
325                                 $runner->insert( $key, $hold, 1 );
326                         }
327                 );
328         }
329 }
330
331
332
333
334
335 1;