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