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};
185 my $e = new_editor(xact => 1, authtoken => $authtoken);
186 return $e->event unless $e->checkauth;
187 return $e->event unless $e->allowed('ABORT_TRANSIT');
189 # ---------------------------------------------------------------------
190 # Find the related copy and/or transit based on whatever data we have
192 $copy = $e->search_asset_copy({barcode=>$barcode, deleted => 'f'})->[0];
193 return $e->event unless $copy;
196 $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
200 $transit = $e->retrieve_action_transit_copy($transitid)
205 $transit = $e->search_action_transit_copy(
206 { target_copy => $copy->id, dest_recv_time => undef })->[0];
207 return $e->event unless $transit;
210 if($transit and !$copy) {
211 $copy = $e->retrieve_asset_copy($transit->target_copy)
214 # ---------------------------------------------------------------------
216 if( $transit->copy_status == OILS_COPY_STATUS_LOST or
217 $transit->copy_status == OILS_COPY_STATUS_MISSING ) {
219 return OpenILS::Event->new('TRANSIT_ABORT_NOT_ALLOWED');
223 if( $transit->dest != $e->requestor->ws_ou
224 and $transit->source != $e->requestor->ws_ou ) {
225 return $e->event unless $e->allowed('ABORT_REMOTE_TRANIST', $e->requestor->ws_ou);
228 # recover the copy status
229 $copy->status( $transit->copy_status );
230 $copy->editor( $e->requestor->id );
231 $copy->edit_date('now');
233 my $holdtransit = $e->retrieve_action_hold_transit_copy($transit->id);
235 return $e->event unless $e->delete_action_transit_copy($transit);
236 return $e->event unless $e->update_asset_copy($copy);
240 # if this is a hold transit, un-capture/un-target the hold
242 $hold = $e->retrieve_action_hold_request($holdtransit->hold)
244 $evt = $holdcode->_reset_hold( $e->requestor, $hold );
252 __PACKAGE__->register_method(
253 method => 'get_open_copy_transit',
254 api_name => 'open-ils.circ.open_copy_transit.retrieve',
256 Retrieves the open transit object for a given copy
257 @param auth The login session key
258 @param copyid The id of the copy
259 @return Transit object
263 sub get_open_copy_transit {
264 my( $self, $conn, $auth, $copyid ) = @_;
265 my $e = new_editor(authtoken=>$auth);
266 return $e->event unless $e->checkauth;
267 return $e->event unless $e->allowed('VIEW_USER'); # XXX rely on editor perms
268 my $t = $e->search_action_transit_copy(
269 { target_copy => $copyid, dest_recv_time => undef });
270 return $e->event unless @$t;
276 __PACKAGE__->register_method(
277 method => 'fetch_transit_by_copy',
278 api_name => 'open-ils.circ.fetch_transit_by_copy',
281 sub fetch_transit_by_copy {
282 my( $self, $conn, $auth, $copyid ) = @_;
283 my $e = new_editor(authtoken=>$auth);
284 return $e->event unless $e->checkauth;
285 my $t = $e->search_action_transit_copy(
287 target_copy => $copyid,
288 dest_recv_time => undef
291 return $e->event unless $t;
292 my $ht = $e->retrieve_action_hold_transit_copy($t->id);
293 return { atc => $t, ahtc => $ht };