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