]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/Transit.pm
LP#1613374 - Check stored copy status before clobbering it.
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Circ / Transit.pm
1 package OpenILS::Application::Circ::Transit;
2 use base 'OpenILS::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::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/;
15
16 my $U                           = "OpenILS::Application::AppUtils";
17 my $holdcode                = "OpenILS::Application::Circ::Holds";
18 $Data::Dumper::Indent   = 0;
19
20
21
22 __PACKAGE__->register_method(
23     method  => "copy_transit_receive",
24     api_name    => "open-ils.circ.copy_transit.receive",
25     notes       => q/
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.
35     /);
36
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');
42     return $evt if $evt;
43     ($copy, $evt) = $U->fetch_copy($params{copyid});
44     ($copy, $evt) = $U->fetch_copy_by_barcode($params{barcode}) unless $copy;
45     return $evt if $evt;
46     my $session = $U->start_db_session();
47     $U->set_audit_info($session, $authtoken, $requestor->id, $requestor->wsid);
48     $evt = transit_receive( $self, $copy, $requestor, $session );
49     $U->commit_db_session($session) if $U->event_equals($evt,'SUCCESS');
50     return $evt;
51 }
52
53 # ------------------------------------------------------------------------------
54 # If the transit destination is different than the requestor's lib,
55 # a ROUTE_TO event is returned with the org set.
56 # If 
57 # ------------------------------------------------------------------------------
58 sub transit_receive {
59     my ( $class, $copy, $requestor, $session ) = @_;
60     $U->logmark;
61
62     my( $transit, $evt );
63     my $copyid = $copy->id;
64
65     my $status_name = $U->copy_status_to_name($copy->status);
66     $logger->debug("Attempting transit receive on copy $copyid. Copy status is $status_name");
67
68     # fetch the transit
69     ($transit, $evt) = $U->fetch_open_transit_by_copy($copyid);
70     return $evt if $evt;
71
72     if( $transit->dest != $requestor->ws_ou ) {
73         $logger->activity("Fowarding transit on copy which is destined ".
74             "for a different location. copy=$copyid,current ".
75             "location=".$requestor->ws_ou.",destination location=".$transit->dest);
76
77         return OpenILS::Event->new('ROUTE_ITEM', org => $transit->dest );
78     }
79
80     # The transit is received, set the receive time
81     $transit->dest_recv_time('now');
82     my $r = $session->request(
83         'open-ils.storage.direct.action.transit_copy.update', $transit )->gather(1);
84     return $U->DB_UPDATE_FAILED($transit) unless $r;
85
86     my $ishold  = 0;
87     my ($ht)        = $U->fetch_hold_transit( $transit->id );
88     if($ht) {
89         $logger->info("Hold transit found in transit receive...");
90         $ishold = 1;
91     }
92
93     $logger->info("Recovering original copy status in transit: ".$transit->copy_status);
94     $copy->status( $transit->copy_status );
95     return $evt if ( $evt = 
96         $U->update_copy( copy => $copy, editor => $requestor->id, session => $session ));
97
98     return OpenILS::Event->new('SUCCESS', ishold => $ishold, 
99         payload => { transit => $transit, holdtransit => $ht } );
100 }
101
102
103
104
105
106 __PACKAGE__->register_method(
107     method  => "copy_transit_create",
108     api_name    => "open-ils.circ.copy_transit.create",
109     notes       => q/
110         Creates a new copy transit.  Requestor must have the 
111         CREATE_COPY_TRANSIT permission.
112         @param authtoken The login session key
113         @param params A param object containing the following keys:
114             copyid      - the copy id
115             destination - the id of the org destination.  If not defined,
116                 defaults to the copy's circ_lib
117         @return SUCCESS event on success, other event on error
118     /);
119
120 sub copy_transit_create {
121
122     my( $self, $client, $authtoken, $params ) = @_;
123     my %params = %$params;
124
125     my( $requestor, $evt ) = 
126         $U->checksesperm( $authtoken, 'CREATE_COPY_TRANSIT' );
127     return $evt if $evt;
128
129     my $copy;
130     ($copy,$evt) = $U->fetch_copy($params{copyid});
131     return $evt if $evt;
132
133     my $session     = $params{session} || $U->start_db_session();
134     my $source      = $requestor->home_ou;
135     my $dest            = $params{destination} || $copy->circ_lib;
136     my $transit     = Fieldmapper::action::transit_copy->new;
137     $U->set_audit_info($session, $authtoken, $requestor->id, $requestor->wsid);
138
139     $logger->activity("User ". $requestor->id ." creating a ".
140         " new copy transit for copy ".$copy->id." to org $dest");
141
142     $transit->source($source);
143     $transit->dest($dest);
144     $transit->target_copy($copy->id);
145     $transit->source_send_time("now");
146     $transit->copy_status($copy->status);
147     
148     $logger->debug("Creating new copy_transit in DB");
149
150     my $s = $session->request(
151         "open-ils.storage.direct.action.transit_copy.create", $transit )->gather(1);
152     return $U->DB_UPDATE_FAILED($transit) unless $s;
153     
154     my $stat = $U->copy_status_from_name('in transit');
155
156     $copy->status($stat->id); 
157     return $evt if ($evt = $U->update_copy(
158         copy => $copy, editor => $requestor->id, session => $session ));
159
160     $U->commit_db_session($session) unless $params{session};
161
162     return OpenILS::Event->new('SUCCESS', 
163         payload => { copy => $copy, transit => $transit } );
164 }
165
166
167 __PACKAGE__->register_method(
168     method => 'abort_transit',
169     api_name    => 'open-ils.circ.transit.abort',
170     signature   => q/
171         Deletes a cleans up a transit
172     /
173 );
174
175 sub abort_transit {
176     my( $self, $conn, $authtoken, $params ) = @_;
177
178     my $copyid      = $$params{copyid};
179     my $barcode     = $$params{barcode};
180     my $transitid   = $$params{transitid};
181
182     my $copy;
183     my $transit;
184     my $evt;
185
186     my $e = new_editor(xact => 1, authtoken => $authtoken);
187     return $e->event unless $e->checkauth;
188     return $e->event unless $e->allowed('ABORT_TRANSIT');
189
190     # ---------------------------------------------------------------------
191     # Find the related copy and/or transit based on whatever data we have
192     if( $barcode ) {
193         $copy = $e->search_asset_copy({barcode=>$barcode, deleted => 'f'})->[0];
194         return $e->event unless $copy;
195
196     } elsif( $copyid ) {
197         $copy = $e->retrieve_asset_copy($copyid) or return $e->event;
198     }
199
200     if( $transitid ) {
201         $transit = $e->retrieve_action_transit_copy($transitid)
202             or return $e->event;
203
204     } elsif( $copy ) {
205
206         $transit = $e->search_action_transit_copy(
207             { target_copy => $copy->id, dest_recv_time => undef })->[0];
208         return $e->event unless $transit;
209     }
210
211     if($transit and !$copy) {
212         $copy = $e->retrieve_asset_copy($transit->target_copy)
213             or return $e->event;
214     }
215     # ---------------------------------------------------------------------
216
217     return __abort_transit( $e, $transit, $copy );
218 }
219
220
221
222 sub __abort_transit {
223
224     my( $e, $transit, $copy, $no_reset_hold, $no_commit ) = @_;
225
226     my $evt;
227     my $hold;
228
229     if( (($transit->copy_status == OILS_COPY_STATUS_LOST || $transit->copy_status == OILS_COPY_STATUS_LOST_AND_PAID)
230              and !$e->allowed('ABORT_TRANSIT_ON_LOST')) or
231         ($transit->copy_status == OILS_COPY_STATUS_MISSING and !$e->allowed('ABORT_TRANSIT_ON_MISSING')) ) {
232         $e->rollback;
233         return OpenILS::Event->new('TRANSIT_ABORT_NOT_ALLOWED', copy_status => $transit->copy_status);
234     }
235
236
237     if( $transit->dest != $e->requestor->ws_ou 
238         and $transit->source != $e->requestor->ws_ou ) {
239         return $e->die_event unless $e->allowed('ABORT_REMOTE_TRANSIT', $e->requestor->ws_ou);
240     }
241
242     my $holdtransit = $e->retrieve_action_hold_transit_copy($transit->id);
243
244     return $e->die_event unless $e->delete_action_transit_copy($transit);
245
246     # Only change the copy status if the copy status is "In Transit."
247     if ($copy->status == OILS_COPY_STATUS_IN_TRANSIT) {
248         # if the status would normally result in 'Reshelving' once the item is checked in
249         if ($transit->copy_status == OILS_COPY_STATUS_AVAILABLE   ||
250             $transit->copy_status == OILS_COPY_STATUS_CHECKED_OUT ||
251             $transit->copy_status == OILS_COPY_STATUS_IN_PROCESS  ||
252             $transit->copy_status == OILS_COPY_STATUS_ON_HOLDS_SHELF  ||
253             $transit->copy_status == OILS_COPY_STATUS_IN_TRANSIT  ||
254             $transit->copy_status == OILS_COPY_STATUS_CATALOGING  ||
255             $transit->copy_status == OILS_COPY_STATUS_ON_RESV_SHELF  ||
256             $transit->copy_status == OILS_COPY_STATUS_RESHELVING) {
257             # set copy to Canceled Transit
258             $copy->status( OILS_COPY_STATUS_CANCELED_TRANSIT);
259         } else {
260             # recover the copy status
261             $copy->status($transit->copy_status);
262         }
263         $copy->editor( $e->requestor->id );
264         $copy->edit_date('now');
265
266         return $e->die_event unless $e->update_asset_copy($copy);
267     }
268
269     $e->commit unless $no_commit;
270
271     # if this is a hold transit, un-capture/un-target the hold
272     if($holdtransit and !$no_reset_hold) {
273         $hold = $e->retrieve_action_hold_request($holdtransit->hold) 
274             or return $e->die_event;
275         $evt = $holdcode->_reset_hold( $e->requestor, $hold );
276         return $evt if $evt;
277     }
278
279     return 1;
280 }
281
282
283 __PACKAGE__->register_method(
284     method      => 'get_open_copy_transit',
285     api_name        => 'open-ils.circ.open_copy_transit.retrieve',
286     signature   => q/
287         Retrieves the open transit object for a given copy
288         @param auth The login session key
289         @param copyid The id of the copy
290         @return Transit object
291  /
292 );
293
294 sub get_open_copy_transit {
295     my( $self, $conn, $auth, $copyid ) = @_;    
296     my $e = new_editor(authtoken=>$auth);
297     return $e->event unless $e->checkauth;
298     return $e->event unless $e->allowed('VIEW_USER'); # XXX rely on editor perms
299     my $t = $e->search_action_transit_copy(
300         { target_copy => $copyid, dest_recv_time => undef });
301     return $e->event unless @$t;
302     return $$t[0];
303 }
304
305
306
307 __PACKAGE__->register_method(
308     method => 'fetch_transit_by_copy',
309     api_name => 'open-ils.circ.fetch_transit_by_copy',
310 );
311
312 sub fetch_transit_by_copy {
313     my( $self, $conn, $auth, $copyid ) = @_;
314     my $e = new_editor(authtoken=>$auth);
315     return $e->event unless $e->checkauth;
316     my $t = $e->search_action_transit_copy(
317         {
318             target_copy => $copyid,
319             dest_recv_time => undef
320         }
321     )->[0];
322     return $e->event unless $t;
323     my $ht = $e->retrieve_action_hold_transit_copy($t->id);
324     return { atc => $t, ahtc => $ht };
325 }
326
327
328
329 __PACKAGE__->register_method(
330     method => 'transits_by_lib',
331     api_name => 'open-ils.circ.transit.retrieve_by_lib',
332 );
333
334
335 # start_date and end_date are optional endpoints for the transit creation date
336 sub transits_by_lib {
337     my( $self, $conn, $auth, $orgid, $start_date, $end_date ) = @_;
338     my $e = new_editor(authtoken=>$auth);
339     return $e->event unless $e->checkauth;
340     return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # eh.. basically the same permission
341
342     my $order_by = {order_by => { atc => 'source_send_time' }};
343     my $search = { dest_recv_time => undef };
344
345     if($end_date) {
346         if($start_date) {
347             $search->{source_send_time} = {between => [$start_date, $end_date]};
348         } else {
349             $search->{source_send_time} = {'<=' => $end_date};
350         }
351     } elsif($start_date) {
352         $search->{source_send_time} = {'>=' => $start_date};
353     }
354
355     $search->{dest} = $orgid;
356
357     my $tos = $e->search_action_transit_copy([ $search, $order_by ], {idlist=>1});
358
359     delete $$search{dest};
360     $search->{source} = $orgid;
361
362     my $froms = $e->search_action_transit_copy([ $search, $order_by ], {idlist=>1});
363
364     return { from => $froms, to => $tos };
365 }
366
367
368
369 __PACKAGE__->register_method(
370     method => 'fetch_transit',
371     api_name => 'open-ils.circ.transit.retrieve',
372 );
373 sub fetch_transit {
374     my( $self, $conn, $auth, $transid ) = @_;
375     my $e = new_editor(authtoken=>$auth);
376     return $e->event unless $e->checkauth;
377     return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # eh.. basically the same permission
378
379     my $ht = $e->retrieve_action_hold_transit_copy($transid);
380     return $ht if $ht;
381
382     my $t = $e->retrieve_action_transit_copy($transid)
383         or return $e->event;
384     return $t;
385 }
386
387     
388
389
390
391
392 1;