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 __PACKAGE__->register_method(
21 method => "copy_transit_receive",
22 api_name => "open-ils.circ.copy_transit.receive",
24 Closes out a copy transit
25 Requestor needs the COPY_TRANSIT_RECEIVE permission
26 @param authtoken The login session key
27 @param params An object of named params including
28 copyid - the id of the copy in quest
29 barcode - the barcode of the copy in question
30 If copyid is not sent, this is used.
31 @return A ROUTE_ITEM if the copy is destined for a different location.
32 A SUCCESS event on success. Other events on error.
35 sub copy_transit_receive {
36 my( $self, $client, $authtoken, $params ) = @_;
37 my %params = %$params;
38 my( $evt, $copy, $requestor );
39 ($requestor, $evt) = $U->checksesperm($authtoken, 'COPY_TRANSIT_RECEIVE');
41 ($copy, $evt) = $U->fetch_copy($params{copyid});
42 ($copy, $evt) = $U->fetch_copy_by_barcode($params{barcode}) unless $copy;
44 my $session = $U->start_db_session();
45 $evt = $self->transit_receive( $copy, $requestor, $session );
46 $U->commit_db_session($session) if $U->event_equals($evt,'SUCCESS');
50 # ------------------------------------------------------------------------------
51 # If the transit destination is different than the requestor's lib,
52 # a ROUTE_TO event is returned with the org set.
54 # ------------------------------------------------------------------------------
56 my ( $class, $copy, $requestor, $session ) = @_;
59 my( $transit, $hold_transit, $evt );
60 my $copyid = $copy->id;
62 my $status_name = $U->copy_status_to_name($copy->status);
63 $logger->debug("Attempting transit receive on copy $copyid. Copy status is $status_name");
66 ($transit, $evt) = $U->fetch_open_transit_by_copy($copyid);
69 if( $transit->dest != $requestor->home_ou ) {
70 $logger->activity("Fowarding transit on copy which is destined ".
71 "for a different location. copy=$copyid,current ".
72 "location=".$requestor->home_ou.",destination location=".$transit->dest);
74 return OpenILS::Event->new('ROUTE_ITEM', org => $transit->dest );
77 # The transit is received, set the receive time
78 $transit->dest_recv_time('now');
79 my $r = $session->request(
80 'open-ils.storage.direct.action.transit_copy.update', $transit )->gather(1);
81 return $U->DB_UPDATE_FAILED($transit) unless $r;
83 # if this is a hold transit, finalize the hold transit
84 if( ($evt = _finish_hold_transit(
85 $session, $requestor, $copy, $transit->id )) ) {
86 return $evt unless $U->event_equals($evt, 'SUCCESS');
92 #recover this copy's status from the transit
93 $copy->status( $transit->copy_status );
94 return OpenILS::Event->new('SUCCESS', payload => $copy);
100 # ------------------------------------------------------------------------------
101 # If we have a hold transit, set the copy's status to 'on holds shelf',
102 # update the copy, and return SUCCESS
103 # ------------------------------------------------------------------------------
104 sub _finish_hold_transit {
105 my( $session, $requestor, $copy, $transid ) = @_;
107 my ($hold_transit, $evt) = $U->fetch_hold_transit( $transid );
108 return undef unless $hold_transit;
110 my $s = $U->copy_status_from_name('on holds shelf');
111 $logger->info("Hold transit found: ".$hold_transit->id.". Routing to holds shelf");
113 $copy->status($s->id);
114 $U->update_copy( copy => $copy,
115 editor => $requestor->id, session => $session );
117 my $r = $session->request(
118 'open-ils.storage.direct.asset.copy.update', $copy )->gather(1);
119 return $U->DB_UPDATE_FAILED($copy) unless $r;
121 return OpenILS::Event->new('SUCCESS');
125 __PACKAGE__->register_method(
126 method => "copy_transit_create",
127 api_name => "open-ils.circ.copy_transit.create",
129 Creates a new copy transit. Requestor must have the
130 CREATE_COPY_TRANSIT permission.
131 @param authtoken The login session key
132 @param params A param object containing the following keys:
134 destination - the id of the org destination. If not defined,
135 defaults to the copy's circ_lib
136 @return SUCCESS event on success, other event on error
139 sub copy_transit_create {
141 my( $self, $client, $authtoken, $params ) = @_;
142 my %params = %$params;
144 my( $requestor, $evt ) =
145 $U->checksesperm( $authtoken, 'CREATE_COPY_TRANSIT' );
149 ($copy,$evt) = $U->fetch_copy($params{copyid});
152 my $session = $params{session} || $U->start_db_session();
153 my $source = $requestor->home_ou;
154 my $dest = $params{destination} || $copy->circ_lib;
155 my $transit = Fieldmapper::action::transit_copy->new;
157 $logger->activity("User ". $requestor->id ." creating a ".
158 " new copy transit for copy ".$copy->id." to org $dest");
160 $transit->source($source);
161 $transit->dest($dest);
162 $transit->target_copy($copy->id);
163 $transit->source_send_time("now");
164 $transit->copy_status($copy->status);
166 $logger->debug("Creating new copy_transit in DB");
168 my $s = $session->request(
169 "open-ils.storage.direct.action.transit_copy.create", $transit )->gather(1);
170 return $U->DB_UPDATE_FAILED($transit) unless $s;
172 my $stat = $U->copy_status_from_name('in transit');
174 $copy->status($stat->id);
175 return $evt if ($evt = $U->update_copy(
176 copy => $copy, editor => $requestor->id, session => $session ));
178 $U->commit_db_session($session) unless $params{session};
180 return OpenILS::Event->new('SUCCESS',
181 payload => { copy => $copy, transit => $transit } );