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);
102 # ------------------------------------------------------------------------------
103 # Constructs and shoves data into the script environment
104 # ------------------------------------------------------------------------------
105 sub _build_circ_script_runner {
106 my( $patron, $title, $copy, $summary ) = @_;
108 my $runner = OpenILS::Utils::ScriptRunner->new( type => 'js' );
110 $runner->insert( 'patron', $patron );
111 $runner->insert( 'title', $title );
112 $runner->insert( 'copy', $copy );
113 $runner->insert( 'standings', $standings );
114 $runner->insert( 'group_tree', $group_tree );
117 $runner->insert( 'result', {} );
118 $runner->insert( 'result.event', 'SUCCESS' );
121 $runner->insert( 'patron_info', {} );
122 $runner->insert( 'patron_info.copy_count', $summary->[0] );
123 $runner->insert( 'patron_info.fines', $summary->[1] );
130 # ------------------------------------------------------------------------------
132 __PACKAGE__->register_method(
133 method => "permit_circ",
134 api_name => "open-ils.circ.permit_checkout_",
135 notes => <<" NOTES");
136 Determines if the given checkout can occur
137 PARAMS( authtoken, barcode => bc, patron => pid, renew => t/f )
142 my( $self, $client, $authtoken, %params ) = @_;
143 my $barcode = $params{barcode};
144 my $patronid = $params{patron};
145 my $isrenew = $params{renew};
146 my ( $requestor, $patron, $env, $evt );
148 # check permisson of the requestor
149 ( $requestor, $patron, $evt ) = $apputils->checkses_requestor(
150 $authtoken, $patronid, 'VIEW_PERMIT_CHECKOUT' );
153 # fetch and build the circulation environment
154 ( $env, $evt ) = create_circ_env( barcode => $barcode,
155 patron => $patron, fetch_patron_circ_summary => 1 );
158 my $runner = $env->{runner};
159 $runner->load($scripts{circ_permit});
160 $runner->run or throw OpenSRF::EX::ERROR ("Circ Permit Script Died");
162 return OpenILS::Event->new($runner->retrieve('result.event'));
166 # ------------------------------------------------------------------------------
168 __PACKAGE__->register_method(
169 method => "circulate",
170 api_name => "open-ils.circ.checkout.barcode_",
171 notes => <<" NOTES");
172 Checks out an item based on barcode
173 PARAMS( authtoken, barcode => bc, patron => pid )
177 my( $self, $client, $authtoken, %params ) = @_;
178 my $barcode = $params{barcode};
179 my $patronid = $params{patron};
183 # ------------------------------------------------------------------------------
185 __PACKAGE__->register_method(
187 api_name => "open-ils.circ.checkin.barcode_",
188 notes => <<" NOTES");
189 PARAMS( authtoken, barcode => bc )
190 Checks in based on barcode
191 Returns an event object whose payload contains the record, circ, and copy
192 If the item needs to be routed, the event is a ROUTE_COPY event
193 with an additional 'route_to' variable set on the event
197 my( $self, $client, $authtoken, %params ) = @_;
198 my $barcode = $params{barcode};
201 # ------------------------------------------------------------------------------
203 __PACKAGE__->register_method(
205 api_name => "open-ils.circ.renew_",
206 notes => <<" NOTES");
207 PARAMS( authtoken, circ => circ_id );
208 open-ils.circ.renew(login_session, circ_object);
209 Renews the provided circulation. login_session is the requestor of the
210 renewal and if the logged in user is not the same as circ->usr, then
211 the logged in user must have RENEW_CIRC permissions.
215 my( $self, $client, $authtoken, %params ) = @_;
216 my $circ = $params{circ};