]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/NonCat.pm
c8c19780877d48d51a7faaf7f5bfceacd54cef86
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / NonCat.pm
1 package OpenILS::Application::Circ::NonCat;
2 use base 'OpenILS::Application';
3 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
5 use Data::Dumper;
6 use DateTime;
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;
15
16 my $U = "OpenILS::Application::AppUtils";
17 my $_dt_parser = DateTime::Format::ISO8601->new;
18
19
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 ) = @_;
23
24         my( $id, $nct, $evt );
25         $circ_time ||= 'now';
26         my $circ = Fieldmapper::action::non_cataloged_circulation->new;
27
28         $logger->activity("Creating non-cataloged circulation for ".
29                 "staff $staffid, patron $patronid, location $circ_lib, and non-cat type $noncat_type");
30
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);
36
37         if( $editor ) {
38                 $evt = $editor->event unless
39                         $circ = $editor->create_action_non_cataloged_circulation( $circ )
40
41
42         } else {
43                 $id = $U->simplereq(
44                         'open-ils.storage',
45                         'open-ils.storage.direct.action.non_cataloged_circulation.create', $circ );
46                 $evt = $U->DB_UPDATE_FAILED($circ) unless $id;
47                 $circ->id($id);
48         }
49
50     if($circ) {
51         my $e = ($editor) ? $editor : new_editor();
52         $circ = noncat_due_date($e, $circ);
53     }
54
55         return( $circ, $evt );
56 }
57
58
59 __PACKAGE__->register_method(
60         method  => "create_noncat_type",
61         api_name        => "open-ils.circ.non_cat_type.create",
62         notes           => q/
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
68                 event on failure
69         /);
70
71 sub create_noncat_type {
72         my( $self, $client, $authtoken, $name, $orgId, $interval, $inhouse ) = @_;
73
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);
77
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);
81         for(@$types) {
82                 if( $_->name eq $name ) {
83                         $e->rollback;
84                         return OpenILS::Event->new('NON_CAT_TYPE_EXISTS', payload => $name);
85                 }
86         }
87
88         my $type = Fieldmapper::config::non_cataloged_type->new;
89         $type->name($name);
90         $type->owning_lib($orgId);
91         $type->circ_duration($interval);
92         $type->in_house( ($inhouse) ? 't' : 'f' );
93
94         $e->create_config_non_cataloged_type($type) or return $e->die_event;
95         $e->commit;
96         return $type;
97 }
98
99
100
101 __PACKAGE__->register_method(
102         method  => "update_noncat_type",
103         api_name        => "open-ils.circ.non_cat_type.update",
104         notes           => q/
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
110         /);
111
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;
116
117         my $otype = $e->retrieve_config_non_cataloged_type($type->id) 
118                 or return $e->die_event;
119
120         return $e->die_event unless 
121                 $e->allowed('UPDATE_NON_CAT_TYPE', $otype->owning_lib);
122
123         $type->owning_lib($otype->owning_lib); # do not allow them to "move" the object
124
125         $e->update_config_non_cataloged_type($type) or return $e->die_event;
126         $e->commit;
127         return 1;
128 }
129
130 __PACKAGE__->register_method(
131         method  => "retrieve_noncat_types_all",
132         api_name        => "open-ils.circ.non_cat_types.retrieve.all",
133         notes           => q/
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
139         /);
140
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);
147 }
148
149
150
151 __PACKAGE__->register_method(
152         method          => 'fetch_noncat',
153         api_name                => 'open-ils.circ.non_cataloged_circulation.retrieve',
154         signature       => q/
155         /
156 );
157
158
159 sub fetch_noncat {
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)
164                 or return $e->event;
165         if( $c->patron ne $e->requestor->id ) {
166                 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # XXX rely on editor perm
167         }
168     return noncat_due_date($e, $c);
169 }
170
171 sub noncat_due_date {
172     my($e, $circ) = @_;
173
174         my $otype = $e->retrieve_config_non_cataloged_type($circ->item_type) 
175                 or return $e->die_event;
176
177         my $duedate = $_dt_parser->parse_datetime( clense_ISO8601($circ->circ_time) );
178         $duedate = $duedate
179                 ->add( seconds => interval_to_seconds($otype->circ_duration) )
180                 ->strftime('%FT%T%z');
181
182         my $offset = $U->storagereq(
183                 'open-ils.storage.actor.org_unit.closed_date.overlap',
184                 $circ->circ_lib,
185                 $duedate
186         );
187
188         $duedate = $offset->{end} if ($offset);
189         $circ->duedate($duedate);
190
191         return $circ;
192 }
193
194
195
196 __PACKAGE__->register_method(
197         method => 'fetch_open_noncats',
198     authoritative => 1,
199         api_name        => 'open-ils.circ.open_non_cataloged_circulation.user',
200         signature => q/
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.
204                 @param auth auth key
205                 @param userid user to retrieve non-cat circs for 
206                         defaults to the session user
207         /
208 );
209
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
217         }
218         return $e->request(
219                 'open-ils.storage.action.open_non_cataloged_circulation.user', $userid );
220 }
221
222
223 __PACKAGE__->register_method(
224         method  => 'delete_noncat',
225         api_name        => 'open-ils.circ.non_cataloged_type.delete',
226 );
227 sub delete_noncat {
228         my( $self, $conn, $auth, $typeid ) = @_;
229         my $e = new_editor(xact=>1, authtoken => $auth);
230         return $e->die_event unless $e->checkauth;
231
232         my $nc = $e->retrieve_config_non_cataloged_type($typeid)
233                 or return $e->die_event;
234
235         $e->allowed('DELETE_NON_CAT_TYPE', $nc->owning_lib) # XXX rely on editor perm
236                 or return $e->die_event;
237
238         #       XXX Add checks to see if this type is in use by a transaction
239
240         $e->delete_config_non_cataloged_type($nc) or return $e->die_event;
241         $e->commit;
242         return 1;
243 }
244
245
246
247 1;