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