]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Booking.pm
add method for retrieving lists of resources by type/attr/available/booked filters
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Booking.pm
1 package OpenILS::Application::Booking;
2
3 use strict;
4 use warnings;
5
6 use OpenILS::Application;
7 use base qw/OpenILS::Application/;
8
9 use OpenILS::Utils::CStoreEditor qw/:funcs/;
10 use OpenILS::Utils::Fieldmapper;
11 use OpenILS::Application::AppUtils;
12 my $U = "OpenILS::Application::AppUtils";
13
14 use OpenSRF::Utils::Logger qw/$logger/;
15
16 sub prepare_new_brt {
17     my ($record_id, $owning_lib, $mvr) = @_;
18
19     my $brt = new Fieldmapper::booking::resource_type;
20     $brt->isnew(1);
21     $brt->name($mvr->title);
22     $brt->record($record_id);
23     $brt->catalog_item('t');
24     $brt->owner($owning_lib);
25
26     return $brt;
27 }
28
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}
33     );
34
35     return $results->[0] if (length @$results > 0);
36     return undef;
37 }
38
39 sub get_mvr {
40     return $U->simplereq(
41         'open-ils.search',
42         'open-ils.search.biblio.record.mods_slim.retrieve.authoritative',
43         shift # record id
44     );
45 }
46
47 sub get_unique_owning_libs {
48     my %hash = ();
49     $hash{$_->call_number->owning_lib} = 1 foreach (@_);    # @_ are copies
50     return keys %hash;
51 }
52
53 sub fetch_copies_by_ids {
54     my ($e, $copy_ids) = @_;
55     my $results = $e->search_asset_copy([
56         {id => $copy_ids},
57         {flesh => 1, flesh_fields => {acp => ['call_number']}}
58     ]);
59     return $results if ref($results) eq 'ARRAY';
60     return [];
61 }
62
63 sub get_single_record_id {
64     my $record_id = undef;
65     foreach (@_) {  # @_ are copies
66         return undef if
67             (defined $record_id && $record_id != $_->call_number->record);
68         $record_id = $_->call_number->record;
69     }
70     return $record_id;
71 }
72
73 __PACKAGE__->register_method(
74     method   => "create_brt_and_brsrc",
75     api_name => "open-ils.booking.create_brt_and_brsrc_from_copies",
76     signature => {
77         params => [
78             {type => 'string', desc => 'Authentication token'},
79             {type => 'array', desc => 'Copy IDs'},
80         ],
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 " .
85             "pairs"}
86     }
87 );
88
89 sub create_brt_and_brsrc {
90     my ($self, $conn, $authtoken, $copy_ids) = @_;
91     my (@created_brt, @created_brsrc);
92     my %brt_table = ();
93
94     my $e = new_editor(xact => 1, authtoken => $authtoken);
95     return $e->die_event unless $e->checkauth;
96
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;
100
101     foreach (get_unique_owning_libs(@copies)) {
102         $brt_table{$_} = get_existing_brt($e, $record_id, $_, $mvr) ||
103             prepare_new_brt($record_id, $_, $mvr);
104     }
105
106     while (my ($owning_lib, $brt) = each %brt_table) {
107         if ($brt->isnew) {
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)
116                 );
117             }
118             push @created_brt, [$brt->id, $brt->record];
119         }
120     }
121
122     foreach (@copies) {
123         if (
124             $e->allowed('CREATE_BOOKING_RESOURCE', $_->call_number->owning_lib)
125         ) {
126             my $brsrc = new Fieldmapper::booking::resource;
127             $brsrc->isnew(1);
128             $brsrc->type($brt_table{$_->call_number->owning_lib}->id);
129             $brsrc->owner($_->call_number->owning_lib);
130             $brsrc->barcode($_->barcode);
131
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.
140         }
141     }
142
143     $e->commit and
144         return {brt => \@created_brt, brsrc => \@created_brsrc} or
145         return $e->die_event;
146 }
147
148 sub res_list_by_attrs {
149     my $self = shift;
150     my $client = shift;
151     my $auth = shift;
152     my $filters = shift;
153
154     return undef unless ($filters->{type} || $filters->{attribute_values});
155     return undef unless ($filters->{type} || $filters->{attribute_values});
156
157     my $query = {
158         'select'   => { brsrc => [ 'id' ] },
159         'from'     => { brsrc => { bram => {} } },
160         'distinct' => 1
161     };
162
163     if ($filters->{type}) {
164         $query->{where}->{type} = $filters->{type};
165     }
166
167     if ($filters->{attribute_values}) {
168
169         $filters->{attribute_values} = [$filters->{attribute_values}]
170             if (!ref($filters->{attribute_values}));
171
172         $query->{having}->{'+bram'}->{value}->{'@>'} = {
173             transform => 'array_accum',
174             value => '{'.join(',', @{ $filters->{attribute_values} } ).'}'
175         };
176     }
177
178     if ($filters->{available}) {
179         $query->{from}->{brsrc}->{bresv} = { field => 'current_resource' };
180
181         if (!ref($filters->{available})) { # just one time, start perhaps
182             $query->{where}->{'+bresv'} = {
183                 '-or' => {
184                     'overbook' => 't',
185                     '-or' => {
186                         start_time => { '>=' => $filters->{available} },
187                         end_time   => { '<=' => $filters->{available} },
188                     }
189                 }
190             };
191         } else { # start and end times
192             $query->{where}->{'+bresv'} = {
193                 '-or' => {
194                     'overbook' => 't',
195                     '-and' => {
196                         '-or' => {
197                             start_time => { '>=' => $filters->{available}->[0] },
198                             end_time   => { '<=' => $filters->{available}->[0] },
199                         },
200                         '-or' => {
201                             start_time => { '>=' => $filters->{available}->[1] },
202                             end_time   => { '<=' => $filters->{available}->[1] },
203                         }
204                     }
205                 }
206             };
207         }
208     }
209
210     if ($filters->{booked}) {
211         $query->{from}->{brsrc}->{bresv} = { field => 'current_resource' };
212
213         if (!ref($filters->{booked})) { # just one time, start perhaps
214             $query->{where}->{'+bresv'} = {
215                 start_time => { '<=' => $filters->{booked} },
216                 end_time   => { '>=' => $filters->{booked} },
217             };
218         } else { # start and end times
219             $query->{where}->{'+bresv'} = {
220                 '-or' => {
221                     '-and' => {
222                         start_time => { '<=' => $filters->{booked}->[0] },
223                         end_time   => { '>=' => $filters->{booked}->[0] },
224                     },
225                     '-and' => {
226                         start_time => { '<=' => $filters->{booked}->[1] },
227                         end_time   => { '>=' => $filters->{booked}->[1] },
228                     }
229                 }
230             };
231         }
232     }
233
234     my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
235     my $ids = $cstore->request( 'open-ils.cstore.json_query.atomic', $query )->gather(1);
236     $ids = [ map { $_->{id} } @$ids ];
237     $cstore->disconnect;
238
239     my $pcrud = OpenSRF::AppSession->connect('open-ils.pcrud');
240     my $allowed_ids = $pcrud->request(
241         'open-ils.pcrud.id_list.brsrc.atomic',
242         $auth => { id => $ids }
243     )->gather(1);
244     $pcrud->disconnect;
245
246     return $allowed_ids;
247 }
248 __PACKAGE__->register_method(
249     method   => "res_list_by_attrs",
250     api_name => "open-ils.booking.resources.filtered_id_list",
251     argc     => 2,
252     signature=> {
253         params => [
254             {type => 'string', desc => 'Authentication token'},
255             {type => 'object', desc => 'Filter object -- see notes for details'}
256         ],
257         return => { desc => "An array of brsrc ids matching the requested filters." },
258     },
259     notes    => <<'NOTES'
260
261 The filter object parameter can contain the following keys:
262  * type             => The id of a booking resource type (brt)
263  * attribute_values => The id of booking resource type attribute values that the resource must have assigned to it (brav)
264  * available        => Either:
265                         A timestamp during which the resources are not reserved.  If the resource is overbookable, this is ignored.
266                         A range of two timestamps which do not overlap any reservations for the resources.  If the resource is overbookable, this is ignored.
267  * booked           => Either:
268                         A timestamp during which the resources are reserved.
269                         A range of two timestamps which overlap a reservation of the resources.
270
271 Note that at least one of 'type' or 'attribute_values' is required.
272
273 NOTES
274
275 );
276
277
278 1;