Bug 29407: Make the pickup locations dropdown JS reusable
[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 C4::Reserves qw( CanItemBeReserved AddReserve CanBookBeReserved );
28 use C4::Auth qw( checkauth );
29
30 use Koha::Items;
31 use Koha::Patrons;
32
33 my $input = CGI->new();
34
35 checkauth($input, 0, { reserveforothers => 'place_holds' }, 'intranet');
36
37 my @bibitems       = $input->multi_param('biblioitem');
38 my @reqbib         = $input->multi_param('reqbib');
39 my $biblionumber   = $input->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 $borrower = Koha::Patrons->find( $borrowernumber );
53 $borrower = $borrower->unblessed if $borrower;
54
55 my $biblionumbers = $input->param('biblionumbers');
56 $biblionumbers ||= $biblionumber . '/';
57
58 my $bad_bibs = $input->param('bad_bibs');
59 my $holds_to_place_count = $input->param('holds_to_place_count') || 1;
60
61 my %bibinfos = ();
62 my @biblionumbers = split '/', $biblionumbers;
63 foreach my $bibnum (@biblionumbers) {
64     my %bibinfo = ();
65     $bibinfo{title}  = $input->param("title_$bibnum");
66     $bibinfo{rank}   = $input->param("rank_$bibnum");
67     $bibinfo{pickup} = $input->param("pickup_$bibnum");
68     $bibinfos{$bibnum} = \%bibinfo;
69 }
70
71 my $found;
72
73 if ( $type eq 'str8' && $borrower ) {
74
75     foreach my $biblionumber ( keys %bibinfos ) {
76         my $count = @bibitems;
77         @bibitems = sort @bibitems;
78         my $i2 = 1;
79         my @realbi;
80         $realbi[0] = $bibitems[0];
81         for ( my $i = 1 ; $i < $count ; $i++ ) {
82             my $i3 = $i2 - 1;
83             if ( $realbi[$i3] ne $bibitems[$i] ) {
84                 $realbi[$i2] = $bibitems[$i];
85                 $i2++;
86             }
87         }
88
89         my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
90         if ( defined $checkitem && $checkitem ne '' ) {
91
92             my $item_pickup_location = $input->param("item_pickup_$checkitem");
93
94             my $item = Koha::Items->find($checkitem);
95
96             if ( $item->biblionumber ne $biblionumber ) {
97                 $biblionumber = $item->biblionumber;
98             }
99
100             my $can_item_be_reserved = CanItemBeReserved($borrower->{'borrowernumber'}, $item->itemnumber, $item_pickup_location)->{status};
101
102             if ( $can_item_be_reserved eq 'OK' || ( $can_item_be_reserved ne 'itemAlreadyOnHold' && $can_override ) ) {
103                 AddReserve(
104                     {
105                         branchcode       => $item_pickup_location,
106                         borrowernumber   => $borrower->{'borrowernumber'},
107                         biblionumber     => $biblionumber,
108                         priority         => $rank[0],
109                         reservation_date => $startdate,
110                         expiration_date  => $expirationdate,
111                         notes            => $notes,
112                         title            => $title,
113                         itemnumber       => $checkitem,
114                         found            => $found,
115                         itemtype         => $itemtype,
116                         non_priority     => $non_priority,
117                     }
118                 );
119
120             }
121
122         } elsif (@biblionumbers > 1) {
123             my $bibinfo = $bibinfos{$biblionumber};
124             if ( $can_override || CanBookBeReserved($borrower->{'borrowernumber'}, $biblionumber)->{status} eq 'OK' ) {
125                 AddReserve(
126                     {
127                         branchcode       => $bibinfo->{pickup},
128                         borrowernumber   => $borrower->{'borrowernumber'},
129                         biblionumber     => $biblionumber,
130                         priority         => $bibinfo->{rank},
131                         reservation_date => $startdate,
132                         expiration_date  => $expirationdate,
133                         notes            => $notes,
134                         title            => $bibinfo->{title},
135                         itemnumber       => $checkitem,
136                         found            => $found,
137                         itemtype         => $itemtype,
138                         non_priority     => $non_priority,
139                     }
140                 );
141             }
142         } else {
143             # place a request on 1st available
144             for ( my $i = 0 ; $i < $holds_to_place_count ; $i++ ) {
145                 if ( $can_override || CanBookBeReserved($borrower->{'borrowernumber'}, $biblionumber)->{status} eq 'OK' ) {
146                     AddReserve(
147                         {
148                             branchcode       => $branch,
149                             borrowernumber   => $borrower->{'borrowernumber'},
150                             biblionumber     => $biblionumber,
151                             priority         => $rank[0],
152                             reservation_date => $startdate,
153                             expiration_date  => $expirationdate,
154                             notes            => $notes,
155                             title            => $title,
156                             itemnumber       => $checkitem,
157                             found            => $found,
158                             itemtype         => $itemtype,
159                             non_priority     => $non_priority,
160                         }
161                     );
162                 }
163             }
164         }
165     }
166
167     if ($bad_bibs) {
168         $biblionumbers .= $bad_bibs;
169     }
170     print $input->redirect("request.pl?biblionumbers=$biblionumbers");
171 }
172 elsif ( $borrowernumber eq '' ) {
173     print $input->header();
174     print "Invalid borrower number please try again";
175
176     # Not sure that Dump() does HTML escaping. Use firebug or something to trace
177     # instead.
178     #print $input->Dump;
179 }