]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ.pm
afa19c86ea03803447f2a44c8374fa51c8f5d222
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ.pm
1 package OpenILS::Application::Circ;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4
5 use OpenILS::Application::Circ::Circulate;
6 use OpenILS::Application::Circ::Rules;
7 use OpenILS::Application::Circ::Survey;
8 use OpenILS::Application::Circ::StatCat;
9 use OpenILS::Application::Circ::Holds;
10 use OpenILS::Application::Circ::Money;
11 use OpenILS::Application::Circ::NonCat;
12
13 use OpenILS::Application::AppUtils;
14 my $apputils = "OpenILS::Application::AppUtils";
15 my $U = $apputils;
16 use OpenSRF::Utils;
17 use OpenILS::Utils::ModsParser;
18 use OpenILS::Event;
19 use OpenSRF::EX qw(:try);
20 use OpenSRF::Utils::Logger qw(:logger);
21 #my $logger = "OpenSRF::Utils::Logger";
22
23
24 # ------------------------------------------------------------------------
25 # Top level Circ package;
26 # ------------------------------------------------------------------------
27
28 sub initialize {
29         my $self = shift;
30         OpenILS::Application::Circ::Rules->initialize();
31         OpenILS::Application::Circ::Circulate->initialize();
32 }
33
34
35 # ------------------------------------------------------------------------
36 # Returns an array of {circ, record} hashes checked out by the user.
37 # ------------------------------------------------------------------------
38 __PACKAGE__->register_method(
39         method  => "checkouts_by_user",
40         api_name        => "open-ils.circ.actor.user.checked_out",
41         NOTES           => <<"  NOTES");
42         Returns a list of open circulations as a pile of objects.  each object
43         contains the relevant copy, circ, and record
44         NOTES
45
46 sub checkouts_by_user {
47         my( $self, $client, $user_session, $user_id ) = @_;
48
49         my( $requestor, $target, $copy, $record, $evt );
50
51         ( $requestor, $target, $evt ) = 
52                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
53         return $evt if $evt;
54
55         my $circs = $apputils->simplereq(
56                 'open-ils.storage',
57                 "open-ils.storage.direct.action.open_circulation.search.atomic", 
58 #               { usr => $target->id, xact_finish => undef } );
59                 { usr => $target->id } );
60
61         my @results;
62         for my $circ (@$circs) {
63
64                 ( $copy, $evt )  = $apputils->fetch_copy($circ->target_copy);
65                 return $evt if $evt;
66
67                 $logger->debug("Retrieving record for copy " . $circ->target_copy);
68
69                 ($record, $evt) = $apputils->fetch_record_by_copy( $circ->target_copy );
70                 return $evt if $evt;
71
72                 my $mods = $apputils->record_to_mvr($record);
73
74                 push( @results, { copy => $copy, circ => $circ, record => $mods } );
75         }
76
77         return \@results;
78
79 }
80
81
82
83 __PACKAGE__->register_method(
84         method  => "checkouts_by_user_slim",
85         api_name        => "open-ils.circ.actor.user.checked_out.slim",
86         NOTES           => <<"  NOTES");
87         Returns a list of open circulation objects
88         NOTES
89
90 sub checkouts_by_user_slim {
91         my( $self, $client, $user_session, $user_id ) = @_;
92
93         my( $requestor, $target, $copy, $record, $evt );
94
95         ( $requestor, $target, $evt ) = 
96                 $apputils->checkses_requestor( $user_session, $user_id, 'VIEW_CIRCULATIONS');
97         return $evt if $evt;
98
99         $logger->debug( 'User ' . $requestor->id . 
100                 " retrieving checked out items for user " . $target->id );
101
102         return $apputils->simplereq(
103                 'open-ils.storage',
104                 "open-ils.storage.direct.action.open_circulation.search.atomic", 
105 #               { usr => $target->id, xact_finish => undef } );
106                 { usr => $target->id } );
107 }
108
109
110
111
112 __PACKAGE__->register_method(
113         method  => "title_from_transaction",
114         api_name        => "open-ils.circ.circ_transaction.find_title",
115         NOTES           => <<"  NOTES");
116         Returns a mods object for the title that is linked to from the 
117         copy from the hold that created the given transaction
118         NOTES
119
120 sub title_from_transaction {
121         my( $self, $client, $login_session, $transactionid ) = @_;
122
123         my( $user, $circ, $title, $evt );
124
125         ( $user, $evt ) = $apputils->checkses( $login_session );
126         return $evt if $evt;
127
128         ( $circ, $evt ) = $apputils->fetch_circulation($transactionid);
129         return $evt if $evt;
130         
131         ($title, $evt) = $apputils->fetch_record_by_copy($circ->target_copy);
132         return $evt if $evt;
133
134         return $apputils->record_to_mvr($title);
135 }
136
137
138 __PACKAGE__->register_method(
139         method  => "set_circ_lost",
140         api_name        => "open-ils.circ.circulation.set_lost",
141         NOTES           => <<"  NOTES");
142         Params are login, circid
143         login must have SET_CIRC_LOST perms
144         Sets a circulation to lost
145         NOTES
146
147 __PACKAGE__->register_method(
148         method  => "set_circ_lost",
149         api_name        => "open-ils.circ.circulation.set_claims_returned",
150         NOTES           => <<"  NOTES");
151         Params are login, circid
152         login must have SET_CIRC_MISSING perms
153         Sets a circulation to lost
154         NOTES
155
156 sub set_circ_lost {
157         my( $self, $client, $login, $circid ) = @_;
158         my( $user, $circ, $evt );
159
160         ( $user, $evt ) = $apputils->checkses($login);
161         return $evt if $evt;
162
163         ( $circ, $evt ) = $apputils->fetch_circulation( $circid );
164         return $evt if $evt;
165
166         if($self->api_name =~ /lost/) {
167                 if($evt = $apputils->checkperms(
168                         $user->id, $circ->circ_lib, "SET_CIRC_LOST")) {
169                         return $evt;
170                 }
171                 $circ->stop_fines("LOST");              
172         }
173
174         # XXX Back date the checkin time so the patron has no fines
175         if($self->api_name =~ /claims_returned/) {
176                 if($evt = $apputils->checkperms(
177                         $user->id, $circ->circ_lib, "SET_CIRC_CLAIMS_RETURNED")) {
178                         return $evt;
179                 }
180                 $circ->stop_fines("CLAIMSRETURNED");
181         }
182
183         my $s = $apputils->simplereq(
184                 'open-ils.storage',
185                 "open-ils.storage.direct.action.circulation.update", $circ );
186
187         if(!$s) { throw OpenSRF::EX::ERROR ("Error updating circulation with id $circid"); }
188 }
189
190 __PACKAGE__->register_method(
191         method          => "create_in_house_use",
192         api_name                => 'open-ils.circ.in_house_use.create',
193         signature       =>      q/
194                 Creates an in-house use action.
195                 @param $authtoken The login session key
196                 @param params A hash of params including
197                         'location' The org unit id where the in-house use occurs
198                         'copyid' The copy in question
199                         'count' The number of in-house uses to apply to this copy
200                 @return An array of id's representing the id's of the newly created
201                 in-house use objects or an event on an error
202         /);
203
204 sub create_in_house_use {
205         my( $self, $client, $authtoken, $params ) = @_;
206
207         my( $staff, $evt, $copy );
208         my $org         = $params->{location};
209         my $copyid      = $params->{copyid};
210         my $count       = $params->{count} || 1;
211
212         ($staff, $evt) = $U->checkses($authtoken);
213         return $evt if $evt;
214
215         ($copy, $evt) = $U->fetch_copy($copyid);
216         return $evt if $evt;
217
218         $evt = $U->check_perms($staff->id, $org, 'CREATE_IN_HOUSE_USE');
219         return $evt if $evt;
220
221         $logger->activity("User " . $staff->id .
222                 " creating $count in-house use(s) for copy $copyid at location $org");
223
224         my @ids;
225         for(1..$count) {
226                 my $ihu = Fieldmapper::action::in_house_use->new;
227
228                 $ihu->item($copyid);
229                 $ihu->staff($staff->id);
230                 $ihu->org_unit($org);
231                 $ihu->use_time('now');
232
233                 my $id = $U->simplereq(
234                         'open-ils.storage',
235                         'open-ils.storage.direct.action.in_house_use.create', $ihu );
236
237                 return $U->DB_UPDATE_FAILED($ihu) unless $id;
238                 push @ids, $id;
239         }
240
241         return \@ids;
242 }
243
244
245
246
247
248
249 1;