fixed debuging code ...
[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.usr.atomic", $user_id );
49         $circs = $circs->gather(1);
50
51         my @results;
52         for my $circ (@$circs) {
53
54                 my $copy = $session->request(
55                         "open-ils.storage.direct.asset.copy.retrieve",
56                         $circ->target_copy );
57
58                 warn "Retrieving record for copy " . $circ->target_copy . "\n";
59
60                 my $record = $session->request(
61                         "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
62                         $circ->target_copy );
63
64                 $copy = $copy->gather(1);
65                 $record = $record->gather(1);
66
67                 use Data::Dumper;
68                 warn Dumper $circ;
69                 my $u = OpenILS::Utils::ModsParser->new();
70                 $u->start_mods_batch( $record->marc() );
71                 my $mods = $u->finish_mods_batch();
72                 $mods->doc_id($record->id());
73
74                 push( @results, { copy => $copy, circ => $circ, record => $mods } );
75         }
76
77         return \@results;
78
79 }
80
81
82 __PACKAGE__->register_method(
83         method  => "title_from_transaction",
84         api_name        => "open-ils.circ.circ_transaction.find_title",
85         NOTES           => <<"  NOTES");
86         Returns a mods object for the title that is linked to from the 
87         copy from the hold that created the given transaction
88         NOTES
89
90 sub title_from_transaction {
91
92         my( $self, $client, $login_session, $transactionid ) = @_;
93         my $user = $apputils->check_user_session($login_session); 
94         my $session = OpenSRF::AppSession->create('open-ils.storage');
95
96         my $circ = $session->request(
97                 "open-ils.storage.direct.action.circulation.retrieve", $transactionid )->gather(1);
98
99         if($circ) {
100                 my $title = $session->request(
101                         "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
102                         $circ->target_copy )->gather(1);
103
104                 if($title) {
105                         my $u = OpenILS::Utils::ModsParser->new();
106                         $u->start_mods_batch( $title->marc );
107                         return $u->finish_mods_batch();
108                 }
109         }
110
111         return undef;   
112 }
113
114
115 __PACKAGE__->register_method(
116         method  => "set_circ_lost",
117         api_name        => "open-ils.circ.circulation.set_lost",
118         NOTES           => <<"  NOTES");
119         Params are login, circid
120         login must have SET_CIRC_LOST perms
121         Sets a circulation to lost
122         NOTES
123
124 __PACKAGE__->register_method(
125         method  => "set_circ_lost",
126         api_name        => "open-ils.circ.circulation.set_claims_returned",
127         NOTES           => <<"  NOTES");
128         Params are login, circid
129         login must have SET_CIRC_MISSING perms
130         Sets a circulation to lost
131         NOTES
132
133 sub set_circ_lost {
134         my( $self, $client, $login, $circid ) = @_;
135
136         my $user = $apputils->check_user_session($login); 
137         my $session = OpenSRF::AppSession->create('open-ils.storage');
138         my $circ = $session->request(
139                 "open-ils.storage.direct.action.circulation.retrieve", $circid )->gather(1);
140
141         if(!$circ) { throw OpenSRF::EX::ERROR ("No circulation exists with id $circid"); }
142
143         if($self->api_name =~ /lost/) {
144                 if($apputils->check_user_perms($user->id, $circ->circ_lib, "SET_CIRC_LOST")) {
145                         return OpenILS::Perm->new("SET_CIRC_LOST");
146                 }
147                 $circ->stop_fines("LOST");              
148         }
149
150         if($self->api_name =~ /claims_returned/) {
151                 if($apputils->check_user_perms($user->id, $circ->circ_lib, "SET_CIRC_CLAIMS_RETURNED")) {
152                         return OpenILS::Perm->new("SET_CIRC_CLAIMS_RETURNED");
153                 }
154                 $circ->stop_fines("CLAIMSRETURNED");
155         }
156
157
158         my $s = $session->request(
159                 "open-ils.storage.direct.action.circulation.update", $circ )->gather(1);
160
161         if(!$s) { throw OpenSRF::EX::ERROR ("Error updating circulation with id $circid"); }
162
163 }
164
165
166
167
168
169
170 1;