Bug 36864: Add classes/CSS/fix markup on request.tt
[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 @holdable_bibs  = $input->multi_param('holdable_bibs');
41 my $borrowernumber = $input->param('borrowernumber');
42 my $notes          = $input->param('notes');
43 my $branch         = $input->param('pickup');
44 my $startdate      = $input->param('reserve_date') || '';
45 my @rank           = $input->multi_param('rank-request');
46 my $title          = $input->param('title');
47 my @checkitems = $input->multi_param('checkitem');
48 my $item_group_id  = $input->param('item_group_id');
49 my $expirationdate = $input->param('expiration_date');
50 my $itemtype       = $input->param('itemtype') || undef;
51 my $non_priority   = $input->param('non_priority');
52 my $op             = $input->param('op') || q{};
53 my $multi_holds    = $input->param('multi_holds');
54
55 my $patron = Koha::Patrons->find( $borrowernumber );
56
57 my $holds_to_place_count = $input->param('holds_to_place_count') || 1;
58
59 my %bibinfos = ();
60 foreach my $bibnum ( @holdable_bibs ) {
61     my %bibinfo = ();
62     $bibinfo{title}  = $input->param("title_$bibnum");
63     $bibinfo{rank}   = $input->param("rank_$bibnum");
64     $bibinfo{pickup} = $input->param("pickup_$bibnum");
65     $bibinfos{$bibnum} = \%bibinfo;
66 }
67
68
69
70 if ( $op eq 'cud-placerequest' && $patron ) {
71     my %failed_holds;
72     foreach my $biblionumber ( keys %bibinfos ) {
73
74         my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
75         if ( @checkitems ) {
76             for ( my $i = 0 ; $i < scalar @checkitems ; $i++ ) {
77                 my $checkitem = $checkitems[$i];
78                 if ( my $item_pickup_location = $input->param("item_pickup_$checkitem") ) {
79
80                     my $item = Koha::Items->find($checkitem);
81
82                     if ( $item->biblionumber ne $biblionumber ) {
83                         $biblionumber = $item->biblionumber;
84                     }
85
86                     my $can_item_be_reserved = CanItemBeReserved( $patron, $item, $item_pickup_location )->{status};
87
88                     if ( $can_item_be_reserved eq 'OK'
89                         || ( $can_item_be_reserved ne 'itemAlreadyOnHold' && $can_override ) )
90                     {
91                         AddReserve(
92                             {
93                                 branchcode       => $item_pickup_location,
94                                 borrowernumber   => $patron->borrowernumber,
95                                 biblionumber     => $biblionumber,
96                                 priority         => $rank[$i],
97                                 reservation_date => $startdate,
98                                 expiration_date  => $expirationdate,
99                                 notes            => $notes,
100                                 title            => $title,
101                                 itemnumber       => $checkitem,
102                                 found            => undef,
103                                 itemtype         => $itemtype,
104                                 non_priority     => $non_priority,
105                             }
106                         );
107
108                     } else {
109                         $failed_holds{$can_item_be_reserved} = 1;
110                     }
111                 }
112             }
113
114         } elsif (@biblionumbers > 1 || $multi_holds) {
115             my $bibinfo = $bibinfos{$biblionumber};
116             if ( $can_override || CanBookBeReserved($patron->borrowernumber, $biblionumber)->{status} eq 'OK' ) {
117                 AddReserve(
118                     {
119                         branchcode       => $bibinfo->{pickup},
120                         borrowernumber   => $patron->borrowernumber,
121                         biblionumber     => $biblionumber,
122                         priority         => $bibinfo->{rank},
123                         reservation_date => $startdate,
124                         expiration_date  => $expirationdate,
125                         notes            => $notes,
126                         title            => $bibinfo->{title},
127                         itemnumber       => undef,
128                         found            => undef,
129                         itemtype         => $itemtype,
130                         non_priority     => $non_priority,
131                     }
132                 );
133             }
134         } else {
135             # place a request on 1st available
136             for ( my $i = 0 ; $i < $holds_to_place_count ; $i++ ) {
137                 if ( $can_override || CanBookBeReserved($patron->borrowernumber, $biblionumber)->{status} eq 'OK' ) {
138                     AddReserve(
139                         {
140                             branchcode       => $branch,
141                             borrowernumber   => $patron->borrowernumber,
142                             biblionumber     => $biblionumber,
143                             priority         => $rank[0],
144                             reservation_date => $startdate,
145                             expiration_date  => $expirationdate,
146                             notes            => $notes,
147                             title            => $title,
148                             itemnumber       => undef,
149                             found            => undef,
150                             itemtype         => $itemtype,
151                             non_priority     => $non_priority,
152                             item_group_id    => $item_group_id,
153                         }
154                     );
155                 }
156             }
157         }
158     }
159
160     my $redirect_url = URI->new("request.pl");
161     my @failed_hold_msgs = ();
162
163     #NOTE: Deduplicate failed hold reason statuses/codes
164     foreach my $msg ( keys %failed_holds ) {
165         push( @failed_hold_msgs, $msg );
166     }
167     $redirect_url->query_form( biblionumber => [@biblionumbers], failed_holds => \@failed_hold_msgs );
168     print $input->redirect($redirect_url);
169 }
170 elsif ( $borrowernumber eq '' ) {
171     print $input->header();
172     print "Invalid borrower number please try again";
173
174     # Not sure that Dump() does HTML escaping. Use firebug or something to trace
175     # instead.
176     #print $input->Dump;
177 }