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),
45 }, $second_cstore_arg]);
48 __PACKAGE__->register_method(
49 "api_name" => "open-ils.circ.copy_location.retrieve.distinct",
50 "method" => "cl_retrieve_distinct",
53 "signature" => q/Retrieve copy locations with distinct names globally/
56 sub cl_retrieve_distinct {
57 my ($self, $client) = @_;
60 my $names = $e->json_query({
62 "acpl" => [{"transform" => "distinct", "column" => "name"}]
64 "from" => {"acpl" => {}},
65 "where" => {"deleted" => "f"}
66 }) or return $e->die_event;
69 $client->respond($_->{"name"}) for @$names;
73 __PACKAGE__->register_method(
74 api_name => 'open-ils.circ.copy_location.create',
75 method => 'cl_create',
78 Creates a new copy location. Requestor must have the CREATE_COPY_LOCATION
79 permission at the location specified on the new location object
80 @param authtoken The login session key
81 @param copyLoc The new copy location object
82 @return The if of the new location object on success, event on error
87 my( $self, $conn, $auth, $location ) = @_;
89 my $e = new_editor(authtoken=>$auth, xact=>1);
90 return $e->die_event unless $e->checkauth;
91 return $e->die_event unless
92 $e->allowed('CREATE_COPY_LOCATION', $location->owning_lib);
94 # make sure there is no copy_location with the same name in the same place
95 my $existing = $e->search_asset_copy_location(
96 {owning_lib => $location->owning_lib, name => $location->name, deleted => "f"}, {idlist=>1});
97 return OpenILS::Event->new('COPY_LOCATION_EXISTS') if @$existing;
99 $e->create_asset_copy_location($location) or return $e->die_event;
101 return $location->id;
106 __PACKAGE__->register_method (
107 api_name => 'open-ils.circ.copy_location.delete',
108 method => 'cl_delete',
111 Deletes a copy location. Requestor must have the
112 DELETE_COPY_LOCATION permission.
113 @param authtoken The login session key
114 @param id The copy location object id
115 @return 1 on success, event on error
120 my( $self, $conn, $auth, $id ) = @_;
122 my $e = new_editor(authtoken=>$auth, xact=>1);
123 return $e->die_event unless $e->checkauth;
125 my $cps = $e->search_asset_copy({location=>$id, deleted=>'f'});
126 return OpenILS::Event->new('COPY_LOCATION_NOT_EMPTY', payload=>$id) if @$cps;
128 my $cloc = $e->retrieve_asset_copy_location($id)
129 or return $e->die_event;
130 return $e->die_event unless
131 $e->allowed('DELETE_COPY_LOCATION', $cloc->owning_lib);
133 $e->delete_asset_copy_location($cloc) or return $e->die_event;
139 __PACKAGE__->register_method (
140 api_name => 'open-ils.circ.copy_location.update',
141 method => 'cl_update',
144 Updates a copy location object. Requestor must have
145 the UPDATE_COPY_LOCATION permission
146 @param authtoken The login session key
147 @param copyLoc The copy location object
148 @return 1 on success, event on error
153 my( $self, $conn, $auth, $location ) = @_;
155 my $e = new_editor(authtoken=>$auth, xact=>1);
156 return $e->die_event unless $e->checkauth;
158 # check permissions against the original copy location
159 my $orig_loc = $e->retrieve_asset_copy_location($location->id)
160 or return $e->die_event;
162 return $e->die_event unless
163 $e->allowed('UPDATE_COPY_LOCATION', $orig_loc->owning_lib);
165 # disallow hijacking of the location
166 $location->owning_lib($orig_loc->owning_lib);
168 $e->update_asset_copy_location($location)
169 or return $e->die_event;
177 __PACKAGE__->register_method(
178 method => 'fetch_loc',
180 api_name => 'open-ils.circ.copy_location.retrieve',
184 my( $self, $con, $id ) = @_;
185 my $e = new_editor();
186 my $cl = $e->retrieve_asset_copy_location($id)
191 __PACKAGE__->register_method(
192 api_name => "open-ils.circ.copy_location_order.update",
193 method => 'update_clo',
198 my($self, $client, $auth, $orders) = @_;
199 return [] unless $orders and @$orders;
201 my $e = new_editor(authtoken => $auth, xact =>1);
202 return $e->die_event unless $e->checkauth;
204 my $org = $$orders[0]->org;
205 return $e->die_event unless $e->allowed('ADMIN_COPY_LOCATION_ORDER', $org);
207 # clear out the previous order entries
208 my $existing = $e->search_asset_copy_location_order({org => $org});
209 $e->delete_asset_copy_location_order($_) or return $e->die_event for @$existing;
211 # create the new order entries
213 for my $order (@$orders) {
214 return $e->die_event(OpenILS::Event->new('BAD_PARAMS')) unless $order->org == $org;
215 $e->create_asset_copy_location_order($order) or return $e->die_event;
216 $client->respond({maximum => scalar(@$orders), progress => $progress}) unless ($progress++ % 10);
219 # fetch the new entries
220 $orders = $e->search_asset_copy_location_order({org => $org});
222 return {orders => $orders};