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;
15 my $U = "OpenILS::Application::AppUtils";
16 my $holdcode = "OpenILS::Application::Circ::Holds";
17 $Data::Dumper::Indent = 0;
21 __PACKAGE__->register_method(
22 method => "copy_transit_receive",
23 api_name => "open-ils.circ.copy_transit.receive",
25 Closes out a copy transit
26 Requestor needs the COPY_TRANSIT_RECEIVE permission
27 @param authtoken The login session key
28 @param params An object of named params including
29 copyid - the id of the copy in quest
30 barcode - the barcode of the copy in question
31 If copyid is not sent, this is used.
32 @return A ROUTE_ITEM if the copy is destined for a different location.
33 A SUCCESS event on success. Other events on error.
36 sub copy_transit_receive {
37 my( $self, $client, $authtoken, $params ) = @_;
38 my %params = %$params;
39 my( $evt, $copy, $requestor );
40 ($requestor, $evt) = $U->checksesperm($authtoken, 'COPY_TRANSIT_RECEIVE');
42 ($copy, $evt) = $U->fetch_copy($params{copyid});
43 ($copy, $evt) = $U->fetch_copy_by_barcode($params{barcode}) unless $copy;
45 my $session = $U->start_db_session();
46 $evt = $self->transit_receive( $copy, $requestor, $session );
47 $U->commit_db_session($session) if $U->event_equals($evt,'SUCCESS');
51 # ------------------------------------------------------------------------------
52 # If the transit destination is different than the requestor's lib,
53 # a ROUTE_TO event is returned with the org set.
55 # ------------------------------------------------------------------------------
57 my ( $class, $copy, $requestor, $session ) = @_;
61 my $copyid = $copy->id;
63 my $status_name = $U->copy_status_to_name($copy->status);
64 $logger->debug("Attempting transit receive on copy $copyid. Copy status is $status_name");
67 ($transit, $evt) = $U->fetch_open_transit_by_copy($copyid);
70 if( $transit->dest != $requestor->home_ou ) {
71 $logger->activity("Fowarding transit on copy which is destined ".
72 "for a different location. copy=$copyid,current ".
73 "location=".$requestor->home_ou.",destination location=".$transit->dest);
75 return OpenILS::Event->new('ROUTE_ITEM', org => $transit->dest );
78 # The transit is received, set the receive time
79 $transit->dest_recv_time('now');
80 my $r = $session->request(
81 'open-ils.storage.direct.action.transit_copy.update', $transit )->gather(1);
82 return $U->DB_UPDATE_FAILED($transit) unless $r;
85 my ($ht) = $U->fetch_hold_transit( $transit->id );
87 $logger->info("Hold transit found in transit receive...");
91 $logger->info("Recovering original copy status in transit: ".$transit->copy_status);
92 $copy->status( $transit->copy_status );
93 return $evt if ( $evt =
94 $U->update_copy( copy => $copy, editor => $requestor->id, session => $session ));
96 return OpenILS::Event->new('SUCCESS', ishold => $ishold,
97 payload => { transit => $transit, holdtransit => $ht } );
104 __PACKAGE__->register_method(
105 method => "copy_transit_create",
106 api_name => "open-ils.circ.copy_transit.create",
108 Creates a new copy transit. Requestor must have the
109 CREATE_COPY_TRANSIT permission.
110 @param authtoken The login session key
111 @param params A param object containing the following keys:
113 destination - the id of the org destination. If not defined,
114 defaults to the copy's circ_lib
115 @return SUCCESS event on success, other event on error
118 sub copy_transit_create {
120 my( $self, $client, $authtoken, $params ) = @_;
121 my %params = %$params;
123 my( $requestor, $evt ) =
124 $U->checksesperm( $authtoken, 'CREATE_COPY_TRANSIT' );
128 ($copy,$evt) = $U->fetch_copy($params{copyid});
131 my $session = $params{session} || $U->start_db_session();
132 my $source = $requestor->home_ou;
133 my $dest = $params{destination} || $copy->circ_lib;
134 my $transit = Fieldmapper::action::transit_copy->new;
136 $logger->activity("User ". $requestor->id ." creating a ".
137 " new copy transit for copy ".$copy->id." to org $dest");
139 $transit->source($source);
140 $transit->dest($dest);
141 $transit->target_copy($copy->id);
142 $transit->source_send_time("now");
143 $transit->copy_status($copy->status);
145 $logger->debug("Creating new copy_transit in DB");
147 my $s = $session->request(
148 "open-ils.storage.direct.action.transit_copy.create", $transit )->gather(1);
149 return $U->DB_UPDATE_FAILED($transit) unless $s;
151 my $stat = $U->copy_status_from_name('in transit');
153 $copy->status($stat->id);
154 return $evt if ($evt = $U->update_copy(
155 copy => $copy, editor => $requestor->id, session => $session ));
157 $U->commit_db_session($session) unless $params{session};
159 return OpenILS::Event->new('SUCCESS',
160 payload => { copy => $copy, transit => $transit } );
164 __PACKAGE__->register_method(
165 method => 'abort_transit',
166 api_name => 'open-ils.circ.transit.abort',
168 Deletes a cleans up a transit
173 my( $self, $conn, $authtoken, $params ) = @_;
175 my $copyid = $$params{copyid};
176 my $barcode = $$params{barcode};
177 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})->[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 # ---------------------------------------------------------------------
216 if( $transit->dest != $e->requestor->ws_ou
217 and $transit->source != $e->requestor->ws_ou ) {
218 return $e->event unless $e->allowed('ABORT_REMOTE_TRANIST', $e->requestor->ws_ou);
221 # recover the copy status
222 $copy->status( $transit->copy_status );
223 $copy->editor( $e->requestor->id );
224 $copy->edit_date('now');
226 my $holdtransit = $e->retrieve_action_hold_transit_copy($transit->id);
228 return $e->event unless $e->delete_action_transit_copy($transit);
229 return $e->event unless $e->update_asset_copy($copy);
233 # if this is a hold transit, un-capture/un-target the hold
235 $hold = $e->retrieve_action_hold_request($holdtransit->hold)
237 $evt = $holdcode->_reset_hold( $e->requestor, $hold );
245 __PACKAGE__->register_method(
246 method => 'get_open_copy_transit',
247 api_name => 'open-ils.circ.open_copy_transit.retrieve',
249 Retrieves the open transit object for a given copy
250 @param auth The login session key
251 @param copyid The id of the copy
252 @return Transit object
256 sub get_open_copy_transit {
257 my( $self, $conn, $auth, $copyid ) = @_;
258 my $e = new_editor(authtoken=>$auth);
259 return $e->event unless $e->checkauth;
260 return $e->event unless $e->allowed('VIEW_USER'); # XXX rely on editor perms
261 my $t = $e->search_action_transit_copy(
262 { target_copy => $copyid, dest_recv_time => undef });
263 return $e->event unless @$t;
269 __PACKAGE__->register_method(
270 method => 'fetch_transit_by_copy',
271 api_name => 'open-ils.circ.fetch_transit_by_copy',
274 sub fetch_transit_by_copy {
275 my( $self, $conn, $auth, $copyid ) = @_;
276 my $e = new_editor(authtoken=>$auth);
277 return $e->event unless $e->checkauth;
278 my $t = $e->search_action_transit_copy(
280 target_copy => $copyid,
281 dest_recv_time => undef
284 return $e->event unless $t;
285 my $ht = $e->retrieve_action_hold_transit_copy($t->id);
286 return { atc => $t, ahtc => $ht };