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 if ($flesh_owning_lib) {
37 $second_cstore_arg->{"flesh"} = 1;
38 $second_cstore_arg->{"flesh_fields"} = {"acpl" => ["owning_lib"]};
41 return new_editor()->search_asset_copy_location([{
42 owning_lib => $U->get_org_full_path($org_id)
43 }, $second_cstore_arg]);
46 __PACKAGE__->register_method(
47 "api_name" => "open-ils.circ.copy_location.retrieve.distinct",
48 "method" => "cl_retrieve_distinct",
51 "signature" => q/Retrieve copy locations with distinct names globally/
54 sub cl_retrieve_distinct {
55 my ($self, $client) = @_;
58 my $names = $e->json_query({
60 "acpl" => [{"transform" => "distinct", "column" => "name"}]
62 "from" => {"acpl" => {}}
63 }) or return $e->die_event;
66 $client->respond($_->{"name"}) for @$names;
70 __PACKAGE__->register_method(
71 api_name => 'open-ils.circ.copy_location.create',
72 method => 'cl_create',
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
84 my( $self, $conn, $auth, $location ) = @_;
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);
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;
96 $e->create_asset_copy_location($location) or return $e->die_event;
103 __PACKAGE__->register_method (
104 api_name => 'open-ils.circ.copy_location.delete',
105 method => 'cl_delete',
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
117 my( $self, $conn, $auth, $id ) = @_;
119 my $e = new_editor(authtoken=>$auth, xact=>1);
120 return $e->die_event unless $e->checkauth;
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);
127 $e->delete_asset_copy_location($cloc) or return $e->die_event;
133 __PACKAGE__->register_method (
134 api_name => 'open-ils.circ.copy_location.update',
135 method => 'cl_update',
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
147 my( $self, $conn, $auth, $location ) = @_;
149 my $e = new_editor(authtoken=>$auth, xact=>1);
150 return $e->die_event unless $e->checkauth;
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;
156 return $e->die_event unless
157 $e->allowed('UPDATE_COPY_LOCATION', $orig_loc->owning_lib);
159 # disallow hijacking of the location
160 $location->owning_lib($orig_loc->owning_lib);
162 $e->update_asset_copy_location($location)
163 or return $e->die_event;
171 __PACKAGE__->register_method(
172 method => 'fetch_loc',
174 api_name => 'open-ils.circ.copy_location.retrieve',
178 my( $self, $con, $id ) = @_;
179 my $e = new_editor();
180 my $cl = $e->retrieve_asset_copy_location($id)
185 __PACKAGE__->register_method(
186 api_name => "open-ils.circ.copy_location_order.update",
187 method => 'update_clo',
192 my($self, $client, $auth, $orders) = @_;
193 return [] unless $orders and @$orders;
195 my $e = new_editor(authtoken => $auth, xact =>1);
196 return $e->die_event unless $e->checkauth;
198 my $org = $$orders[0]->org;
199 return $e->die_event unless $e->allowed('ADMIN_COPY_LOCATION_ORDER', $org);
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;
205 # create the new order entries
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);
213 # fetch the new entries
214 $orders = $e->search_asset_copy_location_order({org => $org});
216 return {orders => $orders};