1 # ---------------------------------------------------------------
2 # Copyright (C) 2005 Georgia Public Library Service
3 # Bill Erickson <highfalutin@gmail.com>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
16 package OpenILS::Application::Circ::Rules;
17 use base qw/OpenSRF::Application/;
18 use strict; use warnings;
20 use OpenSRF::Utils::SettingsClient;
21 use OpenILS::Utils::Fieldmapper;
23 use OpenSRF::Utils::Logger qw(:level);
25 use Template qw(:template);
28 use Time::HiRes qw(time);
29 use OpenILS::Utils::ModsParser;
32 use OpenSRF::EX qw(:try);
34 use OpenILS::Application::AppUtils;
35 my $apputils = "OpenILS::Application::AppUtils";
36 use Digest::MD5 qw(md5_hex);
38 my $log = "OpenSRF::Utils::Logger";
40 # ----------------------------------------------------------------
43 my $permission_script;
45 my $recurring_fines_script;
47 my $permit_hold_script;
48 my $permit_renew_script;
49 # ----------------------------------------------------------------
52 # data used for this circulation transaction
53 my $circ_objects = {};
55 # some static data from the database
59 my $shelving_locations;
64 # memcache for caching the circ objects
68 use constant NO_COPY => 100;
73 my $conf = OpenSRF::Utils::SettingsClient->new;
75 # ----------------------------------------------------------------
76 # set up the rules scripts
77 # ----------------------------------------------------------------
78 $circ_script = $conf->config_value(
79 "apps", "open-ils.circ","app_settings", "rules", "main");
81 $permission_script = $conf->config_value(
82 "apps", "open-ils.circ","app_settings", "rules", "permission");
84 $duration_script = $conf->config_value(
85 "apps", "open-ils.circ","app_settings", "rules", "duration");
87 $recurring_fines_script = $conf->config_value(
88 "apps", "open-ils.circ","app_settings", "rules", "recurring_fines");
90 $max_fines_script = $conf->config_value(
91 "apps", "open-ils.circ","app_settings", "rules", "max_fines");
93 $permit_hold_script = $conf->config_value(
94 "apps", "open-ils.circ","app_settings", "rules", "permit_hold");
96 $permit_renew_script = $conf->config_value(
97 "apps", "open-ils.circ","app_settings", "rules", "permit_renew");
99 $log->debug("Loaded rules scripts for circ:\n".
100 "main - $circ_script : permit circ - $permission_script\n".
101 "duration - $duration_script : recurring - $recurring_fines_script\n".
102 "max fines - $max_fines_script : permit hold - $permit_hold_script", DEBUG);
105 $cache_handle = OpenSRF::Utils::Cache->new();
109 sub _grab_patron_standings {
111 if(!$patron_standings) {
112 my $standing_req = $session->request(
113 "open-ils.storage.direct.config.standing.retrieve.all.atomic");
114 $patron_standings = $standing_req->gather(1);
116 { map { (''.$_->id => $_->value) } @$patron_standings };
120 sub _grab_patron_profiles {
122 if(!$patron_profiles) {
123 my $profile_req = $session->request(
124 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
125 $patron_profiles = $profile_req->gather(1);
127 { map { (''.$_->id => $_->name) } @$patron_profiles };
134 my $patron_id = shift;
135 my $patron_req = $session->request(
136 "open-ils.storage.direct.actor.user.retrieve",
138 return $patron_req->gather(1);
142 sub _grab_title_by_copy {
145 my $title_req = $session->request(
146 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
148 return $title_req->gather(1);
151 sub _grab_patron_summary {
153 my $patron_id = shift;
154 my $summary_req = $session->request(
155 "open-ils.storage.action.circulation.patron_summary",
157 return $summary_req->gather(1);
160 sub _grab_copy_by_barcode {
161 my($session, $barcode) = @_;
162 warn "Searching for copy with barcode $barcode\n";
163 my $copy_req = $session->request(
164 "open-ils.storage.fleshed.asset.copy.search.barcode",
166 return $copy_req->gather(1);
169 sub _grab_copy_by_id {
170 my($session, $id) = @_;
171 warn "Searching for copy with id $id\n";
172 my $copy_req = $session->request(
173 "open-ils.storage.direct.asset.copy.retrieve",
175 my $c = $copy_req->gather(1);
176 if($c) { return _grab_copy_by_barcode($session, $c->barcode); }
181 sub gather_hold_objects {
182 my($session, $hold, $copy, $args) = @_;
184 _grab_patron_standings($session);
185 _grab_patron_profiles($session);
189 $copy = _grab_copy_by_barcode($session, $copy->barcode);
191 my $hold_objects = {};
192 $hold_objects->{standings} = $patron_standings;
193 $hold_objects->{copy} = $copy;
194 $hold_objects->{hold} = $hold;
195 $hold_objects->{title} = $$args{title} || _grab_title_by_copy($session, $copy->id);
196 $hold_objects->{requestor} = _grab_user($session, $hold->requestor);
197 my $patron = _grab_user($session, $hold->usr);
199 $copy->status( $copy->status->name );
200 $patron->standing($patron_standings->{$patron->standing()});
201 $patron->profile( $patron_profiles->{$patron->profile});
203 $hold_objects->{patron} = $patron;
205 return $hold_objects;
210 __PACKAGE__->register_method(
211 method => "permit_hold",
212 api_name => "open-ils.circ.permit_hold",
213 notes => <<" NOTES");
214 Determines whether a given copy is eligible to be held
218 my( $self, $client, $hold, $copy, $args ) = @_;
220 my $session = OpenSRF::AppSession->create("open-ils.storage");
222 # collect items necessary for circ calculation
223 my $hold_objects = gather_hold_objects( $session, $hold, $copy, $args );
225 $stash = Template::Stash->new(
226 circ_objects => $hold_objects,
229 $stash->set("run_block", $permit_hold_script);
231 # grab the number of copies checked out by the patron as
232 # well as the total fines
233 my $summary = _grab_patron_summary($session, $hold_objects->{patron}->id);
235 $summary->[1] ||= 0.0;
237 $stash->set("patron_copies", $summary->[0] );
238 $stash->set("patron_fines", $summary->[1] );
240 # run the permissibility script
242 my $result = $stash->get("result");
244 # 0 means OK in the script
245 return 1 if($result->[0] == 0);
254 # ----------------------------------------------------------------
255 # Collect all of the objects necessary for calculating the
257 # ----------------------------------------------------------------
258 sub gather_circ_objects {
259 my( $session, $barcode_string, $patron_id ) = @_;
261 throw OpenSRF::EX::ERROR
262 ("gather_circ_objects needs data")
263 unless ($barcode_string and $patron_id);
265 warn "Gathering circ objects with barcode $barcode_string and patron id $patron_id\n";
267 # see if all of the circ objects are in cache
268 my $cache_key = "circ_object_" . md5_hex( $barcode_string, $patron_id );
269 $circ_objects = $cache_handle->get_cache($cache_key);
272 $stash = Template::Stash->new(
273 circ_objects => $circ_objects,
275 target_copy_status => 1,
280 # only necessary if the circ objects have not been built yet
282 _grab_patron_standings($session);
283 _grab_patron_profiles($session);
286 my $copy = _grab_copy_by_barcode($session, $barcode_string);
287 if(!$copy) { return NO_COPY; }
289 my $patron = _grab_user($session, $patron_id);
291 $copy->status( $copy->status->name );
292 $circ_objects->{copy} = $copy;
294 $patron->standing($patron_standings->{$patron->standing()});
295 $patron->profile( $patron_profiles->{$patron->profile});
296 $circ_objects->{patron} = $patron;
297 $circ_objects->{standings} = $patron_standings;
299 #$circ_objects->{title} = $title_req->gather(1);
300 $circ_objects->{title} = _grab_title_by_copy($session, $circ_objects->{copy}->id);
301 $cache_handle->put_cache( $cache_key, $circ_objects, 30 );
303 $stash = Template::Stash->new(
304 circ_objects => $circ_objects,
306 target_copy_status => 1,
316 my $template = Template->new(
324 my $status = $template->process($circ_script);
327 throw OpenSRF::EX::ERROR
328 ("Error processing circ script " . $template->error());
331 warn "Script result: $result\n";
337 __PACKAGE__->register_method(
338 method => "permit_circ",
339 api_name => "open-ils.circ.permit_checkout",
343 my( $self, $client, $user_session, $barcode, $user_id, $outstanding_count ) = @_;
346 if(defined($outstanding_count) && $outstanding_count eq "renew") {
348 $outstanding_count = 0;
349 } else { $outstanding_count ||= 0; }
352 my $session = OpenSRF::AppSession->create("open-ils.storage");
354 # collect items necessary for circ calculation
355 my $status = gather_circ_objects( $session, $barcode, $user_id );
357 if( $status == NO_COPY ) {
358 return { record => undef,
360 text => "No copy available with barcode $barcode"
364 $stash->set("run_block", $permission_script);
366 # grab the number of copies checked out by the patron as
367 # well as the total fines
368 my $summary_req = $session->request(
369 "open-ils.storage.action.circulation.patron_summary",
370 $stash->get("circ_objects")->{patron}->id );
371 my $summary = $summary_req->gather(1);
374 $summary->[1] ||= 0.0;
376 $stash->set("patron_copies", $summary->[0] + $outstanding_count );
377 $stash->set("patron_fines", $summary->[1] );
378 $stash->set("renew", 1) if $renew;
380 # run the permissibility script
384 my $arr = $stash->get("result");
385 return { status => $arr->[0], text => $arr->[1] };
391 __PACKAGE__->register_method(
392 method => "circulate",
393 api_name => "open-ils.circ.checkout.barcode",
397 my( $self, $client, $user_session, $barcode, $patron, $isrenew, $numrenews ) = @_;
400 my $session = $apputils->start_db_session();
402 gather_circ_objects( $session, $barcode, $patron );
404 # grab the copy statuses if we don't already have them
405 if(!$copy_statuses) {
406 my $csreq = $session->request(
407 "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
408 $copy_statuses = $csreq->gather(1);
411 # put copy statuses into the stash
412 $stash->set("copy_statuses", $copy_statuses );
414 my $copy = $circ_objects->{copy};
415 my ($circ, $duration, $recurring, $max) = run_circ_scripts($session);
418 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
419 gmtime(OpenSRF::Utils->interval_to_seconds($circ->duration) + int(time()));
420 $year += 1900; $mon += 1;
421 my $due_date = sprintf(
422 '%s-%0.2d-%0.2dT%s:%0.2d:%0.s2-00',
423 $year, $mon, $mday, $hour, $min, $sec);
425 warn "Setting due date to $due_date\n";
426 $circ->due_date($due_date);
429 warn "Renewing circ.... ".$circ->id ." and setting num renews to " . $numrenews - 1 . "\n";
432 $circ->renewal_remaining($numrenews - 1);
436 # commit new circ object to db
437 my $commit = $session->request(
438 "open-ils.storage.direct.action.circulation.create",
440 my $id = $commit->gather(1);
443 throw OpenSRF::EX::ERROR
444 ("Error creating new circulation object");
447 # update the copy with the new circ
448 $copy->status( $stash->get("target_copy_status") );
449 $copy->location( $copy->location->id );
450 $copy->circ_lib( $copy->circ_lib->id ); #XXX XXX needs to point to the lib that actually checked out the item (user->home_ou)?
453 my $copy_update = $session->request(
454 "open-ils.storage.direct.asset.copy.update",
456 $copy_update->gather(1);
458 $apputils->commit_db_session($session);
460 # remove our circ object from the cache
461 $cache_handle->delete_cache("circ_object_" . md5_hex($barcode, $patron));
463 # re-retrieve the the committed circ object
464 $circ = $apputils->simple_scalar_request(
466 "open-ils.storage.direct.action.circulation.retrieve",
470 # push the rules and due date into the circ object
471 $circ->duration_rule($duration);
472 $circ->max_fine_rule($max);
473 $circ->recuring_fine_rule($recurring);
475 # turn the biblio record into a friendly object
476 my $obj = $stash->get("circ_objects");
477 my $u = OpenILS::Utils::ModsParser->new();
478 $u->start_mods_batch( $circ_objects->{title}->marc() );
479 my $mods = $u->finish_mods_batch();
482 return { circ => $circ, copy => $copy, record => $mods };
488 # runs the duration, recurring_fines, and max_fines scripts.
489 # builds the new circ object based on the rules returned from
491 # returns (circ, duration_rule, recurring_fines_rule, max_fines_rule)
492 sub run_circ_scripts {
495 # go through all of the scripts and process
496 # each script returns
497 # [ rule_name, level (appropriate to the script) ]
498 $stash->set("result", [] );
499 $stash->set("run_block", $duration_script);
501 my $duration_rule = $stash->get("result");
503 $stash->set("run_block", $recurring_fines_script);
504 $stash->set("result", [] );
506 my $rec_fines_rule = $stash->get("result");
508 $stash->set("run_block", $max_fines_script);
509 $stash->set("result", [] );
511 my $max_fines_rule = $stash->get("result");
513 my $obj = $stash->get("circ_objects");
515 # ----------------------------------------------------------
516 # find the rules objects based on the rule names returned from
517 # the various scripts.
518 my $dur_req = $session->request(
519 "open-ils.storage.direct.config.rules.circ_duration.search.name.atomic",
520 $duration_rule->[0] );
522 my $rec_req = $session->request(
523 "open-ils.storage.direct.config.rules.recuring_fine.search.name.atomic",
524 $rec_fines_rule->[0] );
526 my $max_req = $session->request(
527 "open-ils.storage.direct.config.rules.max_fine.search.name.atomic",
528 $max_fines_rule->[0] );
530 my $duration = $dur_req->gather(1)->[0];
531 my $recurring = $rec_req->gather(1)->[0];
532 my $max = $max_req->gather(1)->[0];
534 my $copy = $circ_objects->{copy};
537 warn "Building a new circulation object with\n".
538 "=> copy " . Dumper($copy) .
539 "=> duration_rule " . Dumper($duration_rule) .
540 "=> rec_files_rule " . Dumper($rec_fines_rule) .
541 "=> duration " . Dumper($duration) .
542 "=> recurring " . Dumper($recurring) .
543 "=> max " . Dumper($max);
546 # build the new circ object
547 my $circ = build_circ_object($session, $copy, $duration_rule->[1],
548 $rec_fines_rule->[1], $duration, $recurring, $max );
550 return ($circ, $duration, $recurring, $max);
554 # ------------------------------------------------------------------
555 # Builds a new circ object
556 # ------------------------------------------------------------------
557 sub build_circ_object {
558 my( $session, $copy, $dur_level, $rec_level,
559 $duration, $recurring, $max ) = @_;
561 my $circ = new Fieldmapper::action::circulation;
563 $circ->circ_lib( $copy->circ_lib->id() );
564 if($dur_level == 1) {
565 $circ->duration( $duration->shrt );
566 } elsif($dur_level == 2) {
567 $circ->duration( $duration->normal );
568 } elsif($dur_level == 3) {
569 $circ->duration( $duration->extended );
572 if($rec_level == 1) {
573 $circ->recuring_fine( $recurring->low );
574 } elsif($rec_level == 2) {
575 $circ->recuring_fine( $recurring->normal );
576 } elsif($rec_level == 3) {
577 $circ->recuring_fine( $recurring->high );
580 $circ->duration_rule( $duration->name );
581 $circ->recuring_fine_rule( $recurring->name );
582 $circ->max_fine_rule( $max->name );
583 $circ->max_fine( $max->amount );
585 $circ->fine_interval($recurring->recurance_interval);
586 $circ->renewal_remaining( $duration->max_renewals );
587 $circ->target_copy( $copy->id );
588 $circ->usr( $circ_objects->{patron}->id );
595 __PACKAGE__->register_method(
597 api_name => "open-ils.circ.checkin.barcode",
598 notes => <<" NOTES");
599 Checks in based on barcode
600 Returns record, status, text, circ, copy, route_to
603 1 = 'copy required to fulfil a hold'
607 my( $self, $client, $user_session, $barcode, $isrenewal ) = @_;
614 my $user = $apputils->check_user_session($user_session);
617 my $session = $apputils->start_db_session();
619 warn "retrieving copy for checkin\n";
621 if(!$shelving_locations) {
622 my $sh_req = $session->request(
623 "open-ils.storage.direct.asset.copy_location.retrieve.all.atomic");
624 $shelving_locations = $sh_req->gather(1);
625 $shelving_locations =
626 { map { (''.$_->id => $_->name) } @$shelving_locations };
629 my $copy_req = $session->request(
630 "open-ils.storage.direct.asset.copy.search.barcode.atomic",
632 $copy = $copy_req->gather(1)->[0];
634 $client->respond_complete(OpenILS::EX->new("UNKNOWN_BARCODE")->ex);
641 # find circ's where the transaction is still open for the
642 # given copy. should only be one.
643 warn "Retrieving circ for checkin\n";
644 my $circ_req = $session->request(
645 "open-ils.storage.direct.action.circulation.search.atomic",
646 { target_copy => $copy->id, xact_finish => undef } );
648 $circ = $circ_req->gather(1)->[0];
652 $err = "No circulation exists for the given barcode";
656 $transaction = $session->request(
657 "open-ils.storage.direct.money.billable_transaction_summary.retrieve", $circ->id)->gather(1);
659 warn "Checking in circ ". $circ->id . "\n";
661 $circ->stop_fines("CHECKIN");
662 $circ->stop_fines("RENEW") if($isrenewal);
663 $circ->xact_finish("now") if($transaction->balance_owed <= 0 );
665 my $cp_up = $session->request(
666 "open-ils.storage.direct.asset.copy.update", $copy );
669 my $ci_up = $session->request(
670 "open-ils.storage.direct.action.circulation.update",
674 $apputils->commit_db_session($session);
676 warn "Checkin succeeded\n";
681 $err = "Error checking in: $e";
686 return { record => undef, status => -1, text => $err };
691 my $status_text = "OK";
693 # check to see if the copy is needed to fulfil a hold
694 my $r = $apputils->simple_scalar_request(
696 "open-ils.storage.direct.action.hold_copy_map.search.target_copy.atomic",
700 if( $user->id ne $circ->usr ) {
702 $status_text = "Copy needed to fulfill hold";
706 my $record = $apputils->simple_scalar_request(
708 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
711 my $u = OpenILS::Utils::ModsParser->new();
712 $u->start_mods_batch( $record->marc() );
713 my $mods = $u->finish_mods_batch();
718 text => $status_text,
721 route_to => $shelving_locations->{$copy->location}
736 # ------------------------------------------------------------------------------
738 # ------------------------------------------------------------------------------
741 __PACKAGE__->register_method(
743 api_name => "open-ils.circ.renew",
744 notes => <<" NOTES");
745 open-ils.circ.renew(login_session, circ_object);
746 Renews the provided circulation. login_session is the requestor of the
747 renewal and if the logged in user is not the same as circ->usr, then
748 the logged in user must have RENEW_CIRC permissions.
752 my($self, $client, $login_session, $circ) = @_;
754 throw OpenSRF::EX::InvalidArg
755 ("open-ils.circ.renew no circ") unless defined($circ);
757 my $user = $apputils->check_user_session($login_session);
759 my $session = OpenSRF::AppSession->create("open-ils.storage");
760 my $copy = _grab_copy_by_id($session, $circ->target_copy);
762 my $r = $session->request(
763 "open-ils.storage.direct.action.hold_copy_map.search.target_copy.atomic",
764 $copy->id )->gather(1);
767 if( $user->id ne $circ->usr ) {
768 if($apputils->check_user_perms($user->id, $user->home_ou, "RENEW_HOLD_OVERRIDE")) {
769 return OpenILS::Perm->new("RENEW_HOLD_OVERRIDE");
773 return OpenILS::EX->new("COPY_NEEDED_FOR_HOLD")->ex;
778 $circ = $session->request(
779 "open-ils.storage.direct.action.circulation.retrieve", $circ )->gather(1);
783 warn "Attempting to renew circ " . $iid . "\n";
785 if($user->id ne $circ->usr) {
786 if($apputils->check_user_perms($user->id, $user->home_ou, "RENEW_CIRC")) {
787 return OpenILS::Perm->new("RENEW_CIRC");
791 if($circ->renewal_remaining <= 0) {
792 return OpenILS::EX->new("MAX_RENEWALS_REACHED")->ex; }
796 # XXX XXX See if the copy this circ points to is needed to fulfill a hold!
797 # XXX check overdue..?
799 my $checkin = $self->method_lookup("open-ils.circ.checkin.barcode");
800 my ($status) = $checkin->run($login_session, $copy->barcode, 1);
801 return $status if ($status->{status} ne "0");
802 warn "Renewal checkin completed for $iid\n";
804 my $permit_checkout = $self->method_lookup("open-ils.circ.permit_checkout");
805 ($status) = $permit_checkout->run($login_session, $copy->barcode, $circ->usr, "renew");
806 return $status if($status->{status} ne "0");
807 warn "Renewal permit checkout completed for $iid\n";
809 my $checkout = $self->method_lookup("open-ils.circ.checkout.barcode");
810 ($status) = $checkout->run($login_session, $copy->barcode, $circ->usr, 1, $circ->renewal_remaining);
811 warn "Renewal checkout completed for $iid\n";