holds policies fixes
[koha.git] / opac / opac-reserve.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 use strict;
19 use CGI;
20 use C4::Biblio;
21 use C4::Items;
22 use C4::Auth;    # checkauth, getborrowernumber.
23 use C4::Koha;
24 use C4::Circulation;
25 use C4::Reserves;
26 use C4::Output;
27 use C4::Dates qw/format_date/;
28 use C4::Context;
29 use C4::Members;
30 use C4::Branch; # GetBranches
31 use C4::Debug;
32 # use Data::Dumper;
33
34 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
35
36 my $query = new CGI;
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38     {
39         template_name   => "opac-reserve.tmpl",
40         query           => $query,
41         type            => "opac",
42         authnotrequired => 0,
43         flagsrequired   => { borrow => 1 },
44         debug           => 1,
45     }
46 );
47
48 sub get_out ($$$) {
49         output_html_with_http_headers(shift,shift,shift); # $query, $cookie, $template->output;
50         exit;
51 }
52
53 # get borrower information ....
54 my ( $borr ) = GetMemberDetails( $borrowernumber );
55
56 # get branches and itemtypes
57 my $branches = GetBranches();
58 my $itemtypes = GetItemTypes();
59
60 # get biblionumber.....
61 my $biblionumber = $query->param('biblionumber');
62 if (! $biblionumber) {
63         $template->param(message=>1,no_biblionumber=>1);
64         &get_out($query, $cookie, $template->output);
65 }
66 $template->param( biblionumber => $biblionumber );
67
68 my $bibdata = GetBiblioData($biblionumber);
69 if (! $bibdata) {
70         $template->param(message=>1,bad_biblionumber=>$biblionumber);
71         &get_out($query, $cookie, $template->output);
72 }
73 $template->param($bibdata);             # FIXME: bad form.
74
75 # get the rank number....
76 my ( $rank, $reserves ) = GetReservesFromBiblionumber( $biblionumber);
77 $template->param( reservecount => $rank );
78
79 foreach my $res (@$reserves) {
80     if ( $res->{'found'} eq 'W' ) {
81         $rank--;
82     }
83 }
84
85 $rank++;
86 $template->param( rank => $rank );
87
88 # pass the pickup branch along....
89 my $branch = $query->param('branch');
90 $template->param( branch => $branch );
91
92 # make sure it's a real branch
93 if ( !$branches->{$branch} ) {
94     $branch = '';
95 }
96 $template->param( branchname => $branches->{$branch}->{'branchname'} );
97
98 # make branch selection options...
99 my @branches;
100 my @select_branch;
101 my %select_branches;
102
103 my @CGIbranchlooparray;
104
105 foreach my $branch ( keys %$branches ) {
106     if ($branch) {
107         my %line;
108         $line{branch} = $branches->{$branch}->{'branchname'};
109         $line{value}  = $branch;
110         if ($line{value} eq C4::Context->userenv->{'branch'}) {
111             $line{selected} = 1;
112         }
113         push @CGIbranchlooparray, \%line;
114     }
115 }
116 @CGIbranchlooparray =
117   sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
118 my $CGIbranchloop = \@CGIbranchlooparray;
119 $template->param( CGIbranch => $CGIbranchloop );
120
121 my @items = GetItemsInfo($biblionumber);
122
123 # an item was previously required to have an itemtype in order to be reserved.
124 # this behavior removed to fix bug 1891 .
125 # my @items = grep { (C4::Context->preference('item-level_itypes') ? $_->{'itype'} : $_->{'itemtype'} ) } @all_items ;
126
127 $template->param( itemcount => scalar(@items) );
128
129 my %itemhash;
130 my $forloan;
131 foreach my $itm (@items) {
132         $debug and warn $itm->{'notforloan'};
133     my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'}, 'a', ( $itm->{'biblioitemnumber'} ) );
134     $itm->{'reservefee'} = sprintf "%.02f", $fee;
135     # pass itype to itemtype for display purposes.  
136     $itm->{'itemtype'} = $itm->{'itype'} if(C4::Context->preference('item-level_itypes'));      
137         $itemhash{$itm->{'itemnumber'}}=$itm;
138     if (!$itm->{'notforloan'} && !($itm->{'itemnotforloan'} > 0)){
139                 $forloan=1;
140         }
141 }
142
143 if ( $query->param('place_reserve') ) {
144     my @bibitems=$query->param('biblioitem');
145     my $notes=$query->param('notes');
146     my $checkitem=$query->param('checkitem');
147     my $found;
148     
149     #if we have an item selectionned, and the pickup branch is the same as the holdingbranch of the document, we force the value $rank and $found.
150     if ($checkitem ne ''){
151         $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
152         my $item = $checkitem;
153         $item = GetItem($item);
154         if ( $item->{'holdingbranch'} eq $branch ){
155             $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
156         }
157     }
158         
159         my $count=@bibitems;
160     @bibitems=sort @bibitems;
161     my $i2=1;
162     my @realbi;
163     $realbi[0]=$bibitems[0];
164     for (my $i=1;$i<$count;$i++) {
165         my $i3=$i2-1;
166         if ($realbi[$i3] ne $bibitems[$i]) {
167             $realbi[$i2]=$bibitems[$i];
168             $i2++;
169         }
170     }
171     # here we actually do the reserveration. Stage 3.
172     if ($query->param('request') eq 'any'){
173         # place a request on 1st available
174         AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
175     } else {
176         AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
177     }
178     print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
179 }
180 else {
181
182     # Here we check that the borrower can actually make reserves Stage 1.
183     my $noreserves     = 0;
184     my $maxoutstanding = C4::Context->preference("maxoutstanding");
185     $template->param( noreserve => 1 ) unless $maxoutstanding;
186     if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
187         my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
188         $template->param( message => 1 );
189         $noreserves = 1;
190         $template->param( too_much_oweing => $amount );
191     }
192     if ( $borr->{gonenoaddress} eq 1 ) {
193         $noreserves = 1;
194         $template->param(
195             message => 1,
196             GNA     => 1
197         );
198     }
199     if ( $borr->{lost} eq 1 ) {
200         $noreserves = 1;
201         $template->param(
202             message => 1,
203             lost    => 1
204         );
205     }
206     if ( $borr->{debarred} eq 1 ) {
207         $noreserves = 1;
208         $template->param(
209             message  => 1,
210             debarred => 1
211         );
212     }
213     my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
214     $template->param( RESERVES => \@reserves );
215     if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
216         $template->param( message => 1 );
217         $noreserves = 1;
218         $template->param( too_many_reserves => scalar(@reserves));
219     }
220     foreach my $res (@reserves) {
221         if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
222             $template->param( message => 1 );
223             $noreserves = 1;
224             $template->param( already_reserved => 1 );
225         }
226     }
227     unless ($noreserves) {
228         $template->param( select_item_types => 1 );
229     }
230 }
231
232
233 my %itemnumbers_of_biblioitem;
234 my @itemnumbers  = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
235 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
236
237 foreach my $itemnumber (@itemnumbers) {
238     my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
239     push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
240 }
241
242 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
243
244 my $notforloan_label_of = get_notforloan_label_of();
245 my $biblioiteminfos_of  = GetBiblioItemInfosOf(@biblioitemnumbers);
246
247 my @itemtypes;
248 foreach my $biblioitemnumber (@biblioitemnumbers) {
249     push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
250 }
251
252 my @bibitemloop;
253
254 foreach my $biblioitemnumber (@biblioitemnumbers) {
255     my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
256
257     $biblioitem->{description} =
258       $itemtypes->{ $biblioitem->{itemtype} }{description};
259
260     my $num_available = 0;
261
262     foreach
263       my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
264     {
265                 my $item = $itemhash{$itemnumber};
266
267         $item->{homebranchname} =
268           $branches->{ $item->{homebranch} }{branchname};
269
270         # if the holdingbranch is different than the homebranch, we show the
271         # holdingbranch of the document too
272         if ( $item->{homebranch} ne $item->{holdingbranch} ) {
273             $item->{holdingbranchname} =
274               $branches->{ $item->{holdingbranch} }{branchname};
275         }
276         
277 #       add information
278         $item->{itemcallnumber} = $item->{itemcallnumber};
279         
280         # if the item is currently on loan, we display its return date and
281         # change the background color
282         my $issues= GetItemIssue($itemnumber);
283         if ( $issues->{'date_due'} ) {
284             $item->{date_due} = format_date($issues->{'date_due'});
285             $item->{backgroundcolor} = 'onloan';
286         }
287
288         # checking reserve
289         my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
290         my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
291
292         if ( defined $reservedate ) {
293             $item->{backgroundcolor} = 'reserved';
294             $item->{reservedate}     = format_date($reservedate);
295             $item->{ReservedForBorrowernumber}     = $reservedfor;
296             $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
297             $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
298             $item->{ExpectedAtLibrary}     = $expectedAt;
299             
300         }
301
302         # Management of the notforloan document
303         if ( $item->{notforloan} || $item->{itemnotforloan}) {
304             $item->{backgroundcolor} = 'other';
305             $item->{notforloanvalue} =
306               $notforloan_label_of->{ $item->{notforloan} };
307         }
308
309         # Management of lost or long overdue items
310         if ( $item->{itemlost} ) {
311
312             # FIXME localized strings should never be in Perl code
313             $item->{message} =
314                 $item->{itemlost} == 1 ? "(lost)"
315               : $item->{itemlost} == 2 ? "(long overdue)"
316               : "";
317             $item->{backgroundcolor} = 'other';
318         }
319
320         # Check of the transfered documents
321         my ( $transfertwhen, $transfertfrom, $transfertto ) =
322           GetTransfers($itemnumber);
323
324         if ( $transfertwhen ne '' ) {
325             $item->{transfertwhen} = format_date($transfertwhen);
326             $item->{transfertfrom} =
327               $branches->{$transfertfrom}{branchname};
328             $item->{transfertto} = $branches->{$transfertto}{branchname};
329                 $item->{nocancel} = 1;
330         }
331
332         # If there is no loan, return and transfer, we show a checkbox.
333         $item->{notforloan} = $item->{notforloan} || 0;
334
335         my $branchitemrule = GetBranchItemRule( $item->{'homebranch'}, $item->{'itype'} );
336         my $policy_holdallowed = 1;
337
338         if ( $branchitemrule->{'holdallowed'} == 0 ||
339                 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $item->{'homebranch'} ) ) {
340             $policy_holdallowed = 0;
341         }
342
343         if (IsAvailableForItemLevelRequest($itemnumber) and $policy_holdallowed) {
344             $item->{available} = 1;
345             $num_available++;
346         }
347
348         # FIXME: move this to a pm
349         my $dbh = C4::Context->dbh;
350         my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
351         $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
352         while (my $wait_hashref = $sth2->fetchrow_hashref) {
353             $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
354         }
355         $item->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $item->{itype} }{imageurl} );
356         push @{ $biblioitem->{itemloop} }, $item;
357     }
358
359     if ( $num_available == 0 ) {
360         $template->param( none_available => 1, message => 1 );
361     }
362
363     push @bibitemloop, $biblioitem;
364 }
365
366 # display infos
367 $template->param(
368         forloan           => $forloan,
369     bibitemloop       => \@bibitemloop,
370 );
371 output_html_with_http_headers $query, $cookie, $template->output;
372
373 # Local Variables:
374 # tab-width: 8
375 # End: