renewals, new exceptions, batch searches, etc.
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / Rules.pm
1 # ---------------------------------------------------------------
2 # Copyright (C) 2005  Georgia Public Library Service 
3 # Bill Erickson <highfalutin@gmail.com>
4
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.
9
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 # ---------------------------------------------------------------
15
16 package OpenILS::Application::Circ::Rules;
17 use base qw/OpenSRF::Application/;
18 use strict; use warnings;
19
20 use OpenSRF::Utils::SettingsClient;
21 use OpenILS::Utils::Fieldmapper;
22 use OpenILS::EX;
23 use OpenSRF::Utils::Logger qw(:level); 
24
25 use Template qw(:template);
26 use Template::Stash; 
27
28 use Time::HiRes qw(time);
29 use OpenILS::Utils::ModsParser;
30
31 use OpenSRF::Utils;
32 use OpenSRF::EX qw(:try);
33
34 use OpenILS::Application::AppUtils;
35 my $apputils = "OpenILS::Application::AppUtils";
36 use Digest::MD5 qw(md5_hex);
37
38 my $log = "OpenSRF::Utils::Logger";
39
40 # ----------------------------------------------------------------
41 # rules scripts
42 my $circ_script;
43 my $permission_script;
44 my $duration_script;
45 my $recurring_fines_script;
46 my $max_fines_script;
47 my $permit_hold_script;
48 my $permit_renew_script;
49 # ----------------------------------------------------------------
50
51
52 # data used for this circulation transaction
53 my $circ_objects = {};
54
55 # some static data from the database
56 my $copy_statuses;
57 my $patron_standings;
58 my $patron_profiles;
59 my $shelving_locations;
60
61 # template stash
62 my $stash;
63
64 # memcache for caching the circ objects
65 my $cache_handle;
66
67
68 use constant NO_COPY => 100;
69
70 sub initialize {
71
72         my $self = shift;
73         my $conf = OpenSRF::Utils::SettingsClient->new;
74
75         # ----------------------------------------------------------------
76         # set up the rules scripts
77         # ----------------------------------------------------------------
78         $circ_script = $conf->config_value(                                     
79                 "apps", "open-ils.circ","app_settings", "rules", "main");
80
81         $permission_script = $conf->config_value(                       
82                 "apps", "open-ils.circ","app_settings", "rules", "permission");
83
84         $duration_script = $conf->config_value(                 
85                 "apps", "open-ils.circ","app_settings", "rules", "duration");
86
87         $recurring_fines_script = $conf->config_value(  
88                 "apps", "open-ils.circ","app_settings", "rules", "recurring_fines");
89
90         $max_fines_script = $conf->config_value(                        
91                 "apps", "open-ils.circ","app_settings", "rules", "max_fines");
92
93         $permit_hold_script = $conf->config_value(
94                 "apps", "open-ils.circ","app_settings", "rules", "permit_hold");
95
96         $permit_renew_script = $conf->config_value(
97                 "apps", "open-ils.circ","app_settings", "rules", "permit_renew");
98
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);
103
104
105         $cache_handle = OpenSRF::Utils::Cache->new();
106 }
107
108
109 sub _grab_patron_standings {
110         my $session = shift;
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);
115                 $patron_standings =
116                         { map { (''.$_->id => $_->value) } @$patron_standings };
117         }
118 }
119
120 sub _grab_patron_profiles {
121         my $session = shift;
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);
126                 $patron_profiles =
127                         { map { (''.$_->id => $_->name) } @$patron_profiles };
128         }
129
130 }
131
132 sub _grab_user {
133         my $session = shift;
134         my $patron_id = shift;
135         my $patron_req  = $session->request(
136                 "open-ils.storage.direct.actor.user.retrieve", 
137                 $patron_id );
138         return $patron_req->gather(1);
139 }
140         
141
142 sub _grab_title_by_copy {
143         my $session = shift;
144         my $copyid = shift;
145         my $title_req   = $session->request(
146                 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
147                 $copyid );
148         return $title_req->gather(1);
149 }
150
151 sub _grab_patron_summary {
152         my $session = shift;
153         my $patron_id = shift;
154         my $summary_req = $session->request(
155                 "open-ils.storage.action.circulation.patron_summary",
156                 $patron_id );
157         return $summary_req->gather(1);
158 }
159
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", 
165                 $barcode );
166         return $copy_req->gather(1);
167 }
168
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", 
174                 $id );
175         my $c = $copy_req->gather(1);
176         if($c) { return _grab_copy_by_barcode($session, $c->barcode); }
177         return undef;
178 }
179
180
181 sub gather_hold_objects {
182         my($session, $hold, $copy, $args) = @_;
183
184         _grab_patron_standings($session);
185         _grab_patron_profiles($session);
186
187
188         # flesh me
189         $copy = _grab_copy_by_barcode($session, $copy->barcode);
190
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);
198
199         $copy->status( $copy->status->name );
200         $patron->standing($patron_standings->{$patron->standing()});
201         $patron->profile( $patron_profiles->{$patron->profile});
202
203         $hold_objects->{patron}         = $patron;
204
205         return $hold_objects;
206 }
207
208
209
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
215         NOTES
216
217 sub permit_hold {
218         my( $self, $client, $hold, $copy, $args ) = @_;
219
220         my $session     = OpenSRF::AppSession->create("open-ils.storage");
221         
222         # collect items necessary for circ calculation
223         my $hold_objects = gather_hold_objects( $session, $hold, $copy, $args );
224
225         $stash = Template::Stash->new(
226                         circ_objects                    => $hold_objects,
227                         result                                  => []);
228
229         $stash->set("run_block", $permit_hold_script);
230
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);
234         $summary->[0] ||= 0;
235         $summary->[1] ||= 0.0;
236
237         $stash->set("patron_copies", $summary->[0] );
238         $stash->set("patron_fines", $summary->[1] );
239
240         # run the permissibility script
241         run_script();
242         my $result = $stash->get("result");
243
244         # 0 means OK in the script
245         return 1 if($result->[0] == 0);
246         return 0;
247
248 }
249
250
251
252
253
254 # ----------------------------------------------------------------
255 # Collect all of the objects necessary for calculating the
256 # circ matrix.
257 # ----------------------------------------------------------------
258 sub gather_circ_objects {
259         my( $session, $barcode_string, $patron_id ) = @_;
260
261         throw OpenSRF::EX::ERROR 
262                 ("gather_circ_objects needs data")
263                         unless ($barcode_string and $patron_id);
264
265         warn "Gathering circ objects with barcode $barcode_string and patron id $patron_id\n";
266
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);
270
271         if($circ_objects) { 
272                 $stash = Template::Stash->new(
273                         circ_objects                    => $circ_objects,
274                         result                                  => [],
275                         target_copy_status      => 1,
276                         );
277                 return;
278         }
279
280         # only necessary if the circ objects have not been built yet
281
282         _grab_patron_standings($session);
283         _grab_patron_profiles($session);
284
285
286         my $copy = _grab_copy_by_barcode($session, $barcode_string);
287         if(!$copy) { return NO_COPY; }
288
289         my $patron = _grab_user($session, $patron_id);
290
291         $copy->status( $copy->status->name );
292         $circ_objects->{copy} = $copy;
293
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;
298
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 );
302
303         $stash = Template::Stash->new(
304                         circ_objects                    => $circ_objects,
305                         result                                  => [],
306                         target_copy_status      => 1,
307                         );
308 }
309
310
311
312 sub run_script {
313
314         my $result;
315
316         my $template = Template->new(
317                 { 
318                         STASH                   => $stash,
319                         ABSOLUTE                => 1, 
320                         OUTPUT          => \$result,
321                 }
322         );
323
324         my $status = $template->process($circ_script);
325
326         if(!$status) { 
327                 throw OpenSRF::EX::ERROR 
328                         ("Error processing circ script " .  $template->error()); 
329         }
330
331         warn "Script result: $result\n";
332 }
333
334
335
336
337 __PACKAGE__->register_method(
338         method  => "permit_circ",
339         api_name        => "open-ils.circ.permit_checkout",
340 );
341
342 sub permit_circ {
343         my( $self, $client, $user_session, $barcode, $user_id, $outstanding_count ) = @_;
344
345         my $renew = 0;
346         if(defined($outstanding_count) && $outstanding_count eq "renew") {
347                 $renew = 1;
348                 $outstanding_count = 0;
349         } else { $outstanding_count ||= 0; }
350
351
352         my $session     = OpenSRF::AppSession->create("open-ils.storage");
353         
354         # collect items necessary for circ calculation
355         my $status = gather_circ_objects( $session, $barcode, $user_id );
356
357         if( $status == NO_COPY ) {
358                 return { record => undef, 
359                         status => NO_COPY, 
360                         text => "No copy available with barcode $barcode"
361                 };
362         }
363
364         $stash->set("run_block", $permission_script);
365
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);
372
373         $summary->[0] ||= 0;
374         $summary->[1] ||= 0.0;
375
376         $stash->set("patron_copies", $summary->[0]  + $outstanding_count );
377         $stash->set("patron_fines", $summary->[1] );
378         $stash->set("renew", 1) if $renew; 
379
380         # run the permissibility script
381         run_script();
382         my $obj = $stash->get("circ_objects");
383
384         # turn the biblio record into a friendly object
385         my $u = OpenILS::Utils::ModsParser->new();
386         $u->start_mods_batch( $obj->{title}->marc() );
387         my $mods = $u->finish_mods_batch();
388
389         my $arr = $stash->get("result");
390         return { record => $mods, status => $arr->[0], text => $arr->[1] };
391
392 }
393
394
395
396 __PACKAGE__->register_method(
397         method  => "circulate",
398         api_name        => "open-ils.circ.checkout.barcode",
399 );
400
401 sub circulate {
402         my( $self, $client, $user_session, $barcode, $patron, $isrenew, $numrenews ) = @_;
403
404
405         my $session = $apputils->start_db_session();
406
407         gather_circ_objects( $session, $barcode, $patron );
408
409         # grab the copy statuses if we don't already have them
410         if(!$copy_statuses) {
411                 my $csreq = $session->request(
412                         "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
413                 $copy_statuses = $csreq->gather(1);
414         }
415
416         # put copy statuses into the stash
417         $stash->set("copy_statuses", $copy_statuses );
418
419         my $copy = $circ_objects->{copy};
420         my ($circ, $duration, $recurring, $max) =  run_circ_scripts($session);
421
422
423
424         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = 
425                 gmtime(OpenSRF::Utils->interval_to_seconds($circ->duration) + int(time()));
426         $year += 1900; $mon += 1;
427         my $due_date = sprintf(
428         '%s-%0.2d-%0.2dT%s:%0.2d:%0.s2-00',
429         $year, $mon, $mday, $hour, $min, $sec);
430
431         warn "Setting due date to $due_date\n";
432         $circ->due_date($due_date);
433
434         if($isrenew) {
435                 warn "Renewing circ....";
436                 $circ->renewal(1);
437                 $circ->clear_id;
438                 $circ->renewal_remaining($numrenews - 1);
439         }
440
441
442         # commit new circ object to db
443         my $commit = $session->request(
444                 "open-ils.storage.direct.action.circulation.create",
445                 $circ );
446         my $id = $commit->gather(1);
447
448         if(!$id) {
449                 throw OpenSRF::EX::ERROR 
450                         ("Error creating new circulation object");
451         }
452
453         # update the copy with the new circ
454         $copy->status( $stash->get("target_copy_status") );
455         $copy->location( $copy->location->id );
456         $copy->circ_lib( $copy->circ_lib->id ); #XXX XXX needs to point to the lib that actually checked out the item (user->home_ou)?
457
458         # commit copy to db
459         my $copy_update = $session->request(
460                 "open-ils.storage.direct.asset.copy.update",
461                 $copy );
462         $copy_update->gather(1);
463
464         $apputils->commit_db_session($session);
465
466         # remove our circ object from the cache
467         $cache_handle->delete_cache("circ_object_" . md5_hex($barcode, $patron));
468
469         # re-retrieve the the committed circ object  
470         $circ = $apputils->simple_scalar_request(
471                 "open-ils.storage",
472                 "open-ils.storage.direct.action.circulation.retrieve",
473                 $id );
474
475
476         # push the rules and due date into the circ object
477         $circ->duration_rule($duration);
478         $circ->max_fine_rule($max);
479         $circ->recuring_fine_rule($recurring);
480
481         return $circ;
482
483 }
484
485
486
487 # runs the duration, recurring_fines, and max_fines scripts.
488 # builds the new circ object based on the rules returned from 
489 # these scripts. 
490 # returns (circ, duration_rule, recurring_fines_rule, max_fines_rule)
491 sub run_circ_scripts {
492         my $session = shift;
493
494         # go through all of the scripts and process
495         # each script returns 
496         # [ rule_name, level (appropriate to the script) ]
497         $stash->set("result", [] );
498         $stash->set("run_block", $duration_script);
499         run_script();
500         my $duration_rule = $stash->get("result");
501
502         $stash->set("run_block", $recurring_fines_script);
503         $stash->set("result", [] );
504         run_script();
505         my $rec_fines_rule = $stash->get("result");
506
507         $stash->set("run_block", $max_fines_script);
508         $stash->set("result", [] );
509         run_script();
510         my $max_fines_rule = $stash->get("result");
511
512         my $obj = $stash->get("circ_objects");
513
514         # ----------------------------------------------------------
515         # find the rules objects based on the rule names returned from
516         # the various scripts.
517         my $dur_req = $session->request(
518                 "open-ils.storage.direct.config.rules.circ_duration.search.name.atomic",
519                 $duration_rule->[0] );
520
521         my $rec_req = $session->request(
522                 "open-ils.storage.direct.config.rules.recuring_fine.search.name.atomic",
523                 $rec_fines_rule->[0] );
524
525         my $max_req = $session->request(
526                 "open-ils.storage.direct.config.rules.max_fine.search.name.atomic",
527                 $max_fines_rule->[0] );
528
529         my $duration    = $dur_req->gather(1)->[0];
530         my $recurring   = $rec_req->gather(1)->[0];
531         my $max                 = $max_req->gather(1)->[0];
532
533         my $copy = $circ_objects->{copy};
534
535         use Data::Dumper;
536         warn "Building a new circulation object with\n".
537                 "=> copy "                              . Dumper($copy) .
538                 "=> duration_rule "     . Dumper($duration_rule) .
539                 "=> rec_files_rule " . Dumper($rec_fines_rule) .
540                 "=> duration "                  . Dumper($duration) .
541                 "=> recurring "         . Dumper($recurring) .
542                 "=> max "                               . Dumper($max);
543
544
545         # build the new circ object
546         my $circ =  build_circ_object($session, $copy, $duration_rule->[1], 
547                         $rec_fines_rule->[1], $duration, $recurring, $max );
548
549         return ($circ, $duration, $recurring, $max);
550
551 }
552
553 # ------------------------------------------------------------------
554 # Builds a new circ object
555 # ------------------------------------------------------------------
556 sub build_circ_object {
557         my( $session, $copy, $dur_level, $rec_level, 
558                         $duration, $recurring, $max ) = @_;
559
560         my $circ = new Fieldmapper::action::circulation;
561
562         $circ->circ_lib( $copy->circ_lib->id() );
563         if($dur_level == 1) {
564                 $circ->duration( $duration->shrt );
565         } elsif($dur_level == 2) {
566                 $circ->duration( $duration->normal );
567         } elsif($dur_level == 3) {
568                 $circ->duration( $duration->extended );
569         }
570
571         if($rec_level == 1) {
572                 $circ->recuring_fine( $recurring->low );
573         } elsif($rec_level == 2) {
574                 $circ->recuring_fine( $recurring->normal );
575         } elsif($rec_level == 3) {
576                 $circ->recuring_fine( $recurring->high );
577         }
578
579         $circ->duration_rule( $duration->name );
580         $circ->recuring_fine_rule( $recurring->name );
581         $circ->max_fine_rule( $max->name );
582         $circ->max_fine( $max->amount );
583
584         $circ->fine_interval($recurring->recurance_interval);
585         $circ->renewal_remaining( $duration->max_renewals );
586         $circ->target_copy( $copy->id );
587         $circ->usr( $circ_objects->{patron}->id );
588
589         return $circ;
590
591 }
592
593 __PACKAGE__->register_method(
594         method  => "checkin",
595         api_name        => "open-ils.circ.checkin.barcode",
596 );
597
598 sub checkin {
599         my( $self, $client, $user_session, $barcode, $isrenewal ) = @_;
600
601         my $err;
602         my $copy;
603
604         try {
605                 my $session = $apputils->start_db_session();
606         
607                 warn "retrieving copy for checkin\n";
608
609                 if(!$shelving_locations) {
610                         my $sh_req = $session->request(
611                                 "open-ils.storage.direct.asset.copy_location.retrieve.all.atomic");
612                         $shelving_locations = $sh_req->gather(1);
613                         $shelving_locations = 
614                                 { map { (''.$_->id => $_->name) } @$shelving_locations };
615                 }
616         
617                 my $copy_req = $session->request(
618                         "open-ils.storage.direct.asset.copy.search.barcode.atomic", 
619                         $barcode );
620                 $copy = $copy_req->gather(1)->[0];
621                 if(!$copy) {
622                         $client->respond_complete(
623                                         OpenILS::EX->new("UNKNOWN_BARCODE")->ex);
624                 }
625
626                 $copy->status(0);
627         
628                 # find circ's where the transaction is still open for the
629                 # given copy.  should only be one.
630                 warn "Retrieving circ for checking\n";
631                 my $circ_req = $session->request(
632                         "open-ils.storage.direct.action.circulation.search.atomic",
633                         { target_copy => $copy->id, xact_finish => undef } );
634         
635                 my $circ = $circ_req->gather(1)->[0];
636         
637                 if(!$circ) {
638                         $err = "No circulation exists for the given barcode";
639
640                 } else {
641         
642                         warn "Checking in circ ". $circ->id . "\n";
643                 
644                         $circ->stop_fines("CHECKIN");
645                         $circ->stop_fines("RENEW") if($isrenewal);
646
647                         $circ->xact_finish("now");
648                 
649                         my $cp_up = $session->request(
650                                 "open-ils.storage.direct.asset.copy.update",
651                                 $copy );
652                         $cp_up->gather(1);
653                 
654                         my $ci_up = $session->request(
655                                 "open-ils.storage.direct.action.circulation.update",
656                                 $circ );
657                         $ci_up->gather(1);
658                 
659                         $apputils->commit_db_session($session);
660                 
661                         warn "Checkin succeeded\n";
662                 }
663         
664         } catch Error with {
665                 my $e = shift;
666                 $err = "Error checking in: $e";
667         };
668         
669         if($err) {
670                 return { record => undef, status => -1, text => $err };
671
672         } else {
673
674                 my $record = $apputils->simple_scalar_request(
675                         "open-ils.storage",
676                         "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
677                         $copy->id() );
678
679                 my $u = OpenILS::Utils::ModsParser->new();
680                 $u->start_mods_batch( $record->marc() );
681                 my $mods = $u->finish_mods_batch();
682                 return { record => $mods, status => 0, text => "OK", 
683                         route_to => $shelving_locations->{$copy->location} };
684         }
685
686         return 1;
687
688 }
689
690
691
692
693
694
695
696
697 # ------------------------------------------------------------------------------
698 # RENEWALS
699 # ------------------------------------------------------------------------------
700
701
702 __PACKAGE__->register_method(
703         method  => "renew",
704         api_name        => "open-ils.circ.renew",
705         notes           => <<"  NOTES");
706         open-ils.circ.renew(login_session, circ_object);
707         Renews the provided circulation.  login_session is the requestor of the
708         renewal and if the logged in user is not the same as circ->usr, then
709         the logged in user must have RENEW_CIRC permissions.
710         NOTES
711
712 sub renew {
713         my($self, $client, $login_session, $circ) = @_;
714         my $user = $apputils->check_user_session($login_session);
715
716         if($user->id ne $circ->usr) {
717                 if($apputils->check_user_perms($user->id, $user->home_ou, "RENEW_CIRC")) {
718                         return OpenILS::Perm->new("RENEW_CIRC");
719                 }
720         }
721
722         if($circ->renewal_remaining <= 0) {
723                 return OpenILS::EX->new("MAX_RENEWALS_REACHED")->ex;
724         }
725
726         # XXX XXX See if the copy this circ points to is needed to fulfill a hold!
727         # XXX check overdue..?
728
729         my $session = OpenSRF::AppSession->create("open-ils.storage");
730         my $copy = _grab_copy_by_id($session, $circ->target_copy);
731         my $checkin = $self->method_lookup("open-ils.circ.checkin.barcode");
732         my ($status) = $checkin->run($login_session, $copy->barcode, 1);
733         return $status if ($status->{status} ne "0"); 
734
735         my $permit_checkout = $self->method_lookup("open-ils.circ.permit_checkout");
736         ($status) = $permit_checkout->run($login_session, $copy->barcode, $circ->usr, "renew");
737         return $status if($status->{status} ne "0");
738
739         my $checkout = $self->method_lookup("open-ils.circ.checkout.barcode");
740         ($status) = $checkout->run($login_session, $copy->barcode, $circ->usr, 1, $circ->renewal_remaining);
741         return $status;
742
743 }
744
745
746 =head
747
748         my $renew_objects = gather_renew_objects( $session, $circ );
749         if(!ref($renew_objects)) {
750                 if($renew_objects == NO_COPY) {
751                         return { 
752                                 status => NO_COPY, 
753                                 text => "No copy available with id " . $circ->target_copy };
754                 }
755         }
756
757         $stash = Template::Stash->new(
758                         circ_objects                    => $renew_objects,
759                         result                                  => []);
760
761         $stash->set("run_block", $permit_renew_script);
762
763         # grab the number of copies checked out by the patron as
764         # well as the total fines
765         my $summary = _grab_patron_summary($session, $renew_objects->{patron}->id);
766         $summary->[0] ||= 0;
767         $summary->[1] ||= 0.0;
768
769         $stash->set("patron_copies", $summary->[0] );
770         $stash->set("patron_fines", $summary->[1] );
771
772         # run the permissibility script
773         run_script();
774
775         return $stash->get("result");
776
777 }
778
779
780
781 sub gather_renew_objects {
782         my($session, $circ) = @_;
783
784         _grab_patron_standings($session);
785         _grab_patron_profiles($session);
786
787         # flesh me
788         my $copy = _grab_copy_by_id($session, $circ->target_copy);
789         if(!$copy) { return NO_COPY; }
790
791         my $renew_objects = {};
792         $renew_objects->{standings} = $patron_standings;
793         $renew_objects->{copy}          = $copy;
794         $renew_objects->{circ}          = $circ;
795         $renew_objects->{title}         = _grab_title_by_copy($session, $copy->id);
796         my $patron                                              = _grab_user($session, $circ->usr);
797
798         $copy->status( $copy->status->name );
799         $patron->standing($patron_standings->{$patron->standing()});
800         $patron->profile( $patron_profiles->{$patron->profile});
801
802         $renew_objects->{patron}                = $patron;
803
804         return $renew_objects;
805 }
806
807 =cut
808
809
810
811
812 1;