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::Application::AppUtils;
11 use OpenILS::Application::Circ::Holds;
12 use OpenSRF::Utils::Logger qw(:logger);
14 my $U = "OpenILS::Application::AppUtils";
15 my $holdcode = "OpenILS::Application::Circ::Holds";
16 $Data::Dumper::Indent = 0;
20 # ------------------------------------------------------------------------------
21 # If the transit destination is different than the requestor's lib,
22 # a ROUTE_TO event is returned with the org set.
24 # ------------------------------------------------------------------------------
26 my ( $class, $copy, $requestor, $session ) = @_;
29 my( $transit, $hold_transit, $evt );
30 my $copyid = $copy->id;
32 my $status_name = $U->copy_status_to_name($copy->status);
33 $logger->debug("Attempting transit receive on copy $copyid. Copy status is $status_name");
36 ($transit, $evt) = $U->fetch_open_transit_by_copy($copyid);
39 if( $transit->dest != $requestor->home_ou ) {
40 $logger->activity("Fowarding transit on copy which is destined ".
41 "for a different location. copy=$copyid,current ".
42 "location=".$requestor->home_ou.",destination location=".$transit->dest);
44 return OpenILS::Event->new('ROUTE_ITEM', org => $transit->dest );
47 # The transit is received, set the receive time
48 $transit->dest_recv_time('now');
49 my $r = $session->request(
50 'open-ils.storage.direct.action.transit_copy.update', $transit )->gather(1);
51 return $U->DB_UPDATE_FAILED($transit) unless $r;
53 # if this is a hold transit, finalize the hold transit
54 if( ($evt = _finish_hold_transit(
55 $session, $requestor, $copy, $transit->id )) ) {
56 return $evt unless $U->event_equals($evt, 'SUCCESS');
62 #recover this copy's status from the transit
63 $copy->status( $transit->copy_status );
64 return OpenILS::Event->('SUCCESS', payload => $copy);
70 # ------------------------------------------------------------------------------
71 # If we have a hold transit, set the copy's status to 'on holds shelf',
72 # update the copy, and return the ROUTE_TO_COPY_LOATION event
73 # ------------------------------------------------------------------------------
74 sub _finish_hold_transit {
75 my( $session, $requestor, $copy, $transid ) = @_;
77 my ($hold_transit, $evt) = $U->fetch_hold_transit( $transid );
78 return undef unless $hold_transit;
80 my $s = $U->copy_status_from_name('on holds shelf');
81 $logger->info("Hold transit found: ".$hold_transit->id.". Routing to holds shelf");
83 $copy->status($s->id);
84 $U->update_copy( copy => $copy,
85 editor => $requestor->id, session => $session );
87 my $r = $session->request(
88 'open-ils.storage.direct.asset.copy.update', $copy )->gather(1);
89 return $U->DB_UPDATE_FAILED($copy) unless $r;
91 return OpenILS::Event->new('SUCCESS');
95 __PACKAGE__->register_method(
96 method => "copy_transit_create",
97 api_name => "open-ils.circ.copy_transit.create",
99 Creates a new copy transit. Requestor must have the
100 CREATE_COPY_TRANSIT permission.
101 @param authtoken The login session key
102 @param params A param object containing the following keys:
104 destination - the id of the org destination. If not defined,
105 defaults to the copy's circ_lib
106 @return SUCCESS event on success, other event on error
109 sub copy_transit_create {
111 my( $self, $client, $authtoken, $params ) = @_;
112 my %params = %$params;
114 my( $requestor, $evt ) =
115 $U->checksesperm( $authtoken, 'CREATE_COPY_TRANSIT' );
119 ($copy,$evt) = $U->fetch_copy($params{copyid});
122 my $session = $U->start_db_session();
123 my $source = $requestor->home_ou;
124 my $dest = $params{destination} || $copy->circ_lib;
125 my $transit = Fieldmapper::action::transit_copy->new;
127 $logger->activity("User ". $requestor->id ." creating a ".
128 " new copy transit for copy ".$copy->id." to org $dest");
130 $transit->source($source);
131 $transit->dest($dest);
132 $transit->target_copy($copy->id);
133 $transit->source_send_time("now");
134 $transit->copy_status($copy->status);
136 $logger->debug("Creating new copy_transit in DB");
138 my $s = $session->request(
139 "open-ils.storage.direct.action.transit_copy.create", $transit )->gather(1);
140 return $U->DB_UPDATE_FAILED($transit) unless $s;
142 my $stat = $U->copy_status_from_name('in transit');
144 $copy->status($stat->id);
145 return $evt if ($evt = $U->update_copy(
146 copy => $copy, editor => $requestor->id, session => $session ));
148 return OpenILS::Event->new('SUCCESS',
149 payload => { copy => $copy, transit => $transit } );