]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/NonCat.pm
5fb1dfa20b21849dad2a9f8d64b19db0011d4de8
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / NonCat.pm
1 package OpenILS::Application::Circ::NonCat;
2 use base 'OpenSRF::Application';
3 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
5 use Data::Dumper;
6 use OpenSRF::Utils::Logger qw(:logger);
7 use OpenILS::Application::AppUtils;
8 use OpenILS::Utils::Fieldmapper;
9 use OpenILS::Utils::Editor;
10 use OpenILS::Utils::CStoreEditor qw/:funcs/;
11 $Data::Dumper::Indent = 0;
12
13 my $U = "OpenILS::Application::AppUtils";
14
15
16 # returns ( $newid, $evt ).  If $evt, then there was an error
17 sub create_non_cat_circ {
18         my( $staffid, $patronid, $circ_lib, $noncat_type, $circ_time, $editor ) = @_;
19
20         my( $id, $nct, $evt );
21         $circ_time ||= 'now';
22         my $circ = Fieldmapper::action::non_cataloged_circulation->new;
23
24         $logger->activity("Creating non-cataloged circulation for ".
25                 "staff $staffid, patron $patronid, location $circ_lib, and non-cat type $noncat_type");
26
27         $circ->patron($patronid);
28         $circ->staff($staffid);
29         $circ->circ_lib($circ_lib);
30         $circ->item_type($noncat_type);
31         $circ->circ_time($circ_time);
32
33         if( $editor ) {
34                 $evt = $editor->event unless
35                         $circ = $editor->create_action_non_cataloged_circulation( $circ )
36
37         } else {
38                 $id = $U->simplereq(
39                         'open-ils.storage',
40                         'open-ils.storage.direct.action.non_cataloged_circulation.create', $circ );
41                 $evt = $U->DB_UPDATE_FAILED($circ) unless $id;
42                 $circ->id($id);
43         }
44
45         return( $circ, $evt );
46 }
47
48
49 __PACKAGE__->register_method(
50         method  => "create_noncat_type",
51         api_name        => "open-ils.circ.non_cat_type.create",
52         notes           => q/
53                 Creates a new non cataloged item type
54                 @param authtoken The login session key
55                 @param name The name of the new type
56                 @param orgId The location where the type will live
57                 @return The type object on success and the corresponding
58                 event on failure
59         /);
60
61 sub create_noncat_type {
62         my( $self, $client, $authtoken, $name, $orgId, $interval, $inhouse ) = @_;
63
64         my $e = new_editor(authtoken=>$authtoken, xact=>1);
65         return $e->die_event unless $e->checkauth;
66         return $e->die_event unless $e->allowed('CREATE_NON_CAT_TYPE', $orgId);
67
68         # grab all of "my" non-cat types and see if one with 
69         # the requested name already exists
70         my $types = retrieve_noncat_types_all($self, $client, $orgId);
71         for(@$types) {
72                 if( $_->name eq $name ) {
73                         $e->rollback;
74                         return OpenILS::Event->new('NON_CAT_TYPE_EXISTS', payload => $name);
75                 }
76         }
77
78         my $type = Fieldmapper::config::non_cataloged_type->new;
79         $type->name($name);
80         $type->owning_lib($orgId);
81         $type->circ_duration($interval);
82         $type->in_house( ($inhouse) ? 't' : 'f' );
83
84         $e->create_config_non_cataloged_type($type) or return $e->die_event;
85         $e->commit;
86         return $type;
87 }
88
89
90
91 __PACKAGE__->register_method(
92         method  => "update_noncat_type",
93         api_name        => "open-ils.circ.non_cat_type.update",
94         notes           => q/
95                 Updates a non-cataloged type object
96                 @param authtoken The login session key
97                 @param type The updated type object
98                 @return The result of the DB update call unless a preceeding event occurs, 
99                         in which case the event will be returned
100         /);
101
102 sub update_noncat_type {
103         my( $self, $client, $authtoken, $type ) = @_;
104         my $e = new_editor(xact=>1, authtoken=>$authtoken);
105         return $e->die_event unless $e->checkauth;
106
107         my $otype = $e->retrieve_config_non_cataloged_type($type->id) 
108                 or return $e->die_event;
109
110         return $e->die_event unless 
111                 $e->allowed('UPDATE_NON_CAT_TYPE', $otype->owning_lib);
112
113         $type->owning_lib($otype->owning_lib); # do not allow them to "move" the object
114
115         $e->update_config_non_cataloged_type($type) or return $e->die_event;
116         $e->commit;
117         return 1;
118 }
119
120 __PACKAGE__->register_method(
121         method  => "retrieve_noncat_types_all",
122         api_name        => "open-ils.circ.non_cat_types.retrieve.all",
123         notes           => q/
124                 Retrieves the non-cat types at the requested location as well
125                 as those above and below the requested location in the org tree
126                 @param orgId The base location at which to retrieve the type objects
127                 @param depth Optional parameter to limit the depth of the tree
128                 @return An array of non cat type objects or an event if an error occurs
129         /);
130
131 sub retrieve_noncat_types_all {
132         my( $self, $client, $orgId, $depth ) = @_;
133         my $meth = 'open-ils.storage.ranged.config.non_cataloged_type.retrieve.atomic';
134         my $svc = 'open-ils.storage';
135         return $U->simplereq($svc, $meth, $orgId, $depth) if defined($depth);
136         return $U->simplereq($svc, $meth, $orgId);
137 }
138
139
140
141 __PACKAGE__->register_method(
142         method          => 'fetch_noncat',
143         api_name                => 'open-ils.circ.non_cataloged_circulation.retrieve',
144         signature       => q/
145         /
146 );
147
148 sub fetch_noncat {
149         my( $self, $conn, $auth, $circid ) = @_;
150         my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
151         return $e->event unless $e->checkauth;
152         my $c = $e->retrieve_action_non_cataloged_circulation($circid)
153                 or return $e->event;
154         if( $c->patron ne $e->requestor->id ) {
155                 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # XXX rely on editor perm
156         }
157         return $c;
158 }
159
160
161
162 __PACKAGE__->register_method(
163         method => 'fetch_open_noncats',
164     authoritative => 1,
165         api_name        => 'open-ils.circ.open_non_cataloged_circulation.user',
166         signature => q/
167                 Returns an id-list of non-cataloged circulations that are considered
168                 open as of now.  a circ is open if circ time + circ duration 
169                 (based on type) is > than now.
170                 @param auth auth key
171                 @param userid user to retrieve non-cat circs for 
172                         defaults to the session user
173         /
174 );
175
176 sub fetch_open_noncats {
177         my( $self, $conn, $auth, $userid ) = @_;
178         my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
179         return $e->event unless $e->checkauth;
180         $userid ||= $e->requestor->id;
181         if( $e->requestor->id ne $userid ) {
182                 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # XXX rely on editor perm
183         }
184         return $e->request(
185                 'open-ils.storage.action.open_non_cataloged_circulation.user', $userid );
186 }
187
188
189 __PACKAGE__->register_method(
190         method  => 'delete_noncat',
191         api_name        => 'open-ils.circ.non_cataloged_type.delete',
192 );
193 sub delete_noncat {
194         my( $self, $conn, $auth, $typeid ) = @_;
195         my $e = new_editor(xact=>1, authtoken => $auth);
196         return $e->die_event unless $e->checkauth;
197
198         my $nc = $e->retrieve_config_non_cataloged_type($typeid)
199                 or return $e->die_event;
200
201         $e->allowed('DELETE_NON_CAT_TYPE', $nc->owning_lib) # XXX rely on editor perm
202                 or return $e->die_event;
203
204         #       XXX Add checks to see if this type is in use by a transaction
205
206         $e->delete_config_non_cataloged_type($nc) or return $e->die_event;
207         $e->commit;
208         return 1;
209 }
210
211
212
213 1;