]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ.pm
circ.checkedout only returns items that are actually checked out
[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::Rules;
6 use OpenILS::Application::Circ::Survey;
7 use OpenILS::Application::Circ::StatCat;
8 use OpenILS::Application::Circ::Holds;
9 use OpenILS::Application::Circ::Money;
10
11 use OpenILS::Application::AppUtils;
12 my $apputils = "OpenILS::Application::AppUtils";
13 use OpenSRF::Utils;
14 use OpenILS::Utils::ModsParser;
15
16
17 # ------------------------------------------------------------------------
18 # Top level Circ package;
19 # ------------------------------------------------------------------------
20
21 sub initialize {
22         my $self = shift;
23         OpenILS::Application::Circ::Rules->initialize();
24 }
25
26
27
28 # ------------------------------------------------------------------------
29 # Returns an array of {circ, record} hashes checked out by the user.
30 # ------------------------------------------------------------------------
31 __PACKAGE__->register_method(
32         method  => "checkouts_by_user",
33         api_name        => "open-ils.circ.actor.user.checked_out",
34         NOTES           => <<"  NOTES");
35         Returns a list of open circulations as a pile of objects.  each object
36         contains the relevant copy, circ, and record
37         NOTES
38
39 sub checkouts_by_user {
40         my( $self, $client, $user_session, $user_id ) = @_;
41
42         my $session = OpenSRF::AppSession->create("open-ils.storage");
43         my $user_obj = $apputils->check_user_session($user_session); 
44
45         if(!$user_id) { $user_id = $user_obj->id(); }
46
47         my $circs = $session->request(
48                 "open-ils.storage.direct.action.open_circulation.search.atomic", 
49                 { usr => $user_id, stop_fines => undef } );
50         $circs = $circs->gather(1);
51
52         my @results;
53         for my $circ (@$circs) {
54
55                 my $copy = $session->request(
56                         "open-ils.storage.direct.asset.copy.retrieve",
57                         $circ->target_copy );
58
59                 warn "Retrieving record for copy " . $circ->target_copy . "\n";
60
61                 my $record = $session->request(
62                         "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
63                         $circ->target_copy );
64
65                 $copy = $copy->gather(1);
66                 $record = $record->gather(1);
67
68                 use Data::Dumper;
69                 warn Dumper $circ;
70                 my $u = OpenILS::Utils::ModsParser->new();
71                 $u->start_mods_batch( $record->marc() );
72                 my $mods = $u->finish_mods_batch();
73                 $mods->doc_id($record->id());
74
75                 push( @results, { copy => $copy, circ => $circ, record => $mods } );
76         }
77
78         return \@results;
79
80 }
81
82
83 __PACKAGE__->register_method(
84         method  => "title_from_transaction",
85         api_name        => "open-ils.circ.circ_transaction.find_title",
86         NOTES           => <<"  NOTES");
87         Returns a mods object for the title that is linked to from the 
88         copy from the hold that created the given transaction
89         NOTES
90
91 sub title_from_transaction {
92
93         my( $self, $client, $login_session, $transactionid ) = @_;
94         my $user = $apputils->check_user_session($login_session); 
95         my $session = OpenSRF::AppSession->create('open-ils.storage');
96
97         my $circ = $session->request(
98                 "open-ils.storage.direct.action.circulation.retrieve", $transactionid )->gather(1);
99
100         if($circ) {
101                 my $title = $session->request(
102                         "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
103                         $circ->target_copy )->gather(1);
104
105                 if($title) {
106                         my $u = OpenILS::Utils::ModsParser->new();
107                         $u->start_mods_batch( $title->marc );
108                         return $u->finish_mods_batch();
109                 }
110         }
111
112         return undef;   
113 }
114
115
116 __PACKAGE__->register_method(
117         method  => "set_circ_lost",
118         api_name        => "open-ils.circ.circulation.set_lost",
119         NOTES           => <<"  NOTES");
120         Params are login, circid
121         login must have SET_CIRC_LOST perms
122         Sets a circulation to lost
123         NOTES
124
125 __PACKAGE__->register_method(
126         method  => "set_circ_lost",
127         api_name        => "open-ils.circ.circulation.set_claims_returned",
128         NOTES           => <<"  NOTES");
129         Params are login, circid
130         login must have SET_CIRC_MISSING perms
131         Sets a circulation to lost
132         NOTES
133
134 sub set_circ_lost {
135         my( $self, $client, $login, $circid ) = @_;
136
137         my $user = $apputils->check_user_session($login); 
138         my $session = OpenSRF::AppSession->create('open-ils.storage');
139         my $circ = $session->request(
140                 "open-ils.storage.direct.action.circulation.retrieve", $circid )->gather(1);
141
142         if(!$circ) { throw OpenSRF::EX::ERROR ("No circulation exists with id $circid"); }
143
144         if($self->api_name =~ /lost/) {
145                 if($apputils->check_user_perms($user->id, $circ->circ_lib, "SET_CIRC_LOST")) {
146                         return OpenILS::Perm->new("SET_CIRC_LOST");
147                 }
148                 $circ->stop_fines("LOST");              
149         }
150
151         if($self->api_name =~ /claims_returned/) {
152                 if($apputils->check_user_perms($user->id, $circ->circ_lib, "SET_CIRC_CLAIMS_RETURNED")) {
153                         return OpenILS::Perm->new("SET_CIRC_CLAIMS_RETURNED");
154                 }
155                 $circ->stop_fines("CLAIMSRETURNED");
156         }
157
158
159         my $s = $session->request(
160                 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
161
162         if(!$s) { throw OpenSRF::EX::ERROR ("Error updating circulation with id $circid"); }
163
164 }
165
166
167
168
169
170
171 1;