]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/CopyLocations.pm
d7f0773293cd44c607dc0df39f3b8e111dbc246f
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Circ / CopyLocations.pm
1 package OpenILS::Application::Circ::CopyLocations;
2 use base 'OpenILS::Application';
3 use strict; use warnings;
4 use Data::Dumper;
5 $Data::Dumper::Indent = 0;
6 use OpenSRF::EX qw(:try);
7 use OpenSRF::Utils::Logger qw(:logger);
8 use OpenILS::Application::AppUtils;
9 use OpenILS::Utils::Fieldmapper;
10 use OpenILS::Utils::CStoreEditor qw/:funcs/;
11 my $U = "OpenILS::Application::AppUtils";
12
13
14 __PACKAGE__->register_method(
15         api_name                => "open-ils.circ.copy_location.retrieve.all",
16         method          => 'cl_retrieve_all',
17         argc                    =>      2,
18         signature       => q/
19                 Retrieves the ranged set of copy locations for the requested org.
20                 If no org is provided, all copy locations are returned
21                 @param orgId The org location id
22                 @param noi18n No i18n in result
23         @param flesh_owning_lib Flesh owning lib in results
24                 @return An array of copy location objects
25                 /);
26
27 sub cl_retrieve_all {
28         my ($self, $client, $org_id, $no_i18n, $flesh_owning_lib) = @_;
29
30         if(!$org_id) {
31                 my $otree = $U->get_org_tree();
32                 $org_id = $otree->id;
33         }
34
35     my $second_cstore_arg = {"no_i18n" => scalar($no_i18n)};
36     if ($flesh_owning_lib) {
37         $second_cstore_arg->{"flesh"} = 1;
38         $second_cstore_arg->{"flesh_fields"} = {"acpl" => ["owning_lib"]};
39     }
40
41     return new_editor()->search_asset_copy_location([{
42         owning_lib => $U->get_org_full_path($org_id)
43     }, $second_cstore_arg]);
44 }
45
46 __PACKAGE__->register_method(
47     "api_name" => "open-ils.circ.copy_location.retrieve.distinct",
48     "method" => "cl_retrieve_distinct",
49     "stream" => 1,
50     "argc" => 0,
51     "signature" => q/Retrieve copy locations with distinct names globally/
52 );
53
54 sub cl_retrieve_distinct {
55     my ($self, $client) = @_;
56
57     my $e = new_editor();
58     my $names = $e->json_query({
59         "select" => {
60             "acpl" => [{"transform" => "distinct", "column" => "name"}]
61         },
62         "from" => {"acpl" => {}}
63     }) or return $e->die_event;
64     $e->disconnect;
65
66     $client->respond($_->{"name"}) for @$names;
67     undef;
68 }
69
70 __PACKAGE__->register_method(
71         api_name                => 'open-ils.circ.copy_location.create',
72         method          => 'cl_create',
73         argc                    => 2,
74         signature       => q/
75                 Creates a new copy location.  Requestor must have the CREATE_COPY_LOCATION
76                 permission at the location specified on the new location object
77                 @param authtoken The login session key
78                 @param copyLoc The new copy location object
79                 @return The if of the new location object on success, event on error
80         /);
81
82
83 sub cl_create {
84     my( $self, $conn, $auth, $location ) = @_;
85
86     my $e = new_editor(authtoken=>$auth, xact=>1);
87     return $e->die_event unless $e->checkauth;
88     return $e->die_event unless 
89         $e->allowed('CREATE_COPY_LOCATION', $location->owning_lib);
90
91     # make sure there is no copy_location with the same name in the same place
92     my $existing = $e->search_asset_copy_location(
93         {owning_lib => $location->owning_lib, name => $location->name}, {idlist=>1});
94     return OpenILS::Event->new('COPY_LOCATION_EXISTS') if @$existing;
95
96     $e->create_asset_copy_location($location) or return $e->die_event;
97     $e->commit;
98     return $location->id;
99 }
100
101
102
103 __PACKAGE__->register_method (
104         api_name                => 'open-ils.circ.copy_location.delete',
105         method          => 'cl_delete',
106         argc                    => 2,
107         signature       => q/
108                 Deletes a copy location. Requestor must have the 
109                 DELETE_COPY_LOCATION permission.
110                 @param authtoken The login session key
111                 @param id The copy location object id
112                 @return 1 on success, event on error
113         /);
114
115
116 sub cl_delete {
117     my( $self, $conn, $auth, $id ) = @_;
118
119     my $e = new_editor(authtoken=>$auth, xact=>1);
120     return $e->die_event unless $e->checkauth;
121
122     my $cloc = $e->retrieve_asset_copy_location($id) 
123         or return $e->die_event;
124     return $e->die_event unless 
125         $e->allowed('DELETE_COPY_LOCATION', $cloc->owning_lib);
126
127     $e->delete_asset_copy_location($cloc) or return $e->die_event;
128     $e->commit;
129     return 1;
130 }
131
132
133 __PACKAGE__->register_method (
134         api_name                => 'open-ils.circ.copy_location.update',
135         method          => 'cl_update',
136         argc                    => 2,
137         signature       => q/
138                 Updates a copy location object.  Requestor must have 
139                 the UPDATE_COPY_LOCATION permission
140                 @param authtoken The login session key
141                 @param copyLoc  The copy location object
142                 @return 1 on success, event on error
143         /);
144
145
146 sub cl_update {
147     my( $self, $conn, $auth, $location ) = @_;
148
149     my $e = new_editor(authtoken=>$auth, xact=>1);
150     return $e->die_event unless $e->checkauth;
151
152     # check permissions against the original copy location
153     my $orig_loc = $e->retrieve_asset_copy_location($location->id)
154         or return $e->die_event;
155
156     return $e->die_event unless 
157         $e->allowed('UPDATE_COPY_LOCATION', $orig_loc->owning_lib);
158
159     # disallow hijacking of the location
160     $location->owning_lib($orig_loc->owning_lib);  
161
162     $e->update_asset_copy_location($location)
163         or return $e->die_event;
164
165     $e->commit;
166     return 1;
167 }
168
169
170
171 __PACKAGE__->register_method(
172         method => 'fetch_loc',
173     authoritative => 1,
174         api_name => 'open-ils.circ.copy_location.retrieve',
175 );
176
177 sub fetch_loc {
178         my( $self, $con, $id ) = @_;
179         my $e = new_editor();
180         my $cl = $e->retrieve_asset_copy_location($id)
181                 or return $e->event;
182         return $cl;
183 }
184
185 __PACKAGE__->register_method(
186     api_name => "open-ils.circ.copy_location_order.update",
187     method => 'update_clo',
188     argc =>     2,
189 );
190
191 sub update_clo {
192     my($self, $client, $auth, $orders) = @_;
193     return [] unless $orders and @$orders;
194
195     my $e = new_editor(authtoken => $auth, xact =>1);
196     return $e->die_event unless $e->checkauth;
197
198     my $org = $$orders[0]->org;
199     return $e->die_event unless $e->allowed('ADMIN_COPY_LOCATION_ORDER', $org);
200
201     # clear out the previous order entries
202     my $existing = $e->search_asset_copy_location_order({org => $org});
203     $e->delete_asset_copy_location_order($_) or return $e->die_event for @$existing;
204
205     # create the new order entries
206     my $progress = 0; 
207     for my $order (@$orders) {
208         return $e->die_event(OpenILS::Event->new('BAD_PARAMS')) unless $order->org == $org;
209         $e->create_asset_copy_location_order($order) or return $e->die_event;
210         $client->respond({maximum => scalar(@$orders), progress => $progress}) unless ($progress++ % 10);
211     }
212
213     # fetch the new entries
214     $orders = $e->search_asset_copy_location_order({org => $org});
215     $e->commit;
216     return {orders => $orders};
217 }
218
219
220 1;