1 package OpenILS::Application::Circ::Rules;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
5 use OpenSRF::Utils::SettingsClient;
6 use OpenILS::Utils::Fieldmapper;
8 use Template qw(:template);
11 use Time::HiRes qw(time);
12 use OpenILS::Utils::ModsParser;
15 use OpenSRF::EX qw(:try);
17 use OpenILS::Application::AppUtils;
18 my $apputils = "OpenILS::Application::AppUtils";
19 use Digest::MD5 qw(md5_hex);
21 # ----------------------------------------------------------------
24 my $permission_script;
26 my $recurring_fines_script;
28 # ----------------------------------------------------------------
31 # data used for this circulation transaction
32 my $circ_objects = {};
34 # some static data from the database
38 my $shelving_locations;
43 # memcache for caching the circ objects
47 use constant NO_COPY => 100;
51 my $conf = OpenSRF::Utils::SettingsClient->new;
53 # ----------------------------------------------------------------
54 # set up the rules scripts
55 # ----------------------------------------------------------------
56 $circ_script = $conf->config_value(
57 "apps", "open-ils.circ","app_settings", "rules", "main");
59 $permission_script = $conf->config_value(
60 "apps", "open-ils.circ","app_settings", "rules", "permission");
62 $duration_script = $conf->config_value(
63 "apps", "open-ils.circ","app_settings", "rules", "duration");
65 $recurring_fines_script = $conf->config_value(
66 "apps", "open-ils.circ","app_settings", "rules", "recurring_fines");
68 $max_fines_script = $conf->config_value(
69 "apps", "open-ils.circ","app_settings", "rules", "max_fines");
72 $cache_handle = OpenSRF::Utils::Cache->new();
77 # ----------------------------------------------------------------
78 # Collect all of the objects necessary for calculating the
80 # ----------------------------------------------------------------
81 sub gather_circ_objects {
82 my( $session, $barcode_string, $patron_id ) = @_;
84 throw OpenSRF::EX::ERROR
85 ("gather_circ_objects needs data")
86 unless ($barcode_string and $patron_id);
88 warn "Gathering circ objects with barcode $barcode_string and patron id $patron_id\n";
92 # see if all of the circ objects are in cache
93 my $cache_key = "circ_object_" . md5_hex( $barcode_string, $patron_id );
94 $circ_objects = $cache_handle->get_cache($cache_key);
97 $stash = Template::Stash->new(
98 circ_objects => $circ_objects,
100 target_copy_status => 1,
105 # grab the patron standing list of we don't already have it
106 # only necessary if the circ objects have not been built yet
107 if(!$patron_standings) {
108 my $standing_req = $session->request(
109 "open-ils.storage.direct.config.standing.retrieve.all.atomic");
110 $patron_standings = $standing_req->gather(1);
112 { map { (''.$_->id => $_->value) } @$patron_standings };
115 # grab patron profiles
116 if(!$patron_profiles) {
117 my $profile_req = $session->request(
118 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
119 $patron_profiles = $profile_req->gather(1);
121 { map { (''.$_->id => $_->name) } @$patron_profiles };
125 my $copy_req = $session->request(
126 "open-ils.storage.fleshed.asset.copy.search.barcode",
129 my $patron_req = $session->request(
130 "open-ils.storage.direct.actor.user.retrieve",
133 my $copy = $copy_req->gather(1)->[0];
134 if(!$copy) { return NO_COPY; }
136 $copy->status( $copy->status->name );
137 $circ_objects->{copy} = $copy;
139 my $patron = $patron_req->gather(1);
140 $patron->standing($patron_standings->{$patron->standing()});
141 $patron->profile( $patron_profiles->{$patron->profile});
142 $circ_objects->{patron} = $patron;
145 my $title_req = $session->request(
146 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
147 $circ_objects->{copy}->id() );
149 $circ_objects->{title} = $title_req->gather(1);
151 $cache_handle->put_cache( $cache_key, $circ_objects, 30 );
153 $stash = Template::Stash->new(
154 circ_objects => $circ_objects,
156 target_copy_status => 1,
167 my $template = Template->new(
175 my $status = $template->process($circ_script);
178 throw OpenSRF::EX::ERROR
179 ("Error processing circ script " . $template->error());
182 warn "Script result: $result\n";
188 __PACKAGE__->register_method(
189 method => "permit_circ",
190 api_name => "open-ils.circ.permit_checkout",
194 my( $self, $client, $user_session, $barcode, $user_id, $outstanding_count ) = @_;
196 $outstanding_count ||= 0;
198 my $session = OpenSRF::AppSession->create("open-ils.storage");
200 # collect items necessary for circ calculation
201 my $status = gather_circ_objects( $session, $barcode, $user_id );
203 if( $status == NO_COPY ) {
204 return { record => undef,
206 text => "No copy available with barcode $barcode"
212 $stash->set("run_block", $permission_script);
214 # grab the number of copies checked out by the patron as
215 # well as the total fines
216 my $summary_req = $session->request(
217 "open-ils.storage.action.circulation.patron_summary",
218 $stash->get("circ_objects")->{patron}->id );
219 my $summary = $summary_req->gather(1);
221 $stash->set("patron_copies", $summary->[0] + $outstanding_count );
222 $stash->set("patron_fines", $summary->[1] );
224 # run the permissibility script
226 my $obj = $stash->get("circ_objects");
228 # turn the biblio record into a friendly object
229 my $u = OpenILS::Utils::ModsParser->new();
230 $u->start_mods_batch( $obj->{title}->marc() );
231 my $mods = $u->finish_mods_batch();
233 my $arr = $stash->get("result");
234 return { record => $mods, status => $arr->[0], text => $arr->[1] };
239 __PACKAGE__->register_method(
240 method => "circulate",
241 api_name => "open-ils.circ.checkout.barcode",
245 my( $self, $client, $user_session, $barcode, $patron ) = @_;
248 my $session = $apputils->start_db_session();
250 gather_circ_objects( $session, $barcode, $patron );
252 # grab the copy statuses if we don't already have them
253 if(!$copy_statuses) {
254 my $csreq = $session->request(
255 "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
256 $copy_statuses = $csreq->gather(1);
259 # put copy statuses into the stash
260 $stash->set("copy_statuses", $copy_statuses );
262 my $copy = $circ_objects->{copy};
263 my ($circ, $duration, $recurring, $max) = run_circ_scripts($session);
265 # commit new circ object to db
266 my $commit = $session->request(
267 "open-ils.storage.direct.action.circulation.create",
269 my $id = $commit->gather(1);
272 throw OpenSRF::EX::ERROR
273 ("Error creating new circulation object");
276 # update the copy with the new circ
277 $copy->status( $stash->get("target_copy_status") );
278 $copy->location( $copy->location->id );
279 $copy->circ_lib( $copy->circ_lib->id );
282 my $copy_update = $session->request(
283 "open-ils.storage.direct.asset.copy.update",
285 $copy_update->gather(1);
287 $apputils->commit_db_session($session);
289 # remove our circ object from the cache
290 $cache_handle->delete_cache("circ_object_" . md5_hex($barcode, $patron));
292 # re-retrieve the the committed circ object
293 $circ = $apputils->simple_scalar_request(
295 "open-ils.storage.direct.action.circulation.retrieve",
299 # push the rules and due date into the circ object
300 $circ->duration_rule($duration);
301 $circ->max_fine_rule($max);
302 $circ->recuring_fine_rule($recurring);
305 OpenSRF::Utils->interval_to_seconds(
306 $circ->duration ) + int(time());
308 $circ->due_date($due_date);
316 # runs the duration, recurring_fines, and max_fines scripts.
317 # builds the new circ object based on the rules returned from
319 # returns (circ, duration_rule, recurring_fines_rule, max_fines_rule)
320 sub run_circ_scripts {
323 # go through all of the scripts and process
324 # each script returns
325 # [ rule_name, level (appropriate to the script) ]
326 $stash->set("result", [] );
327 $stash->set("run_block", $duration_script);
329 my $duration_rule = $stash->get("result");
331 $stash->set("run_block", $recurring_fines_script);
332 $stash->set("result", [] );
334 my $rec_fines_rule = $stash->get("result");
336 $stash->set("run_block", $max_fines_script);
337 $stash->set("result", [] );
339 my $max_fines_rule = $stash->get("result");
341 my $obj = $stash->get("circ_objects");
343 # ----------------------------------------------------------
344 # find the rules objects based on the rule names returned from
345 # the various scripts.
346 my $dur_req = $session->request(
347 "open-ils.storage.direct.config.rules.circ_duration.search.name",
348 $duration_rule->[0] );
350 my $rec_req = $session->request(
351 "open-ils.storage.direct.config.rules.recuring_fine.search.name",
352 $rec_fines_rule->[0] );
354 my $max_req = $session->request(
355 "open-ils.storage.direct.config.rules.max_fine.search.name",
356 $max_fines_rule->[0] );
358 my $duration = $dur_req->gather(1)->[0];
359 my $recurring = $rec_req->gather(1)->[0];
360 my $max = $max_req->gather(1)->[0];
362 my $copy = $circ_objects->{copy};
364 # build the new circ object
365 my $circ = build_circ_object($session, $copy, $duration_rule->[1],
366 $rec_fines_rule->[1], $duration, $recurring, $max );
368 return ($circ, $duration, $recurring, $max);
372 # ------------------------------------------------------------------
373 # Builds a new circ object
374 # ------------------------------------------------------------------
375 sub build_circ_object {
376 my( $session, $copy, $dur_level, $rec_level,
377 $duration, $recurring, $max ) = @_;
379 my $circ = new Fieldmapper::action::circulation;
381 $circ->circ_lib( $copy->circ_lib->id() );
382 if($dur_level == 1) {
383 $circ->duration( $duration->shrt );
384 } elsif($dur_level == 2) {
385 $circ->duration( $duration->normal );
386 } elsif($dur_level == 3) {
387 $circ->duration( $duration->extended );
390 if($rec_level == 1) {
391 $circ->recuring_fine( $recurring->low );
392 } elsif($rec_level == 2) {
393 $circ->recuring_fine( $recurring->normal );
394 } elsif($rec_level == 3) {
395 $circ->recuring_fine( $recurring->high );
398 $circ->duration_rule( $duration->name );
399 $circ->recuring_fine_rule( $recurring->name );
400 $circ->max_fine_rule( $max->name );
401 $circ->max_fine( $max->amount );
403 $circ->fine_interval($recurring->recurance_interval);
404 $circ->renewal_remaining( $duration->max_renewals );
405 $circ->target_copy( $copy->id );
406 $circ->usr( $circ_objects->{patron}->id );
412 __PACKAGE__->register_method(
414 api_name => "open-ils.circ.checkin.barcode",
418 my( $self, $user_session, $client, $barcode ) = @_;
424 my $session = $apputils->start_db_session();
426 warn "retrieving copy for checkin\n";
428 if(!$shelving_locations) {
429 my $sh_req = $session->request(
430 "open-ils.storage.direct.asset.copy_location.retrieve.all.atomic");
431 $shelving_locations = $sh_req->gather(1);
432 $shelving_locations =
433 { map { (''.$_->id => $_->name) } @$shelving_locations };
436 my $copy_req = $session->request(
437 "open-ils.storage.direct.asset.copy.search.barcode",
439 $copy = $copy_req->gather(1)->[0];
442 # find circ's where the transaction is still open for the
443 # given copy. should only be one.
444 warn "Retrieving circ for checking\n";
445 my $circ_req = $session->request(
446 "open-ils.storage.direct.action.circulation.search.atomic",
447 { target_copy => $copy->id, xact_finish => undef } );
449 my $circ = $circ_req->gather(1)->[0];
452 $err = "No circulation exists for the given barcode";
456 warn "Checking in circ ". $circ->id . "\n";
458 $circ->stop_fines("CHECKIN");
459 $circ->xact_finish("now");
461 my $cp_up = $session->request(
462 "open-ils.storage.direct.asset.copy.update",
466 my $ci_up = $session->request(
467 "open-ils.storage.direct.action.circulation.update",
471 $apputils->commit_db_session($session);
473 warn "Checkin succeeded\n";
478 $err = "Error checking in: $e";
482 return { record => undef, status => -1, text => $err };
486 my $record = $apputils->simple_scalar_request(
488 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
491 my $u = OpenILS::Utils::ModsParser->new();
492 $u->start_mods_batch( $record->marc() );
493 my $mods = $u->finish_mods_batch();
494 return { record => $mods, status => 0, text => "OK",
495 route_to => $shelving_locations->{$copy->location} };