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::ScriptRunner;
10 use OpenILS::Utils::Editor;
11 use OpenILS::Application::AppUtils;
12 use OpenILS::Application::Circ::Holds;
13 use OpenSRF::Utils::Logger qw(:logger);
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 } );
103 __PACKAGE__->register_method(
104 method => "copy_transit_create",
105 api_name => "open-ils.circ.copy_transit.create",
107 Creates a new copy transit. Requestor must have the
108 CREATE_COPY_TRANSIT permission.
109 @param authtoken The login session key
110 @param params A param object containing the following keys:
112 destination - the id of the org destination. If not defined,
113 defaults to the copy's circ_lib
114 @return SUCCESS event on success, other event on error
117 sub copy_transit_create {
119 my( $self, $client, $authtoken, $params ) = @_;
120 my %params = %$params;
122 my( $requestor, $evt ) =
123 $U->checksesperm( $authtoken, 'CREATE_COPY_TRANSIT' );
127 ($copy,$evt) = $U->fetch_copy($params{copyid});
130 my $session = $params{session} || $U->start_db_session();
131 my $source = $requestor->home_ou;
132 my $dest = $params{destination} || $copy->circ_lib;
133 my $transit = Fieldmapper::action::transit_copy->new;
135 $logger->activity("User ". $requestor->id ." creating a ".
136 " new copy transit for copy ".$copy->id." to org $dest");
138 $transit->source($source);
139 $transit->dest($dest);
140 $transit->target_copy($copy->id);
141 $transit->source_send_time("now");
142 $transit->copy_status($copy->status);
144 $logger->debug("Creating new copy_transit in DB");
146 my $s = $session->request(
147 "open-ils.storage.direct.action.transit_copy.create", $transit )->gather(1);
148 return $U->DB_UPDATE_FAILED($transit) unless $s;
150 my $stat = $U->copy_status_from_name('in transit');
152 $copy->status($stat->id);
153 return $evt if ($evt = $U->update_copy(
154 copy => $copy, editor => $requestor->id, session => $session ));
156 $U->commit_db_session($session) unless $params{session};
158 return OpenILS::Event->new('SUCCESS',
159 payload => { copy => $copy, transit => $transit } );
163 __PACKAGE__->register_method(
164 method => 'abort_transit',
165 api_name => 'open-ils.circ.transit.abort',
167 Deletes a cleans up a transit
172 my( $self, $conn, $authtoken, $params ) = @_;
174 my $copyid = $$params{copyid};
175 my $barcode = $$params{barcode};
176 my $transitid = $$params{transitid};
186 ($reqr, $evt) = $U->checksesperm($authtoken, 'ABORT_TRANSIT');
189 # ---------------------------------------------------------------------
190 # Find the related copy and/or transit based on whatever data we have
192 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
196 ($copy, $evt) = $U->fetch_copy($copyid);
201 ($transit, $evt) = $U->fetch_transit($transitid);
205 ($transit, $evt) = $U->fetch_open_transit_by_copy($copy->id);
210 ($copy, $evt) = $U->fetch_copy($transit->tartet_copy);
213 # ---------------------------------------------------------------------
216 if( $transit->dest != $reqr->ws_ou
217 and $transit->source != $reqr->ws_ou ) {
218 $evt = $U->check_perms($reqr->id, $reqr->ws_ou, 'ABORT_REMOTE_TRANIST');
222 # recover the copy status
223 $copy->status( $transit->copy_status );
224 $copy->editor( $reqr->id );
225 $copy->edit_date('now');
227 ($holdtransit) = $U->fetch_hold_transit($transit->id);
229 # update / delete the objects
230 my $session = $U->start_db_session();
233 # if this is a hold transit, un-capture/un-target the hold
235 ($hold, $evt) = $U->fetch_hold($holdtransit->hold);
237 $evt = $holdcode->_reset_hold( $reqr, $hold, $session);
241 return $U->DB_UPDATE_FAILED($transit) unless
243 'open-ils.storage.direct.action.transit_copy.delete',
244 $transit->id )->gather(1);
247 return $U->DB_UPDATE_FAILED($copy) unless
249 'open-ils.storage.direct.asset.copy.update', $copy )->gather(1);
251 $U->commit_db_session($session);
257 __PACKAGE__->register_method(
258 method => 'get_open_copy_transit',
259 api_name => 'open-ils.circ.open_copy_transit.retrieve',
261 Retrieves the open transit object for a given copy
262 @param auth The login session key
263 @param copyid The id of the copy
264 @return Transit object
268 sub get_open_copy_transit {
269 my( $self, $conn, $auth, $copyid ) = @_;
270 my $e = OpenILS::Utils::Editor->new(authtoken=>$auth);
271 return $e->event unless $e->checkauth;
272 return $e->event unless $e->allowed('VIEW_USER'); # XXX rely on editor perms
273 my $t = $e->search_action_transit_copy(
274 { target_copy => $copyid, dest_recv_time => undef });
275 return $e->event unless @$t;