Bug 30152: Elasticsearch - queries with OR don't work with limits
[koha.git] / reserve / placerequest.pl
1 #!/usr/bin/perl
2
3 #script to place reserves/requests
4 #written 2/1/00 by chris@katipo.oc.nz
5
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23
24 use Modern::Perl;
25
26 use CGI qw ( -utf8 );
27 use URI;
28 use C4::Reserves qw( CanItemBeReserved AddReserve CanBookBeReserved );
29 use C4::Auth qw( checkauth );
30
31 use Koha::Items;
32 use Koha::Patrons;
33
34 my $input = CGI->new();
35
36 checkauth($input, 0, { reserveforothers => 'place_holds' }, 'intranet');
37
38 my @reqbib         = $input->multi_param('reqbib');
39 my @biblionumbers   = $input->multi_param('biblionumber');
40 my $borrowernumber = $input->param('borrowernumber');
41 my $notes          = $input->param('notes');
42 my $branch         = $input->param('pickup');
43 my $startdate      = $input->param('reserve_date') || '';
44 my @rank           = $input->multi_param('rank-request');
45 my $type           = $input->param('type');
46 my $title          = $input->param('title');
47 my $checkitem      = $input->param('checkitem');
48 my $expirationdate = $input->param('expiration_date');
49 my $itemtype       = $input->param('itemtype') || undef;
50 my $non_priority   = $input->param('non_priority');
51
52 my $patron = Koha::Patrons->find( $borrowernumber );
53
54 my $bad_bibs_param = $input->param('bad_bibs');
55 my @bad_bibs = split '/', $bad_bibs_param;
56 my $holds_to_place_count = $input->param('holds_to_place_count') || 1;
57
58 my %bibinfos = ();
59 foreach my $bibnum (@biblionumbers) {
60     my %bibinfo = ();
61     $bibinfo{title}  = $input->param("title_$bibnum");
62     $bibinfo{rank}   = $input->param("rank_$bibnum");
63     $bibinfo{pickup} = $input->param("pickup_$bibnum");
64     $bibinfos{$bibnum} = \%bibinfo;
65 }
66
67 my $found;
68
69 if ( $type eq 'str8' && $patron ) {
70
71     foreach my $biblionumber ( keys %bibinfos ) {
72
73         my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
74         if ( defined $checkitem && $checkitem ne '' ) {
75
76             my $item_pickup_location = $input->param("item_pickup_$checkitem");
77
78             my $item = Koha::Items->find($checkitem);
79
80             if ( $item->biblionumber ne $biblionumber ) {
81                 $biblionumber = $item->biblionumber;
82             }
83
84             my $can_item_be_reserved = CanItemBeReserved($patron, $item, $item_pickup_location)->{status};
85
86             if ( $can_item_be_reserved eq 'OK' || ( $can_item_be_reserved ne 'itemAlreadyOnHold' && $can_override ) ) {
87                 AddReserve(
88                     {
89                         branchcode       => $item_pickup_location,
90                         borrowernumber   => $patron->borrowernumber,
91                         biblionumber     => $biblionumber,
92                         priority         => $rank[0],
93                         reservation_date => $startdate,
94                         expiration_date  => $expirationdate,
95                         notes            => $notes,
96                         title            => $title,
97                         itemnumber       => $checkitem,
98                         found            => $found,
99                         itemtype         => $itemtype,
100                         non_priority     => $non_priority,
101                     }
102                 );
103
104             }
105
106         } elsif (@biblionumbers > 1) {
107             my $bibinfo = $bibinfos{$biblionumber};
108             if ( $can_override || CanBookBeReserved($patron->borrowernumber, $biblionumber)->{status} eq 'OK' ) {
109                 AddReserve(
110                     {
111                         branchcode       => $bibinfo->{pickup},
112                         borrowernumber   => $patron->borrowernumber,
113                         biblionumber     => $biblionumber,
114                         priority         => $bibinfo->{rank},
115                         reservation_date => $startdate,
116                         expiration_date  => $expirationdate,
117                         notes            => $notes,
118                         title            => $bibinfo->{title},
119                         itemnumber       => $checkitem,
120                         found            => $found,
121                         itemtype         => $itemtype,
122                         non_priority     => $non_priority,
123                     }
124                 );
125             }
126         } else {
127             # place a request on 1st available
128             for ( my $i = 0 ; $i < $holds_to_place_count ; $i++ ) {
129                 if ( $can_override || CanBookBeReserved($patron->borrowernumber, $biblionumber)->{status} eq 'OK' ) {
130                     AddReserve(
131                         {
132                             branchcode       => $branch,
133                             borrowernumber   => $patron->borrowernumber,
134                             biblionumber     => $biblionumber,
135                             priority         => $rank[0],
136                             reservation_date => $startdate,
137                             expiration_date  => $expirationdate,
138                             notes            => $notes,
139                             title            => $title,
140                             itemnumber       => $checkitem,
141                             found            => $found,
142                             itemtype         => $itemtype,
143                             non_priority     => $non_priority,
144                         }
145                     );
146                 }
147             }
148         }
149     }
150
151     if (@bad_bibs) {
152         push @biblionumbers, @bad_bibs;
153     }
154     my $redirect_url = URI->new("request.pl");
155     $redirect_url->query_form( biblionumber => [@biblionumbers]);
156     print $input->redirect($redirect_url);
157 }
158 elsif ( $borrowernumber eq '' ) {
159     print $input->header();
160     print "Invalid borrower number please try again";
161
162     # Not sure that Dump() does HTML escaping. Use firebug or something to trace
163     # instead.
164     #print $input->Dump;
165 }