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";
12 my $evt = "environment";
22 # -----------------------------------------------------------------------
32 # fetch_patron_circ_info - load info on items out, overdues, and fines.
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 # -----------------------------------------------------------------------
38 my( $class, $args ) = @_;
43 my $editor = $$args{editor} || new_editor();
45 $args->{_direct} = {} unless $args->{_direct};
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;
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;
60 return build_runner($editor, $args);
68 my $runner = OpenILS::Utils::ScriptRunner->new;
71 weaken($gt); # just to be safe
72 $runner->insert( "$evt.groupTree", $gt, 1);
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);
82 $runner->insert( "$evt.patronItemsOut", $ctx->{patronItemsOut}, 1 );
83 $runner->insert( "$evt.patronOverdueCount", $ctx->{patronOverdue}, 1 );
84 $runner->insert( "$evt.patronFines", $ctx->{patronFines}, 1 );
86 $runner->insert("$evt.$_", $ctx->{_direct}->{$_}, 1) for keys %{$ctx->{_direct}};
88 $ctx->{runner} = $runner;
90 insert_org_methods( $editor, $ctx );
101 if($ctx->{copy_id}) {
102 $ctx->{copy} = $e->retrieve_asset_copy($ctx->{copy_id})
105 } elsif( $ctx->{copy_barcode} ) {
107 my $cps = $e->search_asset_copy({barcode => $ctx->{copy_barcode}});
108 return $e->event unless @$cps;
109 $ctx->{copy} = $$cps[0];
113 return undef unless my $copy = $ctx->{copy};
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;
125 # Flesh the status and location
127 grep { $_->id == $copy->status } @COPY_STATUSES )
128 unless ref $copy->status;
131 grep { $_->id == $copy->location } @COPY_LOCATIONS )
132 unless ref $copy->location;
135 $e->retrieve_actor_org_unit($copy->circ_lib))
136 unless ref $copy->circ_lib;
138 $ctx->{volume} = $e->retrieve_asset_call_number(
139 $ctx->{copy}->call_number) or return $e->event;
141 $ctx->{title} = $e->retrieve_biblio_record_entry(
142 $ctx->{volume}->record) or return $e->event;
144 if(!$ctx->{titleDescriptor}) {
145 $ctx->{titleDescriptor} = $e->search_metabib_record_descriptor(
146 { record => $ctx->{title}->id }) or return $e->event;
148 $ctx->{titleDescriptor} = $ctx->{titleDescriptor}->[0];
151 #insert_copy_method();
158 sub fetch_user_data {
161 if(!$ctx->{patron}) {
163 if( $ctx->{patron_id} ) {
164 $ctx->{patron} = $e->retrieve_actor_user($ctx->{patron_id});
166 } elsif( $ctx->{patron_barcode} ) {
168 my $card = $e->search_actor_card(
169 { barcode => $ctx->{patron_barcode} } ) or return $e->event;
171 $ctx->{patron} = $e->search_actor_user(
172 { card => $card->[0]->id }) or return $e->event;
173 $ctx->{patron} = $ctx->{patron}->[0];
177 return undef unless my $patron = $ctx->{patron};
180 $e->retrieve_actor_org_unit($patron->home_ou) )
181 unless ref $patron->home_ou;
183 $patron->home_ou->ou_type(
184 $patron->home_ou->ou_type->id)
185 if ref $patron->home_ou->ou_type;
188 $GROUP_TREE = $e->search_permission_grp_tree(
193 flesh_fields => { pgt => ['children'] }
198 flatten_groups($GROUP_TREE);
201 $patron->profile( $GROUP_SET{$patron->profile} )
202 unless ref $patron->profile;
204 $patron->card($e->retrieve_actor_card($patron->card));
206 $ctx->{requestor} = $ctx->{requestor} || $e->requestor;
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) );
214 if( $ctx->{fetch_patron_circ_info} ) {
217 OpenILS::Application::Actor::_checked_out(1, $e, $patron->id);
219 $ctx->{patronOverdue} = $circ_counts->{overdue} || 0;
220 $ctx->{patronItemsOut} = $ctx->{patronOverdue} + $circ_counts->{out};
223 my $fxacts = $e->search_money_open_billable_transaction_summary(
224 { usr => $patron->id, balance_owed => { ">" => 0 } });
227 $fines += $_->balance_owed for @$fxacts;
228 $ctx->{patronFines} = $fines;
230 $logger->debug("script_builder: patron fines determined to be $fines");
231 $logger->debug("script_builder: patron overdue count is " . $ctx->{patronOverdue});
240 return undef unless $tree;
241 $GROUP_SET{$tree->id} = $tree;
242 if( $tree->children ) {
243 flatten_groups($_) for @{$tree->children};
247 sub flatten_org_tree {
249 return undef unless $tree;
250 push( @ORG_LIST, $tree );
251 if( $tree->children ) {
252 flatten_org_tree($_) for @{$tree->children};
258 sub insert_org_methods {
259 my ( $editor, $ctx ) = @_;
260 my $runner = $ctx->{runner};
263 $ORG_TREE = $editor->search_actor_org_unit(
265 {"parent_ou" => undef },
268 flesh_fields => { aou => ['children'] },
269 order_by => { aou => 'name'}
273 flatten_org_tree($ORG_TREE);
279 $runner->insert(__OILS_FUNC_isOrgDescendent =>
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);
292 sub is_org_descendent {
293 my( $parent, $child ) = @_;
294 return 0 unless $parent and $child;
296 return 1 if $parent->id == $child->id;
297 } while( ($child) = grep { $_->id == $child->parent_ou } @ORG_LIST );
303 # if( $ctx->{copy} ) {
305 # # allows a script to fetch a hold that is currently targeting the
307 # $runner->insert_method( 'environment.copy', '__OILS_FUNC_fetch_hold', sub {
309 # my $hold = $holdcode->fetch_related_holds($ctx->{copy}->id);
310 # $hold = undef unless $hold;
311 # $runner->insert( $key, $hold, 1 );