1 package OpenILS::Application::Circ::NonCat;
2 use base 'OpenSRF::Application';
3 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
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;
13 my $U = "OpenILS::Application::AppUtils";
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 ) = @_;
20 my( $id, $nct, $evt );
22 my $circ = Fieldmapper::action::non_cataloged_circulation->new;
24 $logger->activity("Creating non-cataloged circulation for ".
25 "staff $staffid, patron $patronid, location $circ_lib, and non-cat type $noncat_type");
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);
34 $evt = $editor->event unless
35 $circ = $editor->create_action_non_cataloged_circulation( $circ )
40 'open-ils.storage.direct.action.non_cataloged_circulation.create', $circ );
41 $evt = $U->DB_UPDATE_FAILED($circ) unless $id;
45 return( $circ, $evt );
49 __PACKAGE__->register_method(
50 method => "create_noncat_type",
51 api_name => "open-ils.circ.non_cat_type.create",
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
61 sub __create_noncat_type {
62 my( $self, $client, $authtoken, $name, $orgId, $interval, $inhouse ) = @_;
63 my( $staff, $evt ) = $U->checkses($authtoken);
66 # grab all of "my" non-cat types and see if one with
67 # the requested name already exists
68 my $types = $self->retrieve_noncat_types_all($client, $orgId);
70 if( $_->name eq $name ) {
71 return OpenILS::Event->new('NON_CAT_TYPE_EXISTS', payload => $name);
75 $evt = $U->check_perms( $staff->id, $orgId, 'CREATE_NON_CAT_TYPE' );
78 my $type = Fieldmapper::config::non_cataloged_type->new;
80 $type->owning_lib($orgId);
81 $type->circ_duration($interval);
82 $type->in_house( ($inhouse) ? 't' : 'f' );
84 my $id = $U->simplereq(
86 'open-ils.storage.direct.config.non_cataloged_type.create', $type );
88 return $U->DB_UPDATE_FAILED($type) unless $id;
94 sub create_noncat_type {
95 my( $self, $client, $authtoken, $name, $orgId, $interval, $inhouse ) = @_;
97 my $e = new_editor(authtoken=>$authtoken, xact=>1);
98 return $e->die_event unless $e->checkauth;
99 return $e->die_event unless $e->allowed('CREATE_NON_CAT_TYPE', $orgId);
101 # grab all of "my" non-cat types and see if one with
102 # the requested name already exists
103 my $types = $self->retrieve_noncat_types_all($client, $orgId);
105 if( $_->name eq $name ) {
107 return OpenILS::Event->new('NON_CAT_TYPE_EXISTS', payload => $name);
111 my $type = Fieldmapper::config::non_cataloged_type->new;
113 $type->owning_lib($orgId);
114 $type->circ_duration($interval);
115 $type->in_house( ($inhouse) ? 't' : 'f' );
117 $e->create_config_non_cataloged_type($type) or return $e->die_event;
124 __PACKAGE__->register_method(
125 method => "update_noncat_type",
126 api_name => "open-ils.circ.non_cat_type.update",
128 Updates a non-cataloged type object
129 @param authtoken The login session key
130 @param type The updated type object
131 @return The result of the DB update call unless a preceeding event occurs,
132 in which case the event will be returned
135 sub update_noncat_type {
136 my( $self, $client, $authtoken, $type ) = @_;
137 my $e = new_editor(xact=>1, authtoken=>$authtoken);
138 return $e->die_event unless $e->checkauth;
140 my $otype = $e->retrieve_config_non_cataloged_type($type->id)
141 or return $e->die_event;
143 return $e->die_event unless
144 $e->allowed('UPDATE_NON_CAT_TYPE', $otype->owning_lib);
146 $type->owning_lib($otype->owning_lib); # do not allow them to "move" the object
148 $e->update_config_non_cataloged_type($type) or return $e->die_event;
153 __PACKAGE__->register_method(
154 method => "retrieve_noncat_types_all",
155 api_name => "open-ils.circ.non_cat_types.retrieve.all",
157 Retrieves the non-cat types at the requested location as well
158 as those above and below the requested location in the org tree
159 @param orgId The base location at which to retrieve the type objects
160 @param depth Optional parameter to limit the depth of the tree
161 @return An array of non cat type objects or an event if an error occurs
164 sub retrieve_noncat_types_all {
165 my( $self, $client, $orgId, $depth ) = @_;
166 my $meth = 'open-ils.storage.ranged.config.non_cataloged_type.retrieve.atomic';
167 my $svc = 'open-ils.storage';
168 return $U->simplereq($svc, $meth, $orgId, $depth) if defined($depth);
169 return $U->simplereq($svc, $meth, $orgId);
174 __PACKAGE__->register_method(
175 method => 'fetch_noncat',
176 api_name => 'open-ils.circ.non_cataloged_circulation.retrieve',
182 my( $self, $conn, $auth, $circid ) = @_;
183 my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
184 return $e->event unless $e->checkauth;
185 my $c = $e->retrieve_action_non_cataloged_circulation($circid)
187 if( $c->patron ne $e->requestor->id ) {
188 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # XXX rely on editor perm
195 __PACKAGE__->register_method(
196 method => 'fetch_open_noncats',
197 api_name => 'open-ils.circ.open_non_cataloged_circulation.user',
199 Returns an id-list of non-cataloged circulations that are considered
200 open as of now. a circ is open if circ time + circ duration
201 (based on type) is > than now.
203 @param userid user to retrieve non-cat circs for
204 defaults to the session user
208 sub fetch_open_noncats {
209 my( $self, $conn, $auth, $userid ) = @_;
210 my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
211 return $e->event unless $e->checkauth;
212 $userid ||= $e->requestor->id;
213 if( $e->requestor->id ne $userid ) {
214 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # XXX rely on editor perm
217 'open-ils.storage.action.open_non_cataloged_circulation.user', $userid );
221 __PACKAGE__->register_method(
222 method => 'delete_noncat',
223 api_name => 'open-ils.circ.non_cataloged_type.delete',
226 my( $self, $conn, $auth, $typeid ) = @_;
227 my $e = new_editor(xact=>1, authtoken => $auth);
228 return $e->die_event unless $e->checkauth;
230 my $nc = $e->retrieve_config_non_cataloged_type($typeid)
231 or return $e->die_event;
233 $e->allowed('DELETE_NON_CAT_TYPE', $nc->owning_lib) # XXX rely on editor perm
234 or return $e->die_event;
236 # XXX Add checks to see if this type is in use by a transaction
238 $e->delete_config_non_cataloged_type($nc) or return $e->die_event;