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