Virtual shelves: per shelf sort, multiple add in OPAC, partial bug 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 require Exporter;
20 use CGI;
21 use C4::Biblio;
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 Data::Dumper;
32
33 my $MAXIMUM_NUMBER_OF_RESERVES = C4::Context->preference("maxreserves");
34
35 my $query = new CGI;
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37     {
38         template_name   => "opac-reserve.tmpl",
39         query           => $query,
40         type            => "opac",
41         authnotrequired => 0,
42         flagsrequired   => { borrow => 1 },
43         debug           => 1,
44     }
45 );
46
47 # get borrower information ....
48 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
49
50 # get biblionumber.....
51 my $biblionumber = $query->param('biblionumber');
52
53 my $bibdata = GetBiblioData($biblionumber);
54 $template->param($bibdata);
55 $template->param( biblionumber => $biblionumber );
56
57 # get the rank number....
58 my ( $rank, $reserves ) = GetReservesFromBiblionumber( $biblionumber);
59 $template->param( reservecount => $rank );
60
61 foreach my $res (@$reserves) {
62     if ( $res->{'found'} eq 'W' ) {
63         $rank--;
64     }
65 }
66
67 $rank++;
68 $template->param( rank => $rank );
69
70 # pass the pickup branch along....
71 my $branch = $query->param('branch');
72 $template->param( branch => $branch );
73
74 my $branches = GetBranches();
75
76 # make sure it's a real branch
77 if ( !$branches->{$branch} ) {
78     $branch = '';
79 }
80 $template->param( branchname => $branches->{$branch}->{'branchname'} );
81
82 # make branch selection options...
83 my @branches;
84 my @select_branch;
85 my %select_branches;
86
87 my @CGIbranchlooparray;
88
89 foreach my $branch ( keys %$branches ) {
90     if ($branch) {
91         my %line;
92         $line{branch} = $branches->{$branch}->{'branchname'};
93         $line{value}  = $branch;
94         if ($line{value} eq C4::Context->userenv->{'branch'}) {
95             $line{selected} = 1;
96         }
97         push @CGIbranchlooparray, \%line;
98     }
99 }
100 @CGIbranchlooparray =
101   sort { $a->{branch} cmp $b->{branch} } @CGIbranchlooparray;
102 my $CGIbranchloop = \@CGIbranchlooparray;
103 $template->param( CGIbranch => $CGIbranchloop );
104
105 #### THIS IS A BIT OF A HACK BECAUSE THE BIBLIOITEMS DATA IS A LITTLE MESSED UP!
106 # get the itemtype data....
107 my @items = GetItemsInfo($biblionumber);
108 #######################################################
109 # old version, add so that old templates still work
110 my %types_old;
111 foreach my $itm (@items) {
112     my $ity = $itm->{'itemtype'};
113     unless ( $types_old{$ity} ) {
114         $types_old{$ity}->{'itemtype'} = $ity;
115         $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} } = 1;
116         $types_old{$ity}->{'description'} = $itm->{'description'};
117     }
118     else {
119         $types_old{$ity}->{'branchinfo'}->{ $itm->{'branchcode'} }++;
120     }
121 }
122
123 foreach my $type ( values %types_old ) {
124     my $copies = "";
125     foreach my $bc ( keys %{ $type->{'branchinfo'} } ) {
126         $copies .=
127             $branches->{$bc}->{'branchname'} . "("
128           . $type->{'branchinfo'}->{$bc} . ")";
129     }
130     $type->{'copies'} = $copies;
131 }
132
133 my @types_old = values %types_old;
134
135 # end old version
136 ################################
137
138 my @temp;
139 foreach my $itm (@items) {
140     push @temp, $itm if $itm->{'itemtype'};
141 }
142 @items = @temp;
143 my $itemcount = @items;
144 $template->param( itemcount => $itemcount );
145
146 my %types;
147 my %itemtypes;
148 my @duedates;
149 #die @items;
150 foreach my $itm (@items) {
151     push @duedates, { date_due => format_date( $itm->{'date_due'} ) }
152       if defined $itm->{'date_due'};
153     $itm->{ $itm->{'publictype'} } = 1;
154     my $fee = GetReserveFee( undef, $borrowernumber, $itm->{'biblionumber'},
155         'a', ( $itm->{'biblioitemnumber'} ) );
156     $fee = sprintf "%.02f", $fee;
157     $itm->{'reservefee'} = $fee;
158     my $pty = $itm->{'publictype'};
159     $itemtypes{ $itm->{'itemtype'} } = $itm;
160     unless ( $types{$pty} ) {
161         $types{$pty}->{'count'} = 1;
162         $types{$pty}->{ $itm->{'itemtype'} } = 1;
163         push @{ $types{$pty}->{'items'} }, $itm;
164     }
165     else {
166         unless ( $types{$pty}->{ $itm->{'itemtype'} } ) {
167             $types{$pty}->{'count'}++;
168             $types{$pty}->{ $itm->{'itemtype'} } = 1;
169             push @{ $types{$pty}->{'items'} }, $itm;
170         }
171     }
172 }
173
174 $template->param( ITEMS => \@duedates );
175
176 my $width = keys %types;
177 my @publictypes = sort { $b->{'count'} <=> $a->{'count'} } values %types;
178 my $typecount;
179 foreach my $pt (@publictypes) {
180     $typecount += $pt->{'count'};
181 }
182 $template->param( onlyone => 1 ) if $typecount == 1;
183
184 my @typerows;
185 for ( my $rownum = 0 ; $rownum < $publictypes[0]->{'count'} ; $rownum++ ) {
186     my @row;
187     foreach my $pty (@publictypes) {
188         my @items = @{ $pty->{'items'} };
189         push @row, $items[$rownum] if defined $items[$rownum];
190     }
191     my $last = @row;
192     $row[ $last - 1 ]->{'last'} = 1 if $last == $width;
193     my $fill = ( $width - $last ) * 2;
194     $fill-- if $fill;
195     push @typerows, { ROW => \@row, fill => $fill };
196 }
197 $template->param( TYPE_ROWS => \@typerows );
198 $width = 2 * $width - 1;
199 $template->param( totalwidth => 2 * $width - 1, );
200
201 if ( $query->param('place_reserve') ) {
202     my @bibitems=$query->param('biblioitem');
203     my $notes=$query->param('notes');
204     my $checkitem=$query->param('checkitem');
205     my $found;
206     
207     #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.
208     if ($checkitem ne ''){
209         $rank = '0';
210         my $item = $checkitem;
211         $item = GetItem($item);
212         if ( $item->{'holdingbranch'} eq $branch ){
213             $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
214         }
215     }
216         
217         my $count=@bibitems;
218     @bibitems=sort @bibitems;
219     my $i2=1;
220     my @realbi;
221     $realbi[0]=$bibitems[0];
222     for (my $i=1;$i<$count;$i++) {
223         my $i3=$i2-1;
224         if ($realbi[$i3] ne $bibitems[$i]) {
225             $realbi[$i2]=$bibitems[$i];
226             $i2++;
227         }
228     }
229     # here we actually do the reserveration. Stage 3.
230     if ($query->param('request') eq 'any'){
231         # place a request on 1st available
232         AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem,$found);
233     } else {
234         AddReserve($branch,$borrowernumber,$biblionumber,'a',\@realbi,$rank,$notes,$bibdata->{'title'},$checkitem, $found);
235     }
236     print $query->redirect("/cgi-bin/koha/opac-user.pl");
237 }
238 else {
239
240     # Here we check that the borrower can actually make reserves Stage 1.
241     my $noreserves     = 0;
242     my $maxoutstanding = C4::Context->preference("maxoutstanding");
243     $template->param( noreserve => 1 ) unless $maxoutstanding;
244     if ( $borr->{'amountoutstanding'} > $maxoutstanding ) {
245         my $amount = sprintf "\$%.02f", $borr->{'amountoutstanding'};
246         $template->param( message => 1 );
247         $noreserves = 1;
248         $template->param( too_much_oweing => $amount );
249     }
250     if ( $borr->{gonenoaddress} eq 1 ) {
251         $noreserves = 1;
252         $template->param(
253             message => 1,
254             GNA     => 1
255         );
256     }
257     if ( $borr->{lost} eq 1 ) {
258         $noreserves = 1;
259         $template->param(
260             message => 1,
261             lost    => 1
262         );
263     }
264     if ( $borr->{debarred} eq 1 ) {
265         $noreserves = 1;
266         $template->param(
267             message  => 1,
268             debarred => 1
269         );
270     }
271     my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
272     $template->param( RESERVES => \@reserves );
273     if ( scalar(@$reserves) >= $MAXIMUM_NUMBER_OF_RESERVES ) {
274         $template->param( message => 1 );
275         $noreserves = 1;
276         $template->param( too_many_reserves => scalar($reserves));
277     }
278     foreach my $res (@$reserves) {
279         if ( $res->{'biblionumber'} == $biblionumber ) {
280             $template->param( message => 1 );
281             $noreserves = 1;
282             $template->param( already_reserved => 1 );
283         }
284     }
285     unless ($noreserves) {
286         $template->param( TYPES             => \@types_old );
287         $template->param( select_item_types => 1 );
288     }
289 }
290
291
292 my @branchcodes;
293 my %itemnumbers_of_biblioitem;
294 my @itemnumbers  = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
295 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
296
297 foreach my $itemnumber (@itemnumbers) {
298     push( @branchcodes,
299         $iteminfos_of->{$itemnumber}->{homebranch},
300         $iteminfos_of->{$itemnumber}->{holdingbranch} );
301
302     my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
303     push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
304 }
305
306 # @branchcodes = uniq @branchcodes;
307
308 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
309
310 my $branchinfos_of      = get_branchinfos_of(@branchcodes);
311 my $notforloan_label_of = get_notforloan_label_of();
312 my $biblioiteminfos_of  = GetBiblioItemInfosOf(@biblioitemnumbers);
313
314 my @itemtypes;
315 foreach my $biblioitemnumber (@biblioitemnumbers) {
316     push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
317 }
318
319 my $itemtypeinfos_of = get_itemtypeinfos_of(@itemtypes);
320
321 my @bibitemloop;
322
323 foreach my $biblioitemnumber (@biblioitemnumbers) {
324     my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
325
326     $biblioitem->{description} =
327       $itemtypeinfos_of->{ $biblioitem->{itemtype} }{description};
328
329     foreach
330       my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
331     {
332         my $item = $iteminfos_of->{$itemnumber};
333
334         $item->{homebranchname} =
335           $branchinfos_of->{ $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               $branchinfos_of->{ $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} ) {
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               $branchinfos_of->{$transfertfrom}{branchname};
395             $item->{transfertto} = $branchinfos_of->{$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         # An item is available only if:
403         if (
404             not defined $reservedate    # not reserved yet
405             and $issues->{'date_due'} eq ''         # not currently on loan
406             and not $item->{itemlost}   # not lost
407             and not $item->{notforloan} # not forbidden to loan
408             and $transfertwhen eq ''    # not currently on transfert
409           )
410         {
411             $item->{available} = 1;
412         }
413
414         push @{ $biblioitem->{itemloop} }, $item;
415     }
416
417     push @bibitemloop, $biblioitem;
418 }
419
420 # display infos
421 $template->param(
422     bibitemloop       => \@bibitemloop,
423 );
424 output_html_with_http_headers $query, $cookie, $template->output;
425
426 # Local Variables:
427 # tab-width: 8
428 # End: