1 package OpenILS::Application::Circ::NonCat;
2 use base 'OpenILS::Application';
3 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
7 use DateTime::Format::ISO8601;
8 use OpenSRF::Utils qw/:datetime/;
9 use OpenSRF::Utils::Logger qw(:logger);
10 use OpenILS::Application::AppUtils;
11 use OpenILS::Utils::Fieldmapper;
12 use OpenILS::Utils::Editor;
13 use OpenILS::Utils::CStoreEditor qw/:funcs/;
14 $Data::Dumper::Indent = 0;
16 my $U = "OpenILS::Application::AppUtils";
17 my $_dt_parser = DateTime::Format::ISO8601->new;
20 # returns ( $newid, $evt ). If $evt, then there was an error
21 sub create_non_cat_circ {
22 my( $staffid, $patronid, $circ_lib, $noncat_type, $circ_time, $editor ) = @_;
24 my( $id, $nct, $evt );
26 my $circ = Fieldmapper::action::non_cataloged_circulation->new;
28 $logger->activity("Creating non-cataloged circulation for ".
29 "staff $staffid, patron $patronid, location $circ_lib, and non-cat type $noncat_type");
31 $circ->patron($patronid);
32 $circ->staff($staffid);
33 $circ->circ_lib($circ_lib);
34 $circ->item_type($noncat_type);
35 $circ->circ_time($circ_time);
38 $evt = $editor->event unless
39 $circ = $editor->create_action_non_cataloged_circulation( $circ )
45 'open-ils.storage.direct.action.non_cataloged_circulation.create', $circ );
46 $evt = $U->DB_UPDATE_FAILED($circ) unless $id;
51 my $e = ($editor) ? $editor : new_editor();
52 $circ = noncat_due_date($e, $circ);
55 return( $circ, $evt );
59 __PACKAGE__->register_method(
60 method => "create_noncat_type",
61 api_name => "open-ils.circ.non_cat_type.create",
63 Creates a new non cataloged item type
64 @param authtoken The login session key
65 @param name The name of the new type
66 @param orgId The location where the type will live
67 @return The type object on success and the corresponding
71 sub create_noncat_type {
72 my( $self, $client, $authtoken, $name, $orgId, $interval, $inhouse ) = @_;
74 my $e = new_editor(authtoken=>$authtoken, xact=>1);
75 return $e->die_event unless $e->checkauth;
76 return $e->die_event unless $e->allowed('CREATE_NON_CAT_TYPE', $orgId);
78 # grab all of "my" non-cat types and see if one with
79 # the requested name already exists
80 my $types = retrieve_noncat_types_all($self, $client, $orgId);
82 if( $_->name eq $name ) {
84 return OpenILS::Event->new('NON_CAT_TYPE_EXISTS', payload => $name);
88 my $type = Fieldmapper::config::non_cataloged_type->new;
90 $type->owning_lib($orgId);
91 $type->circ_duration($interval);
92 $type->in_house( ($inhouse) ? 't' : 'f' );
94 $e->create_config_non_cataloged_type($type) or return $e->die_event;
101 __PACKAGE__->register_method(
102 method => "update_noncat_type",
103 api_name => "open-ils.circ.non_cat_type.update",
105 Updates a non-cataloged type object
106 @param authtoken The login session key
107 @param type The updated type object
108 @return The result of the DB update call unless a preceeding event occurs,
109 in which case the event will be returned
112 sub update_noncat_type {
113 my( $self, $client, $authtoken, $type ) = @_;
114 my $e = new_editor(xact=>1, authtoken=>$authtoken);
115 return $e->die_event unless $e->checkauth;
117 my $otype = $e->retrieve_config_non_cataloged_type($type->id)
118 or return $e->die_event;
120 return $e->die_event unless
121 $e->allowed('UPDATE_NON_CAT_TYPE', $otype->owning_lib);
123 $type->owning_lib($otype->owning_lib); # do not allow them to "move" the object
125 $e->update_config_non_cataloged_type($type) or return $e->die_event;
130 __PACKAGE__->register_method(
131 method => "retrieve_noncat_types_all",
132 api_name => "open-ils.circ.non_cat_types.retrieve.all",
134 Retrieves the non-cat types at the requested location as well
135 as those above and below the requested location in the org tree
136 @param orgId The base location at which to retrieve the type objects
137 @param depth Optional parameter to limit the depth of the tree
138 @return An array of non cat type objects or an event if an error occurs
141 sub retrieve_noncat_types_all {
142 my( $self, $client, $orgId, $depth ) = @_;
143 my $meth = 'open-ils.storage.ranged.config.non_cataloged_type.retrieve.atomic';
144 my $svc = 'open-ils.storage';
145 return $U->simplereq($svc, $meth, $orgId, $depth) if defined($depth);
146 return $U->simplereq($svc, $meth, $orgId);
151 __PACKAGE__->register_method(
152 method => 'fetch_noncat',
153 api_name => 'open-ils.circ.non_cataloged_circulation.retrieve',
160 my( $self, $conn, $auth, $circid ) = @_;
161 my $e = new_editor( authtoken => $auth );
162 return $e->event unless $e->checkauth;
163 my $c = $e->retrieve_action_non_cataloged_circulation($circid)
165 if( $c->patron ne $e->requestor->id ) {
166 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # XXX rely on editor perm
168 return noncat_due_date($e, $c);
171 sub noncat_due_date {
174 my $otype = $e->retrieve_config_non_cataloged_type($circ->item_type)
175 or return $e->die_event;
177 my $duedate = $_dt_parser->parse_datetime( clense_ISO8601($circ->circ_time) );
179 ->add( seconds => interval_to_seconds($otype->circ_duration) )
180 ->strftime('%FT%T%z');
182 my $offset = $U->storagereq(
183 'open-ils.storage.actor.org_unit.closed_date.overlap',
188 $duedate = $offset->{end} if ($offset);
189 $circ->duedate($duedate);
196 __PACKAGE__->register_method(
197 method => 'fetch_open_noncats',
199 api_name => 'open-ils.circ.open_non_cataloged_circulation.user',
201 Returns an id-list of non-cataloged circulations that are considered
202 open as of now. a circ is open if circ time + circ duration
203 (based on type) is > than now.
205 @param userid user to retrieve non-cat circs for
206 defaults to the session user
210 sub fetch_open_noncats {
211 my( $self, $conn, $auth, $userid ) = @_;
212 my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
213 return $e->event unless $e->checkauth;
214 $userid ||= $e->requestor->id;
215 if( $e->requestor->id ne $userid ) {
216 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # XXX rely on editor perm
219 'open-ils.storage.action.open_non_cataloged_circulation.user', $userid );
223 __PACKAGE__->register_method(
224 method => 'delete_noncat',
225 api_name => 'open-ils.circ.non_cataloged_type.delete',
228 my( $self, $conn, $auth, $typeid ) = @_;
229 my $e = new_editor(xact=>1, authtoken => $auth);
230 return $e->die_event unless $e->checkauth;
232 my $nc = $e->retrieve_config_non_cataloged_type($typeid)
233 or return $e->die_event;
235 $e->allowed('DELETE_NON_CAT_TYPE', $nc->owning_lib) # XXX rely on editor perm
236 or return $e->die_event;
238 # XXX Add checks to see if this type is in use by a transaction
240 $e->delete_config_non_cataloged_type($nc) or return $e->die_event;