1 package OpenILS::Application::Circ::CopyLocations;
2 use base 'OpenILS::Application';
3 use strict; use warnings;
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";
14 __PACKAGE__->register_method(
15 api_name => "open-ils.circ.copy_location.retrieve.all",
16 method => 'cl_retrieve_all',
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
28 my ($self, $client, $org_id, $no_i18n, $flesh_owning_lib) = @_;
31 my $otree = $U->get_org_tree();
35 my $second_cstore_arg = {"no_i18n" => scalar($no_i18n)};
36 $second_cstore_arg->{"order_by"} = {"acpl" => "name"};
37 if ($flesh_owning_lib) {
38 $second_cstore_arg->{"flesh"} = 1;
39 $second_cstore_arg->{"flesh_fields"} = {"acpl" => ["owning_lib"]};
42 return new_editor()->search_asset_copy_location([{
43 owning_lib => $U->get_org_full_path($org_id)
44 }, $second_cstore_arg]);
47 __PACKAGE__->register_method(
48 "api_name" => "open-ils.circ.copy_location.retrieve.distinct",
49 "method" => "cl_retrieve_distinct",
52 "signature" => q/Retrieve copy locations with distinct names globally/
55 sub cl_retrieve_distinct {
56 my ($self, $client) = @_;
59 my $names = $e->json_query({
61 "acpl" => [{"transform" => "distinct", "column" => "name"}]
63 "from" => {"acpl" => {}}
64 }) or return $e->die_event;
67 $client->respond($_->{"name"}) for @$names;
71 __PACKAGE__->register_method(
72 api_name => 'open-ils.circ.copy_location.create',
73 method => 'cl_create',
76 Creates a new copy location. Requestor must have the CREATE_COPY_LOCATION
77 permission at the location specified on the new location object
78 @param authtoken The login session key
79 @param copyLoc The new copy location object
80 @return The if of the new location object on success, event on error
85 my( $self, $conn, $auth, $location ) = @_;
87 my $e = new_editor(authtoken=>$auth, xact=>1);
88 return $e->die_event unless $e->checkauth;
89 return $e->die_event unless
90 $e->allowed('CREATE_COPY_LOCATION', $location->owning_lib);
92 # make sure there is no copy_location with the same name in the same place
93 my $existing = $e->search_asset_copy_location(
94 {owning_lib => $location->owning_lib, name => $location->name}, {idlist=>1});
95 return OpenILS::Event->new('COPY_LOCATION_EXISTS') if @$existing;
97 $e->create_asset_copy_location($location) or return $e->die_event;
104 __PACKAGE__->register_method (
105 api_name => 'open-ils.circ.copy_location.delete',
106 method => 'cl_delete',
109 Deletes a copy location. Requestor must have the
110 DELETE_COPY_LOCATION permission.
111 @param authtoken The login session key
112 @param id The copy location object id
113 @return 1 on success, event on error
118 my( $self, $conn, $auth, $id ) = @_;
120 my $e = new_editor(authtoken=>$auth, xact=>1);
121 return $e->die_event unless $e->checkauth;
123 my $cloc = $e->retrieve_asset_copy_location($id)
124 or return $e->die_event;
125 return $e->die_event unless
126 $e->allowed('DELETE_COPY_LOCATION', $cloc->owning_lib);
128 $e->delete_asset_copy_location($cloc) or return $e->die_event;
134 __PACKAGE__->register_method (
135 api_name => 'open-ils.circ.copy_location.update',
136 method => 'cl_update',
139 Updates a copy location object. Requestor must have
140 the UPDATE_COPY_LOCATION permission
141 @param authtoken The login session key
142 @param copyLoc The copy location object
143 @return 1 on success, event on error
148 my( $self, $conn, $auth, $location ) = @_;
150 my $e = new_editor(authtoken=>$auth, xact=>1);
151 return $e->die_event unless $e->checkauth;
153 # check permissions against the original copy location
154 my $orig_loc = $e->retrieve_asset_copy_location($location->id)
155 or return $e->die_event;
157 return $e->die_event unless
158 $e->allowed('UPDATE_COPY_LOCATION', $orig_loc->owning_lib);
160 # disallow hijacking of the location
161 $location->owning_lib($orig_loc->owning_lib);
163 $e->update_asset_copy_location($location)
164 or return $e->die_event;
172 __PACKAGE__->register_method(
173 method => 'fetch_loc',
175 api_name => 'open-ils.circ.copy_location.retrieve',
179 my( $self, $con, $id ) = @_;
180 my $e = new_editor();
181 my $cl = $e->retrieve_asset_copy_location($id)
186 __PACKAGE__->register_method(
187 api_name => "open-ils.circ.copy_location_order.update",
188 method => 'update_clo',
193 my($self, $client, $auth, $orders) = @_;
194 return [] unless $orders and @$orders;
196 my $e = new_editor(authtoken => $auth, xact =>1);
197 return $e->die_event unless $e->checkauth;
199 my $org = $$orders[0]->org;
200 return $e->die_event unless $e->allowed('ADMIN_COPY_LOCATION_ORDER', $org);
202 # clear out the previous order entries
203 my $existing = $e->search_asset_copy_location_order({org => $org});
204 $e->delete_asset_copy_location_order($_) or return $e->die_event for @$existing;
206 # create the new order entries
208 for my $order (@$orders) {
209 return $e->die_event(OpenILS::Event->new('BAD_PARAMS')) unless $order->org == $org;
210 $e->create_asset_copy_location_order($order) or return $e->die_event;
211 $client->respond({maximum => scalar(@$orders), progress => $progress}) unless ($progress++ % 10);
214 # fetch the new entries
215 $orders = $e->search_asset_copy_location_order({org => $org});
217 return {orders => $orders};