]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Circulate.pm
updated docs on permit method
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / Circulate.pm
1 package OpenILS::Application::Circ::Circulate;
2 use base 'OpenSRF::Application';
3 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
5 use Data::Dumper;
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";
13
14 my %scripts;                    # - circulation script filenames
15
16 my $script_libs;                # - any additional script libraries
17 my %cache;
18
19 my %contexts;                   # - Script runner contexts
20
21 # ------------------------------------------------------------------------------
22 # Load the circ script from the config
23 # ------------------------------------------------------------------------------
24 sub initialize {
25
26         my $self = shift;
27         my $conf = OpenSRF::Utils::SettingsClient->new;
28         my @pfx = ( "apps", "open-ils.circ","app_settings", "scripts" );
29
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' );
37
38         $logger->error( "Missing circ script(s)" ) 
39                 unless( $p and $d and $f and $m and $pr and $ph );
40
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;
47
48         $lb = [ $lb ] unless ref($lb);
49         $script_libs = $lb;
50
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");
54 }
55
56
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 # ------------------------------------------------------------------------------
62 sub create_circ_ctx {
63         my %params = @_;
64
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};
70
71         my $evt;
72         my $ctx = {};
73         $ctx->{patron} = $patron;
74         $ctx->{type} = $params{type};
75
76         if(!defined($cache{patron_standings})) {
77                 $cache{patron_standings} = $apputils->fetch_patron_standings();
78                 $cache{group_tree} = $apputils->fetch_permission_group_tree();
79         }
80
81         $ctx->{patron_standings} = $cache{patron_standings};
82         $ctx->{group_tree} = $cache{group_tree};
83
84         $cache{copy_statuses} = $apputils->fetch_copy_statuses 
85                 if( $fetch_cstatus and !defined($cache{copy_statuses}) );
86
87         $cache{copy_locations} = $apputils->fetch_copy_locations 
88                 if( $fetch_clocs and !defined($cache{copy_locations}));
89
90         $ctx->{copy_statuses} = $cache{copy_statuses};
91         $ctx->{copy_locations} = $cache{copy_locations};
92
93         $ctx->{patron_circ_summary} = 
94                 $apputils->fetch_patron_circ_summary($patron->id) if $fetch_summary;
95
96         ( $ctx->{copy}, $evt ) = $apputils->fetch_copy_by_barcode( $barcode );
97         return ( undef, $evt ) if $evt;
98
99         ( $ctx->{title}, $evt ) = $apputils->fetch_record_by_copy( $ctx->{copy}->id );
100         return ( undef, $evt ) if $evt;
101
102         _doctor_circ_objects($ctx);
103         _build_circ_script_runner($ctx);
104         _add_script_runner_methods( $ctx );
105
106         return $ctx;
107 }
108
109
110 # ------------------------------------------------------------------------------
111 # Patches up circ objects to make them easier to use from within the script
112 # environment
113 # ------------------------------------------------------------------------------
114 sub _doctor_circ_objects {
115         my $ctx = shift;
116
117         my $patron = $ctx->{patron};
118         my $copy = $ctx->{copy};
119                         
120         for my $s (@{$ctx->{patron_standings}}) {
121                 $patron->standing($s) if ( $s->id eq $ctx->{patron}->standing );
122         }
123
124         # set the patron ptofile to the profile name
125         $patron->profile( _get_patron_profile( $patron, $ctx->{group_tree} ) );
126
127         # flesh the org unit
128         $patron->home_ou( $apputils->fetch_org_unit( $patron->home_ou ) );
129
130         # set the copy status to a status name
131         $copy->status( _get_copy_status( $copy, $ctx->{copy_statuses} ) );
132
133         # set the copy location to the location object
134         $copy->location( _get_copy_location( $copy, $ctx->{copy_locations} ) );
135
136 }
137
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 );
146                 return $ret if $ret;
147         }
148         return undef;
149 }
150
151 sub _get_copy_status {
152         my( $copy, $cstatus ) = @_;
153         for my $status (@$cstatus) {
154                 return $status if( $status->id eq $copy->status ) 
155         }
156         return undef;
157 }
158
159 sub _get_copy_location {
160         my( $copy, $locations ) = @_;
161         for my $loc (@$locations) {
162                 return $loc if $loc->id eq $copy->location;
163         }
164 }
165
166
167 # ------------------------------------------------------------------------------
168 # Constructs and shoves data into the script environment
169 # ------------------------------------------------------------------------------
170 sub _build_circ_script_runner {
171         my $ctx = shift;
172
173         $logger->debug("Loading script environment for circulation");
174
175         my $runner;
176         if( $runner = $contexts{$ctx->{type}} ) {
177                 $runner->refresh_context;
178         } else {
179                 $runner = OpenILS::Utils::ScriptRunner->new unless $runner;
180                 $contexts{type} = $runner;
181         }
182
183         for(@$script_libs) {
184                 $logger->debug("Loading circ script lib path $_");
185                 $runner->add_path( $_ );
186         }
187
188         $runner->insert( 'patron',              $ctx->{patron} );
189         $runner->insert( 'title',               $ctx->{title} );
190         $runner->insert( 'copy',                $ctx->{copy} );
191
192         # circ script result
193         $runner->insert( 'result', {} );
194         $runner->insert( 'result.event', 'SUCCESS' );
195
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] );
200         }
201
202         $ctx->{runner} = $runner;
203         return $runner;
204 }
205
206
207 sub _add_script_runner_methods {
208         my $ctx = shift;
209         my $runner = $ctx->{runner};
210
211         # allows a script to fetch a hold that is currently targeting the
212         # copy in question
213         $runner->insert_method( 'copy', '__OILS_FUNC_fetch_hold', sub {
214                         my $key = shift;
215                         my $hold = $holdcode->fetch_open_hold_by_current_copy($ctx->{copy}->id);
216                         $hold = undef unless $hold;
217                         $runner->insert( $key, $hold );
218                 }
219         );
220 }
221
222 # ------------------------------------------------------------------------------
223
224 __PACKAGE__->register_method(
225         method  => "permit_circ",
226         api_name        => "open-ils.circ.permit_checkout_",
227         notes           => q/
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
236         /);
237
238 sub permit_circ {
239         my( $self, $client, $authtoken, %params ) = @_;
240
241         my $barcode             = $params{barcode};
242         my $patronid    = $params{patron};
243         my $isrenew             = $params{renew};
244         my ( $requestor, $patron, $ctx, $evt );
245
246
247         # check permisson of the requestor
248         ( $requestor, $patron, $evt ) = 
249                 $apputils->checkses_requestor( 
250                 $authtoken, $patronid, 'VIEW_PERMIT_CHECKOUT' );
251         return $evt if $evt;
252
253         $logger->info("Checking circulation permission for staff: " . $requestor->id .
254                 ", patron " . $patron->id . ", and barcode $barcode" );
255
256         # fetch and build the circulation environment
257         ( $ctx, $evt ) = create_circ_ctx( 
258                 barcode                                                 => $barcode, 
259                 patron                                                  => $patron, 
260                 type                                                            => 'permit',
261                 fetch_patron_circ_summary       => 1,
262                 fetch_copy_statuses                     => 1, 
263                 fetch_copy_locations                    => 1, 
264                 );
265         return $evt if $evt;
266
267         # run the script
268         my $runner = $ctx->{runner};
269
270         $runner->load($scripts{circ_permit});
271         $runner->run or throw OpenSRF::EX::ERROR ("Circ Permit Script Died: $@");
272
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);
276 }
277
278
279 # ------------------------------------------------------------------------------
280
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 )
287         NOTES
288
289 sub circulate {
290         my( $self, $client, $authtoken, %params ) = @_;
291         my $barcode             = $params{barcode};
292         my $patronid    = $params{patron};
293 }
294
295
296 # ------------------------------------------------------------------------------
297
298 __PACKAGE__->register_method(
299         method  => "checkin",
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
307         NOTES
308
309 sub checkin {
310         my( $self, $client, $authtoken, %params ) = @_;
311         my $barcode             = $params{barcode};
312 }
313
314 # ------------------------------------------------------------------------------
315
316 __PACKAGE__->register_method(
317         method  => "renew",
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.
325         NOTES
326
327 sub renew {
328         my( $self, $client, $authtoken, %params ) = @_;
329         my $circ        = $params{circ};
330 }
331
332         
333
334
335 1;