1 package OpenILS::Application::Booking;
6 use OpenILS::Application;
7 use base qw/OpenILS::Application/;
9 use OpenILS::Utils::CStoreEditor qw/:funcs/;
10 use OpenILS::Utils::Fieldmapper;
11 use OpenILS::Application::AppUtils;
12 my $U = "OpenILS::Application::AppUtils";
14 use OpenSRF::Utils::Logger qw/$logger/;
17 my ($record_id, $owning_lib, $mvr) = @_;
19 my $brt = new Fieldmapper::booking::resource_type;
21 $brt->name($mvr->title);
22 $brt->record($record_id);
23 $brt->catalog_item('t');
24 $brt->owner($owning_lib);
29 sub get_existing_brt {
30 my ($e, $record_id, $owning_lib, $mvr) = @_;
31 my $results = $e->search_booking_resource_type(
32 {name => $mvr->title, owner => $owning_lib, record => $record_id}
35 return $results->[0] if (length @$results > 0);
42 'open-ils.search.biblio.record.mods_slim.retrieve.authoritative',
47 sub get_unique_owning_libs {
49 $hash{$_->call_number->owning_lib} = 1 foreach (@_); # @_ are copies
53 sub fetch_copies_by_ids {
54 my ($e, $copy_ids) = @_;
55 my $results = $e->search_asset_copy([
57 {flesh => 1, flesh_fields => {acp => ['call_number']}}
59 return $results if ref($results) eq 'ARRAY';
63 sub get_single_record_id {
64 my $record_id = undef;
65 foreach (@_) { # @_ are copies
67 (defined $record_id && $record_id != $_->call_number->record);
68 $record_id = $_->call_number->record;
73 __PACKAGE__->register_method(
74 method => "create_brt_and_brsrc",
75 api_name => "open-ils.booking.create_brt_and_brsrc_from_copies",
78 {type => 'string', desc => 'Authentication token'},
79 {type => 'array', desc => 'Copy IDs'},
81 return => { desc => "A two-element hash. The 'brt' element " .
82 "is a list of created booking resource types described by " .
83 "id/copyid pairs. The 'brsrc' element is a similar " .
84 "list of created booking resources described by copy/recordid " .
89 sub create_brt_and_brsrc {
90 my ($self, $conn, $authtoken, $copy_ids) = @_;
91 my (@created_brt, @created_brsrc);
94 my $e = new_editor(xact => 1, authtoken => $authtoken);
95 return $e->die_event unless $e->checkauth;
97 my @copies = @{fetch_copies_by_ids($e, $copy_ids)};
98 my $record_id = get_single_record_id(@copies) or return $e->die_event;
99 my $mvr = get_mvr($record_id) or return $e->die_event;
101 foreach (get_unique_owning_libs(@copies)) {
102 $brt_table{$_} = get_existing_brt($e, $record_id, $_, $mvr) ||
103 prepare_new_brt($record_id, $_, $mvr);
106 while (my ($owning_lib, $brt) = each %brt_table) {
108 if ($e->allowed('CREATE_BOOKING_RESOURCE_TYPE', $owning_lib)) {
109 # We can/should abort if this creation fails, because the
110 # logic isn't going to be trying to create any redundnat
111 # brt's, therefore any error will be more serious than
112 # that. See the different take on creating brsrc's below.
113 return $e->die_event unless (
114 # v-- Important: assignment modifies original hash
115 $brt = $e->create_booking_resource_type($brt)
118 push @created_brt, [$brt->id, $brt->record];
124 $e->allowed('CREATE_BOOKING_RESOURCE', $_->call_number->owning_lib)
126 my $brsrc = new Fieldmapper::booking::resource;
128 $brsrc->type($brt_table{$_->call_number->owning_lib}->id);
129 $brsrc->owner($_->call_number->owning_lib);
130 $brsrc->barcode($_->barcode);
132 # We don't want to abort the transaction or do anything dramatic if
133 # this fails, because quite possibly a user selected many copies on
134 # which to perform this "create booking resource" operation, and
135 # among those copies there may be some that we still need to
136 # create, and some that we don't. So we just do what we can.
137 push @created_brsrc, [$brsrc->id, $_->id] if
138 ($brsrc = $e->create_booking_resource($brsrc));
139 # ^--- Important: assignment.
144 return {brt => \@created_brt, brsrc => \@created_brsrc} or
145 return $e->die_event;
148 sub resource_list_by_attrs {
154 return undef unless ($filters->{type} || $filters->{attribute_values});
157 'select' => { brsrc => [ 'id' ] },
158 'from' => { brsrc => {} },
163 if ($filters->{type}) {
164 $query->{where}->{type} = $filters->{type};
167 if ($filters->{attribute_values}) {
169 $query->{from}->{brsrc}->{bram} = { field => 'resource' };
171 $filters->{attribute_values} = [$filters->{attribute_values}]
172 if (!ref($filters->{attribute_values}));
174 $query->{having}->{'+bram'}->{value}->{'@>'} = {
175 transform => 'array_accum',
176 value => '{'.join(',', @{ $filters->{attribute_values} } ).'}'
180 if ($filters->{available}) {
181 $query->{from}->{brsrc}->{bresv} = { field => 'current_resource' };
183 if (!ref($filters->{available})) { # just one time, start perhaps
184 $query->{where}->{'+bresv'} = {
188 start_time => { '>=' => $filters->{available} },
189 end_time => { '<=' => $filters->{available} },
193 } else { # start and end times
194 $query->{where}->{'+bresv'} = {
199 start_time => { '>=' => $filters->{available}->[0] },
200 end_time => { '<=' => $filters->{available}->[0] },
203 start_time => { '>=' => $filters->{available}->[1] },
204 end_time => { '<=' => $filters->{available}->[1] },
212 if ($filters->{booked}) {
213 $query->{from}->{brsrc}->{bresv} = { field => 'current_resource' };
215 if (!ref($filters->{booked})) { # just one time, start perhaps
216 $query->{where}->{'+bresv'} = {
217 start_time => { '<=' => $filters->{booked} },
218 end_time => { '>=' => $filters->{booked} },
220 } else { # start and end times
221 $query->{where}->{'+bresv'} = {
224 start_time => { '<=' => $filters->{booked}->[0] },
225 end_time => { '>=' => $filters->{booked}->[0] },
228 start_time => { '<=' => $filters->{booked}->[1] },
229 end_time => { '>=' => $filters->{booked}->[1] },
236 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
237 my $ids = $cstore->request( 'open-ils.cstore.json_query.atomic', $query )->gather(1);
238 $ids = [ map { $_->{id} } @$ids ];
241 my $pcrud = OpenSRF::AppSession->connect('open-ils.pcrud');
242 my $allowed_ids = $pcrud->request(
243 'open-ils.pcrud.id_list.brsrc.atomic',
244 $auth => { id => $ids }
250 __PACKAGE__->register_method(
251 method => "resource_list_by_attrs",
252 api_name => "open-ils.booking.resources.filtered_id_list",
256 {type => 'string', desc => 'Authentication token'},
257 {type => 'object', desc => 'Filter object -- see notes for details'}
259 return => { desc => "An array of brsrc ids matching the requested filters." },
263 The filter object parameter can contain the following keys:
264 * type => The id of a booking resource type (brt)
265 * attribute_values => The ids of booking resource type attribute values that the resource must have assigned to it (brav)
266 * available => Either:
267 A timestamp during which the resources are not reserved. If the resource is overbookable, this is ignored.
268 A range of two timestamps which do not overlap any reservations for the resources. If the resource is overbookable, this is ignored.
270 A timestamp during which the resources are reserved.
271 A range of two timestamps which overlap a reservation of the resources.
273 Note that at least one of 'type' or 'attribute_values' is required.
279 sub reservation_list_by_filters {
285 return undef unless ($filters->{user} || $filters->{resource} || $filters->{type} || $filters->{attribute_values});
288 'select' => { bresv => [ 'id' ] },
289 'from' => { bresv => {} },
294 if ($filters->{fields}) {
295 $query->{where} = $filters->{fields};
299 if ($filters->{user}) {
300 $query->{where}->{usr} = $filters->{user};
303 if ($filters->{type}) {
304 $query->{where}->{target_resource_type} = $filters->{type};
307 if ($filters->{resource}) {
308 $query->{where}->{target_resource} = $filters->{resource};
311 if ($filters->{attribute_values}) {
313 $query->{from}->{bresv}->{bravm} = { field => 'reservation' };
315 $filters->{attribute_values} = [$filters->{attribute_values}]
316 if (!ref($filters->{attribute_values}));
318 $query->{having}->{'+bravm'}->{attr_value}->{'@>'} = {
319 transform => 'array_accum',
320 value => '{'.join(',', @{ $filters->{attribute_values} } ).'}'
324 if ($filters->{search_start} || $filters->{search_end}) {
326 $query->{where}->{'-or'} = {};
328 $query->{where}->{'-or'}->{start_time} = { 'between' => [ $filters->{search_start}, $filters->{search_end} ] }
329 if ($filters->{search_start});
331 $query->{where}->{'-or'}->{end_time} = { 'between' => [ $filters->{search_start}, $filters->{search_end} ] }
332 if ($filters->{search_end});
335 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
336 my $ids = $cstore->request( 'open-ils.cstore.json_query.atomic', $query )->gather(1);
337 $ids = [ map { $_->{id} } @$ids ];
342 __PACKAGE__->register_method(
343 method => "reservation_list_by_filters",
344 api_name => "open-ils.booking.reservations.filtered_id_list",
348 {type => 'string', desc => 'Authentication token'},
349 {type => 'object', desc => 'Filter object -- see notes for details'}
351 return => { desc => "An array of brsrc ids matching the requested filters." },
355 The filter object parameter can contain the following keys:
356 * user => The id of a user that has requested a bookable item -- filters on bresv.usr
357 * type => The id of a booking resource type (brt) -- filters on bresv.target_resource_type
358 * resource => The id of a booking resource (brsrc) -- filters on bresv.target_resource
359 * attribute_values => The ids of booking resource type attribute values that the resource must have assigned to it (brav)
360 * search_start => If search_end is not specified, booking interval (start_time to end_time) must contain this timestamp.
361 * search_end => If search_start is not specified, booking interval (start_time to end_time) must contain this timestamp.
362 * fields => An object containing any combination of bresv search filters in standard cstore/pcrud search format.
364 Note that at least one of 'user', 'type', 'resource' or 'attribute_values' is required. If both search_start and search_end are specified,
365 then the result includes any reservations that overlap with that time range. Any filter fields supplied in 'fields' are overridden
366 by the top-level filters ('user', 'type', 'resource').