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 my $U = "OpenILS::Application::AppUtils";
10 my $evt = "environment";
16 # -----------------------------------------------------------------------
26 # fetch_patron_circ_info - load info on items out, overdues, and fines.
28 # _direct - this is a hash of key/value pairs to shove directly into the
29 # script runner. Use this to cover data not covered by this module
30 # -----------------------------------------------------------------------
32 my( $class, $args ) = @_;
37 my $editor = $$args{editor} || new_editor();
39 $args->{_direct} = {} unless $args->{_direct};
41 $evt = fetch_bib_data($editor, $args);
42 push(@evts, $evt) if $evt;
43 $evt = fetch_user_data($editor, $args);
44 push(@evts, $evt) if $evt;
45 $args->{_event} = \@evts;
46 return build_runner($editor, $args);
53 my $runner = OpenILS::Utils::ScriptRunner->new;
55 $runner->insert( "$evt.patron", $ctx->{patron}, 1);
56 $runner->insert( "$evt.copy", $ctx->{copy}, 1);
57 $runner->insert( "$evt.volume", $ctx->{volume}, 1);
58 $runner->insert( "$evt.title", $ctx->{title}, 1);
59 $runner->insert( "$evt.requestor", $ctx->{requestor}, 1);
60 $runner->insert( "$evt.titleDescriptor", $ctx->{titleDescriptor}, 1);
62 $runner->insert( "$evt.patronItemsOut", $ctx->{patronItemsOut} );
63 $runner->insert( "$evt.patronOverdueCount", $ctx->{patronOverdue} );
64 $runner->insert( "$evt.patronFines", $ctx->{patronFines} );
67 $runner->insert("result", {});
68 $runner->insert("result.event", 'SUCCESS');
69 $runner->insert("result.events", []);
70 $runner->insert('result.fatalEvents', []);
71 $runner->insert('result.infoEvents', []);
73 $runner->insert("$evt.$_", $ctx->{_direct}->{$_}) for keys %{$ctx->{_direct}};
75 $ctx->{runner} = $runner;
86 $ctx->{copy} = $e->retrieve_asset_copy($ctx->{copy_id})
89 } elsif( $ctx->{copy_barcode} ) {
91 $ctx->{copy} = $e->search_asset_copy(
92 {barcode => $ctx->{copy_barcode}}) or return $e->event;
93 $ctx->{copy} = $ctx->{copy}->[0];
97 return undef unless my $copy = $ctx->{copy};
99 # --------------------------------------------------------------------
100 # Fetch/Cache the copy status and location objects
101 # --------------------------------------------------------------------
102 if(!@COPY_STATUSES) {
103 my $s = $e->retrieve_all_config_copy_status();
104 @COPY_STATUSES = @$s;
105 $s = $e->retrieve_all_asset_copy_location();
106 @COPY_LOCATIONS = @$s;
109 # Flesh the status and location
111 grep { $_->id == $copy->status } @COPY_STATUSES )
112 unless ref $copy->status;
115 grep { $_->id == $copy->location } @COPY_LOCATIONS )
116 unless ref $copy->location;
119 $e->retrieve_actor_org_unit($copy->circ_lib))
120 unless ref $copy->circ_lib;
122 $ctx->{volume} = $e->retrieve_asset_call_number(
123 $ctx->{copy}->call_number) or return $e->event;
125 $ctx->{title} = $e->retrieve_biblio_record_entry(
126 $ctx->{volume}->record) or return $e->event;
128 if(!$ctx->{titleDescriptor}) {
129 $ctx->{titleDescriptor} = $e->search_metabib_record_descriptor(
130 { record => $ctx->{title}->id }) or return $e->event;
132 $ctx->{titleDescriptor} = $ctx->{titleDescriptor}->[0];
140 sub fetch_user_data {
143 if(!$ctx->{patron}) {
145 if( $ctx->{patron_id} ) {
146 $ctx->{patron} = $e->retrieve_actor_user($ctx->{patron_id});
148 } elsif( $ctx->{patron_barcode} ) {
150 my $card = $e->search_actor_card(
151 { barcode => $ctx->{patron_barcode} } ) or return $e->event;
153 $ctx->{patron} = $e->search_actor_user(
154 { card => $card->[0]->id }) or return $e->event;
155 $ctx->{patron} = $ctx->{patron}->[0];
159 return undef unless my $patron = $ctx->{patron};
162 $e->retrieve_actor_org_unit($patron->home_ou) )
163 unless ref $patron->home_ou;
167 my $s = $e->retrieve_all_permission_grp_tree();
171 warn 'patron profile = ' . $patron->profile . "\n";
173 grep { $_->id == $patron->profile } @GROUP_LIST )
174 unless ref $patron->profile;
175 warn 'patron profile = ' . $patron->profile->name . "\n";
177 $ctx->{requestor} = $ctx->{requestor} || $e->requestor;
179 # this could alter the requestor object within the editor..
180 #if( my $req = $ctx->{requestor} ) {
181 # $req->home_ou( $e->retrieve_actor_org_unit($requestor->home_ou) );
182 # $req->ws_ou( $e->retrieve_actor_org_unit($requestor->ws_ou) );
185 if( $ctx->{fetch_patron_circ_info} ) {
188 OpenILS::Application::Actor::_checked_out(1, $e, $patron->id);
190 $ctx->{patronOverdue} = $circ_counts->{overdue} || 0;
191 $ctx->{patronItemsOut} = $ctx->{patronOverdue} + $circ_counts->{out};
194 my $fxacts = $e->search_money_open_billable_transaction_summary(
195 { usr => $patron->id, balance_owed => { ">" => 0 } });
198 $fines += $_->balance_owed for @$fxacts;
199 $ctx->{patronFines} = $fines;