]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/NonCat.pm
wired up delete
[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 $Data::Dumper::Indent = 0;
11
12 my $U = "OpenILS::Application::AppUtils";
13
14
15 # returns ( $newid, $evt ).  If $evt, then there was an error
16 sub create_non_cat_circ {
17         my( $staffid, $patronid, $circ_lib, $noncat_type, $circ_time ) = @_;
18
19         my( $id, $nct, $evt );
20         $circ_time ||= 'now';
21         my $circ = Fieldmapper::action::non_cataloged_circulation->new;
22
23         $logger->activity("Creating non-cataloged circulation for ".
24                 "staff $staffid, patron $patronid, location $circ_lib, and non-cat type $noncat_type");
25
26         $circ->patron($patronid);
27         $circ->staff($staffid);
28         $circ->circ_lib($circ_lib);
29         $circ->item_type($noncat_type);
30         $circ->circ_time($circ_time);
31
32         $id = $U->simplereq(
33                 'open-ils.storage',
34                 'open-ils.storage.direct.action.non_cataloged_circulation.create', $circ );
35         $evt = $U->DB_UPDATE_FAILED($circ) unless $id;
36
37         $circ->id($id);
38         return( $circ, $evt );
39 }
40
41
42 __PACKAGE__->register_method(
43         method  => "create_noncat_type",
44         api_name        => "open-ils.circ.non_cat_type.create",
45         notes           => q/
46                 Creates a new non cataloged item type
47                 @param authtoken The login session key
48                 @param name The name of the new type
49                 @param orgId The location where the type will live
50                 @return The type object on success and the corresponding
51                 event on failure
52         /);
53
54 sub create_noncat_type {
55         my( $self, $client, $authtoken, $name, $orgId ) = @_;
56         my( $staff, $evt ) = $U->checkses($authtoken);
57         return $evt if $evt;
58
59         # grab all of "my" non-cat types and see if one with 
60         # the requested name already exists
61         my $types = $self->retrieve_noncat_types_all($client, $orgId);
62         if(ref($types)) {
63                 for(@$types) {
64                         return OpenILS::Event->new('NON_CAT_TYPE_EXISTS') if $_->name eq $name;
65                 }
66         }
67
68         $evt = $U->check_perms( $staff->id, $orgId, 'CREATE_NON_CAT_TYPE' );
69         return $evt if $evt;
70
71         my $type = Fieldmapper::config::non_cataloged_type->new;
72         $type->name($name);
73         $type->owning_lib($orgId);
74
75         my $id = $U->simplereq(
76                 'open-ils.storage',
77                 'open-ils.storage.direct.config.non_cataloged_type.create', $type );
78
79         return $U->DB_UPDATE_FAILED($type) unless $id;
80         $type->id($id);
81         return $type;
82 }
83
84 __PACKAGE__->register_method(
85         method  => "update_noncat_type",
86         api_name        => "open-ils.circ.non_cat_type.update",
87         notes           => q/
88                 Updates a non-cataloged type object
89                 @param authtoken The login session key
90                 @param type The updated type object
91                 @return The result of the DB update call unless a preceeding event occurs, 
92                         in which case the event will be returned
93         /);
94
95 sub update_noncat_type {
96         my( $self, $client, $authtoken, $type ) = @_;
97         my( $staff, $evt ) = $U->checkses($authtoken);
98         return $evt if $evt;
99
100         my $otype;
101         ($otype, $evt) = $U->fetch_non_cat_type($type->id);
102         return $evt if $evt;
103
104         $type->owning_lib($otype->owning_lib); # do not allow them to "move" the object
105
106         $evt = $U->check_perms( $staff->id, $type->owning_lib, 'UPDATE_NON_CAT_TYPE' );
107         return $evt if $evt;
108
109         return $U->simplereq(
110                 'open-ils.storage',
111                 'open-ils.storage.direct.config.non_cataloged_type.update', $type );
112 }
113
114 __PACKAGE__->register_method(
115         method  => "retrieve_noncat_types_all",
116         api_name        => "open-ils.circ.non_cat_types.retrieve.all",
117         notes           => q/
118                 Retrieves the non-cat types at the requested location as well
119                 as those above and below the requested location in the org tree
120                 @param orgId The base location at which to retrieve the type objects
121                 @param depth Optional parameter to limit the depth of the tree
122                 @return An array of non cat type objects or an event if an error occurs
123         /);
124
125 sub retrieve_noncat_types_all {
126         my( $self, $client, $orgId, $depth ) = @_;
127         my $meth = 'open-ils.storage.ranged.config.non_cataloged_type.retrieve.atomic';
128         my $svc = 'open-ils.storage';
129         return $U->simplereq($svc, $meth, $orgId, $depth) if defined($depth);
130         return $U->simplereq($svc, $meth, $orgId);
131 }
132
133
134
135 __PACKAGE__->register_method(
136         method          => 'fetch_noncat',
137         api_name                => 'open-ils.circ.non_cataloged_circulation.retrieve',
138         signature       => q/
139         /
140 );
141
142 sub fetch_noncat {
143         my( $self, $conn, $auth, $circid ) = @_;
144         my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
145         return $e->event unless $e->checkauth;
146         return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # XXX rely on editor perm
147         my $c = $e->retrieve_action_non_cataloged_circulation($circid)
148                 or return $e->event;
149         return $c;
150 }
151
152
153
154 __PACKAGE__->register_method(
155         method => 'fetch_open_noncats',
156         api_name        => 'open-ils.circ.open_non_cataloged_circulation.user',
157         signature => q/
158                 Returns an id-list of non-cataloged circulations that are considered
159                 open as of now.  a circ is open if circ time + circ duration 
160                 (based on type) is > than now.
161                 @param auth auth key
162                 @param userid user to retrieve non-cat circs for 
163                         defaults to the session user
164         /
165 );
166
167 sub fetch_open_noncats {
168         my( $self, $conn, $auth, $userid ) = @_;
169         my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
170         return $e->event unless $e->checkauth;
171         return $e->event unless $e->allowed('VIEW_CIRCULATIONS'); # XXX rely on editor perm
172         $userid ||= $e->requestor->id;
173         return $e->request(
174                 'open-ils.storage.action.open_non_cataloged_circulation.user', $userid );
175 }
176
177
178 __PACKAGE__->register_method(
179         method  => 'delete_noncat',
180         api_name        => 'open-ils.circ.non_cataloged_type.delete',
181 );
182 sub delete_noncat {
183         my( $self, $conn, $auth, $typeid ) = @_;
184         my $e = OpenILS::Utils::Editor->new( authtoken => $auth );
185         return $e->event unless $e->checkauth;
186
187         my $nc = $e->retrieve_config_non_cataloged_type($typeid)
188                 or return $e->event;
189
190         $e->allowed('DELETE_NON_CAT_TYPE', $nc->owning_lib) # XXX rely on editor perm
191                 or return $e->event;
192
193         $e->delete_config_non_cataloged_type($nc) or return $e->event;
194         return 1;
195 }
196
197
198
199 1;