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