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