]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Transit.pm
added a transit abort method
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / Transit.pm
1 package OpenILS::Application::Circ::Transit;
2 use base 'OpenSRF::Application';
3 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
5 use Data::Dumper;
6 use OpenSRF::Utils;
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);
13
14 my $U                                                   = "OpenILS::Application::AppUtils";
15 my $holdcode                            = "OpenILS::Application::Circ::Holds";
16 $Data::Dumper::Indent   = 0;
17
18
19
20 __PACKAGE__->register_method(
21         method  => "copy_transit_receive",
22         api_name        => "open-ils.circ.copy_transit.receive",
23         notes           => q/
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.
33         /);
34
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');
40         return $evt if $evt;
41         ($copy, $evt) = $U->fetch_copy($params{copyid});
42         ($copy, $evt) = $U->fetch_copy_by_barcode($params{barcode}) unless $copy;
43         return $evt if $evt;
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');
47         return $evt;
48 }
49
50 # ------------------------------------------------------------------------------
51 # If the transit destination is different than the requestor's lib,
52 # a ROUTE_TO event is returned with the org set.
53 # If 
54 # ------------------------------------------------------------------------------
55 sub transit_receive {
56         my ( $class, $copy, $requestor, $session ) = @_;
57         $U->logmark;
58
59         my( $transit, $evt );
60         my $copyid = $copy->id;
61
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");
64
65         # fetch the transit
66         ($transit, $evt) = $U->fetch_open_transit_by_copy($copyid);
67         return $evt if $evt;
68
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);
73
74                 return OpenILS::Event->new('ROUTE_ITEM', org => $transit->dest );
75         }
76
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;
82
83         my $ishold      = 0;
84         my ($ht)                = $U->fetch_hold_transit( $transit->id );
85         if($ht) {
86                 $logger->info("Hold transit found in transit receive...");
87                 $ishold = 1;
88         }
89
90         $logger->info("Recovering original copy status in transit: ".$transit->copy_status);
91         $copy->status( $transit->copy_status );
92         return $evt if ( $evt = 
93                 $U->update_copy( copy => $copy, editor => $requestor->id, session => $session ));
94
95         return OpenILS::Event->new('SUCCESS', ishold => $ishold, 
96                 payload => { transit => $transit, holdtransit => $ht } );
97 }
98
99
100
101
102 __PACKAGE__->register_method(
103         method  => "copy_transit_create",
104         api_name        => "open-ils.circ.copy_transit.create",
105         notes           => q/
106                 Creates a new copy transit.  Requestor must have the 
107                 CREATE_COPY_TRANSIT permission.
108                 @param authtoken The login session key
109                 @param params A param object containing the following keys:
110                         copyid          - the copy id
111                         destination     - the id of the org destination.  If not defined,
112                                 defaults to the copy's circ_lib
113                 @return SUCCESS event on success, other event on error
114         /);
115
116 sub copy_transit_create {
117
118         my( $self, $client, $authtoken, $params ) = @_;
119         my %params = %$params;
120
121         my( $requestor, $evt ) = 
122                 $U->checksesperm( $authtoken, 'CREATE_COPY_TRANSIT' );
123         return $evt if $evt;
124
125         my $copy;
126         ($copy,$evt) = $U->fetch_copy($params{copyid});
127         return $evt if $evt;
128
129         my $session             = $params{session} || $U->start_db_session();
130         my $source              = $requestor->home_ou;
131         my $dest                        = $params{destination} || $copy->circ_lib;
132         my $transit             = Fieldmapper::action::transit_copy->new;
133
134         $logger->activity("User ". $requestor->id ." creating a ".
135                 " new copy transit for copy ".$copy->id." to org $dest");
136
137         $transit->source($source);
138         $transit->dest($dest);
139         $transit->target_copy($copy->id);
140         $transit->source_send_time("now");
141         $transit->copy_status($copy->status);
142         
143         $logger->debug("Creating new copy_transit in DB");
144
145         my $s = $session->request(
146                 "open-ils.storage.direct.action.transit_copy.create", $transit )->gather(1);
147         return $U->DB_UPDATE_FAILED($transit) unless $s;
148         
149         my $stat = $U->copy_status_from_name('in transit');
150
151         $copy->status($stat->id); 
152         return $evt if ($evt = $U->update_copy(
153                 copy => $copy, editor => $requestor->id, session => $session ));
154
155         $U->commit_db_session($session) unless $params{session};
156
157         return OpenILS::Event->new('SUCCESS', 
158                 payload => { copy => $copy, transit => $transit } );
159 }
160
161
162 __PACKAGE__->register_method(
163         method => 'abort_transit',
164         api_name        => 'open-ils.circ.transit.abort',
165         signature       => q/
166                 Deletes a cleans up a transit
167         /
168 );
169
170 sub abort_transit {
171         my( $self, $conn, $authtoken, $params ) = @_;
172
173         my $copyid              = $$params{copyid};
174         my $barcode             = $$params{barcode};
175         my $transitid   = $$params{transitid};
176
177         my $reqr;
178         my $copy;
179         my $transit;
180         my $holdtransit;
181         my $hold;
182         my $evt;
183
184
185         ($reqr, $evt) = $U->checksesperm($authtoken, 'ABORT_TRANSIT');
186         return $evt if $evt;
187
188         # ---------------------------------------------------------------------
189         # Find the related copy and/or transit based on whatever data we have
190         if( $barcode ) {
191                 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
192                 return $evt if $evt;
193
194         } elsif( $copyid ) {
195                 ($copy, $evt) = $U->fetch_copy($copyid);
196                 return $evt if $evt;
197         }
198
199         if( $transitid ) {
200                 ($transit, $evt) = $U->fetch_transit($transitid);
201                 return $evt if $evt;
202
203         } else {
204                 ($transit, $evt) = $U->fetch_open_transit_by_copy($copy->id);
205                 return $evt if $evt;
206         }
207
208         if(!$copy) {
209                 ($copy, $evt) = $U->fetch_copy($transit->tartet_copy);
210                 return $evt if $evt;
211         }
212         # ---------------------------------------------------------------------
213
214
215         if( $transit->dest != $reqr->ws_ou 
216                 and $transit->source != $reqr->ws_ou ) {
217                 $evt = $U->check_perms($reqr->id, $reqr->ws_ou, 'ABORT_REMOTE_TRANIST');
218                 return $evt if $evt;
219         }
220
221         # recover the copy status
222         $copy->status( $transit->copy_status );
223         $copy->editor( $reqr->id );
224         $copy->edit_date('now');
225
226         ($holdtransit) = $U->fetch_hold_transit($transit->id);
227
228         # if this is a hold transit, un-capture/un-target the hold
229         if($holdtransit) {
230                 ($hold, $evt) = $U->fetch_hold($holdtransit->hold);                     
231                 return $evt if $evt;
232                 $hold->clear_capture_time;
233                 $hold->clear_current_copy;
234         }
235
236         # update / delete the objects
237         my $session = $U->start_db_session();
238
239         return $U->DB_UPDATE_FAILED($transit) unless 
240                 $session->request(
241                         'open-ils.storage.direct.action.transit_copy.delete', 
242                         $transit->id )->gather(1);
243
244
245         return $U->DB_UPDATE_FAILED($copy) unless 
246                 $session->request(
247                         'open-ils.storage.direct.asset.copy.update', $copy )->gather(1);
248
249
250         if($hold) {
251                 return $U->DB_UPDATE_FAILED($hold) unless 
252                         $session->request( 
253                                 'open-ils.storage.direct.action.hold_request.update', $hold )->gather(1);
254         }       
255
256         $U->commit_db_session($session);
257
258         return 1;
259 }
260         
261
262
263
264
265 1;