Bug 24446: Catch daterequested in circ returns
[koha.git] / circ / returns.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #           2006 SAN-OP
5 #           2007-2010 BibLibre, Paul POULAIN
6 #           2010 Catalyst IT
7 #           2011 PTFS-Europe Ltd.
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23
24 =head1 returns.pl
25
26 script to execute returns of books
27
28 =cut
29
30 use Modern::Perl;
31
32 # FIXME There are weird things going on with $patron and $borrowernumber in this script
33
34 use CGI qw ( -utf8 );
35 use DateTime;
36
37 use C4::Auth qw/:DEFAULT get_session/;
38 use C4::Output;
39 use C4::Circulation;
40 use C4::Reserves;
41 use C4::Biblio;
42 use C4::Circulation;
43 use C4::Context;
44 use C4::Items;
45 use C4::Koha;   # FIXME : is it still useful ?
46 use C4::Members::Messaging;
47 use C4::Members;
48 use C4::Output;
49 use C4::Reserves;
50 use C4::RotatingCollections;
51 use Koha::AuthorisedValues;
52 use Koha::BiblioFrameworks;
53 use Koha::Calendar;
54 use Koha::Checkouts;
55 use Koha::DateUtils;
56 use Koha::Holds;
57 use Koha::Items;
58 use Koha::Patrons;
59
60 my $query = CGI->new;
61
62 #getting the template
63 my ( $template, $librarian, $cookie, $flags ) = get_template_and_user(
64     {
65         template_name   => "circ/returns.tt",
66         query           => $query,
67         type            => "intranet",
68         flagsrequired   => { circulate => "circulate_remaining_permissions" },
69     }
70 );
71
72 my $sessionID = $query->cookie("CGISESSID");
73 my $session = get_session($sessionID);
74 my $desk_id = C4::Context->userenv->{"desk_id"} || '';
75
76 # Print a reserve slip on this page
77 if ( $query->param('print_slip') ) {
78     $template->param(
79         print_slip     => 1,
80         reserve_id => scalar $query->param('reserve_id'),
81     );
82 }
83
84 #####################
85 #Global vars
86 my $userenv = C4::Context->userenv;
87 my $userenv_branch = $userenv->{'branch'} // '';
88 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
89
90 my $overduecharges = (C4::Context->preference('finesMode') && C4::Context->preference('finesMode') eq 'production');
91
92 # Set up the item stack ....
93 my %returneditems;
94 my %riduedate;
95 my %riborrowernumber;
96 my @inputloop;
97 foreach ( $query->param ) {
98     my $counter;
99     if (/ri-(\d*)/) {
100         $counter = $1;
101         if ($counter > 20) {
102             next;
103         }
104     }
105     else {
106         next;
107     }
108
109     my %input;
110     my $barcode        = $query->param("ri-$counter");
111     my $duedate        = $query->param("dd-$counter");
112     my $borrowernumber = $query->param("bn-$counter");
113     $counter++;
114
115     # decode barcode    ## Didn't we already decode them before passing them back last time??
116     $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
117     $barcode = barcodedecode($barcode) if(C4::Context->preference('itemBarcodeInputFilter'));
118
119     ######################
120     #Are these lines still useful ?
121     $returneditems{$counter}    = $barcode;
122     $riduedate{$counter}        = $duedate;
123     $riborrowernumber{$counter} = $borrowernumber;
124
125     #######################
126     $input{counter}        = $counter;
127     $input{barcode}        = $barcode;
128     $input{duedate}        = $duedate;
129     $input{borrowernumber} = $borrowernumber;
130     push( @inputloop, \%input );
131 }
132
133 ############
134 # Deal with the requests....
135
136 if ($query->param('WT-itemNumber')){
137         updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
138 }
139
140 my $itemnumber = $query->param('itemnumber');
141 if ( $query->param('reserve_id') ) {
142     my $borrowernumber = $query->param('borrowernumber');
143     my $reserve_id     = $query->param('reserve_id');
144     my $diffBranchReturned = $query->param('diffBranch');
145     my $cancel_reserve = $query->param('cancel_reserve');
146     # fix up item type for display
147     my $item = Koha::Items->find( $itemnumber );
148     my $biblio = $item->biblio;
149
150     if ( $cancel_reserve ) {
151         my $hold = Koha::Holds->find( $reserve_id );
152         if ( $hold ) {
153             $hold->cancel( { charge_cancel_fee => !$forgivemanualholdsexpire } );
154         } # FIXME else?
155     } else {
156         my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ? $diffBranchReturned : undef;
157         # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
158         # i.e., whether to apply waiting status
159         ModReserveAffect( $itemnumber, $borrowernumber, $diffBranchSend, $reserve_id, $desk_id );
160     }
161 #   check if we have other reserves for this document, if we have a return send the message of transfer
162     my ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
163
164     my $patron = Koha::Patrons->find( $nextreservinfo );
165     if ( $messages->{'transfert'} ) {
166         $template->param(
167             itemtitle      => $biblio->title,
168             itembiblionumber => $biblio->biblionumber,
169             iteminfo       => $biblio->author,
170             patron         => $patron,
171             diffbranch     => 1,
172         );
173     }
174 }
175
176 my $borrower;
177 my $returned = 0;
178 my $messages;
179 my $issue;
180 my $barcode     = $query->param('barcode');
181 my $exemptfine  = $query->param('exemptfine');
182 if (
183   $exemptfine &&
184   !C4::Auth::haspermission(C4::Context->userenv->{'id'}, {'updatecharges' => 'writeoff'})
185 ) {
186     # silently prevent unauthorized operator from forgiving overdue
187     # fines by manually tweaking form parameters
188     undef $exemptfine;
189 }
190 my $dropboxmode = $query->param('dropboxmode');
191 my $dotransfer  = $query->param('dotransfer');
192 my $canceltransfer = $query->param('canceltransfer');
193 my $dest = $query->param('dest');
194 #dropbox: get last open day (today - 1)
195 my $dropboxdate = Koha::Checkouts::calculate_dropbox_date();
196
197 my $return_date_override = $query->param('return_date_override');
198 my $return_date_override_dt;
199 my $return_date_override_remember =
200   $query->param('return_date_override_remember');
201 if ($return_date_override) {
202     if ( C4::Context->preference('SpecifyReturnDate') ) {
203         $return_date_override_dt = eval {dt_from_string( $return_date_override ) };
204         if ( $return_date_override_dt ) {
205             # note that we've overriden the return date
206             $template->param( return_date_was_overriden => 1);
207             # Save the original format if we are remembering for this series
208             $template->param(
209                 return_date_override          => $return_date_override,
210                 return_date_override_remember => 1
211             ) if ($return_date_override_remember);
212
213             $return_date_override =
214               DateTime::Format::MySQL->format_datetime( $return_date_override_dt );
215         }
216     }
217     else {
218         $return_date_override = q{};
219     }
220 }
221
222 if ($dotransfer){
223 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
224     my $transferitem = $query->param('transferitem');
225     my $tobranch     = $query->param('tobranch');
226     my $trigger      = $query->param('trigger');
227     ModItemTransfer($transferitem, $userenv_branch, $tobranch, $trigger);
228 }
229
230 if ($canceltransfer){
231     DeleteTransfer($itemnumber);
232     if($dest eq "ttr"){
233         print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
234         exit;
235     } else {
236         $template->param( transfercancelled => 1);
237     }
238 }
239
240 # actually return book and prepare item table.....
241 my $returnbranch;
242 if ($barcode) {
243     $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
244     $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
245     my $item = Koha::Items->find({ barcode => $barcode });
246
247     if ( $item ) {
248         $itemnumber = $item->itemnumber;
249         # Check if we should display a checkin message, based on the the item
250         # type of the checked in item
251         my $itemtype = Koha::ItemTypes->find( $item->effective_itemtype );
252         if ( $itemtype && $itemtype->checkinmsg ) {
253             $template->param(
254                 checkinmsg     => $itemtype->checkinmsg,
255                 checkinmsgtype => $itemtype->checkinmsgtype,
256             );
257         }
258
259         # make sure return branch respects home branch circulation rules, default to homebranch
260         my $hbr = GetBranchItemRule($item->homebranch, $itemtype ? $itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
261         $returnbranch = $hbr ne 'noreturn' ? $item->$hbr : $userenv_branch; # can be noreturn, homebranch or holdingbranch
262
263         my $materials = $item->materials;
264         my $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => '', kohafield =>'items.materials', authorised_value => $materials });
265         $materials = $descriptions->{lib} // $materials;
266
267         my $checkout = $item->checkout;
268         my $biblio   = $item->biblio;
269         $template->param(
270             title                => $biblio->title,
271             homebranch           => $item->homebranch,
272             holdingbranch        => $item->holdingbranch,
273             returnbranch         => $returnbranch,
274             author               => $biblio->author,
275             itembarcode          => $item->barcode,
276             itemtype             => $item->effective_itemtype,
277             ccode                => $item->ccode,
278             itembiblionumber     => $biblio->biblionumber,
279             biblionumber         => $biblio->biblionumber,
280             additional_materials => $materials,
281             issue                => $checkout,
282         );
283     } # FIXME else we should not call AddReturn but set BadBarcode directly instead
284
285     my %input = (
286         counter => 0,
287         first   => 1,
288         barcode => $barcode,
289     );
290
291     my $return_date = $dropboxmode ? $dropboxdate : $return_date_override_dt;
292
293     # Block return if multi-part and confirm has not been received
294     my $needs_confirm =
295          C4::Context->preference("CircConfirmItemParts")
296       && $item
297       && $item->materials
298       && !$query->param('multiple_confirm');
299     $template->param( 'multiple_confirmed' => 1 )
300       if $query->param('multiple_confirm');
301
302     # do the return
303     ( $returned, $messages, $issue, $borrower ) =
304       AddReturn( $barcode, $userenv_branch, $exemptfine, $return_date )
305           unless $needs_confirm;
306
307     if ($returned) {
308         my $time_now = dt_from_string()->truncate( to => 'minute');
309         my $date_due_dt = dt_from_string( $issue->date_due, 'sql' );
310         my $duedate = $date_due_dt->strftime('%Y-%m-%d %H:%M');
311         $returneditems{0}      = $barcode;
312         $riborrowernumber{0}   = $borrower->{'borrowernumber'};
313         $riduedate{0}          = $duedate;
314         $input{borrowernumber} = $borrower->{'borrowernumber'};
315         $input{duedate}        = $duedate;
316         unless ( $dropboxmode ) {
317             $input{return_overdue} = 1 if (DateTime->compare($date_due_dt, dt_from_string()) == -1);
318         } else {
319             $input{return_overdue} = 1 if (DateTime->compare($date_due_dt, $dropboxdate) == -1);
320         }
321         push( @inputloop, \%input );
322
323         if ( C4::Context->preference("FineNotifyAtCheckin") ) {
324             my $patron = Koha::Patrons->find( $borrower->{borrowernumber} );
325             my $balance = $patron->account->balance;
326
327             if ($balance > 0) {
328                 $template->param( fines => sprintf("%.2f", $balance) );
329                 $template->param( fineborrowernumber => $borrower->{'borrowernumber'} );
330             }
331         }
332
333         if (C4::Context->preference("WaitingNotifyAtCheckin") ) {
334             #Check for waiting holds
335             my $patron = Koha::Patrons->find( $borrower->{borrowernumber} );
336             my $waiting_holds = $patron->holds->search({ found => 'W', branchcode => $userenv_branch })->count;
337             if ($waiting_holds > 0) {
338                 $template->param(
339                     waiting_holds       => $waiting_holds,
340                     holdsborrowernumber => $borrower->{'borrowernumber'},
341                     holdsfirstname => $borrower->{'firstname'},
342                     holdssurname => $borrower->{'surname'},
343                 );
344             }
345         }
346     } elsif ( C4::Context->preference('ShowAllCheckins') and !$messages->{'BadBarcode'} and !$needs_confirm ) {
347         $input{duedate}   = 0;
348         $returneditems{0} = $barcode;
349         $riduedate{0}     = 0;
350         push( @inputloop, \%input );
351     }
352     $template->param( privacy => $borrower->{privacy} );
353
354     if ( $needs_confirm ) {
355         $template->param( needs_confirm => $needs_confirm );
356     }
357 }
358 $template->param( inputloop => \@inputloop );
359
360 my $found    = 0;
361 my $waiting  = 0;
362 my $reserved = 0;
363
364 # new op dev : we check if the document must be returned to his homebranch directly,
365 #  if the document is transferred, we have warning message .
366
367 if ( $messages->{'WasTransfered'} ) {
368     $template->param(
369         found          => 1,
370         transfer       => $messages->{'WasTransfered'},
371         trigger        => $messages->{'TransferTrigger'},
372         itemnumber     => $itemnumber,
373     );
374 }
375
376 if ( $messages->{'NeedsTransfer'} ){
377     $template->param(
378         found          => 1,
379         needstransfer  => $messages->{'NeedsTransfer'},
380         trigger        => $messages->{'TransferTrigger'},
381     );
382 }
383
384 if ( $messages->{'Wrongbranch'} ){
385     $template->param(
386         wrongbranch => 1,
387         rightbranch => $messages->{'Wrongbranch'}->{'Rightbranch'},
388     );
389 }
390
391 # case of wrong transfert, if the document wasn't transferred to the right library (according to branchtransfer (tobranch) BDD)
392
393 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
394     $template->param(
395         WrongTransfer  => 1,
396         TransferWaitingAt => $messages->{'WrongTransfer'},
397         WrongTransferItem => $messages->{'WrongTransferItem'},
398     );
399
400     my $reserve    = $messages->{'ResFound'};
401     if ( $reserve ) {
402         my $patron = Koha::Patrons->find( $reserve->{'borrowernumber'} );
403         $template->param(
404             patron => $patron,
405         );
406     }
407     $template->param(
408         wtransfertFrom  => $userenv_branch,
409     );
410 }
411
412 #
413 # reserve found and item arrived at the expected branch
414 #
415 if ( $messages->{'ResFound'}) {
416     my $reserve    = $messages->{'ResFound'};
417     my $patron = Koha::Patrons->find( $reserve->{borrowernumber} );
418     my $holdmsgpreferences =  C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $reserve->{'borrowernumber'}, message_name   => 'Hold_Filled' } );
419     my $branchCheck = ( $userenv_branch eq $reserve->{branchcode} );
420     if ( $reserve->{'ResFound'} eq "Reserved" && C4::Context->preference('HoldsAutoFill') ) {
421         my $item = Koha::Items->find( $itemnumber );
422         my $biblio = $item->biblio;
423
424         my $diffBranchSend = !$branchCheck ? $reserve->{branchcode} : undef;
425         ModReserveAffect( $reserve->{itemnumber}, $reserve->{borrowernumber}, $diffBranchSend, $reserve->{reserve_id}, $desk_id );
426         my ( $messages, $nextreservinfo ) = GetOtherReserves($reserve->{itemnumber});
427
428         $template->param(
429             hold_auto_filled => 1,
430             print_slip       => C4::Context->preference('HoldsAutoFillPrintSlip'),
431             reserve_id       => $nextreservinfo->{reserve_id},
432         );
433
434         if ( $messages->{'transfert'} ) {
435             $template->param(
436                 itemtitle        => $biblio->title,
437                 itembiblionumber => $biblio->biblionumber,
438                 iteminfo         => $biblio->author,
439                 diffbranch       => 1,
440             );
441         }
442     } elsif ( $reserve->{'ResFound'} eq "Waiting" ) {
443         $template->param(
444             waiting      => $branchCheck ? 1 : undef,
445         );
446     } elsif ( $reserve->{'ResFound'} eq "Reserved" || $reserve->{'ResFound'} eq "Processing" ) {
447         $template->param(
448             intransit    => $branchCheck ? undef : 1,
449             transfertodo => $branchCheck ? undef : 1,
450             reserve_id   => $reserve->{reserve_id},
451             reserved     => 1,
452         );
453     } # else { ; }  # error?
454
455     # same params for Waiting or Reserved
456     $template->param(
457         found          => 1,
458         patron         => $patron,
459         barcode        => $barcode,
460         destbranch     => $reserve->{'branchcode'},
461         reservenotes   => $reserve->{'reservenotes'},
462         reserve_id     => $reserve->{reserve_id},
463         bormessagepref => $holdmsgpreferences->{'transports'},
464     );
465 }
466
467 # Error Messages
468 my @errmsgloop;
469 foreach my $code ( keys %$messages ) {
470     my %err;
471     my $exit_required_p = 0;
472     if ( $code eq 'BadBarcode' ) {
473         $err{badbarcode} = 1;
474         $err{msg}        = $messages->{'BadBarcode'};
475     }
476     elsif ( $code eq 'NotIssued' ) {
477         $err{notissued} = 1;
478         $err{msg} = '';
479     }
480     elsif ( $code eq 'LocalUse' ) {
481         $err{localuse} = 1;
482     }
483     elsif ( $code eq 'WasLost' ) {
484         $err{waslost} = 1;
485         $exit_required_p = 1 if C4::Context->preference("BlockReturnOfLostItems");
486     }
487     elsif ( $code eq 'LostItemFeeRefunded' ) {
488         $template->param( LostItemFeeRefunded => 1 );
489     }
490     elsif ( $code eq 'LostItemFeeCharged' ) {
491         $template->param( LostItemFeeCharged => 1 );
492     }
493     elsif ( $code eq 'LostItemFeeRestored' ) {
494         $template->param( LostItemFeeRestored => 1 );
495     }
496     elsif ( $code eq 'ResFound' ) {
497         ;    # FIXME... anything to do here?
498     }
499     elsif ( $code eq 'WasReturned' ) {
500         ;    # FIXME... anything to do here?
501     }
502     elsif ( $code eq 'WasTransfered' ) {
503         ;    # FIXME... anything to do here?
504     }
505     elsif ( $code eq 'withdrawn' ) {
506         $err{withdrawn} = 1;
507         $exit_required_p = 1 if C4::Context->preference("BlockReturnOfWithdrawnItems");
508     }
509     elsif ( $code eq 'WrongTransfer' ) {
510         ;    # FIXME... anything to do here?
511     }
512     elsif ( $code eq 'WrongTransferItem' ) {
513         ;    # FIXME... anything to do here?
514     }
515     elsif ( $code eq 'NeedsTransfer' ) {
516     }
517     elsif ( $code eq 'TransferTrigger' ) {
518         ;    # Handled alongside NeedsTransfer
519     }
520     elsif ( $code eq 'TransferArrived' ) {
521         $err{transferred} = $messages->{'TransferArrived'};
522     }
523     elsif ( $code eq 'Wrongbranch' ) {
524     }
525     elsif ( $code eq 'Debarred' ) {
526         $err{debarred}            = $messages->{'Debarred'};
527         $err{debarcardnumber}     = $borrower->{cardnumber};
528         $err{debarborrowernumber} = $borrower->{borrowernumber};
529         $err{debarname}           = "$borrower->{firstname} $borrower->{surname}";
530     }
531     elsif ( $code eq 'PrevDebarred' ) {
532         $err{prevdebarred}        = $messages->{'PrevDebarred'};
533     }
534     elsif ( $code eq 'ForeverDebarred' ) {
535         $err{foreverdebarred}        = $messages->{'ForeverDebarred'};
536     }
537     elsif ( $code eq 'ItemLocationUpdated' ) {
538         $err{ItemLocationUpdated} = $messages->{ItemLocationUpdated};
539     }
540     elsif ( $code eq 'NotForLoanStatusUpdated' ) {
541         $err{NotForLoanStatusUpdated} = $messages->{NotForLoanStatusUpdated};
542     }
543     elsif ( $code eq 'DataCorrupted' ) {
544         $err{data_corrupted} = 1;
545     }
546     elsif ( $code eq 'ReturnClaims' ) {
547         $template->param( ReturnClaims => $messages->{ReturnClaims} );
548     } else {
549         die "Unknown error code $code";    # note we need all the (empty) elsif's above, or we die.
550         # This forces the issue of staying in sync w/ Circulation.pm
551     }
552     if (%err) {
553         push( @errmsgloop, \%err );
554     }
555     last if $exit_required_p;
556 }
557 $template->param( errmsgloop => \@errmsgloop );
558
559 #set up so only the last 8 returned items display (make for faster loading pages)
560 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
561 my $count = 0;
562 my @riloop;
563 my $shelflocations =
564   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) };
565 foreach ( sort { $a <=> $b } keys %returneditems ) {
566     my %ri;
567     if ( $count++ < $returned_counter ) {
568         my $bar_code = $returneditems{$_};
569         if ($riduedate{$_}) {
570             my $duedate = dt_from_string( $riduedate{$_}, 'sql');
571             $ri{year}  = $duedate->year();
572             $ri{month} = $duedate->month();
573             $ri{day}   = $duedate->day();
574             $ri{hour}   = $duedate->hour();
575             $ri{minute}   = $duedate->minute();
576             $ri{duedate} = output_pref($duedate);
577             my $patron = Koha::Patrons->find( $riborrowernumber{$_} );
578             unless ( $dropboxmode ) {
579                 $ri{return_overdue} = 1 if (DateTime->compare($duedate, dt_from_string()) == -1);
580             } else {
581                 $ri{return_overdue} = 1 if (DateTime->compare($duedate, $dropboxdate) == -1);
582             }
583             $ri{patron} = $patron,
584             $ri{borissuescount} = $patron->checkouts->count;
585         }
586         else {
587             $ri{borrowernumber} = $riborrowernumber{$_};
588         }
589
590         my $item = Koha::Items->find({ barcode => $bar_code });
591         next unless $item; # FIXME The item has been deleted in the meantime,
592                            # we could handle that better displaying a message in the template
593
594         my $biblio = $item->biblio;
595         # FIXME pass $item to the template and we are done here...
596         $ri{itembiblionumber}    = $biblio->biblionumber;
597         $ri{itemtitle}           = $biblio->title;
598         $ri{subtitle}            = $biblio->subtitle;
599         $ri{part_name}           = $biblio->part_name;
600         $ri{part_number}         = $biblio->part_number;
601         $ri{itemauthor}          = $biblio->author;
602         $ri{itemcallnumber}      = $item->itemcallnumber;
603         $ri{dateaccessioned}     = $item->dateaccessioned;
604         $ri{recordtype}          = $biblio->itemtype;
605         $ri{itemtype}            = $item->itype;
606         $ri{itemnote}            = $item->itemnotes;
607         $ri{itemnotes_nonpublic} = $item->itemnotes_nonpublic;
608         $ri{ccode}               = $item->ccode;
609         $ri{enumchron}           = $item->enumchron;
610         $ri{itemnumber}          = $item->itemnumber;
611         $ri{barcode}             = $bar_code;
612         $ri{homebranch}          = $item->homebranch;
613         $ri{holdingbranch}       = $item->holdingbranch;
614         $ri{damaged}             = $item->damaged;
615
616         $ri{location} = $item->location;
617         my $shelfcode = $ri{'location'};
618         $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
619
620     }
621     else {
622         last;
623     }
624     push @riloop, \%ri;
625 }
626
627 $template->param(
628     riloop         => \@riloop,
629     errmsgloop     => \@errmsgloop,
630     exemptfine     => $exemptfine,
631     dropboxmode    => $dropboxmode,
632     dropboxdate    => $dropboxdate,
633     forgivemanualholdsexpire => $forgivemanualholdsexpire,
634     overduecharges => $overduecharges,
635     AudioAlerts        => C4::Context->preference("AudioAlerts"),
636 );
637
638 if ( $barcode ) {
639     my $item_from_barcode = Koha::Items->find({barcode => $barcode }); # How many times do we fetch this item?!?
640     if ( $item_from_barcode ) {
641         $itemnumber = $item_from_barcode->itemnumber;
642         my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
643         if ( $holdingBranch and $collectionBranch ) {
644             $holdingBranch //= '';
645             $collectionBranch //= $returnbranch;
646             if ( ! ( $holdingBranch eq $collectionBranch ) ) {
647                 $template->param(
648                   collectionItemNeedsTransferred => 1,
649                   collectionBranch => $collectionBranch,
650                 );
651             }
652         }
653     }
654 }
655
656 $template->param( itemnumber => $itemnumber );
657
658 # Checking if there is a Fast Cataloging Framework
659 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
660
661 # actually print the page!
662 output_html_with_http_headers $query, $cookie, $template->output;