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