]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Transit.pm
added in a copy_transit_create 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 # ------------------------------------------------------------------------------
21 # If the transit destination is different than the requestor's lib,
22 # a ROUTE_TO event is returned with the org set.
23 # If 
24 # ------------------------------------------------------------------------------
25 sub transit_receive {
26         my ( $class, $copy, $requestor, $session ) = @_;
27         $U->logmark;
28
29         my( $transit, $hold_transit, $evt );
30         my $copyid = $copy->id;
31
32         my $status_name = $U->copy_status_to_name($copy->status);
33         $logger->debug("Attempting transit receive on copy $copyid. Copy status is $status_name");
34
35         # fetch the transit
36         ($transit, $evt) = $U->fetch_open_transit_by_copy($copyid);
37         return $evt if $evt;
38
39         if( $transit->dest != $requestor->home_ou ) {
40                 $logger->activity("Fowarding transit on copy which is destined ".
41                         "for a different location. copy=$copyid,current ".
42                         "location=".$requestor->home_ou.",destination location=".$transit->dest);
43
44                 return OpenILS::Event->new('ROUTE_ITEM', org => $transit->dest );
45         }
46
47         # The transit is received, set the receive time
48         $transit->dest_recv_time('now');
49         my $r = $session->request(
50                 'open-ils.storage.direct.action.transit_copy.update', $transit )->gather(1);
51         return $U->DB_UPDATE_FAILED($transit) unless $r;
52
53         # if this is a hold transit, finalize the hold transit
54         if( ($evt = _finish_hold_transit( 
55                         $session, $requestor, $copy, $transit->id )) ) {
56                 return $evt unless $U->event_equals($evt, 'SUCCESS');
57                 $evt = undef;
58         }
59         
60         $U->logmark;
61
62         #recover this copy's status from the transit
63         $copy->status( $transit->copy_status );
64         return OpenILS::Event->('SUCCESS', payload => $copy);
65
66 }
67
68
69
70 # ------------------------------------------------------------------------------
71 # If we have a hold transit, set the copy's status to 'on holds shelf',
72 # update the copy, and return the ROUTE_TO_COPY_LOATION event
73 # ------------------------------------------------------------------------------
74 sub _finish_hold_transit {
75         my( $session, $requestor, $copy, $transid ) = @_;
76         $U->logmark;
77         my ($hold_transit, $evt) = $U->fetch_hold_transit( $transid );
78         return undef unless $hold_transit;
79
80         my $s = $U->copy_status_from_name('on holds shelf');
81         $logger->info("Hold transit found: ".$hold_transit->id.". Routing to holds shelf");
82
83         $copy->status($s->id);
84         $U->update_copy( copy => $copy, 
85                 editor => $requestor->id, session => $session );
86
87         my $r = $session->request( 
88                 'open-ils.storage.direct.asset.copy.update', $copy )->gather(1);
89         return $U->DB_UPDATE_FAILED($copy) unless $r;
90
91         return OpenILS::Event->new('SUCCESS');
92 }
93
94
95 __PACKAGE__->register_method(
96         method  => "copy_transit_create",
97         api_name        => "open-ils.circ.copy_transit.create",
98         notes           => q/
99                 Creates a new copy transit.  Requestor must have the 
100                 CREATE_COPY_TRANSIT permission.
101                 @param authtoken The login session key
102                 @param params A param object containing the following keys:
103                         copyid          - the copy id
104                         destination     - the id of the org destination.  If not defined,
105                                 defaults to the copy's circ_lib
106                 @return SUCCESS event on success, other event on error
107         /);
108
109 sub copy_transit_create {
110
111         my( $self, $client, $authtoken, $params ) = @_;
112         my %params = %$params;
113
114         my( $requestor, $evt ) = 
115                 $U->checksesperm( $authtoken, 'CREATE_COPY_TRANSIT' );
116         return $evt if $evt;
117
118         my $copy;
119         ($copy,$evt) = $U->fetch_copy($params{copyid});
120         return $evt if $evt;
121
122         my $session             = $U->start_db_session();
123         my $source              = $requestor->home_ou;
124         my $dest                        = $params{destination} || $copy->circ_lib;
125         my $transit             = Fieldmapper::action::transit_copy->new;
126
127         $logger->activity("User ". $requestor->id ." creating a ".
128                 " new copy transit for copy ".$copy->id." to org $dest");
129
130         $transit->source($source);
131         $transit->dest($dest);
132         $transit->target_copy($copy->id);
133         $transit->source_send_time("now");
134         $transit->copy_status($copy->status);
135         
136         $logger->debug("Creating new copy_transit in DB");
137
138         my $s = $session->request(
139                 "open-ils.storage.direct.action.transit_copy.create", $transit )->gather(1);
140         return $U->DB_UPDATE_FAILED($transit) unless $s;
141         
142         my $stat = $U->copy_status_from_name('in transit');
143
144         $copy->status($stat->id); 
145         return $evt if ($evt = $U->update_copy(
146                 copy => $copy, editor => $requestor->id, session => $session ));
147
148         return OpenILS::Event->new('SUCCESS', 
149                 payload => { copy => $copy, transit => $transit } );
150 }
151         
152
153
154
155
156 666;