1 package OpenILS::Application::Circ::Circulate;
2 use base 'OpenSRF::Application';
3 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
6 use OpenSRF::Utils::Logger qw(:logger);
7 use OpenILS::Utils::ScriptRunner;
8 use OpenILS::Application::AppUtils;
9 use OpenILS::Application::Circ::Holds;
10 $Data::Dumper::Indent = 0;
11 my $apputils = "OpenILS::Application::AppUtils";
12 my $holdcode = "OpenILS::Application::Circ::Holds";
14 my %scripts; # - circulation script filenames
16 my $script_libs; # - any additional script libraries
19 my %contexts; # - Script runner contexts
21 # ------------------------------------------------------------------------------
22 # Load the circ script from the config
23 # ------------------------------------------------------------------------------
27 my $conf = OpenSRF::Utils::SettingsClient->new;
28 my @pfx = ( "apps", "open-ils.circ","app_settings", "scripts" );
30 my $p = $conf->config_value( @pfx, 'permission' );
31 my $d = $conf->config_value( @pfx, 'duration' );
32 my $f = $conf->config_value( @pfx, 'recurring_fines' );
33 my $m = $conf->config_value( @pfx, 'max_fines' );
34 my $pr = $conf->config_value( @pfx, 'permit_renew' );
35 my $ph = $conf->config_value( @pfx, 'permit_hold' );
36 my $lb = $conf->config_value( 'apps', 'open-ils.circ', 'app_settings', 'script_path' );
38 $logger->error( "Missing circ script(s)" )
39 unless( $p and $d and $f and $m and $pr and $ph );
41 $scripts{circ_permit} = $p;
42 $scripts{circ_duration} = $d;
43 $scripts{circ_recurring_fines}= $f;
44 $scripts{circ_max_fines} = $m;
45 $scripts{circ_renew_permit} = $pr;
46 $scripts{hold_permit} = $ph;
48 $lb = [ $lb ] unless ref($lb);
51 $logger->debug("Loaded rules scripts for circ: " .
52 "circ permit : $p, circ duration :$d , circ recurring fines : $f, " .
53 "circ max fines : $m, circ renew permit : $pr, permit hold: $ph");
57 # ------------------------------------------------------------------------------
58 # Loads the necessary circ objects and pushes them into the script environment
59 # Returns ( $data, $evt ). if $evt is defined, then an
60 # unexpedted event occurred and should be dealt with / returned to the caller
61 # ------------------------------------------------------------------------------
65 my $barcode = $params{barcode};
66 my $patron = $params{patron};
67 my $fetch_summary = $params{fetch_patron_circ_summary};
68 my $fetch_cstatus = $params{fetch_copy_statuses};
69 my $fetch_clocs = $params{fetch_copy_locations};
73 $ctx->{patron} = $patron;
74 $ctx->{type} = $params{type};
76 if(!defined($cache{patron_standings})) {
77 $cache{patron_standings} = $apputils->fetch_patron_standings();
78 $cache{group_tree} = $apputils->fetch_permission_group_tree();
81 $ctx->{patron_standings} = $cache{patron_standings};
82 $ctx->{group_tree} = $cache{group_tree};
84 $cache{copy_statuses} = $apputils->fetch_copy_statuses
85 if( $fetch_cstatus and !defined($cache{copy_statuses}) );
87 $cache{copy_locations} = $apputils->fetch_copy_locations
88 if( $fetch_clocs and !defined($cache{copy_locations}));
90 $ctx->{copy_statuses} = $cache{copy_statuses};
91 $ctx->{copy_locations} = $cache{copy_locations};
93 $ctx->{patron_circ_summary} =
94 $apputils->fetch_patron_circ_summary($patron->id) if $fetch_summary;
96 ( $ctx->{copy}, $evt ) = $apputils->fetch_copy_by_barcode( $barcode );
97 return ( undef, $evt ) if $evt;
99 ( $ctx->{title}, $evt ) = $apputils->fetch_record_by_copy( $ctx->{copy}->id );
100 return ( undef, $evt ) if $evt;
102 _doctor_circ_objects($ctx);
103 _build_circ_script_runner($ctx);
104 _add_script_runner_methods( $ctx );
110 # ------------------------------------------------------------------------------
111 # Patches up circ objects to make them easier to use from within the script
113 # ------------------------------------------------------------------------------
114 sub _doctor_circ_objects {
117 my $patron = $ctx->{patron};
118 my $copy = $ctx->{copy};
120 for my $s (@{$ctx->{patron_standings}}) {
121 $patron->standing($s) if ( $s->id eq $ctx->{patron}->standing );
124 # set the patron ptofile to the profile name
125 $patron->profile( _get_patron_profile( $patron, $ctx->{group_tree} ) );
128 $patron->home_ou( $apputils->fetch_org_unit( $patron->home_ou ) );
130 # set the copy status to a status name
131 $copy->status( _get_copy_status( $copy, $ctx->{copy_statuses} ) );
133 # set the copy location to the location object
134 $copy->location( _get_copy_location( $copy, $ctx->{copy_locations} ) );
138 # recurse and find the patron profile name from the tree
139 # another option would be to grab the groups for the patron
140 # and cycle through those until the "profile" group has been found
141 sub _get_patron_profile {
142 my( $patron, $group_tree ) = @_;
143 return $group_tree if ($group_tree->id eq $patron->profile);
144 for my $child (@{$group_tree->children}) {
145 my $ret = _get_patron_profile( $patron, $child );
151 sub _get_copy_status {
152 my( $copy, $cstatus ) = @_;
153 for my $status (@$cstatus) {
154 return $status if( $status->id eq $copy->status )
159 sub _get_copy_location {
160 my( $copy, $locations ) = @_;
161 for my $loc (@$locations) {
162 return $loc if $loc->id eq $copy->location;
167 # ------------------------------------------------------------------------------
168 # Constructs and shoves data into the script environment
169 # ------------------------------------------------------------------------------
170 sub _build_circ_script_runner {
173 $logger->debug("Loading script environment for circulation");
176 if( $runner = $contexts{$ctx->{type}} ) {
177 $runner->refresh_context;
179 $runner = OpenILS::Utils::ScriptRunner->new unless $runner;
180 $contexts{type} = $runner;
184 $logger->debug("Loading circ script lib path $_");
185 $runner->add_path( $_ );
188 $runner->insert( 'patron', $ctx->{patron} );
189 $runner->insert( 'title', $ctx->{title} );
190 $runner->insert( 'copy', $ctx->{copy} );
193 $runner->insert( 'result', {} );
194 $runner->insert( 'result.event', 'SUCCESS' );
196 if(ref($ctx->{patron_circ_summary})) {
197 $runner->insert( 'patron_info', {} );
198 $runner->insert( 'patron_info.items_out', $ctx->{patron_circ_summary}->[0] );
199 $runner->insert( 'patron_info.fines', $ctx->{patron_circ_summary}->[1] );
202 $ctx->{runner} = $runner;
207 sub _add_script_runner_methods {
209 my $runner = $ctx->{runner};
211 # allows a script to fetch a hold that is currently targeting the
213 $runner->insert_method( 'copy', '__OILS_FUNC_fetch_hold', sub {
215 my $hold = $holdcode->fetch_open_hold_by_current_copy($ctx->{copy}->id);
216 $hold = undef unless $hold;
217 $runner->insert( $key, $hold );
222 # ------------------------------------------------------------------------------
224 __PACKAGE__->register_method(
225 method => "permit_circ",
226 api_name => "open-ils.circ.permit_checkout_",
228 Determines if the given checkout can occur
229 @param authtoken The login session key
230 @param params A trailing list of named params including
231 barcode : The copy barcode,
232 patron : The patron the checkout is occurring for,
233 renew : true or false - whether or not this is a renewal
234 @return The event that occurred during the permit check.
235 If all is well, the SUCCESS event is returned
239 my( $self, $client, $authtoken, %params ) = @_;
241 my $barcode = $params{barcode};
242 my $patronid = $params{patron};
243 my $isrenew = $params{renew};
244 my ( $requestor, $patron, $ctx, $evt );
247 # check permisson of the requestor
248 ( $requestor, $patron, $evt ) =
249 $apputils->checkses_requestor(
250 $authtoken, $patronid, 'VIEW_PERMIT_CHECKOUT' );
253 $logger->info("Checking circulation permission for staff: " . $requestor->id .
254 ", patron " . $patron->id . ", and barcode $barcode" );
256 # fetch and build the circulation environment
257 ( $ctx, $evt ) = create_circ_ctx(
261 fetch_patron_circ_summary => 1,
262 fetch_copy_statuses => 1,
263 fetch_copy_locations => 1,
268 my $runner = $ctx->{runner};
270 $runner->load($scripts{circ_permit});
271 $runner->run or throw OpenSRF::EX::ERROR ("Circ Permit Script Died: $@");
273 my $evtname = $runner->retrieve('result.event');
274 $logger->activity("Permit Circ for user $patronid and barcode $barcode returned event: $evtname");
275 return OpenILS::Event->new($evtname);
279 # ------------------------------------------------------------------------------
281 __PACKAGE__->register_method(
282 method => "circulate",
283 api_name => "open-ils.circ.checkout.barcode_",
284 notes => <<" NOTES");
285 Checks out an item based on barcode
286 PARAMS( authtoken, barcode => bc, patron => pid )
290 my( $self, $client, $authtoken, %params ) = @_;
291 my $barcode = $params{barcode};
292 my $patronid = $params{patron};
296 # ------------------------------------------------------------------------------
298 __PACKAGE__->register_method(
300 api_name => "open-ils.circ.checkin.barcode_",
301 notes => <<" NOTES");
302 PARAMS( authtoken, barcode => bc )
303 Checks in based on barcode
304 Returns an event object whose payload contains the record, circ, and copy
305 If the item needs to be routed, the event is a ROUTE_COPY event
306 with an additional 'route_to' variable set on the event
310 my( $self, $client, $authtoken, %params ) = @_;
311 my $barcode = $params{barcode};
314 # ------------------------------------------------------------------------------
316 __PACKAGE__->register_method(
318 api_name => "open-ils.circ.renew_",
319 notes => <<" NOTES");
320 PARAMS( authtoken, circ => circ_id );
321 open-ils.circ.renew(login_session, circ_object);
322 Renews the provided circulation. login_session is the requestor of the
323 renewal and if the logged in user is not the same as circ->usr, then
324 the logged in user must have RENEW_CIRC permissions.
328 my( $self, $client, $authtoken, %params ) = @_;
329 my $circ = $params{circ};