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}->{$_}) for keys %{$ctx->{_direct}};
89 $ORG_TREE = $editor->search_actor_org_unit(
91 {"parent_ou" => undef },
94 flesh_fields => { aou => ['children'] },
95 order_by => { aou => 'name'}
99 flatten_org_tree($ORG_TREE);
101 #insert_org_methods( $editor, $ctx );
103 $runner->insert( "$evt.orgList", $ORG_TREE, 1 );
105 $ctx->{runner} = $runner;
116 if($ctx->{copy_id}) {
117 $ctx->{copy} = $e->retrieve_asset_copy($ctx->{copy_id})
120 } elsif( $ctx->{copy_barcode} ) {
122 my $cps = $e->search_asset_copy({barcode => $ctx->{copy_barcode}});
123 return $e->event unless @$cps;
124 $ctx->{copy} = $$cps[0];
128 return undef unless my $copy = $ctx->{copy};
130 # --------------------------------------------------------------------
131 # Fetch/Cache the copy status and location objects
132 # --------------------------------------------------------------------
133 if(!@COPY_STATUSES) {
134 my $s = $e->retrieve_all_config_copy_status();
135 @COPY_STATUSES = @$s;
136 $s = $e->retrieve_all_asset_copy_location();
137 @COPY_LOCATIONS = @$s;
140 # Flesh the status and location
142 grep { $_->id == $copy->status } @COPY_STATUSES )
143 unless ref $copy->status;
146 grep { $_->id == $copy->location } @COPY_LOCATIONS )
147 unless ref $copy->location;
150 $e->retrieve_actor_org_unit($copy->circ_lib))
151 unless ref $copy->circ_lib;
153 $ctx->{volume} = $e->retrieve_asset_call_number(
154 $ctx->{copy}->call_number) or return $e->event;
156 $ctx->{title} = $e->retrieve_biblio_record_entry(
157 $ctx->{volume}->record) or return $e->event;
159 if(!$ctx->{titleDescriptor}) {
160 $ctx->{titleDescriptor} = $e->search_metabib_record_descriptor(
161 { record => $ctx->{title}->id }) or return $e->event;
163 $ctx->{titleDescriptor} = $ctx->{titleDescriptor}->[0];
166 #insert_copy_method();
173 sub fetch_user_data {
176 if(!$ctx->{patron}) {
178 if( $ctx->{patron_id} ) {
179 $ctx->{patron} = $e->retrieve_actor_user($ctx->{patron_id});
181 } elsif( $ctx->{patron_barcode} ) {
183 my $card = $e->search_actor_card(
184 { barcode => $ctx->{patron_barcode} } ) or return $e->event;
186 $ctx->{patron} = $e->search_actor_user(
187 { card => $card->[0]->id }) or return $e->event;
188 $ctx->{patron} = $ctx->{patron}->[0];
192 return undef unless my $patron = $ctx->{patron};
195 $e->retrieve_actor_org_unit($patron->home_ou) )
196 unless ref $patron->home_ou;
198 $patron->home_ou->ou_type(
199 $patron->home_ou->ou_type->id)
200 if ref $patron->home_ou->ou_type;
203 $GROUP_TREE = $e->search_permission_grp_tree(
208 flesh_fields => { pgt => ['children'] }
213 flatten_groups($GROUP_TREE);
216 $patron->profile( $GROUP_SET{$patron->profile} )
217 unless ref $patron->profile;
219 $patron->card($e->retrieve_actor_card($patron->card));
221 $ctx->{requestor} = $ctx->{requestor} || $e->requestor;
223 # this could alter the requestor object within the editor..
224 #if( my $req = $ctx->{requestor} ) {
225 # $req->home_ou( $e->retrieve_actor_org_unit($requestor->home_ou) );
226 # $req->ws_ou( $e->retrieve_actor_org_unit($requestor->ws_ou) );
229 if( $ctx->{fetch_patron_circ_info} ) {
232 OpenILS::Application::Actor::_checked_out(1, $e, $patron->id);
234 $ctx->{patronOverdue} = $circ_counts->{overdue} || 0;
235 $ctx->{patronItemsOut} = $ctx->{patronOverdue} + $circ_counts->{out};
238 my $fxacts = $e->search_money_open_billable_transaction_summary(
239 { usr => $patron->id, balance_owed => { ">" => 0 } });
242 $fines += $_->balance_owed for @$fxacts;
243 $ctx->{patronFines} = $fines;
245 $logger->debug("script_builder: patron fines determined to be $fines");
246 $logger->debug("script_builder: patron overdue count is " . $ctx->{patronOverdue});
255 return undef unless $tree;
256 $GROUP_SET{$tree->id} = $tree;
257 if( $tree->children ) {
258 flatten_groups($_) for @{$tree->children};
262 sub flatten_org_tree {
264 return undef unless $tree;
265 push( @ORG_LIST, $tree );
266 if( $tree->children ) {
267 flatten_org_tree($_) for @{$tree->children};
273 sub insert_org_methods {
274 my ( $editor, $ctx ) = @_;
275 my $runner = $ctx->{runner};
278 $ORG_TREE = $editor->search_actor_org_unit(
280 {"parent_ou" => undef },
283 flesh_fields => { aou => ['children'] },
284 order_by => { aou => 'name'}
288 flatten_org_tree($ORG_TREE);
294 $runner->insert(__OILS_FUNC_isOrgDescendent =>
296 my( $write_key, $sname, $id ) = @_;
297 my ($parent) = grep { $_->shortname eq $sname } @ORG_LIST;
298 my ($child) = grep { $_->id == $id } @ORG_LIST;
299 my $val = is_org_descendent( $parent, $child );
300 $r->insert($write_key, $val);
307 sub is_org_descendent {
308 my( $parent, $child ) = @_;
309 return 0 unless $parent and $child;
311 return 1 if $parent->id == $child->id;
312 } while( ($child) = grep { $_->id == $child->parent_ou } @ORG_LIST );
318 # if( $ctx->{copy} ) {
320 # # allows a script to fetch a hold that is currently targeting the
322 # $runner->insert_method( 'environment.copy', '__OILS_FUNC_fetch_hold', sub {
324 # my $hold = $holdcode->fetch_related_holds($ctx->{copy}->id);
325 # $hold = undef unless $hold;
326 # $runner->insert( $key, $hold, 1 );