[followup](bug #3348) fix spent values and spent resume
[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     my $canreserve=0;
149     
150     #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.
151     if ($checkitem ne ''){
152         $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
153         my $item = $checkitem;
154         $item = GetItem($item);
155         if ( $item->{'holdingbranch'} eq $branch ){
156             $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
157         }
158     }
159         
160         my $count=@bibitems;
161     @bibitems=sort @bibitems;
162     my $i2=1;
163     my @realbi;
164     $realbi[0]=$bibitems[0];
165     for (my $i=1;$i<$count;$i++) {
166         my $i3=$i2-1;
167         if ($realbi[$i3] ne $bibitems[$i]) {
168             $realbi[$i2]=$bibitems[$i];
169             $i2++;
170         }
171     }
172     
173     if ($checkitem ne ''){
174         $canreserve = 1 if CanItemBeReserved($borrowernumber,$checkitem);
175         $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
176         my $item = GetItem($checkitem);
177         if ( $item->{'holdingbranch'} eq $branch ){
178             $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
179         }
180     }
181     else {
182         $canreserve = 1 if CanBookBeReserved($borrowernumber,$biblionumber);
183         # Inserts a null into the 'itemnumber' field of 'reserves' table.
184         $checkitem = undef;
185     }
186
187     # here we actually do the reserveration. Stage 3.
188     AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,
189                 $bibdata->{'title'},$checkitem, $found) if ($canreserve);
190     print $query->redirect("/cgi-bin/koha/opac-user.pl#opac-user-holds");
191 }
192 else {
193
194     # Here we check that the borrower can actually make reserves Stage 1.
195     my $noreserves     = 0;
196     my $maxoutstanding = C4::Context->preference("maxoutstanding");
197     $template->param( noreserve => 1 ) unless $maxoutstanding;
198     if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
199         my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
200         $template->param( message => 1 );
201         $noreserves = 1;
202         $template->param( too_much_oweing => $amount );
203     }
204     if ( $borr->{gonenoaddress} eq 1 ) {
205         $noreserves = 1;
206         $template->param(
207             message => 1,
208             GNA     => 1
209         );
210     }
211     if ( $borr->{lost} eq 1 ) {
212         $noreserves = 1;
213         $template->param(
214             message => 1,
215             lost    => 1
216         );
217     }
218     if ( $borr->{debarred} eq 1 ) {
219         $noreserves = 1;
220         $template->param(
221             message  => 1,
222             debarred => 1
223         );
224     }
225     my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
226     $template->param( RESERVES => \@reserves );
227     if ( scalar(@reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
228         $template->param( message => 1 );
229         $noreserves = 1;
230         $template->param( too_many_reserves => scalar(@reserves));
231     }
232     foreach my $res (@reserves) {
233         if ( $res->{'biblionumber'} == $biblionumber && $res->{'borrowernumber'} == $borrowernumber) {
234             $template->param( message => 1 );
235             $noreserves = 1;
236             $template->param( already_reserved => 1 );
237         }
238     }
239     unless ($noreserves) {
240         $template->param( select_item_types => 1 );
241     }
242 }
243
244
245 my %itemnumbers_of_biblioitem;
246 my @itemnumbers  = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
247 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
248
249 foreach my $itemnumber (@itemnumbers) {
250     my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
251     push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
252 }
253
254 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
255
256 my $notforloan_label_of = get_notforloan_label_of();
257 my $biblioiteminfos_of  = GetBiblioItemInfosOf(@biblioitemnumbers);
258
259 my @itemtypes;
260 foreach my $biblioitemnumber (@biblioitemnumbers) {
261     push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
262 }
263
264 my @bibitemloop;
265 my $num_available = 0;
266
267 foreach my $biblioitemnumber (@biblioitemnumbers) {
268     my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
269
270     $biblioitem->{description} =
271       $itemtypes->{ $biblioitem->{itemtype} }{description};
272
273     foreach
274       my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
275     {
276                 my $item = $itemhash{$itemnumber};
277
278         $item->{homebranchname} =
279           $branches->{ $item->{homebranch} }{branchname};
280
281         # if the holdingbranch is different than the homebranch, we show the
282         # holdingbranch of the document too
283         if ( $item->{homebranch} ne $item->{holdingbranch} ) {
284             $item->{holdingbranchname} =
285               $branches->{ $item->{holdingbranch} }{branchname};
286         }
287         
288 #       add information
289         $item->{itemcallnumber} = $item->{itemcallnumber};
290         
291         # if the item is currently on loan, we display its return date and
292         # change the background color
293         my $issues= GetItemIssue($itemnumber);
294         if ( $issues->{'date_due'} ) {
295             $item->{date_due} = format_date($issues->{'date_due'});
296             $item->{backgroundcolor} = 'onloan';
297         }
298
299         # checking reserve
300         my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
301         my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
302
303         if ( defined $reservedate ) {
304             $item->{backgroundcolor} = 'reserved';
305             $item->{reservedate}     = format_date($reservedate);
306             $item->{ReservedForBorrowernumber}     = $reservedfor;
307             $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
308             $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
309             $item->{ExpectedAtLibrary}     = $expectedAt;
310             
311         }
312
313         # Management of the notforloan document
314         if ( $item->{notforloan} || $item->{itemnotforloan}) {
315             $item->{backgroundcolor} = 'other';
316             $item->{notforloanvalue} =
317               $notforloan_label_of->{ $item->{notforloan} };
318         }
319
320         # Management of lost or long overdue items
321         if ( $item->{itemlost} ) {
322
323             # FIXME localized strings should never be in Perl code
324             $item->{message} =
325                 $item->{itemlost} == 1 ? "(lost)"
326               : $item->{itemlost} == 2 ? "(long overdue)"
327               : "";
328             $item->{backgroundcolor} = 'other';
329         }
330
331         # Check of the transfered documents
332         my ( $transfertwhen, $transfertfrom, $transfertto ) =
333           GetTransfers($itemnumber);
334
335         if ( $transfertwhen ne '' ) {
336             $item->{transfertwhen} = format_date($transfertwhen);
337             $item->{transfertfrom} =
338               $branches->{$transfertfrom}{branchname};
339             $item->{transfertto} = $branches->{$transfertto}{branchname};
340                 $item->{nocancel} = 1;
341         }
342
343         # If there is no loan, return and transfer, we show a checkbox.
344         $item->{notforloan} = $item->{notforloan} || 0;
345
346         my $branch = C4::Circulation::_GetCircControlBranch($item, $borr);
347
348         my $branchitemrule = GetBranchItemRule( $branch, $item->{'itemtype'} );
349
350         my $policy_holdallowed = 1;
351         
352         if ( $branchitemrule->{'holdallowed'} == 0 ||
353                 ( $branchitemrule->{'holdallowed'} == 1 && $borr->{'branchcode'} ne $item->{'homebranch'} ) ) {
354             $policy_holdallowed = 0;
355         }
356
357         if (IsAvailableForItemLevelRequest($itemnumber) and $policy_holdallowed and CanItemBeReserved($borrowernumber,$itemnumber)) {
358             $item->{available} = 1;
359             $num_available++;
360         }
361
362         # FIXME: move this to a pm
363         my $dbh = C4::Context->dbh;
364         my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
365         $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
366         while (my $wait_hashref = $sth2->fetchrow_hashref) {
367             $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
368         }
369         
370         $item->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes->{ $item->{itype} }{imageurl} );
371
372         push @{ $biblioitem->{itemloop} }, $item;
373     }
374     
375     if(not CanBookBeReserved($borrowernumber,$biblionumber)){
376         $biblioitem->{available} = undef;
377         $biblioitem->{notholdable} = 1;
378     }
379     
380     push @bibitemloop, $biblioitem;
381 }
382
383 if ( $num_available == 0 ) {
384         $template->param( none_available => 1, message => 1 );
385 }
386
387 # display infos
388 $template->param(
389         forloan           => $forloan,
390     bibitemloop       => \@bibitemloop,
391 );
392 output_html_with_http_headers $query, $cookie, $template->output;
393
394 # Local Variables:
395 # tab-width: 8
396 # End: