1 package OpenILS::Application::Circ::Transit;
2 use base 'OpenILS::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 = transit_receive( $self, $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 and !$e->allowed('ABORT_TRANSIT_ON_LOST')) or
228 ($transit->copy_status == OILS_COPY_STATUS_MISSING and !$e->allowed('ABORT_TRANSIT_ON_MISSING')) ) {
230 return OpenILS::Event->new('TRANSIT_ABORT_NOT_ALLOWED', copy_status => $transit->copy_status);
234 if( $transit->dest != $e->requestor->ws_ou
235 and $transit->source != $e->requestor->ws_ou ) {
236 return $e->die_event unless $e->allowed('ABORT_REMOTE_TRANSIT', $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);
247 $logger->info("setting copy to reshelving on hold transit abort");
248 $copy->status( OILS_COPY_STATUS_RESHELVING );
251 return $e->die_event unless $e->delete_action_transit_copy($transit);
252 return $e->die_event unless $e->update_asset_copy($copy);
256 # if this is a hold transit, un-capture/un-target the hold
257 if($holdtransit and !$no_reset_hold) {
258 $hold = $e->retrieve_action_hold_request($holdtransit->hold)
259 or return $e->die_event;
260 $evt = $holdcode->_reset_hold( $e->requestor, $hold );
268 __PACKAGE__->register_method(
269 method => 'get_open_copy_transit',
270 api_name => 'open-ils.circ.open_copy_transit.retrieve',
272 Retrieves the open transit object for a given copy
273 @param auth The login session key
274 @param copyid The id of the copy
275 @return Transit object
279 sub get_open_copy_transit {
280 my( $self, $conn, $auth, $copyid ) = @_;
281 my $e = new_editor(authtoken=>$auth);
282 return $e->event unless $e->checkauth;
283 return $e->event unless $e->allowed('VIEW_USER'); # XXX rely on editor perms
284 my $t = $e->search_action_transit_copy(
285 { target_copy => $copyid, dest_recv_time => undef });
286 return $e->event unless @$t;
292 __PACKAGE__->register_method(
293 method => 'fetch_transit_by_copy',
294 api_name => 'open-ils.circ.fetch_transit_by_copy',
297 sub fetch_transit_by_copy {
298 my( $self, $conn, $auth, $copyid ) = @_;
299 my $e = new_editor(authtoken=>$auth);
300 return $e->event unless $e->checkauth;
301 my $t = $e->search_action_transit_copy(
303 target_copy => $copyid,
304 dest_recv_time => undef
307 return $e->event unless $t;
308 my $ht = $e->retrieve_action_hold_transit_copy($t->id);
309 return { atc => $t, ahtc => $ht };
314 __PACKAGE__->register_method(
315 method => 'transits_by_lib',
316 api_name => 'open-ils.circ.transit.retrieve_by_lib',
320 # start_date and end_date are optional endpoints for the transit creation date
321 sub transits_by_lib {
322 my( $self, $conn, $auth, $orgid, $start_date, $end_date ) = @_;
323 my $e = new_editor(authtoken=>$auth);
324 return $e->event unless $e->checkauth;
325 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # eh.. basically the same permission
327 my $order_by = {order_by => { atc => 'source_send_time' }};
328 my $search = { dest_recv_time => undef };
332 $search->{source_send_time} = {between => [$start_date, $end_date]};
334 $search->{source_send_time} = {'<=' => $end_date};
336 } elsif($start_date) {
337 $search->{source_send_time} = {'>=' => $start_date};
340 $search->{dest} = $orgid;
342 my $tos = $e->search_action_transit_copy([ $search, $order_by ], {idlist=>1});
344 delete $$search{dest};
345 $search->{source} = $orgid;
347 my $froms = $e->search_action_transit_copy([ $search, $order_by ], {idlist=>1});
349 return { from => $froms, to => $tos };
354 __PACKAGE__->register_method(
355 method => 'fetch_transit',
356 api_name => 'open-ils.circ.transit.retrieve',
359 my( $self, $conn, $auth, $transid ) = @_;
360 my $e = new_editor(authtoken=>$auth);
361 return $e->event unless $e->checkauth;
362 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # eh.. basically the same permission
364 my $ht = $e->retrieve_action_hold_transit_copy($transid);
367 my $t = $e->retrieve_action_transit_copy($transid)