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 my $U = "OpenILS::Application::AppUtils";
11 my $evt = "environment";
18 # -----------------------------------------------------------------------
28 # fetch_patron_circ_info - load info on items out, overdues, and fines.
30 # _direct - this is a hash of key/value pairs to shove directly into the
31 # script runner. Use this to cover data not covered by this module
32 # -----------------------------------------------------------------------
34 my( $class, $args ) = @_;
39 my $editor = $$args{editor} || new_editor();
41 $args->{_direct} = {} unless $args->{_direct};
43 $evt = fetch_bib_data($editor, $args);
44 push(@evts, $evt) if $evt;
45 $evt = fetch_user_data($editor, $args);
46 push(@evts, $evt) if $evt;
47 $args->{_event} = \@evts;
48 return build_runner($editor, $args);
55 my $runner = OpenILS::Utils::ScriptRunner->new;
57 $runner->insert( "$evt.groupTree", $GROUP_TREE, 1);
59 $runner->insert( "$evt.patron", $ctx->{patron}, 1);
60 $runner->insert( "$evt.copy", $ctx->{copy}, 1);
61 $runner->insert( "$evt.volume", $ctx->{volume}, 1);
62 $runner->insert( "$evt.title", $ctx->{title}, 1);
63 $runner->insert( "$evt.requestor", $ctx->{requestor}, 1);
64 $runner->insert( "$evt.titleDescriptor", $ctx->{titleDescriptor}, 1);
66 $runner->insert( "$evt.patronItemsOut", $ctx->{patronItemsOut}, 1 );
67 $runner->insert( "$evt.patronOverdueCount", $ctx->{patronOverdue}, 1 );
68 $runner->insert( "$evt.patronFines", $ctx->{patronFines}, 1 );
70 $runner->insert("$evt.$_", $ctx->{_direct}->{$_}) for keys %{$ctx->{_direct}};
72 $ctx->{runner} = $runner;
83 $ctx->{copy} = $e->retrieve_asset_copy($ctx->{copy_id})
86 } elsif( $ctx->{copy_barcode} ) {
88 $ctx->{copy} = $e->search_asset_copy(
89 {barcode => $ctx->{copy_barcode}}) or return $e->event;
90 $ctx->{copy} = $ctx->{copy}->[0];
94 return undef unless my $copy = $ctx->{copy};
96 # --------------------------------------------------------------------
97 # Fetch/Cache the copy status and location objects
98 # --------------------------------------------------------------------
100 my $s = $e->retrieve_all_config_copy_status();
101 @COPY_STATUSES = @$s;
102 $s = $e->retrieve_all_asset_copy_location();
103 @COPY_LOCATIONS = @$s;
106 # Flesh the status and location
108 grep { $_->id == $copy->status } @COPY_STATUSES )
109 unless ref $copy->status;
112 grep { $_->id == $copy->location } @COPY_LOCATIONS )
113 unless ref $copy->location;
116 $e->retrieve_actor_org_unit($copy->circ_lib))
117 unless ref $copy->circ_lib;
119 $ctx->{volume} = $e->retrieve_asset_call_number(
120 $ctx->{copy}->call_number) or return $e->event;
122 $ctx->{title} = $e->retrieve_biblio_record_entry(
123 $ctx->{volume}->record) or return $e->event;
125 if(!$ctx->{titleDescriptor}) {
126 $ctx->{titleDescriptor} = $e->search_metabib_record_descriptor(
127 { record => $ctx->{title}->id }) or return $e->event;
129 $ctx->{titleDescriptor} = $ctx->{titleDescriptor}->[0];
137 sub fetch_user_data {
140 if(!$ctx->{patron}) {
142 if( $ctx->{patron_id} ) {
143 $ctx->{patron} = $e->retrieve_actor_user($ctx->{patron_id});
145 } elsif( $ctx->{patron_barcode} ) {
147 my $card = $e->search_actor_card(
148 { barcode => $ctx->{patron_barcode} } ) or return $e->event;
150 $ctx->{patron} = $e->search_actor_user(
151 { card => $card->[0]->id }) or return $e->event;
152 $ctx->{patron} = $ctx->{patron}->[0];
156 return undef unless my $patron = $ctx->{patron};
159 $e->retrieve_actor_org_unit($patron->home_ou) )
160 unless ref $patron->home_ou;
164 $GROUP_TREE = $e->search_permission_grp_tree(
169 flesh_fields => { pgt => ['children'] }
174 _flatten_groups($GROUP_TREE);
177 $patron->profile( $GROUP_SET{$patron->profile} )
178 unless ref $patron->profile;
181 # grep { $_->id == $patron->profile } @GROUP_LIST )
182 # unless ref $patron->profile;
184 $patron->card($e->retrieve_actor_card($patron->card));
186 $ctx->{requestor} = $ctx->{requestor} || $e->requestor;
188 # this could alter the requestor object within the editor..
189 #if( my $req = $ctx->{requestor} ) {
190 # $req->home_ou( $e->retrieve_actor_org_unit($requestor->home_ou) );
191 # $req->ws_ou( $e->retrieve_actor_org_unit($requestor->ws_ou) );
194 if( $ctx->{fetch_patron_circ_info} ) {
197 OpenILS::Application::Actor::_checked_out(1, $e, $patron->id);
199 $ctx->{patronOverdue} = $circ_counts->{overdue} || 0;
200 $ctx->{patronItemsOut} = $ctx->{patronOverdue} + $circ_counts->{out};
203 my $fxacts = $e->search_money_open_billable_transaction_summary(
204 { usr => $patron->id, balance_owed => { ">" => 0 } });
207 $fines += $_->balance_owed for @$fxacts;
208 $ctx->{patronFines} = $fines;
210 $logger->debug("script_builder: patron fines determined to be $fines");
211 $logger->debug("script_builder: patron overdue count is " . $ctx->{patronOverdue});
218 sub _flatten_groups {
220 return undef unless $tree;
221 $GROUP_SET{$tree->id} = $tree;
222 if( $tree->children ) {
223 _flatten_groups($_) for @{$tree->children};