]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/CopyLocations.pm
Fix in-transit hold retarget
[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        $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"]};
40     }
41
42     return new_editor()->search_asset_copy_location([{
43         owning_lib => $U->get_org_full_path($org_id)
44     }, $second_cstore_arg]);
45 }
46
47 __PACKAGE__->register_method(
48     "api_name" => "open-ils.circ.copy_location.retrieve.distinct",
49     "method" => "cl_retrieve_distinct",
50     "stream" => 1,
51     "argc" => 0,
52     "signature" => q/Retrieve copy locations with distinct names globally/
53 );
54
55 sub cl_retrieve_distinct {
56     my ($self, $client) = @_;
57
58     my $e = new_editor();
59     my $names = $e->json_query({
60         "select" => {
61             "acpl" => [{"transform" => "distinct", "column" => "name"}]
62         },
63         "from" => {"acpl" => {}}
64     }) or return $e->die_event;
65     $e->disconnect;
66
67     $client->respond($_->{"name"}) for @$names;
68     undef;
69 }
70
71 __PACKAGE__->register_method(
72         api_name                => 'open-ils.circ.copy_location.create',
73         method          => 'cl_create',
74         argc                    => 2,
75         signature       => q/
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
81         /);
82
83
84 sub cl_create {
85     my( $self, $conn, $auth, $location ) = @_;
86
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);
91
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;
96
97     $e->create_asset_copy_location($location) or return $e->die_event;
98     $e->commit;
99     return $location->id;
100 }
101
102
103
104 __PACKAGE__->register_method (
105         api_name                => 'open-ils.circ.copy_location.delete',
106         method          => 'cl_delete',
107         argc                    => 2,
108         signature       => q/
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
114         /);
115
116
117 sub cl_delete {
118     my( $self, $conn, $auth, $id ) = @_;
119
120     my $e = new_editor(authtoken=>$auth, xact=>1);
121     return $e->die_event unless $e->checkauth;
122
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);
127
128     $e->delete_asset_copy_location($cloc) or return $e->die_event;
129     $e->commit;
130     return 1;
131 }
132
133
134 __PACKAGE__->register_method (
135         api_name                => 'open-ils.circ.copy_location.update',
136         method          => 'cl_update',
137         argc                    => 2,
138         signature       => q/
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
144         /);
145
146
147 sub cl_update {
148     my( $self, $conn, $auth, $location ) = @_;
149
150     my $e = new_editor(authtoken=>$auth, xact=>1);
151     return $e->die_event unless $e->checkauth;
152
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;
156
157     return $e->die_event unless 
158         $e->allowed('UPDATE_COPY_LOCATION', $orig_loc->owning_lib);
159
160     # disallow hijacking of the location
161     $location->owning_lib($orig_loc->owning_lib);  
162
163     $e->update_asset_copy_location($location)
164         or return $e->die_event;
165
166     $e->commit;
167     return 1;
168 }
169
170
171
172 __PACKAGE__->register_method(
173         method => 'fetch_loc',
174     authoritative => 1,
175         api_name => 'open-ils.circ.copy_location.retrieve',
176 );
177
178 sub fetch_loc {
179         my( $self, $con, $id ) = @_;
180         my $e = new_editor();
181         my $cl = $e->retrieve_asset_copy_location($id)
182                 or return $e->event;
183         return $cl;
184 }
185
186 __PACKAGE__->register_method(
187     api_name => "open-ils.circ.copy_location_order.update",
188     method => 'update_clo',
189     argc =>     2,
190 );
191
192 sub update_clo {
193     my($self, $client, $auth, $orders) = @_;
194     return [] unless $orders and @$orders;
195
196     my $e = new_editor(authtoken => $auth, xact =>1);
197     return $e->die_event unless $e->checkauth;
198
199     my $org = $$orders[0]->org;
200     return $e->die_event unless $e->allowed('ADMIN_COPY_LOCATION_ORDER', $org);
201
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;
205
206     # create the new order entries
207     my $progress = 0; 
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);
212     }
213
214     # fetch the new entries
215     $orders = $e->search_asset_copy_location_order({org => $org});
216     $e->commit;
217     return {orders => $orders};
218 }
219
220
221 1;