]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/NonCat.pm
Merge branch 'opac-tt-poc' of ssh://senator@yeti.esilibrary.com/home/evergreen/evergr...
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / 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
72 sub create_noncat_type {
73         my( $self, $client, $authtoken, $name, $orgId, $interval, $inhouse ) = @_;
74
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);
78
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);
82         for(@$types) {
83                 if( $_->name eq $name ) {
84                         $e->rollback;
85                         return OpenILS::Event->new('NON_CAT_TYPE_EXISTS', payload => $name);
86                 }
87         }
88
89         my $type = Fieldmapper::config::non_cataloged_type->new;
90         $type->name($name);
91         $type->owning_lib($orgId);
92         $type->circ_duration($interval);
93         $type->in_house( ($inhouse) ? 't' : 'f' );
94
95         $e->create_config_non_cataloged_type($type) or return $e->die_event;
96         $e->commit;
97         return $type;
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
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;
117
118         my $otype = $e->retrieve_config_non_cataloged_type($type->id) 
119                 or return $e->die_event;
120
121         return $e->die_event unless 
122                 $e->allowed('UPDATE_NON_CAT_TYPE', $otype->owning_lib);
123
124         $type->owning_lib($otype->owning_lib); # do not allow them to "move" the object
125
126         $e->update_config_non_cataloged_type($type) or return $e->die_event;
127         $e->commit;
128         return 1;
129 }
130
131 __PACKAGE__->register_method(
132     method    => "retrieve_noncat_types_all",
133     api_name  => "open-ils.circ.non_cat_types.retrieve.all",
134     signature => {
135         desc   => 'Retrieves the non-cat types at the requested location as well '
136                 . 'as those above and below it in the org tree',
137         params => [
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' }
140         ],
141         return => {
142             desc => 'Array of non-cat objects, event on error'
143         },
144     }
145 );
146
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);
153 }
154
155
156 __PACKAGE__->register_method(
157     method    => 'fetch_noncat',
158     api_name  => 'open-ils.circ.non_cataloged_circulation.retrieve',
159     signature => {
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',
162         params => [
163             { desc => 'Authentication token', type => 'string' },
164             { desc => 'Circulation ID',       type => 'number' }
165         ],
166         return => {
167             desc => 'Circulation object, event on error',
168         },
169     }
170 );
171
172 sub fetch_noncat {
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)
177                 or return $e->event;
178         if( $c->patron ne $e->requestor->id ) {
179                 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # XXX rely on editor perm
180         }
181     return noncat_due_date($e, $c);
182 }
183
184 sub noncat_due_date {
185     my($e, $circ) = @_;
186
187         my $otype = $e->retrieve_config_non_cataloged_type($circ->item_type) 
188                 or return $e->die_event;
189
190         my $duedate = $_dt_parser->parse_datetime( cleanse_ISO8601($circ->circ_time) );
191         $duedate = $duedate
192                 ->add( seconds => interval_to_seconds($otype->circ_duration) )
193                 ->strftime('%FT%T%z');
194
195         my $offset = $U->storagereq(
196                 'open-ils.storage.actor.org_unit.closed_date.overlap',
197                 $circ->circ_lib,
198                 $duedate
199         );
200
201         $duedate = $offset->{end} if ($offset);
202         $circ->duedate($duedate);
203
204         return $circ;
205 }
206
207
208
209 __PACKAGE__->register_method(
210     method        => 'fetch_open_noncats',
211     authoritative => 1,
212     api_name      => 'open-ils.circ.open_non_cataloged_circulation.user',
213     signature     => {
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',
217                 params => [
218             { desc => 'Authentication token',                            type => 'string' },
219             { desc => 'UserID (optional: defaults to the session user)', type => 'number' }
220                 ],
221         return => {
222             desc => 'Array of non-cataloged circ IDs, event on error'
223         },
224     }
225 );
226
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
234         }
235         return $e->request(
236                 'open-ils.storage.action.open_non_cataloged_circulation.user', $userid );
237 }
238
239
240 __PACKAGE__->register_method(
241     method   => 'delete_noncat',
242     api_name => 'open-ils.circ.non_cataloged_type.delete',
243 );
244 sub delete_noncat {
245         my( $self, $conn, $auth, $typeid ) = @_;
246         my $e = new_editor(xact=>1, authtoken => $auth);
247         return $e->die_event unless $e->checkauth;
248
249         my $nc = $e->retrieve_config_non_cataloged_type($typeid)
250                 or return $e->die_event;
251
252         $e->allowed('DELETE_NON_CAT_TYPE', $nc->owning_lib) # XXX rely on editor perm
253                 or return $e->die_event;
254
255         #       XXX Add checks to see if this type is in use by a transaction
256
257         $e->delete_config_non_cataloged_type($nc) or return $e->die_event;
258         $e->commit;
259         return 1;
260 }
261
262
263 1;