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