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";
25 # -----------------------------------------------------------------------
35 # fetch_patron_circ_info - load info on items out, overdues, and fines.
37 # _direct - this is a hash of key/value pairs to shove directly into the
38 # script runner. Use this to cover data not covered by this module
39 # -----------------------------------------------------------------------
41 my( $class, $args ) = @_;
47 my $editor = $$args{editor};
50 $editor = new_editor(xact => 1);
54 $args->{_direct} = {} unless $args->{_direct};
55 #$args->{editor} = $editor;
57 $evt = fetch_bib_data($editor, $args);
58 push(@evts, $evt) if $evt;
59 $evt = fetch_user_data($editor, $args);
60 push(@evts, $evt) if $evt;
64 push( @e, $_->{textcode} ) for @evts;
65 $logger->info("script_builder: some events occurred: @e");
66 $logger->debug("script_builder: some events occurred: " . Dumper(\@evts));
67 $args->{_events} = \@evts;
70 my $r = build_runner($editor, $args);
71 $editor->rollback if $rollback;
80 my $runner = OpenILS::Utils::ScriptRunner->new;
83 $runner->insert( "$evt.groupTree", $gt, 1);
86 $runner->insert( "$evt.patron", $ctx->{patron}, 1);
87 $runner->insert( "$evt.copy", $ctx->{copy}, 1);
88 $runner->insert( "$evt.volume", $ctx->{volume}, 1);
89 $runner->insert( "$evt.title", $ctx->{title}, 1);
91 if( ref $ctx->{requestor} ) {
92 $runner->insert( "$evt.requestor", $ctx->{requestor}, 1);
93 if($ctx->{requestor}->ws_ou) {
94 $runner->insert( "$evt.location",
95 $editor->retrieve_actor_org_unit($ctx->{requestor}->ws_ou), 1);
99 $runner->insert( "$evt.patronItemsOut", $ctx->{patronItemsOut}, 1 );
100 $runner->insert( "$evt.patronOverdueCount", $ctx->{patronOverdue}, 1 );
101 $runner->insert( "$evt.patronFines", $ctx->{patronFines}, 1 );
103 $runner->insert("$evt.$_", $ctx->{_direct}->{$_}, 1) for keys %{$ctx->{_direct}};
105 insert_org_methods( $editor, $runner );
106 insert_copy_methods( $editor, $ctx, $runner );
118 acp => [ 'location', 'status', 'circ_lib', 'age_protect', 'call_number' ],
124 $ctx->{copy_id} = $ctx->{copy}->id
125 unless $ctx->{copy_id} or $ctx->{copy_barcode};
130 if($ctx->{copy_id}) {
131 $copy = $e->retrieve_asset_copy(
132 [$ctx->{copy_id}, $flesh ]) or return $e->event;
134 } elsif( $ctx->{copy_barcode} ) {
136 $copy = $e->search_asset_copy(
137 [{barcode => $ctx->{copy_barcode}, deleted => 'f'}, $flesh ])->[0]
141 return undef unless $copy;
143 my $vol = $copy->call_number;
144 my $rec = $vol->record;
145 $ctx->{copy} = $copy;
146 $ctx->{volume} = $vol;
147 $copy->call_number($vol->id);
148 $ctx->{title} = $rec;
149 $vol->record($rec->id);
156 sub fetch_user_data {
162 au => [ qw/ profile home_ou card / ],
163 aou => [ 'ou_type' ],
167 if( $ctx->{patron} ) {
168 $ctx->{patron_id} = $ctx->{patron}->id unless $ctx->{patron_id};
173 if( $ctx->{patron_id} ) {
174 $patron = $e->retrieve_actor_user([$ctx->{patron_id}, $flesh]);
176 } elsif( $ctx->{patron_barcode} ) {
178 my $card = $e->search_actor_card(
179 { barcode => $ctx->{patron_barcode} } )->[0] or return $e->event;
181 $patron = $e->search_actor_user(
182 [{ card => $card->id }, $flesh ]
183 )->[0] or return $e->event;
185 } elsif( $ctx->{fetch_patron_by_circ_copy} ) {
187 if( my $copy = $ctx->{copy} ) {
188 my $circs = $e->search_action_circulation(
189 { target_copy => $copy->id, checkin_time => undef });
191 if( my $circ = $circs->[0] ) {
192 $patron = $e->retrieve_actor_user([$circ->usr, $flesh])
198 return undef unless $ctx->{patron} = $patron;
202 $ctx->{requestor} = $ctx->{requestor} || $e->requestor;
204 if( $ctx->{fetch_patron_circ_info} ) {
206 OpenILS::Application::Actor::_checked_out(1, $e, $patron->id);
208 $ctx->{patronOverdue} = $circ_counts->{overdue} || 0;
209 my $out = $ctx->{patronOverdue} + $circ_counts->{out};
211 $ctx->{patronItemsOut} = $out
212 unless( $ctx->{patronItemsOut} and $ctx->{patronItemsOut} > $out );
214 $logger->debug("script_builder: patron overdue count is " . $ctx->{patronOverdue});
217 if( $ctx->{fetch_patron_money_info} ) {
218 $ctx->{patronFines} = $U->patron_money_owed($patron->id);
219 $logger->debug("script_builder: patron fines determined to be ".$ctx->{patronFines});
222 unless( $ctx->{ignore_user_status} ) {
223 return OpenILS::Event->new('PATRON_INACTIVE')
224 unless $U->is_true($patron->active);
226 return OpenILS::Event->new('PATRON_CARD_INACTIVE')
227 unless $U->is_true($patron->card->active);
229 my $expire = DateTime::Format::ISO8601->new->parse_datetime(
230 clense_ISO8601($patron->expire_date));
232 return OpenILS::Event->new('PATRON_ACCOUNT_EXPIRED')
233 if( CORE::time > $expire->epoch ) ;
245 $GROUP_TREE = $e->search_permission_grp_tree(
250 flesh_fields => { pgt => ['children'] }
257 return undef unless $tree;
258 $GROUP_SET{$tree->id} = $tree;
259 if( $tree->children ) {
260 flatten_groups($e, $_) for @{$tree->children};
264 sub flatten_org_tree {
266 return undef unless $tree;
267 push( @ORG_LIST, $tree );
268 if( $tree->children ) {
269 flatten_org_tree($_) for @{$tree->children};
275 sub insert_org_methods {
276 my ( $editor, $runner ) = @_;
279 $ORG_TREE = $editor->search_actor_org_unit(
281 {"parent_ou" => undef },
284 flesh_fields => { aou => ['children'] },
285 order_by => { aou => 'name'}
289 flatten_org_tree($ORG_TREE);
295 $r->insert(__OILS_FUNC_isOrgDescendent =>
297 my( $write_key, $sname, $id ) = @_;
298 my ($parent) = grep { $_->shortname eq $sname } @ORG_LIST;
299 my ($child) = grep { $_->id == $id } @ORG_LIST;
300 my $val = is_org_descendent( $parent, $child );
301 $logger->debug("script_builder: is_org_desc $sname:$id returned val $val, writing to $write_key");
302 $r->insert($write_key, $val, 1) if $val;
307 $r->insert(__OILS_FUNC_hasCommonAncestor =>
309 my( $write_key, $orgid1, $orgid2, $depth ) = @_;
310 my $val = has_common_ancestor( $orgid1, $orgid2, $depth );
311 $logger->debug("script_builder: has_common_ancestor resturned $val");
312 $r->insert($write_key, $val, 1) if $val;
319 sub is_org_descendent {
320 my( $parent, $child ) = @_;
321 return 0 unless $parent and $child;
322 $logger->debug("script_builder: is_org_desc checking parent=".$parent->id.", child=".$child->id);
324 return 0 unless defined $child->parent_ou;
325 return 1 if $parent->id == $child->id;
326 } while( ($child) = grep { $_->id == $child->parent_ou } @ORG_LIST );
330 sub has_common_ancestor {
331 my( $org1, $org2, $depth ) = @_;
332 return 0 unless $org1 and $org2;
333 $logger->debug("script_builder: has_common_ancestor checking orgs $org1 : $org2");
335 return 1 if $org1 == $org2;
336 ($org1) = grep { $_->id == $org1 } @ORG_LIST;
337 ($org2) = grep { $_->id == $org2 } @ORG_LIST;
339 my $p1 = find_parent_at_depth($org1, $depth);
340 my $p2 = find_parent_at_depth($org2, $depth);
342 return 1 if $p1->id == $p2->id;
347 sub find_parent_at_depth {
350 return undef unless $org and $depth;
353 my ($t) = grep { $_->id == $org->ou_type } @OU_TYPES;
354 return $org if $t->depth == $depth;
355 } while( ($org) = grep { $_->id == $org->parent_ou } @ORG_LIST );
362 @OU_TYPES = @{new_editor()->retrieve_all_actor_org_unit_type()};
365 sub insert_copy_methods {
366 my( $e, $ctx, $runner ) = @_;
367 my $reqr = $ctx->{requestor} || $e->requestor;
368 if( my $copy = $ctx->{copy} ) {
369 $runner->insert_method( 'environment.copy', '__OILS_FUNC_fetch_best_hold', sub {
371 $logger->debug("script_builder: searching for permitted hold for copy ".$copy->barcode);
372 my ($hold) = $holdcode->find_nearest_permitted_hold( $e, $copy, $reqr, 1 );
373 $runner->insert( $key, $hold, 1 );