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";
17 # -----------------------------------------------------------------------
27 # fetch_patron_circ_info - load info on items out, overdues, and fines.
29 # _direct - this is a hash of key/value pairs to shove directly into the
30 # script runner. Use this to cover data not covered by this module
31 # -----------------------------------------------------------------------
33 my( $class, $args ) = @_;
38 my $editor = $$args{editor} || new_editor();
40 $args->{_direct} = {} unless $args->{_direct};
42 $evt = fetch_bib_data($editor, $args);
43 push(@evts, $evt) if $evt;
44 $evt = fetch_user_data($editor, $args);
45 push(@evts, $evt) if $evt;
46 $args->{_event} = \@evts;
47 return build_runner($editor, $args);
54 my $runner = OpenILS::Utils::ScriptRunner->new;
56 $runner->insert( "$evt.patron", $ctx->{patron}, 1);
57 $runner->insert( "$evt.copy", $ctx->{copy}, 1);
58 $runner->insert( "$evt.volume", $ctx->{volume}, 1);
59 $runner->insert( "$evt.title", $ctx->{title}, 1);
60 $runner->insert( "$evt.requestor", $ctx->{requestor}, 1);
61 $runner->insert( "$evt.titleDescriptor", $ctx->{titleDescriptor}, 1);
63 $runner->insert( "$evt.patronItemsOut", $ctx->{patronItemsOut}, 1 );
64 $runner->insert( "$evt.patronOverdueCount", $ctx->{patronOverdue}, 1 );
65 $runner->insert( "$evt.patronFines", $ctx->{patronFines}, 1 );
68 $runner->insert("result", {});
69 $runner->insert("result.event", 'SUCCESS');
70 $runner->insert("result.events", []);
71 $runner->insert('result.fatalEvents', []);
72 $runner->insert('result.infoEvents', []);
74 $runner->insert("$evt.$_", $ctx->{_direct}->{$_}) for keys %{$ctx->{_direct}};
76 $ctx->{runner} = $runner;
87 $ctx->{copy} = $e->retrieve_asset_copy($ctx->{copy_id})
90 } elsif( $ctx->{copy_barcode} ) {
92 $ctx->{copy} = $e->search_asset_copy(
93 {barcode => $ctx->{copy_barcode}}) or return $e->event;
94 $ctx->{copy} = $ctx->{copy}->[0];
98 return undef unless my $copy = $ctx->{copy};
100 # --------------------------------------------------------------------
101 # Fetch/Cache the copy status and location objects
102 # --------------------------------------------------------------------
103 if(!@COPY_STATUSES) {
104 my $s = $e->retrieve_all_config_copy_status();
105 @COPY_STATUSES = @$s;
106 $s = $e->retrieve_all_asset_copy_location();
107 @COPY_LOCATIONS = @$s;
110 # Flesh the status and location
112 grep { $_->id == $copy->status } @COPY_STATUSES )
113 unless ref $copy->status;
116 grep { $_->id == $copy->location } @COPY_LOCATIONS )
117 unless ref $copy->location;
120 $e->retrieve_actor_org_unit($copy->circ_lib))
121 unless ref $copy->circ_lib;
123 $ctx->{volume} = $e->retrieve_asset_call_number(
124 $ctx->{copy}->call_number) or return $e->event;
126 $ctx->{title} = $e->retrieve_biblio_record_entry(
127 $ctx->{volume}->record) or return $e->event;
129 if(!$ctx->{titleDescriptor}) {
130 $ctx->{titleDescriptor} = $e->search_metabib_record_descriptor(
131 { record => $ctx->{title}->id }) or return $e->event;
133 $ctx->{titleDescriptor} = $ctx->{titleDescriptor}->[0];
141 sub fetch_user_data {
144 if(!$ctx->{patron}) {
146 if( $ctx->{patron_id} ) {
147 $ctx->{patron} = $e->retrieve_actor_user($ctx->{patron_id});
149 } elsif( $ctx->{patron_barcode} ) {
151 my $card = $e->search_actor_card(
152 { barcode => $ctx->{patron_barcode} } ) or return $e->event;
154 $ctx->{patron} = $e->search_actor_user(
155 { card => $card->[0]->id }) or return $e->event;
156 $ctx->{patron} = $ctx->{patron}->[0];
160 return undef unless my $patron = $ctx->{patron};
163 $e->retrieve_actor_org_unit($patron->home_ou) )
164 unless ref $patron->home_ou;
168 my $s = $e->retrieve_all_permission_grp_tree();
173 grep { $_->id == $patron->profile } @GROUP_LIST )
174 unless ref $patron->profile;
176 $patron->card($e->retrieve_actor_card($patron->card));
178 $ctx->{requestor} = $ctx->{requestor} || $e->requestor;
180 # this could alter the requestor object within the editor..
181 #if( my $req = $ctx->{requestor} ) {
182 # $req->home_ou( $e->retrieve_actor_org_unit($requestor->home_ou) );
183 # $req->ws_ou( $e->retrieve_actor_org_unit($requestor->ws_ou) );
186 if( $ctx->{fetch_patron_circ_info} ) {
189 OpenILS::Application::Actor::_checked_out(1, $e, $patron->id);
191 $ctx->{patronOverdue} = $circ_counts->{overdue} || 0;
192 $ctx->{patronItemsOut} = $ctx->{patronOverdue} + $circ_counts->{out};
195 my $fxacts = $e->search_money_open_billable_transaction_summary(
196 { usr => $patron->id, balance_owed => { ">" => 0 } });
199 $fines += $_->balance_owed for @$fxacts;
200 $ctx->{patronFines} = $fines;
202 $logger->debug("script_builder: patron fines determined to be $fines");
203 $logger->debug("script_builder: patron overdue count is " . $ctx->{patronOverdue});