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