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 OpenILS::Application::Circ::Holds;
9 use DateTime::Format::ISO8601;
10 use OpenSRF::Utils qw/:datetime/;
11 use Scalar::Util qw/weaken/;
12 my $U = "OpenILS::Application::AppUtils";
15 my $holdcode = "OpenILS::Application::Circ::Holds";
17 my $evt = "environment";
24 # -----------------------------------------------------------------------
34 # fetch_patron_circ_info - load info on items out, overdues, and fines.
36 # _direct - this is a hash of key/value pairs to shove directly into the
37 # script runner. Use this to cover data not covered by this module
38 # -----------------------------------------------------------------------
40 my( $class, $args ) = @_;
45 my $editor = $$args{editor} || new_editor();
47 $args->{_direct} = {} unless $args->{_direct};
49 $evt = fetch_bib_data($editor, $args);
50 push(@evts, $evt) if $evt;
51 $evt = fetch_user_data($editor, $args);
52 push(@evts, $evt) if $evt;
56 push( @e, $_->{textcode} ) for @evts;
57 $logger->info("script_builder: some events occurred: @e");
58 $logger->debug("script_builder: some events occurred: " . Dumper(\@evts));
59 $args->{_events} = \@evts;
62 return build_runner($editor, $args);
70 my $runner = OpenILS::Utils::ScriptRunner->new;
73 $runner->insert( "$evt.groupTree", $gt, 1);
76 $runner->insert( "$evt.patron", $ctx->{patron}, 1);
77 $runner->insert( "$evt.copy", $ctx->{copy}, 1);
78 $runner->insert( "$evt.volume", $ctx->{volume}, 1);
79 $runner->insert( "$evt.title", $ctx->{title}, 1);
80 $runner->insert( "$evt.requestor", $ctx->{requestor}, 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 insert_org_methods( $editor, $runner );
89 insert_copy_methods( $editor, $ctx, $runner );
100 my $flesh = { flesh => 1, flesh_fields => { acp => [ 'location', 'status', 'circ_lib' ] } };
102 if($ctx->{copy_id}) {
103 $ctx->{copy} = $e->retrieve_asset_copy(
104 [$ctx->{copy_id}, $flesh ]) or return $e->event;
106 } elsif( $ctx->{copy_barcode} ) {
108 $ctx->{copy} = $e->search_asset_copy(
109 [{barcode => $ctx->{copy_barcode}}, $flesh ])->[0]
114 return undef unless my $copy = $ctx->{copy};
116 $copy->location($e->retrieve_asset_copy_location($copy->location))
117 unless( ref $copy->location );
119 $copy->status($e->retrieve_config_copy_status($copy->status))
120 unless( ref $copy->status );
123 $e->retrieve_actor_org_unit($copy->circ_lib))
124 unless ref $copy->circ_lib;
126 $ctx->{volume} = $e->retrieve_asset_call_number(
127 $ctx->{copy}->call_number) or return $e->event;
129 $ctx->{title} = $e->retrieve_biblio_record_entry(
130 $ctx->{volume}->record) or return $e->event;
133 $e->retrieve_config_rules_age_hold_protect($copy->age_protect))
134 if $ctx->{flesh_age_protect} and $copy->age_protect;
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} } )->[0] or return $e->event;
154 $ctx->{patron} = $e->search_actor_user(
155 { card => $card->id })->[0] or return $e->event;
157 } elsif( $ctx->{fetch_patron_by_circ_copy} ) {
159 if( my $copy = $ctx->{copy} ) {
160 my $circs = $e->search_action_circulation(
161 { target_copy => $copy->id, stop_fines_time => undef });
163 if( my $circ = $circs->[0] ) {
164 $ctx->{patron} = $e->retrieve_actor_user($circ->usr)
171 return undef unless my $patron = $ctx->{patron};
173 return OpenILS::Event->new('PATRON_INACTIVE')
174 unless $U->is_true($patron->active);
176 $patron->card($e->retrieve_actor_card($patron->card));
178 return OpenILS::Event->new('PATRON_CARD_INACTIVE')
179 unless $U->is_true($patron->card->active);
181 my $expire = DateTime::Format::ISO8601->new->parse_datetime(
182 clense_ISO8601($patron->expire_date));
184 return OpenILS::Event->new('PATRON_ACCOUNT_EXPIRED')
185 if( CORE::time > $expire->epoch ) ;
188 $e->retrieve_actor_org_unit($patron->home_ou) )
189 unless ref $patron->home_ou;
191 $patron->home_ou->ou_type(
192 $patron->home_ou->ou_type->id)
193 if ref $patron->home_ou->ou_type;
196 $GROUP_TREE = $e->search_permission_grp_tree(
201 flesh_fields => { pgt => ['children'] }
206 flatten_groups($GROUP_TREE);
209 $patron->profile( $GROUP_SET{$patron->profile} )
210 unless ref $patron->profile;
213 $ctx->{requestor} = $ctx->{requestor} || $e->requestor;
215 # this could alter the requestor object within the editor..
216 #if( my $req = $ctx->{requestor} ) {
217 # $req->home_ou( $e->retrieve_actor_org_unit($requestor->home_ou) );
218 # $req->ws_ou( $e->retrieve_actor_org_unit($requestor->ws_ou) );
221 if( $ctx->{fetch_patron_circ_info} ) {
224 OpenILS::Application::Actor::_checked_out(1, $e, $patron->id);
226 $ctx->{patronOverdue} = $circ_counts->{overdue} || 0;
227 $ctx->{patronItemsOut} = $ctx->{patronOverdue} + $circ_counts->{out};
230 my $fxacts = $e->search_money_open_billable_transaction_summary(
231 { usr => $patron->id, balance_owed => { "!=" => 0 } });
234 $fines += $_->balance_owed for @$fxacts;
235 $ctx->{patronFines} = $fines;
237 $logger->debug("script_builder: patron fines determined to be $fines");
238 $logger->debug("script_builder: patron overdue count is " . $ctx->{patronOverdue});
247 return undef unless $tree;
248 $GROUP_SET{$tree->id} = $tree;
249 if( $tree->children ) {
250 flatten_groups($_) for @{$tree->children};
254 sub flatten_org_tree {
256 return undef unless $tree;
257 push( @ORG_LIST, $tree );
258 if( $tree->children ) {
259 flatten_org_tree($_) for @{$tree->children};
265 sub insert_org_methods {
266 my ( $editor, $runner ) = @_;
269 $ORG_TREE = $editor->search_actor_org_unit(
271 {"parent_ou" => undef },
274 flesh_fields => { aou => ['children'] },
275 order_by => { aou => 'name'}
279 flatten_org_tree($ORG_TREE);
285 $r->insert(__OILS_FUNC_isOrgDescendent =>
287 my( $write_key, $sname, $id ) = @_;
288 my ($parent) = grep { $_->shortname eq $sname } @ORG_LIST;
289 my ($child) = grep { $_->id == $id } @ORG_LIST;
290 my $val = is_org_descendent( $parent, $child );
291 $logger->debug("script_builder: is_org_desc returned val $val, writing to $write_key");
292 $r->insert($write_key, $val, 1) if $val; # Needs testing, was dying before
300 sub is_org_descendent {
301 my( $parent, $child ) = @_;
302 return 0 unless $parent and $child;
303 $logger->debug("script_builder: is_org_desc checking parent=".$parent->id.", child=".$child->id);
305 return 0 unless defined $child->parent_ou;
306 return 1 if $parent->id == $child->id;
307 } while( ($child) = grep { $_->id == $child->parent_ou } @ORG_LIST );
311 sub insert_copy_methods {
312 my( $e, $ctx, $runner ) = @_;
313 if( my $copy = $ctx->{copy} ) {
314 $runner->insert_method( 'environment.copy', '__OILS_FUNC_fetch_best_hold', sub {
316 $logger->debug("script_builder: searching for permitted hold for copy ".$copy->barcode);
317 my ($hold) = $holdcode->find_nearest_permitted_hold(
318 OpenSRF::AppSession->create('open-ils.storage'), $copy, $e->requestor );
319 $runner->insert( $key, $hold, 1 );