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