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