Changes to allow librarian to turn off item-level holds in the OPAC. Requires the...
[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 require Exporter;
20 use CGI;
21 use C4::Biblio;
22 use C4::Items;
23 use C4::Auth;    # checkauth, getborrowernumber.
24 use C4::Koha;
25 use C4::Circulation;
26 use C4::Reserves;
27 use C4::Output;
28 use C4::Dates qw/format_date/;
29 use C4::Context;
30 use C4::Members;
31 use C4::Branch; # GetBranches
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 # get borrower information ....
49 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
50
51 # get branches and itemtypes
52 my $branches = GetBranches();
53 my $itemtypes = GetItemTypes();
54
55 # get biblionumber.....
56 my $biblionumber = $query->param('biblionumber');
57
58 my $bibdata = GetBiblioData($biblionumber);
59 $template->param($bibdata);
60 $template->param( biblionumber => $biblionumber );
61
62 # get the rank number....
63 my ( $rank, $reserves ) = GetReservesFromBiblionumber( $biblionumber);
64 $template->param( reservecount => $rank );
65
66 foreach my $res (@$reserves) {
67     if ( $res->{'found'} eq 'W' ) {
68         $rank--;
69     }
70 }
71
72 $rank++;
73 $template->param( rank => $rank );
74
75 # pass the pickup branch along....
76 my $branch = $query->param('branch');
77 $template->param( branch => $branch );
78
79 # make sure it's a real branch
80 if ( !$branches->{$branch} ) {
81     $branch = '';
82 }
83 $template->param( branchname => $branches->{$branch}->{'branchname'} );
84
85 # make branch selection options...
86 my @branches;
87 my @select_branch;
88 my %select_branches;
89
90 my @CGIbranchlooparray;
91
92 foreach my $branch ( keys %$branches ) {
93     if ($branch) {
94         my %line;
95         $line{branch} = $branches->{$branch}->{'branchname'};
96         $line{value}  = $branch;
97         if ($line{value} eq C4::Context->userenv->{'branch'}) {
98             $line{selected} = 1;
99         }
100         push @CGIbranchlooparray, \%line;
101     }
102 }
103 @CGIbranchlooparray =
104   sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
105 my $CGIbranchloop = \@CGIbranchlooparray;
106 $template->param( CGIbranch => $CGIbranchloop );
107
108 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
109 # get the itemtype data....
110 my @items = GetItemsInfo($biblionumber);
111 #######################################################
112 # old version, add so that old templates still work
113 my %types_old;
114 foreach my $itm (@items) {
115     my $ity = $itm->{'itemtype'};
116     unless ( $types_old{$ity} ) {
117         $types_old{$ity}->{'itemtype'} = $ity;
118         $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} } = 1;
119         $types_old{$ity}->{'description'} = $itm->{'description'};
120     }
121     else {
122         $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} }++;
123     }
124 }
125
126 foreach my $type ( values %types_old ) {
127     my $copies = "";
128     foreach my $bc ( keys %{ $type->{'branchinfo'} } ) {
129         $copies .=
130             $branches->{$bc}->{'branchname'} . "("
131           . $type->{'branchinfo'}->{$bc} . ")";
132     }
133     $type->{'copies'} = $copies;
134 }
135
136 my @types_old = values %types_old;
137
138 # end old version
139 ################################
140
141 my @temp;
142 foreach my $itm (@items) {
143     push @temp, $itm if $itm->{'itemtype'};
144 }
145 @items = @temp;
146 my $itemcount = @items;
147 $template->param( itemcount => $itemcount );
148
149 my %types;
150 my %itemtypes;
151 my @duedates;
152 #die @items;
153 my %itemhash;
154 my $forloan;
155 foreach my $itm (@items) {
156     push @duedates, { date_due => format_date( $itm->{'date_due'} ) }
157       if defined $itm->{'date_due'};
158     $itm->{ $itm->{'publictype'} } = 1;
159         warn $itm->{'notforloan'};
160     my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'},
161         'a', ( $itm->{'biblioitemnumber'} ) );
162     $fee = sprintf "%.02f", $fee;
163     $itm->{'reservefee'} = $fee;
164     my $pty = $itm->{'publictype'};
165     $itemtypes{ $itm->{'itemtype'} } = $itm;
166     unless ( $types{$pty} ) {
167         $types{$pty}->{'count'} = 1;
168         $types{$pty}->{ $itm->{'itemtype'} } = 1;
169         push @{ $types{$pty}->{'items'} }, $itm;
170     }
171     else {
172         unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
173             $types{$pty}->{'count'}++;
174             $types{$pty}->{ $itm->{'itemtype'} } = 1;
175             push @{ $types{$pty}->{'items'} }, $itm;
176         }
177     }
178         $itemhash{$itm->{'itemnumber'}}=$itm;
179         if (!$itm->{'notforloan'} && !$itm->{'itemnotforloan'}){
180                 $forloan=1;
181         }
182 }
183
184 $template->param( ITEMS => \@duedates );
185
186 my $width = keys %types;
187 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
188 my $typecount;
189 foreach my $pt (@publictypes) {
190     $typecount += $pt->{'count'};
191 }
192 $template->param( onlyone => 1 ) if $typecount == 1;
193
194 my @typerows;
195 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
196     my @row;
197     foreach my $pty (@publictypes) {
198         my @items = @{ $pty->{'items'} };
199         push @row, $items[$rownum] if defined $items[$rownum];
200     }
201     my $last = @row;
202     $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
203     my $fill = ( $width - $last ) * 2;
204     $fill-- if $fill;
205     push @typerows, { ROW => \@row, fill => $fill };
206 }
207 $template->param( TYPE_ROWS => \@typerows );
208 $width = 2 * $width - 1;
209 $template->param( totalwidth => 2 * $width - 1, );
210
211 if ( $query->param('place_reserve') ) {
212     my @bibitems=$query->param('biblioitem');
213     my $notes=$query->param('notes');
214     my $checkitem=$query->param('checkitem');
215     my $found;
216     
217     #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.
218     if ($checkitem ne ''){
219         $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
220         my $item = $checkitem;
221         $item = GetItem($item);
222         if ( $item->{'holdingbranch'} eq $branch ){
223             $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
224         }
225     }
226         
227         my $count=@bibitems;
228     @bibitems=sort @bibitems;
229     my $i2=1;
230     my @realbi;
231     $realbi[0]=$bibitems[0];
232     for (my $i=1;$i<$count;$i++) {
233         my $i3=$i2-1;
234         if ($realbi[$i3] ne $bibitems[$i]) {
235             $realbi[$i2]=$bibitems[$i];
236             $i2++;
237         }
238     }
239     # here we actually do the reserveration. Stage 3.
240     if ($query->param('request') eq 'any'){
241         # place a request on 1st available
242         AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
243     } else {
244         AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
245     }
246     print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
247 }
248 else {
249
250     # Here we check that the borrower can actually make reserves Stage 1.
251     my $noreserves     = 0;
252     my $maxoutstanding = C4::Context->preference("maxoutstanding");
253     $template->param( noreserve => 1 ) unless $maxoutstanding;
254     if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
255         my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
256         $template->param( message => 1 );
257         $noreserves = 1;
258         $template->param( too_much_oweing => $amount );
259     }
260     if ( $borr->{gonenoaddress} eq 1 ) {
261         $noreserves = 1;
262         $template->param(
263             message => 1,
264             GNA     => 1
265         );
266     }
267     if ( $borr->{lost} eq 1 ) {
268         $noreserves = 1;
269         $template->param(
270             message => 1,
271             lost    => 1
272         );
273     }
274     if ( $borr->{debarred} eq 1 ) {
275         $noreserves = 1;
276         $template->param(
277             message  => 1,
278             debarred => 1
279         );
280     }
281     my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
282     $template->param( RESERVES => \@reserves );
283     if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
284         $template->param( message => 1 );
285         $noreserves = 1;
286         $template->param( too_many_reserves => scalar(@reserves));
287     }
288     foreach my $res (@reserves) {
289         if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
290             $template->param( message => 1 );
291             $noreserves = 1;
292             $template->param( already_reserved => 1 );
293         }
294     }
295     unless ($noreserves) {
296         $template->param( TYPES             => \@types_old );
297         $template->param( select_item_types => 1 );
298     }
299 }
300
301
302 my %itemnumbers_of_biblioitem;
303 my @itemnumbers  = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
304 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
305
306 foreach my $itemnumber (@itemnumbers) {
307     my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
308     push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
309 }
310
311 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
312
313 my $notforloan_label_of = get_notforloan_label_of();
314 my $biblioiteminfos_of  = GetBiblioItemInfosOf(@biblioitemnumbers);
315
316 my @itemtypes;
317 foreach my $biblioitemnumber (@biblioitemnumbers) {
318     push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
319 }
320
321 my @bibitemloop;
322
323 foreach my $biblioitemnumber (@biblioitemnumbers) {
324     my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
325
326     $biblioitem->{description} =
327       $itemtypes->{ $biblioitem->{itemtype} }{description};
328
329     foreach
330       my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
331     {
332                 my $item = $itemhash{$itemnumber};
333
334         $item->{homebranchname} =
335           $branches->{ $item->{homebranch} }{branchname};
336
337         # if the holdingbranch is different than the homebranch, we show the
338         # holdingbranch of the document too
339         if ( $item->{homebranch} ne $item->{holdingbranch} ) {
340             $item->{holdingbranchname} =
341               $branches->{ $item->{holdingbranch} }{branchname};
342         }
343         
344 #       add information
345         $item->{itemcallnumber} = $item->{itemcallnumber};
346         
347         # if the item is currently on loan, we display its return date and
348         # change the background color
349         my $issues= GetItemIssue($itemnumber);
350         if ( $issues->{'date_due'} ) {
351             $item->{date_due} = format_date($issues->{'date_due'});
352             $item->{backgroundcolor} = 'onloan';
353         }
354
355         # checking reserve
356         my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
357         my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
358
359         if ( defined $reservedate ) {
360             $item->{backgroundcolor} = 'reserved';
361             $item->{reservedate}     = format_date($reservedate);
362             $item->{ReservedForBorrowernumber}     = $reservedfor;
363             $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
364             $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
365             $item->{ExpectedAtLibrary}     = $expectedAt;
366             
367         }
368
369         # Management of the notforloan document
370         if ( $item->{notforloan} || $item->{itemnotforloan}) {
371             $item->{backgroundcolor} = 'other';
372             $item->{notforloanvalue} =
373               $notforloan_label_of->{ $item->{notforloan} };
374         }
375
376         # Management of lost or long overdue items
377         if ( $item->{itemlost} ) {
378
379             # FIXME localized strings should never be in Perl code
380             $item->{message} =
381                 $item->{itemlost} == 1 ? "(lost)"
382               : $item->{itemlost} == 2 ? "(long overdue)"
383               : "";
384             $item->{backgroundcolor} = 'other';
385         }
386
387         # Check of the transfered documents
388         my ( $transfertwhen, $transfertfrom, $transfertto ) =
389           GetTransfers($itemnumber);
390
391         if ( $transfertwhen ne '' ) {
392             $item->{transfertwhen} = format_date($transfertwhen);
393             $item->{transfertfrom} =
394               $branches->{$transfertfrom}{branchname};
395             $item->{transfertto} = $branches->{$transfertto}{branchname};
396                 $item->{nocancel} = 1;
397         }
398
399         # If there is no loan, return and transfer, we show a checkbox.
400         $item->{notforloan} = $item->{notforloan} || 0;
401
402         # FIXME: every library will define this differently
403         # An item is available only if:
404         if (
405             not defined $reservedate    # not reserved yet
406             and $issues->{'date_due'} eq ''         # not currently on loan
407             and not $item->{itemlost}   # not lost
408             and not $item->{notforloan} # not forbidden to loan
409             and $transfertwhen eq ''    # not currently on transfert
410           )
411         {
412             $item->{available} = 1;
413         }
414
415         # FIXME: move this to a pm
416         my $dbh = C4::Context->dbh;
417         my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
418         $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
419         while (my $wait_hashref = $sth2->fetchrow_hashref) {
420             $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
421         }
422         $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
423         push @{ $biblioitem->{itemloop} }, $item;
424     }
425
426     push @bibitemloop, $biblioitem;
427 }
428
429 # display infos
430 $template->param(
431         forloan           => $forloan,
432     bibitemloop       => \@bibitemloop,
433 );
434 output_html_with_http_headers $query, $cookie, $template->output;
435
436 # Local Variables:
437 # tab-width: 8
438 # End: