]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/CopyLocations.pm
LP#1661688: Add a link and other tweaks to alternate hold pickup feature
[working/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         deleted => "f"
45     }, $second_cstore_arg]);
46 }
47
48 __PACKAGE__->register_method(
49     "api_name" => "open-ils.circ.copy_location.retrieve.distinct",
50     "method" => "cl_retrieve_distinct",
51     "stream" => 1,
52     "argc" => 0,
53     "signature" => q/Retrieve copy locations with distinct names globally/
54 );
55
56 sub cl_retrieve_distinct {
57     my ($self, $client) = @_;
58
59     my $e = new_editor();
60     my $names = $e->json_query({
61         "select" => {
62             "acpl" => [{"transform" => "distinct", "column" => "name"}]
63         },
64         "from" => {"acpl" => {}},
65         "where" => {"deleted" => "f"}
66     }) or return $e->die_event;
67     $e->disconnect;
68
69     $client->respond($_->{"name"}) for @$names;
70     undef;
71 }
72
73 __PACKAGE__->register_method(
74     api_name        => 'open-ils.circ.copy_location.create',
75     method      => 'cl_create',
76     argc            => 2,
77     signature   => q/
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
83     /);
84
85
86 sub cl_create {
87     my( $self, $conn, $auth, $location ) = @_;
88
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);
93
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;
98
99     $e->create_asset_copy_location($location) or return $e->die_event;
100     $e->commit;
101     return $location->id;
102 }
103
104
105
106 __PACKAGE__->register_method (
107     api_name        => 'open-ils.circ.copy_location.delete',
108     method      => 'cl_delete',
109     argc            => 2,
110     signature   => q/
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
116     /);
117
118
119 sub cl_delete {
120     my( $self, $conn, $auth, $id ) = @_;
121
122     my $e = new_editor(authtoken=>$auth, xact=>1);
123     return $e->die_event unless $e->checkauth;
124
125     my $cps = $e->search_asset_copy({location=>$id, deleted=>'f'});
126     return OpenILS::Event->new('COPY_LOCATION_NOT_EMPTY', payload=>$id) if @$cps;
127
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);
132
133     $e->delete_asset_copy_location($cloc) or return $e->die_event;
134     $e->commit;
135     return 1;
136 }
137
138
139 __PACKAGE__->register_method (
140     api_name        => 'open-ils.circ.copy_location.update',
141     method      => 'cl_update',
142     argc            => 2,
143     signature   => q/
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
149     /);
150
151
152 sub cl_update {
153     my( $self, $conn, $auth, $location ) = @_;
154
155     my $e = new_editor(authtoken=>$auth, xact=>1);
156     return $e->die_event unless $e->checkauth;
157
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;
161
162     return $e->die_event unless 
163         $e->allowed('UPDATE_COPY_LOCATION', $orig_loc->owning_lib);
164
165     # disallow hijacking of the location
166     $location->owning_lib($orig_loc->owning_lib);  
167
168     $e->update_asset_copy_location($location)
169         or return $e->die_event;
170
171     $e->commit;
172     return 1;
173 }
174
175
176
177 __PACKAGE__->register_method(
178     method => 'fetch_loc',
179     authoritative => 1,
180     api_name => 'open-ils.circ.copy_location.retrieve',
181 );
182
183 sub fetch_loc {
184     my( $self, $con, $id ) = @_;
185     my $e = new_editor();
186     my $cl = $e->retrieve_asset_copy_location($id)
187         or return $e->event;
188     return $cl;
189 }
190
191 __PACKAGE__->register_method(
192     api_name => "open-ils.circ.copy_location_order.update",
193     method => 'update_clo',
194     argc => 2,
195 );
196
197 sub update_clo {
198     my($self, $client, $auth, $orders) = @_;
199     return [] unless $orders and @$orders;
200
201     my $e = new_editor(authtoken => $auth, xact =>1);
202     return $e->die_event unless $e->checkauth;
203
204     my $org = $$orders[0]->org;
205     return $e->die_event unless $e->allowed('ADMIN_COPY_LOCATION_ORDER', $org);
206
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;
210
211     # create the new order entries
212     my $progress = 0; 
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);
217     }
218
219     # fetch the new entries
220     $orders = $e->search_asset_copy_location_order({org => $org});
221     $e->commit;
222     return {orders => $orders};
223 }
224
225
226 1;