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