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