1 package OpenILS::Application::Circ::Transit;
2 use base 'OpenSRF::Application';
3 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
7 use OpenSRF::Utils::Cache;
8 use Digest::MD5 qw(md5_hex);
9 use OpenILS::Utils::CStoreEditor qw/:funcs/;
10 use OpenILS::Application::AppUtils;
11 use OpenILS::Application::Circ::Holds;
12 use OpenSRF::Utils::Logger qw(:logger);
13 use OpenSRF::AppSession;
14 use OpenILS::Const qw/:const/;
16 my $U = "OpenILS::Application::AppUtils";
17 my $holdcode = "OpenILS::Application::Circ::Holds";
18 $Data::Dumper::Indent = 0;
22 __PACKAGE__->register_method(
23 method => "copy_transit_receive",
24 api_name => "open-ils.circ.copy_transit.receive",
26 Closes out a copy transit
27 Requestor needs the COPY_TRANSIT_RECEIVE permission
28 @param authtoken The login session key
29 @param params An object of named params including
30 copyid - the id of the copy in quest
31 barcode - the barcode of the copy in question
32 If copyid is not sent, this is used.
33 @return A ROUTE_ITEM if the copy is destined for a different location.
34 A SUCCESS event on success. Other events on error.
37 sub copy_transit_receive {
38 my( $self, $client, $authtoken, $params ) = @_;
39 my %params = %$params;
40 my( $evt, $copy, $requestor );
41 ($requestor, $evt) = $U->checksesperm($authtoken, 'COPY_TRANSIT_RECEIVE');
43 ($copy, $evt) = $U->fetch_copy($params{copyid});
44 ($copy, $evt) = $U->fetch_copy_by_barcode($params{barcode}) unless $copy;
46 my $session = $U->start_db_session();
47 $evt = $self->transit_receive( $copy, $requestor, $session );
48 $U->commit_db_session($session) if $U->event_equals($evt,'SUCCESS');
52 # ------------------------------------------------------------------------------
53 # If the transit destination is different than the requestor's lib,
54 # a ROUTE_TO event is returned with the org set.
56 # ------------------------------------------------------------------------------
58 my ( $class, $copy, $requestor, $session ) = @_;
62 my $copyid = $copy->id;
64 my $status_name = $U->copy_status_to_name($copy->status);
65 $logger->debug("Attempting transit receive on copy $copyid. Copy status is $status_name");
68 ($transit, $evt) = $U->fetch_open_transit_by_copy($copyid);
71 if( $transit->dest != $requestor->home_ou ) {
72 $logger->activity("Fowarding transit on copy which is destined ".
73 "for a different location. copy=$copyid,current ".
74 "location=".$requestor->home_ou.",destination location=".$transit->dest);
76 return OpenILS::Event->new('ROUTE_ITEM', org => $transit->dest );
79 # The transit is received, set the receive time
80 $transit->dest_recv_time('now');
81 my $r = $session->request(
82 'open-ils.storage.direct.action.transit_copy.update', $transit )->gather(1);
83 return $U->DB_UPDATE_FAILED($transit) unless $r;
86 my ($ht) = $U->fetch_hold_transit( $transit->id );
88 $logger->info("Hold transit found in transit receive...");
92 $logger->info("Recovering original copy status in transit: ".$transit->copy_status);
93 $copy->status( $transit->copy_status );
94 return $evt if ( $evt =
95 $U->update_copy( copy => $copy, editor => $requestor->id, session => $session ));
97 return OpenILS::Event->new('SUCCESS', ishold => $ishold,
98 payload => { transit => $transit, holdtransit => $ht } );
105 __PACKAGE__->register_method(
106 method => "copy_transit_create",
107 api_name => "open-ils.circ.copy_transit.create",
109 Creates a new copy transit. Requestor must have the
110 CREATE_COPY_TRANSIT permission.
111 @param authtoken The login session key
112 @param params A param object containing the following keys:
114 destination - the id of the org destination. If not defined,
115 defaults to the copy's circ_lib
116 @return SUCCESS event on success, other event on error
119 sub copy_transit_create {
121 my( $self, $client, $authtoken, $params ) = @_;
122 my %params = %$params;
124 my( $requestor, $evt ) =
125 $U->checksesperm( $authtoken, 'CREATE_COPY_TRANSIT' );
129 ($copy,$evt) = $U->fetch_copy($params{copyid});
132 my $session = $params{session} || $U->start_db_session();
133 my $source = $requestor->home_ou;
134 my $dest = $params{destination} || $copy->circ_lib;
135 my $transit = Fieldmapper::action::transit_copy->new;
137 $logger->activity("User ". $requestor->id ." creating a ".
138 " new copy transit for copy ".$copy->id." to org $dest");
140 $transit->source($source);
141 $transit->dest($dest);
142 $transit->target_copy($copy->id);
143 $transit->source_send_time("now");
144 $transit->copy_status($copy->status);
146 $logger->debug("Creating new copy_transit in DB");
148 my $s = $session->request(
149 "open-ils.storage.direct.action.transit_copy.create", $transit )->gather(1);
150 return $U->DB_UPDATE_FAILED($transit) unless $s;
152 my $stat = $U->copy_status_from_name('in transit');
154 $copy->status($stat->id);
155 return $evt if ($evt = $U->update_copy(
156 copy => $copy, editor => $requestor->id, session => $session ));
158 $U->commit_db_session($session) unless $params{session};
160 return OpenILS::Event->new('SUCCESS',
161 payload => { copy => $copy, transit => $transit } );
165 __PACKAGE__->register_method(
166 method => 'abort_transit',
167 api_name => 'open-ils.circ.transit.abort',
169 Deletes a cleans up a transit
174 my( $self, $conn, $authtoken, $params ) = @_;
176 my $copyid = $$params{copyid};
177 my $barcode = $$params{barcode};
178 my $transitid = $$params{transitid};
184 my $e = new_editor(xact => 1, authtoken => $authtoken);
185 return $e->event unless $e->checkauth;
186 return $e->event unless $e->allowed('ABORT_TRANSIT');
188 # ---------------------------------------------------------------------
189 # Find the related copy and/or transit based on whatever data we have
191 $copy = $e->search_asset_copy({barcode=>$barcode, deleted => 'f'})->[0];
192 return $e->event unless $copy;
195 $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
199 $transit = $e->retrieve_action_transit_copy($transitid)
204 $transit = $e->search_action_transit_copy(
205 { target_copy => $copy->id, dest_recv_time => undef })->[0];
206 return $e->event unless $transit;
209 if($transit and !$copy) {
210 $copy = $e->retrieve_asset_copy($transit->target_copy)
213 # ---------------------------------------------------------------------
215 return __abort_transit( $e, $transit, $copy );
220 sub __abort_transit {
222 my( $e, $transit, $copy, $no_reset_hold ) = @_;
227 if( $transit->copy_status == OILS_COPY_STATUS_LOST or
228 $transit->copy_status == OILS_COPY_STATUS_MISSING ) {
230 return OpenILS::Event->new('TRANSIT_ABORT_NOT_ALLOWED');
234 if( $transit->dest != $e->requestor->ws_ou
235 and $transit->source != $e->requestor->ws_ou ) {
236 return $e->event unless $e->allowed('ABORT_REMOTE_TRANIST', $e->requestor->ws_ou);
239 # recover the copy status
240 $copy->status( $transit->copy_status );
241 $copy->editor( $e->requestor->id );
242 $copy->edit_date('now');
244 my $holdtransit = $e->retrieve_action_hold_transit_copy($transit->id);
246 return $e->event unless $e->delete_action_transit_copy($transit);
247 return $e->event unless $e->update_asset_copy($copy);
251 # if this is a hold transit, un-capture/un-target the hold
252 if($holdtransit and !$no_reset_hold) {
253 $hold = $e->retrieve_action_hold_request($holdtransit->hold)
255 $evt = $holdcode->_reset_hold( $e->requestor, $hold );
263 __PACKAGE__->register_method(
264 method => 'get_open_copy_transit',
265 api_name => 'open-ils.circ.open_copy_transit.retrieve',
267 Retrieves the open transit object for a given copy
268 @param auth The login session key
269 @param copyid The id of the copy
270 @return Transit object
274 sub get_open_copy_transit {
275 my( $self, $conn, $auth, $copyid ) = @_;
276 my $e = new_editor(authtoken=>$auth);
277 return $e->event unless $e->checkauth;
278 return $e->event unless $e->allowed('VIEW_USER'); # XXX rely on editor perms
279 my $t = $e->search_action_transit_copy(
280 { target_copy => $copyid, dest_recv_time => undef });
281 return $e->event unless @$t;
287 __PACKAGE__->register_method(
288 method => 'fetch_transit_by_copy',
289 api_name => 'open-ils.circ.fetch_transit_by_copy',
292 sub fetch_transit_by_copy {
293 my( $self, $conn, $auth, $copyid ) = @_;
294 my $e = new_editor(authtoken=>$auth);
295 return $e->event unless $e->checkauth;
296 my $t = $e->search_action_transit_copy(
298 target_copy => $copyid,
299 dest_recv_time => undef
302 return $e->event unless $t;
303 my $ht = $e->retrieve_action_hold_transit_copy($t->id);
304 return { atc => $t, ahtc => $ht };
309 __PACKAGE__->register_method(
310 method => 'transits_by_lib',
311 api_name => 'open-ils.circ.transit.retrieve_by_lib',
315 sub transits_by_lib {
316 my( $self, $conn, $auth, $orgid ) = @_;
317 my $e = new_editor(authtoken=>$auth);
318 return $e->event unless $e->checkauth;
319 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # eh.. basically the same permission
321 my $tos = $e->search_action_transit_copy(
324 dest_recv_time => undef,
327 order_by => { atc => 'source_send_time' }
332 my $froms = $e->search_action_transit_copy(
335 dest_recv_time => undef,
338 order_by => { atc => 'source_send_time' }
343 return { from => $froms, to => $tos };
348 __PACKAGE__->register_method(
349 method => 'fetch_transit',
350 api_name => 'open-ils.circ.transit.retrieve',
353 my( $self, $conn, $auth, $transid ) = @_;
354 my $e = new_editor(authtoken=>$auth);
355 return $e->event unless $e->checkauth;
356 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # eh.. basically the same permission
358 my $ht = $e->retrieve_action_hold_transit_copy($transid);
361 my $t = $e->retrieve_action_transit_copy($transid)