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
42 # memcache for caching the circ objects
48 my $conf = OpenSRF::Utils::SettingsClient->new;
50 # ----------------------------------------------------------------
51 # set up the rules scripts
52 # ----------------------------------------------------------------
53 $circ_script = $conf->config_value(
54 "apps", "open-ils.circ","app_settings", "rules", "main");
56 $permission_script = $conf->config_value(
57 "apps", "open-ils.circ","app_settings", "rules", "permission");
59 $duration_script = $conf->config_value(
60 "apps", "open-ils.circ","app_settings", "rules", "duration");
62 $recurring_fines_script = $conf->config_value(
63 "apps", "open-ils.circ","app_settings", "rules", "recurring_fines");
65 $max_fines_script = $conf->config_value(
66 "apps", "open-ils.circ","app_settings", "rules", "max_fines");
69 $cache_handle = OpenSRF::Utils::Cache->new();
74 # ----------------------------------------------------------------
75 # Collect all of the objects necessary for calculating the
77 # ----------------------------------------------------------------
78 sub gather_circ_objects {
79 my( $session, $barcode_string, $patron_id ) = @_;
81 throw OpenSRF::EX::ERROR
82 ("gather_circ_objects needs data")
83 unless ($barcode_string and $patron_id);
85 warn "Gathering circ objects with barcode $barcode_string and patron id $patron_id\n";
89 # see if all of the circ objects are in cache
90 my $cache_key = "circ_object_" . md5_hex( $barcode_string, $patron_id );
91 $circ_objects = $cache_handle->get_cache($cache_key);
94 $stash = Template::Stash->new(
95 circ_objects => $circ_objects,
97 target_copy_status => 1,
102 # grab the patron standing list of we don't already have it
103 # only necessary if the circ objects have not been built yet
104 if(!$patron_standings) {
105 my $standing_req = $session->request(
106 "open-ils.storage.direct.config.standing.retrieve.all.atomic");
107 $patron_standings = $standing_req->gather(1);
109 { map { (''.$_->id => $_->value) } @$patron_standings };
112 # grab patron profiles
113 if(!$patron_profiles) {
114 my $profile_req = $session->request(
115 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
116 $patron_profiles = $profile_req->gather(1);
118 { map { (''.$_->id => $_->name) } @$patron_profiles };
122 my $copy_req = $session->request(
123 "open-ils.storage.fleshed.asset.copy.search.barcode",
126 my $patron_req = $session->request(
127 "open-ils.storage.direct.actor.user.retrieve",
130 my $copy = $copy_req->gather(1)->[0];
131 $copy->status( $copy->status->name );
132 $circ_objects->{copy} = $copy;
134 my $patron = $patron_req->gather(1);
135 $patron->standing($patron_standings->{$patron->standing()});
136 $patron->profile( $patron_profiles->{$patron->profile});
137 $circ_objects->{patron} = $patron;
140 my $title_req = $session->request(
141 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
142 $circ_objects->{copy}->id() );
144 $circ_objects->{title} = $title_req->gather(1);
146 $cache_handle->put_cache( $cache_key, $circ_objects, 30 );
148 $stash = Template::Stash->new(
149 circ_objects => $circ_objects,
151 target_copy_status => 1,
162 my $template = Template->new(
170 my $status = $template->process($circ_script);
173 throw OpenSRF::EX::ERROR
174 ("Error processing circ script " . $template->error());
177 warn "Script result: $result\n";
183 __PACKAGE__->register_method(
184 method => "permit_circ",
185 api_name => "open-ils.circ.permit_circ",
189 my( $self, $client, $barcode, $user_id, $outstanding_count ) = @_;
191 $outstanding_count ||= 0;
193 my $session = OpenSRF::AppSession->create("open-ils.storage");
195 # collect items necessary for circ calculation
196 gather_circ_objects( $session, $barcode, $user_id );
198 $stash->set("run_block", $permission_script);
200 # grab the number of copies checked out by the patron as
201 # well as the total fines
202 my $summary_req = $session->request(
203 "open-ils.storage.action.circulation.patron_summary",
204 $stash->get("circ_objects")->{patron}->id );
205 my $summary = $summary_req->gather(1);
207 $stash->set("patron_copies", $summary->[0] + $outstanding_count );
208 $stash->set("patron_fines", $summary->[1] );
210 # run the permissibility script
212 my $obj = $stash->get("circ_objects");
214 # turn the biblio record into a friendly object
215 my $u = OpenILS::Utils::ModsParser->new();
216 $u->start_mods_batch( $obj->{title}->marc() );
217 my $mods = $u->finish_mods_batch();
219 my $arr = $stash->get("result");
220 return { record => $mods, status => $arr->[0], text => $arr->[1] };
225 __PACKAGE__->register_method(
226 method => "circulate",
227 api_name => "open-ils.circ.circulate",
231 my( $self, $client, $barcode, $patron ) = @_;
234 my $session = $apputils->start_db_session();
236 gather_circ_objects( $session, $barcode, $patron );
238 # grab the copy statuses if we don't already have them
239 if(!$copy_statuses) {
240 my $csreq = $session->request(
241 "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
242 $copy_statuses = $csreq->gather(1);
245 # put copy statuses into the stash
246 $stash->set("copy_statuses", $copy_statuses );
248 my $copy = $circ_objects->{copy};
249 my ($circ, $duration, $recurring, $max) = run_circ_scripts($session);
251 # commit new circ object to db
252 my $commit = $session->request(
253 "open-ils.storage.direct.action.circulation.create",
255 my $id = $commit->gather(1);
258 throw OpenSRF::EX::ERROR
259 ("Error creating new circulation object");
262 # update the copy with the new circ
263 $copy->status( $stash->get("target_copy_status") );
264 $copy->location( $copy->location->id );
265 $copy->circ_lib( $copy->circ_lib->id );
268 my $copy_update = $session->request(
269 "open-ils.storage.direct.asset.copy.update",
271 $copy_update->gather(1);
273 $apputils->commit_db_session($session);
275 # remove our circ object from the cache
276 $cache_handle->delete_cache("circ_object_" . md5_hex($barcode, $patron));
278 # re-retrieve the the committed circ object
279 $circ = $apputils->simple_scalar_request(
281 "open-ils.storage.direct.action.circulation.retrieve",
285 # push the rules and due date into the circ object
286 $circ->duration_rule($duration);
287 $circ->max_fine_rule($max);
288 $circ->recuring_fine_rule($recurring);
291 OpenSRF::Utils->interval_to_seconds(
292 $circ->duration ) + int(time());
294 $circ->due_date($due_date);
302 # runs the duration, recurring_fines, and max_fines scripts.
303 # builds the new circ object based on the rules returned from
305 # returns (circ, duration_rule, recurring_fines_rule, max_fines_rule)
306 sub run_circ_scripts {
309 # go through all of the scripts and process
310 # each script returns
311 # [ rule_name, level (appropriate to the script) ]
312 $stash->set("result", [] );
313 $stash->set("run_block", $duration_script);
315 my $duration_rule = $stash->get("result");
317 $stash->set("run_block", $recurring_fines_script);
318 $stash->set("result", [] );
320 my $rec_fines_rule = $stash->get("result");
322 $stash->set("run_block", $max_fines_script);
323 $stash->set("result", [] );
325 my $max_fines_rule = $stash->get("result");
327 my $obj = $stash->get("circ_objects");
329 # ----------------------------------------------------------
330 # find the rules objects based on the rule names returned from
331 # the various scripts.
332 my $dur_req = $session->request(
333 "open-ils.storage.direct.config.rules.circ_duration.search.name",
334 $duration_rule->[0] );
336 my $rec_req = $session->request(
337 "open-ils.storage.direct.config.rules.recuring_fine.search.name",
338 $rec_fines_rule->[0] );
340 my $max_req = $session->request(
341 "open-ils.storage.direct.config.rules.max_fine.search.name",
342 $max_fines_rule->[0] );
344 my $duration = $dur_req->gather(1)->[0];
345 my $recurring = $rec_req->gather(1)->[0];
346 my $max = $max_req->gather(1)->[0];
348 my $copy = $circ_objects->{copy};
350 # build the new circ object
351 my $circ = build_circ_object($session, $copy, $duration_rule->[1],
352 $rec_fines_rule->[1], $duration, $recurring, $max );
354 return ($circ, $duration, $recurring, $max);
358 # ------------------------------------------------------------------
359 # Builds a new circ object
360 # ------------------------------------------------------------------
361 sub build_circ_object {
362 my( $session, $copy, $dur_level, $rec_level,
363 $duration, $recurring, $max ) = @_;
365 my $circ = new Fieldmapper::action::circulation;
367 $circ->circ_lib( $copy->circ_lib->id() );
368 if($dur_level == 1) {
369 $circ->duration( $duration->shrt );
370 } elsif($dur_level == 2) {
371 $circ->duration( $duration->normal );
372 } elsif($dur_level == 3) {
373 $circ->duration( $duration->extended );
376 if($rec_level == 1) {
377 $circ->recuring_fine( $recurring->low );
378 } elsif($rec_level == 2) {
379 $circ->recuring_fine( $recurring->normal );
380 } elsif($rec_level == 3) {
381 $circ->recuring_fine( $recurring->high );
384 $circ->duration_rule( $duration->name );
385 $circ->recuring_fine_rule( $recurring->name );
386 $circ->max_fine_rule( $max->name );
387 $circ->max_fine( $max->amount );
389 $circ->fine_interval($recurring->recurance_interval);
390 $circ->renewal_remaining( $duration->max_renewals );
391 $circ->target_copy( $copy->id );
392 $circ->usr( $circ_objects->{patron}->id );
398 __PACKAGE__->register_method(
400 api_name => "open-ils.circ.checkin.barcode",
404 my( $self, $client, $barcode ) = @_;
406 my $session = $apputils->start_db_session();
408 warn "retrieving copy for checkin\n";
410 my $copy_req = $session->request(
411 "open-ils.storage.direct.asset.copy.search.barcode",
413 my $copy = $copy_req->gather(1)->[0];
416 warn "Retrieving circ for checking\n";
417 my $circ_req = $session->request(
418 "open-ils.storage.direct.action.circulation.search.atomic",
419 { target_copy => $copy->id, xact_finish => undef } );
421 my $circ = $circ_req->gather(1)->[0];
424 throw OpenSRF::EX::ERROR
425 ("No circulation exists for the given barcode");
428 warn "Checking in circ ". $circ->id . "\n";
430 $circ->stop_fines("CHECKIN");
431 $circ->xact_finish("now");
433 my $cp_up = $session->request(
434 "open-ils.storage.direct.asset.copy.update",
438 my $ci_up = $session->request(
439 "open-ils.storage.direct.action.circulation.update",
443 $apputils->commit_db_session($session);
445 warn "Checkin succeeded\n";