1 package OpenILS::Application::Circ::Circulate;
2 use base 'OpenSRF::Application';
3 use strict; use warnings;
5 use OpenSRF::EX qw(:try);
6 use OpenSRF::Utils::Logger qw(:logger);
7 use OpenILS::Utils::ScriptRunner;
8 use OpenILS::Application::AppUtils;
9 my $apputils = "OpenILS::Application::AppUtils";
11 my %scripts; # - circulation script filenames
12 my $standings; # - cached patron standings
13 my $group_tree; # - cached permission group tree
15 # ------------------------------------------------------------------------------
16 # Load the circ script from the config
17 # ------------------------------------------------------------------------------
21 my $conf = OpenSRF::Utils::SettingsClient->new;
22 my @pfx = ( "apps", "open-ils.circ","app_settings", "scripts" );
24 my $p = $conf->config_value( @pfx, 'permission' );
25 my $d = $conf->config_value( @pfx, 'duration' );
26 my $f = $conf->config_value( @pfx, 'recurring_fines' );
27 my $m = $conf->config_value( @pfx, 'max_fines' );
28 my $pr = $conf->config_value( @pfx, 'permit_renew' );
29 my $ph = $conf->config_value( @pfx, 'permit_hold' );
31 $logger->error( "Missing circ script(s)" )
32 unless( $p and $d and $f and $m and $pr and $ph );
34 $scripts{circ_permit} = $p;
35 $scripts{circ_duration} = $d;
36 $scripts{circ_recurring_fines}= $f;
37 $scripts{circ_max_fines} = $m;
38 $scripts{circ_renew_permit} = $pr;
39 $scripts{hold_permit} = $ph;
41 $logger->debug("Loaded rules scripts for circ: " .
42 "circ permit : $p, circ duration :$d , circ recurring fines : $f, " .
43 "circ max fines : $m, circ renew permit : $pr, permit hold: $ph");
47 # ------------------------------------------------------------------------------
48 # Loads the necessary circ objects and pushes them into the script environment
49 # Returns ( $data, $evt ). if $evt is defined, then an
50 # unexpedted event occurred and should be dealt with / returned to the caller
51 # ------------------------------------------------------------------------------
55 my $barcode = $params{barcode};
56 my $patron = $params{patron};
57 my $summary = $params{fetch_patron_circ_summary};
59 my ( $copy, $title, $evt );
62 $standings = $apputils->fetch_patron_standings();
63 $group_tree = $apputils->fetch_permission_group_tree();
66 ( $copy, $evt ) = $apputils->fetch_copy_by_barcode( $barcode );
67 return ( undef, $evt ) if $evt;
69 ( $title, $evt ) = $apputils->fetch_record_by_copy( $copy->id );
70 return ( undef, $evt ) if $evt;
72 $summary = $apputils->fetch_patron_circ_summary($patron->id) if $summary;
74 _doctor_circ_objects( $patron, $title, $copy, $summary );
76 my $runner = _build_circ_script_runner( $patron, $title, $copy, $summary );
83 standings => $standings,
84 group_tree => $group_tree,
85 circ_summary => $summary,
90 # ------------------------------------------------------------------------------
91 # Patches up circ objects to make them easier to use from within the script
93 # ------------------------------------------------------------------------------
94 sub _doctor_circ_objects {
95 my( $patron, $title, $copy, $summary ) = @_;
96 for my $s (@$standings) {
97 $patron->standing( $s->value) if( $s->id eq $patron->standing);
105 # ------------------------------------------------------------------------------
106 # Constructs and shoves data into the script environment
107 # ------------------------------------------------------------------------------
108 sub _build_circ_script_runner {
109 my( $patron, $title, $copy, $summary ) = @_;
111 my $runner = OpenILS::Utils::ScriptRunner->new( type => 'js' );
113 $runner->insert( 'patron', $patron );
114 $runner->insert( 'title', $title );
115 $runner->insert( 'copy', $copy );
116 $runner->insert( 'standings', $standings );
117 $runner->insert( 'group_tree', $group_tree );
120 $runner->insert( 'result', {} );
121 $runner->insert( 'result.event', 'SUCCESS' );
124 $runner->insert( 'patron_info', {} );
125 $runner->insert( 'patron_info.copy_count', $summary->[0] );
126 $runner->insert( 'patron_info.fines', $summary->[1] );
133 # ------------------------------------------------------------------------------
135 __PACKAGE__->register_method(
136 method => "permit_circ",
137 api_name => "open-ils.circ.permit_checkout_",
138 notes => <<" NOTES");
139 Determines if the given checkout can occur
140 PARAMS( authtoken, barcode => bc, patron => pid, renew => t/f )
145 my( $self, $client, $authtoken, %params ) = @_;
146 my $barcode = $params{barcode};
147 my $patronid = $params{patron};
148 my $isrenew = $params{renew};
149 my ( $requestor, $patron, $env, $evt );
151 # check permisson of the requestor
152 ( $requestor, $patron, $evt ) = $apputils->checkses_requestor(
153 $authtoken, $patronid, 'VIEW_PERMIT_CHECKOUT' );
156 # fetch and build the circulation environment
157 ( $env, $evt ) = create_circ_env( barcode => $barcode,
158 patron => $patron, fetch_patron_circ_summary => 1 );
161 my $runner = $env->{runner};
162 $runner->load($scripts{circ_permit});
163 $runner->run or throw OpenSRF::EX::ERROR ("Circ Permit Script Died");
165 return OpenILS::Event->new($runner->retrieve('result.event'));
169 # ------------------------------------------------------------------------------
171 __PACKAGE__->register_method(
172 method => "circulate",
173 api_name => "open-ils.circ.checkout.barcode_",
174 notes => <<" NOTES");
175 Checks out an item based on barcode
176 PARAMS( authtoken, barcode => bc, patron => pid )
180 my( $self, $client, $authtoken, %params ) = @_;
181 my $barcode = $params{barcode};
182 my $patronid = $params{patron};
186 # ------------------------------------------------------------------------------
188 __PACKAGE__->register_method(
190 api_name => "open-ils.circ.checkin.barcode_",
191 notes => <<" NOTES");
192 PARAMS( authtoken, barcode => bc )
193 Checks in based on barcode
194 Returns an event object whose payload contains the record, circ, and copy
195 If the item needs to be routed, the event is a ROUTE_COPY event
196 with an additional 'route_to' variable set on the event
200 my( $self, $client, $authtoken, %params ) = @_;
201 my $barcode = $params{barcode};
204 # ------------------------------------------------------------------------------
206 __PACKAGE__->register_method(
208 api_name => "open-ils.circ.renew_",
209 notes => <<" NOTES");
210 PARAMS( authtoken, circ => circ_id );
211 open-ils.circ.renew(login_session, circ_object);
212 Renews the provided circulation. login_session is the requestor of the
213 renewal and if the logged in user is not the same as circ->usr, then
214 the logged in user must have RENEW_CIRC permissions.
218 my( $self, $client, $authtoken, %params ) = @_;
219 my $circ = $params{circ};