]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/NonCat.pm
moved delete to cstore editor transaction
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / NonCat.pm
1 package OpenILS::Application::Circ::NonCat;
2 use base 'OpenSRF::Application';
3 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
5 use Data::Dumper;
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;
12
13 my $U = "OpenILS::Application::AppUtils";
14
15
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 ) = @_;
19
20         my( $id, $nct, $evt );
21         $circ_time ||= 'now';
22         my $circ = Fieldmapper::action::non_cataloged_circulation->new;
23
24         $logger->activity("Creating non-cataloged circulation for ".
25                 "staff $staffid, patron $patronid, location $circ_lib, and non-cat type $noncat_type");
26
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);
32
33         if( $editor ) {
34                 $evt = $editor->event unless
35                         $circ = $editor->create_action_non_cataloged_circulation( $circ )
36
37         } else {
38                 $id = $U->simplereq(
39                         'open-ils.storage',
40                         'open-ils.storage.direct.action.non_cataloged_circulation.create', $circ );
41                 $evt = $U->DB_UPDATE_FAILED($circ) unless $id;
42                 $circ->id($id);
43         }
44
45         return( $circ, $evt );
46 }
47
48
49 __PACKAGE__->register_method(
50         method  => "create_noncat_type",
51         api_name        => "open-ils.circ.non_cat_type.create",
52         notes           => q/
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
58                 event on failure
59         /);
60
61 sub __create_noncat_type {
62         my( $self, $client, $authtoken, $name, $orgId, $interval, $inhouse ) = @_;
63         my( $staff, $evt ) = $U->checkses($authtoken);
64         return $evt if $evt;
65
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);
69         for(@$types) {
70                 if( $_->name eq $name ) {
71                         return OpenILS::Event->new('NON_CAT_TYPE_EXISTS', payload => $name);
72                 }
73         }
74
75         $evt = $U->check_perms( $staff->id, $orgId, 'CREATE_NON_CAT_TYPE' );
76         return $evt if $evt;
77
78         my $type = Fieldmapper::config::non_cataloged_type->new;
79         $type->name($name);
80         $type->owning_lib($orgId);
81         $type->circ_duration($interval);
82         $type->in_house( ($inhouse) ? 't' : 'f' );
83
84         my $id = $U->simplereq(
85                 'open-ils.storage',
86                 'open-ils.storage.direct.config.non_cataloged_type.create', $type );
87
88         return $U->DB_UPDATE_FAILED($type) unless $id;
89         $type->id($id);
90         return $type;
91 }
92
93
94 sub create_noncat_type {
95         my( $self, $client, $authtoken, $name, $orgId, $interval, $inhouse ) = @_;
96
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);
100
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);
104         for(@$types) {
105                 if( $_->name eq $name ) {
106                         $e->rollback;
107                         return OpenILS::Event->new('NON_CAT_TYPE_EXISTS', payload => $name);
108                 }
109         }
110
111         my $type = Fieldmapper::config::non_cataloged_type->new;
112         $type->name($name);
113         $type->owning_lib($orgId);
114         $type->circ_duration($interval);
115         $type->in_house( ($inhouse) ? 't' : 'f' );
116
117         $e->create_config_non_cataloged_type($type) or return $e->die_event;
118         $e->commit;
119         return $type;
120 }
121
122
123
124 __PACKAGE__->register_method(
125         method  => "update_noncat_type",
126         api_name        => "open-ils.circ.non_cat_type.update",
127         notes           => q/
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
133         /);
134
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;
139
140         my $otype = $e->retrieve_config_non_cataloged_type($type->id) 
141                 or return $e->die_event;
142
143         return $e->die_event unless 
144                 $e->allowed('UPDATE_NON_CAT_TYPE', $otype->owning_lib);
145
146         $type->owning_lib($otype->owning_lib); # do not allow them to "move" the object
147
148         $e->update_config_non_cataloged_type($type) or return $e->die_event;
149         $e->commit;
150         return 1;
151 }
152
153 __PACKAGE__->register_method(
154         method  => "retrieve_noncat_types_all",
155         api_name        => "open-ils.circ.non_cat_types.retrieve.all",
156         notes           => q/
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
162         /);
163
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);
170 }
171
172
173
174 __PACKAGE__->register_method(
175         method          => 'fetch_noncat',
176         api_name                => 'open-ils.circ.non_cataloged_circulation.retrieve',
177         signature       => q/
178         /
179 );
180
181 sub fetch_noncat {
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)
186                 or return $e->event;
187         if( $c->patron ne $e->requestor->id ) {
188                 return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # XXX rely on editor perm
189         }
190         return $c;
191 }
192
193
194
195 __PACKAGE__->register_method(
196         method => 'fetch_open_noncats',
197         api_name        => 'open-ils.circ.open_non_cataloged_circulation.user',
198         signature => q/
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.
202                 @param auth auth key
203                 @param userid user to retrieve non-cat circs for 
204                         defaults to the session user
205         /
206 );
207
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
215         }
216         return $e->request(
217                 'open-ils.storage.action.open_non_cataloged_circulation.user', $userid );
218 }
219
220
221 __PACKAGE__->register_method(
222         method  => 'delete_noncat',
223         api_name        => 'open-ils.circ.non_cataloged_type.delete',
224 );
225 sub delete_noncat {
226         my( $self, $conn, $auth, $typeid ) = @_;
227         my $e = new_editor(xact=>1, authtoken => $auth);
228         return $e->die_event unless $e->checkauth;
229
230         my $nc = $e->retrieve_config_non_cataloged_type($typeid)
231                 or return $e->die_event;
232
233         $e->allowed('DELETE_NON_CAT_TYPE', $nc->owning_lib) # XXX rely on editor perm
234                 or return $e->die_event;
235
236         #       XXX Add checks to see if this type is in use by a transaction
237
238         $e->delete_config_non_cataloged_type($nc) or return $e->die_event;
239         $e->commit;
240         return 1;
241 }
242
243
244
245 1;