]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Transit.pm
bug fixes, clean up
[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::Utils::Editor;
11 use OpenILS::Application::AppUtils;
12 use OpenILS::Application::Circ::Holds;
13 use OpenSRF::Utils::Logger qw(:logger);
14
15 my $U                                                   = "OpenILS::Application::AppUtils";
16 my $holdcode                            = "OpenILS::Application::Circ::Holds";
17 $Data::Dumper::Indent   = 0;
18
19
20
21 __PACKAGE__->register_method(
22         method  => "copy_transit_receive",
23         api_name        => "open-ils.circ.copy_transit.receive",
24         notes           => q/
25                 Closes out a copy transit
26                 Requestor needs the COPY_TRANSIT_RECEIVE permission
27                 @param authtoken The login session key
28                 @param params An object of named params including
29                         copyid - the id of the copy in quest
30                         barcode - the barcode of the copy in question 
31                                 If copyid is not sent, this is used.
32                 @return A ROUTE_ITEM if the copy is destined for a different location.
33                         A SUCCESS event on success. Other events on error.
34         /);
35
36 sub copy_transit_receive {
37         my( $self, $client, $authtoken, $params ) = @_;
38         my %params = %$params;
39         my( $evt, $copy, $requestor );
40         ($requestor, $evt) = $U->checksesperm($authtoken, 'COPY_TRANSIT_RECEIVE');
41         return $evt if $evt;
42         ($copy, $evt) = $U->fetch_copy($params{copyid});
43         ($copy, $evt) = $U->fetch_copy_by_barcode($params{barcode}) unless $copy;
44         return $evt if $evt;
45         my $session = $U->start_db_session();
46         $evt = $self->transit_receive( $copy, $requestor, $session );
47         $U->commit_db_session($session) if $U->event_equals($evt,'SUCCESS');
48         return $evt;
49 }
50
51 # ------------------------------------------------------------------------------
52 # If the transit destination is different than the requestor's lib,
53 # a ROUTE_TO event is returned with the org set.
54 # If 
55 # ------------------------------------------------------------------------------
56 sub transit_receive {
57         my ( $class, $copy, $requestor, $session ) = @_;
58         $U->logmark;
59
60         my( $transit, $evt );
61         my $copyid = $copy->id;
62
63         my $status_name = $U->copy_status_to_name($copy->status);
64         $logger->debug("Attempting transit receive on copy $copyid. Copy status is $status_name");
65
66         # fetch the transit
67         ($transit, $evt) = $U->fetch_open_transit_by_copy($copyid);
68         return $evt if $evt;
69
70         if( $transit->dest != $requestor->home_ou ) {
71                 $logger->activity("Fowarding transit on copy which is destined ".
72                         "for a different location. copy=$copyid,current ".
73                         "location=".$requestor->home_ou.",destination location=".$transit->dest);
74
75                 return OpenILS::Event->new('ROUTE_ITEM', org => $transit->dest );
76         }
77
78         # The transit is received, set the receive time
79         $transit->dest_recv_time('now');
80         my $r = $session->request(
81                 'open-ils.storage.direct.action.transit_copy.update', $transit )->gather(1);
82         return $U->DB_UPDATE_FAILED($transit) unless $r;
83
84         my $ishold      = 0;
85         my ($ht)                = $U->fetch_hold_transit( $transit->id );
86         if($ht) {
87                 $logger->info("Hold transit found in transit receive...");
88                 $ishold = 1;
89         }
90
91         $logger->info("Recovering original copy status in transit: ".$transit->copy_status);
92         $copy->status( $transit->copy_status );
93         return $evt if ( $evt = 
94                 $U->update_copy( copy => $copy, editor => $requestor->id, session => $session ));
95
96         return OpenILS::Event->new('SUCCESS', ishold => $ishold, 
97                 payload => { transit => $transit, holdtransit => $ht } );
98 }
99
100
101
102
103 __PACKAGE__->register_method(
104         method  => "copy_transit_create",
105         api_name        => "open-ils.circ.copy_transit.create",
106         notes           => q/
107                 Creates a new copy transit.  Requestor must have the 
108                 CREATE_COPY_TRANSIT permission.
109                 @param authtoken The login session key
110                 @param params A param object containing the following keys:
111                         copyid          - the copy id
112                         destination     - the id of the org destination.  If not defined,
113                                 defaults to the copy's circ_lib
114                 @return SUCCESS event on success, other event on error
115         /);
116
117 sub copy_transit_create {
118
119         my( $self, $client, $authtoken, $params ) = @_;
120         my %params = %$params;
121
122         my( $requestor, $evt ) = 
123                 $U->checksesperm( $authtoken, 'CREATE_COPY_TRANSIT' );
124         return $evt if $evt;
125
126         my $copy;
127         ($copy,$evt) = $U->fetch_copy($params{copyid});
128         return $evt if $evt;
129
130         my $session             = $params{session} || $U->start_db_session();
131         my $source              = $requestor->home_ou;
132         my $dest                        = $params{destination} || $copy->circ_lib;
133         my $transit             = Fieldmapper::action::transit_copy->new;
134
135         $logger->activity("User ". $requestor->id ." creating a ".
136                 " new copy transit for copy ".$copy->id." to org $dest");
137
138         $transit->source($source);
139         $transit->dest($dest);
140         $transit->target_copy($copy->id);
141         $transit->source_send_time("now");
142         $transit->copy_status($copy->status);
143         
144         $logger->debug("Creating new copy_transit in DB");
145
146         my $s = $session->request(
147                 "open-ils.storage.direct.action.transit_copy.create", $transit )->gather(1);
148         return $U->DB_UPDATE_FAILED($transit) unless $s;
149         
150         my $stat = $U->copy_status_from_name('in transit');
151
152         $copy->status($stat->id); 
153         return $evt if ($evt = $U->update_copy(
154                 copy => $copy, editor => $requestor->id, session => $session ));
155
156         $U->commit_db_session($session) unless $params{session};
157
158         return OpenILS::Event->new('SUCCESS', 
159                 payload => { copy => $copy, transit => $transit } );
160 }
161
162
163 __PACKAGE__->register_method(
164         method => 'abort_transit',
165         api_name        => 'open-ils.circ.transit.abort',
166         signature       => q/
167                 Deletes a cleans up a transit
168         /
169 );
170
171 sub abort_transit {
172         my( $self, $conn, $authtoken, $params ) = @_;
173
174         my $copyid              = $$params{copyid};
175         my $barcode             = $$params{barcode};
176         my $transitid   = $$params{transitid};
177
178         my $reqr;
179         my $copy;
180         my $transit;
181         my $holdtransit;
182         my $hold;
183         my $evt;
184
185
186         ($reqr, $evt) = $U->checksesperm($authtoken, 'ABORT_TRANSIT');
187         return $evt if $evt;
188
189         # ---------------------------------------------------------------------
190         # Find the related copy and/or transit based on whatever data we have
191         if( $barcode ) {
192                 ($copy, $evt) = $U->fetch_copy_by_barcode($barcode);
193                 return $evt if $evt;
194
195         } elsif( $copyid ) {
196                 ($copy, $evt) = $U->fetch_copy($copyid);
197                 return $evt if $evt;
198         }
199
200         if( $transitid ) {
201                 ($transit, $evt) = $U->fetch_transit($transitid);
202                 return $evt if $evt;
203
204         } else {
205                 ($transit, $evt) = $U->fetch_open_transit_by_copy($copy->id);
206                 return $evt if $evt;
207         }
208
209         if(!$copy) {
210                 ($copy, $evt) = $U->fetch_copy($transit->tartet_copy);
211                 return $evt if $evt;
212         }
213         # ---------------------------------------------------------------------
214
215
216         if( $transit->dest != $reqr->ws_ou 
217                 and $transit->source != $reqr->ws_ou ) {
218                 $evt = $U->check_perms($reqr->id, $reqr->ws_ou, 'ABORT_REMOTE_TRANIST');
219                 return $evt if $evt;
220         }
221
222         # recover the copy status
223         $copy->status( $transit->copy_status );
224         $copy->editor( $reqr->id );
225         $copy->edit_date('now');
226
227         ($holdtransit) = $U->fetch_hold_transit($transit->id);
228
229         # update / delete the objects
230         my $session = $U->start_db_session();
231
232
233         # if this is a hold transit, un-capture/un-target the hold
234         if($holdtransit) {
235                 ($hold, $evt) = $U->fetch_hold($holdtransit->hold);                     
236                 return $evt if $evt;
237                 $evt = $holdcode->_reset_hold( $reqr, $hold, $session);
238                 return $evt if $evt;
239         }
240
241         return $U->DB_UPDATE_FAILED($transit) unless 
242                 $session->request(
243                         'open-ils.storage.direct.action.transit_copy.delete', 
244                         $transit->id )->gather(1);
245
246
247         return $U->DB_UPDATE_FAILED($copy) unless 
248                 $session->request(
249                         'open-ils.storage.direct.asset.copy.update', $copy )->gather(1);
250
251         $U->commit_db_session($session);
252
253         return 1;
254 }
255
256
257 __PACKAGE__->register_method(
258         method          => 'get_open_copy_transit',
259         api_name                => 'open-ils.circ.open_copy_transit.retrieve',
260         signature       => q/
261                 Retrieves the open transit object for a given copy
262                 @param auth The login session key
263                 @param copyid The id of the copy
264                 @return Transit object
265  /
266 );
267
268 sub get_open_copy_transit {
269         my( $self, $conn, $auth, $copyid ) = @_;        
270         my $e = OpenILS::Utils::Editor->new(authtoken=>$auth);
271         return $e->event unless $e->checkauth;
272         return $e->event unless $e->allowed('VIEW_USER'); # XXX rely on editor perms
273         my $t = $e->search_action_transit_copy(
274                 { target_copy => $copyid, dest_recv_time => undef });
275         return $e->event unless @$t;
276         return $$t[0];
277 }
278
279
280         
281
282
283
284
285 1;