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
72 sub create_noncat_type {
73 my( $self, $client, $authtoken, $name, $orgId, $interval, $inhouse ) = @_;
75 my $e = new_editor(authtoken=>$authtoken, xact=>1);
76 return $e->die_event unless $e->checkauth;
77 return $e->die_event unless $e->allowed('CREATE_NON_CAT_TYPE', $orgId);
79 # grab all of "my" non-cat types and see if one with
80 # the requested name already exists
81 my $types = retrieve_noncat_types_all($self, $client, $orgId);
83 if( $_->name eq $name ) {
85 return OpenILS::Event->new('NON_CAT_TYPE_EXISTS', payload => $name);
89 my $type = Fieldmapper::config::non_cataloged_type->new;
91 $type->owning_lib($orgId);
92 $type->circ_duration($interval);
93 $type->in_house( ($inhouse) ? 't' : 'f' );
95 $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
113 sub update_noncat_type {
114 my( $self, $client, $authtoken, $type ) = @_;
115 my $e = new_editor(xact=>1, authtoken=>$authtoken);
116 return $e->die_event unless $e->checkauth;
118 my $otype = $e->retrieve_config_non_cataloged_type($type->id)
119 or return $e->die_event;
121 return $e->die_event unless
122 $e->allowed('UPDATE_NON_CAT_TYPE', $otype->owning_lib);
124 $type->owning_lib($otype->owning_lib); # do not allow them to "move" the object
126 $e->update_config_non_cataloged_type($type) or return $e->die_event;
131 __PACKAGE__->register_method(
132 method => "retrieve_noncat_types_all",
133 api_name => "open-ils.circ.non_cat_types.retrieve.all",
135 desc => 'Retrieves the non-cat types at the requested location as well '
136 . 'as those above and below it in the org tree',
138 { name => 'orgId', desc => 'Org unit ID of the base location', type => 'number' },
139 { name => 'depth', desc => 'Depth limit of the tree (optional)', type => 'number' }
142 desc => 'Array of non-cat objects, event on error'
147 sub retrieve_noncat_types_all {
148 my( $self, $client, $orgId, $depth ) = @_;
149 my $meth = 'open-ils.storage.ranged.config.non_cataloged_type.retrieve.atomic';
150 my $svc = 'open-ils.storage';
151 return $U->simplereq($svc, $meth, $orgId, $depth) if defined($depth);
152 return $U->simplereq($svc, $meth, $orgId);
156 __PACKAGE__->register_method(
157 method => 'fetch_noncat',
158 api_name => 'open-ils.circ.non_cataloged_circulation.retrieve',
160 desc => 'Retrieve a circulation on a non cataloged item for a given Circ ID. If the operator is not the '
161 . 'patron owner of the circ, the VIEW_CIRCULATIONS permission is required',
163 { desc => 'Authentication token', type => 'string' },
164 { desc => 'Circulation ID', type => 'number' }
167 desc => 'Circulation object, event on error',
173 my( $self, $conn, $auth, $circid ) = @_;
174 my $e = new_editor( authtoken => $auth );
175 return $e->event unless $e->checkauth;
176 my $c = $e->retrieve_action_non_cataloged_circulation($circid)
178 if( $c->patron ne $e->requestor->id ) {
179 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # XXX rely on editor perm
181 return noncat_due_date($e, $c);
184 sub noncat_due_date {
187 my $otype = $e->retrieve_config_non_cataloged_type($circ->item_type)
188 or return $e->die_event;
190 my $duedate = $_dt_parser->parse_datetime( cleanse_ISO8601($circ->circ_time) );
192 ->add( seconds => interval_to_seconds($otype->circ_duration) )
193 ->strftime('%FT%T%z');
195 my $offset = $U->storagereq(
196 'open-ils.storage.actor.org_unit.closed_date.overlap',
201 $duedate = $offset->{end} if ($offset);
202 $circ->duedate($duedate);
209 __PACKAGE__->register_method(
210 method => 'fetch_open_noncats',
212 api_name => 'open-ils.circ.open_non_cataloged_circulation.user',
214 desca => 'For a given user, returns an id-list of non-cataloged circulations that are considered open as of now. ' .
215 'A circ is open if circ time + circ duration (based on type) is > than now. If trying to view the circs ' .
216 'of another user, the VIEW_CIRCULATIONS permission is required',
218 { desc => 'Authentication token', type => 'string' },
219 { desc => 'UserID (optional: defaults to the session user)', type => 'number' }
222 desc => 'Array of non-cataloged circ IDs, event on error'
227 sub fetch_open_noncats {
228 my( $self, $conn, $auth, $userid ) = @_;
229 my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
230 return $e->event unless $e->checkauth;
231 $userid ||= $e->requestor->id;
232 if( $e->requestor->id ne $userid ) {
233 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # XXX rely on editor perm
236 'open-ils.storage.action.open_non_cataloged_circulation.user', $userid );
240 __PACKAGE__->register_method(
241 method => 'delete_noncat',
242 api_name => 'open-ils.circ.non_cataloged_type.delete',
245 my( $self, $conn, $auth, $typeid ) = @_;
246 my $e = new_editor(xact=>1, authtoken => $auth);
247 return $e->die_event unless $e->checkauth;
249 my $nc = $e->retrieve_config_non_cataloged_type($typeid)
250 or return $e->die_event;
252 $e->allowed('DELETE_NON_CAT_TYPE', $nc->owning_lib) # XXX rely on editor perm
253 or return $e->die_event;
255 # XXX Add checks to see if this type is in use by a transaction
257 $e->delete_config_non_cataloged_type($nc) or return $e->die_event;