Bug 16570: Do not tell all checked-in items are part of a rotating collection
[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 strict;
31 use warnings;
32
33 use Carp 'verbose';
34 $SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
35
36 use CGI qw ( -utf8 );
37 use DateTime;
38 use C4::Context;
39 use C4::Auth qw/:DEFAULT get_session/;
40 use C4::Output;
41 use C4::Circulation;
42 use C4::Print;
43 use C4::Reserves;
44 use C4::Biblio;
45 use C4::Items;
46 use C4::Members;
47 use C4::Members::Messaging;
48 use C4::Branch; # GetBranches GetBranchName
49 use C4::Koha;   # FIXME : is it still useful ?
50 use C4::RotatingCollections;
51 use Koha::DateUtils;
52 use Koha::Calendar;
53
54 my $query = new CGI;
55
56 #getting the template
57 my ( $template, $librarian, $cookie ) = get_template_and_user(
58     {
59         template_name   => "circ/returns.tt",
60         query           => $query,
61         type            => "intranet",
62         authnotrequired => 0,
63         flagsrequired   => { circulate => "circulate_remaining_permissions" },
64     }
65 );
66
67 my $sessionID = $query->cookie("CGISESSID");
68 my $session = get_session($sessionID);
69 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
70     # no branch set we can't return
71     print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
72     exit;
73 }
74
75 # Print a reserve slip on this page
76 if ( $query->param('print_slip') ) {
77     $template->param(
78         print_slip     => 1,
79         borrowernumber => scalar $query->param('borrowernumber'),
80         biblionumber   => scalar $query->param('biblionumber'),
81     );
82 }
83
84 #####################
85 #Global vars
86 my $branches = GetBranches();
87 my $printers = GetPrinters();
88 my $userenv = C4::Context->userenv;
89 my $userenv_branch = $userenv->{'branch'} // '';
90 my $printer = $userenv->{'branchprinter'} // '';
91 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
92
93 my $overduecharges = (C4::Context->preference('finesMode') && C4::Context->preference('finesMode') ne 'off');
94 #
95 # Some code to handle the error if there is no branch or printer setting.....
96 #
97
98 # Set up the item stack ....
99 my %returneditems;
100 my %riduedate;
101 my %riborrowernumber;
102 my @inputloop;
103 foreach ( $query->param ) {
104     my $counter;
105     if (/ri-(\d*)/) {
106         $counter = $1;
107         if ($counter > 20) {
108             next;
109         }
110     }
111     else {
112         next;
113     }
114
115     my %input;
116     my $barcode        = $query->param("ri-$counter");
117     my $duedate        = $query->param("dd-$counter");
118     my $borrowernumber = $query->param("bn-$counter");
119     $counter++;
120
121     # decode barcode    ## Didn't we already decode them before passing them back last time??
122     $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
123     $barcode = barcodedecode($barcode) if(C4::Context->preference('itemBarcodeInputFilter'));
124
125     ######################
126     #Are these lines still useful ?
127     $returneditems{$counter}    = $barcode;
128     $riduedate{$counter}        = $duedate;
129     $riborrowernumber{$counter} = $borrowernumber;
130
131     #######################
132     $input{counter}        = $counter;
133     $input{barcode}        = $barcode;
134     $input{duedate}        = $duedate;
135     $input{borrowernumber} = $borrowernumber;
136     push( @inputloop, \%input );
137 }
138
139 ############
140 # Deal with the requests....
141
142 if ($query->param('WT-itemNumber')){
143         updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
144 }
145
146 if ( $query->param('resbarcode') ) {
147     my $item           = $query->param('itemnumber');
148     my $borrowernumber = $query->param('borrowernumber');
149     my $resbarcode     = $query->param('resbarcode');
150     my $diffBranchReturned = $query->param('diffBranch');
151     my $iteminfo   = GetBiblioFromItemNumber($item);
152     my $cancel_reserve = $query->param('cancel_reserve');
153     # fix up item type for display
154     $iteminfo->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $iteminfo->{'itype'} : $iteminfo->{'itemtype'};
155
156     if ( $cancel_reserve ) {
157         CancelReserve({ borrowernumber => $borrowernumber, itemnumber => $item, charge_cancel_fee => !$forgivemanualholdsexpire });
158     } else {
159         my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ? $diffBranchReturned : undef;
160         # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
161         # i.e., whether to apply waiting status
162         ModReserveAffect( $item, $borrowernumber, $diffBranchSend);
163     }
164 #   check if we have other reserves for this document, if we have a return send the message of transfer
165     my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
166
167     my $borr = GetMember( borrowernumber => $nextreservinfo );
168     my $name   = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
169     if ( $messages->{'transfert'} ) {
170         $template->param(
171             itemtitle      => $iteminfo->{'title'},
172             itemnumber     => $iteminfo->{'itemnumber'},
173             itembiblionumber => $iteminfo->{'biblionumber'},
174             iteminfo       => $iteminfo->{'author'},
175             tobranchname   => GetBranchName($messages->{'transfert'}),
176             name           => $name,
177             borrowernumber => $borrowernumber,
178             borcnum        => $borr->{'cardnumber'},
179             borfirstname   => $borr->{'firstname'},
180             borsurname     => $borr->{'surname'},
181             diffbranch     => 1,
182         );
183     }
184 }
185
186 my $borrower;
187 my $returned = 0;
188 my $messages;
189 my $issueinformation;
190 my $itemnumber;
191 my $barcode     = $query->param('barcode');
192 my $exemptfine  = $query->param('exemptfine');
193 if (
194   $exemptfine &&
195   !C4::Auth::haspermission(C4::Context->userenv->{'id'}, {'updatecharges' => 'writeoff'})
196 ) {
197     # silently prevent unauthorized operator from forgiving overdue
198     # fines by manually tweaking form parameters
199     undef $exemptfine;
200 }
201 my $dropboxmode = $query->param('dropboxmode');
202 my $dotransfer  = $query->param('dotransfer');
203 my $canceltransfer = $query->param('canceltransfer');
204 my $dest = $query->param('dest');
205 my $calendar    = Koha::Calendar->new( branchcode => $userenv_branch );
206 #dropbox: get last open day (today - 1)
207 my $today       = DateTime->now( time_zone => C4::Context->tz());
208 my $dropboxdate = $calendar->addDate($today, -1);
209
210 my $return_date_override = $query->param('return_date_override');
211 my $return_date_override_remember =
212   $query->param('return_date_override_remember');
213 if ($return_date_override) {
214     if ( C4::Context->preference('SpecifyReturnDate') ) {
215         my $return_date_override_dt = eval {dt_from_string( $return_date_override ) };
216         if ( $return_date_override_dt ) {
217             # note that we've overriden the return date
218             $template->param( return_date_was_overriden => 1);
219             # Save the original format if we are remembering for this series
220             $template->param(
221                 return_date_override          => $return_date_override,
222                 return_date_override_remember => 1
223             ) if ($return_date_override_remember);
224
225             $return_date_override =
226               DateTime::Format::MySQL->format_datetime( $return_date_override_dt );
227         }
228     }
229     else {
230         $return_date_override = q{};
231     }
232 }
233
234 if ($dotransfer){
235 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
236     my $transferitem = $query->param('transferitem');
237     my $tobranch     = $query->param('tobranch');
238     ModItemTransfer($transferitem, $userenv_branch, $tobranch);
239 }
240
241 if ($canceltransfer){
242     $itemnumber=$query->param('itemnumber');
243     DeleteTransfer($itemnumber);
244     if($dest eq "ttr"){
245         print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
246         exit;
247     } else {
248         $template->param( transfercancelled => 1);
249     }
250 }
251
252 # actually return book and prepare item table.....
253 my $returnbranch;
254 if ($barcode) {
255     $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
256     $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
257     $itemnumber = GetItemnumberFromBarcode($barcode);
258
259 #
260 # save the return
261 #
262
263     # get biblio description
264     my $biblio = GetBiblioFromItemNumber($itemnumber);
265     # fix up item type for display
266     $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
267
268     # Check if we should display a checkin message, based on the the item
269     # type of the checked in item
270     my $itemtype = Koha::ItemTypes->find( $biblio->{'itemtype'} );
271     if ( $itemtype && $itemtype->checkinmsg ) {
272         $template->param(
273             checkinmsg     => $itemtype->checkinmsg,
274             checkinmsgtype => $itemtype->checkinmsgtype,
275         );
276     }
277
278     # make sure return branch respects home branch circulation rules, default to homebranch
279     my $hbr = GetBranchItemRule($biblio->{'homebranch'}, $itemtype ? $itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
280     $returnbranch = $biblio->{$hbr};
281
282     my $materials = $biblio->{'materials'};
283     my $avcode = GetAuthValCode('items.materials');
284     if ($avcode) {
285         $materials = GetKohaAuthorisedValueLib($avcode, $materials);
286     }
287
288     $template->param(
289         title            => $biblio->{'title'},
290         homebranch       => $biblio->{'homebranch'},
291         holdingbranch    => $biblio->{'holdingbranch'},
292         returnbranch     => $returnbranch,
293         author           => $biblio->{'author'},
294         itembarcode      => $biblio->{'barcode'},
295         itemtype         => $biblio->{'itemtype'},
296         ccode            => $biblio->{'ccode'},
297         itembiblionumber => $biblio->{'biblionumber'},
298         biblionumber     => $biblio->{'biblionumber'},
299         borrower         => $borrower,
300         additional_materials => $materials,
301     );
302
303     my %input = (
304         counter => 0,
305         first   => 1,
306         barcode => $barcode,
307     );
308
309     # do the return
310     ( $returned, $messages, $issueinformation, $borrower ) =
311       AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override, $dropboxdate );
312
313     if ($returned) {
314         my $time_now = DateTime->now( time_zone => C4::Context->tz )->truncate( to => 'minute');
315         my $duedate = $issueinformation->{date_due}->strftime('%Y-%m-%d %H:%M');
316         $returneditems{0}      = $barcode;
317         $riborrowernumber{0}   = $borrower->{'borrowernumber'};
318         $riduedate{0}          = $duedate;
319         $input{borrowernumber} = $borrower->{'borrowernumber'};
320         $input{duedate}        = $duedate;
321         unless ( $dropboxmode ) {
322             $input{return_overdue} = 1 if (DateTime->compare($issueinformation->{date_due}, DateTime->now()) == -1);
323         } else {
324             $input{return_overdue} = 1 if (DateTime->compare($issueinformation->{date_due}, $dropboxdate) == -1);
325         }
326         push( @inputloop, \%input );
327
328         if ( C4::Context->preference("FineNotifyAtCheckin") ) {
329             my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrower->{'borrowernumber'} );
330             if ($fines && $fines > 0) {
331                 $template->param( fines => sprintf("%.2f",$fines) );
332                 $template->param( fineborrowernumber => $borrower->{'borrowernumber'} );
333             }
334         }
335
336         if (C4::Context->preference("WaitingNotifyAtCheckin") ) {
337             #Check for waiting holds
338             my @reserves = GetReservesFromBorrowernumber($borrower->{'borrowernumber'});
339             my $waiting_holds = 0;
340             foreach my $num_res (@reserves) {
341                 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
342                     $waiting_holds++;
343                 }
344             }
345             if ($waiting_holds > 0) {
346                 $template->param(
347                     waiting_holds       => $waiting_holds,
348                     holdsborrowernumber => $borrower->{'borrowernumber'},
349                     holdsfirstname => $borrower->{'firstname'},
350                     holdssurname => $borrower->{'surname'},
351                 );
352             }
353         }
354     } elsif ( C4::Context->preference('ShowAllCheckins') and !$messages->{'BadBarcode'} ) {
355         $input{duedate}   = 0;
356         $returneditems{0} = $barcode;
357         $riduedate{0}     = 0;
358         push( @inputloop, \%input );
359     }
360     $template->param( privacy => $borrower->{privacy} );
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 transfered, we have warning message .
370
371 if ( $messages->{'WasTransfered'} ) {
372     $template->param(
373         found          => 1,
374         transfer       => 1,
375         itemnumber     => $itemnumber,
376     );
377 }
378
379 if ( $messages->{'NeedsTransfer'} ){
380     $template->param(
381         found          => 1,
382         needstransfer  => $messages->{'NeedsTransfer'},
383         itemnumber     => $itemnumber,
384     );
385 }
386
387 if ( $messages->{'Wrongbranch'} ){
388     $template->param(
389         wrongbranch => 1,
390         rightbranch => $messages->{'Wrongbranch'}->{'Rightbranch'},
391     );
392 }
393
394 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
395
396 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
397     $template->param(
398         WrongTransfer  => 1,
399         TransferWaitingAt => $messages->{'WrongTransfer'},
400         WrongTransferItem => $messages->{'WrongTransferItem'},
401         itemnumber => $itemnumber,
402     );
403
404     my $reserve    = $messages->{'ResFound'};
405     my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
406     my $borr = C4::Members::GetMember( borrowernumber => $reserve->{'borrowernumber'} );
407     my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
408     $template->param(
409             wname           => $name,
410             wborfirstname   => $borr->{'firstname'},
411             wborsurname     => $borr->{'surname'},
412             wbortitle       => $borr->{'title'},
413             wborphone       => $borr->{'phone'},
414             wboremail       => $borr->{'email'},
415             wborstnum       => $borr->{streetnumber},
416             wboraddress     => $borr->{'address'},
417             wboraddress2    => $borr->{'address2'},
418             wborcity        => $borr->{'city'},
419             wborzip         => $borr->{'zipcode'},
420             wborrowernumber => $reserve->{'borrowernumber'},
421             wborcnum        => $borr->{'cardnumber'},
422             wtransfertFrom  => $userenv_branch,
423     );
424 }
425
426 #
427 # reserve found and item arrived at the expected branch
428 #
429 if ( $messages->{'ResFound'}) {
430     my $reserve    = $messages->{'ResFound'};
431     my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
432     my $borr = C4::Members::GetMember( borrowernumber => $reserve->{'borrowernumber'} );
433     my $holdmsgpreferences =  C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $reserve->{'borrowernumber'}, message_name   => 'Hold_Filled' } );
434     if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
435         if ( $reserve->{'ResFound'} eq "Waiting" ) {
436             $template->param(
437                 waiting      => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
438             );
439         } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
440             $template->param(
441                 intransit    => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
442                 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
443                 resbarcode   => $barcode,
444                 reserved     => 1,
445             );
446         }
447
448         # same params for Waiting or Reserved
449         $template->param(
450             found          => 1,
451             currentbranch  => $branches->{$userenv_branch}->{'branchname'},
452             destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
453             name           => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
454             borfirstname   => $borr->{'firstname'},
455             borsurname     => $borr->{'surname'},
456             bortitle       => $borr->{'title'},
457             borphone       => $borr->{'phone'},
458             boremail       => $borr->{'email'},
459             boraddress     => $borr->{'address'},
460             boraddress2    => $borr->{'address2'},
461             borstnum       => $borr->{streetnumber},
462             borcity        => $borr->{'city'},
463             borzip         => $borr->{'zipcode'},
464             borcnum        => $borr->{'cardnumber'},
465             debarred       => $borr->{'debarred'},
466             gonenoaddress  => $borr->{'gonenoaddress'},
467             barcode        => $barcode,
468             destbranch     => $reserve->{'branchcode'},
469             borrowernumber => $reserve->{'borrowernumber'},
470             itemnumber     => $reserve->{'itemnumber'},
471             reservenotes   => $reserve->{'reservenotes'},
472             bormessagepref => $holdmsgpreferences->{'transports'},
473         );
474     } # else { ; }  # error?
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         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'} if $messages->{'IsPermanent'};
490     }
491     elsif ( $code eq 'LocalUse' ) {
492         $err{localuse} = 1;
493     }
494     elsif ( $code eq 'WasLost' ) {
495         $err{waslost} = 1;
496     }
497     elsif ( $code eq 'LostItemFeeRefunded' ) {
498         $template->param( LostItemFeeRefunded => 1 );
499     }
500     elsif ( $code eq 'ResFound' ) {
501         ;    # FIXME... anything to do here?
502     }
503     elsif ( $code eq 'WasReturned' ) {
504         ;    # FIXME... anything to do here?
505     }
506     elsif ( $code eq 'WasTransfered' ) {
507         ;    # FIXME... anything to do here?
508     }
509     elsif ( $code eq 'withdrawn' ) {
510         $err{withdrawn} = 1;
511         $exit_required_p = 1 if C4::Context->preference("BlockReturnOfWithdrawnItems");
512     }
513     elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
514         if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
515             $err{ispermanent} = 1;
516             $err{msg}         =
517               $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
518         }
519     }
520     elsif ( $code eq 'WrongTransfer' ) {
521         ;    # FIXME... anything to do here?
522     }
523     elsif ( $code eq 'WrongTransferItem' ) {
524         ;    # FIXME... anything to do here?
525     }
526     elsif ( $code eq 'NeedsTransfer' ) {
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 'NotForLoanStatusUpdated' ) {
543         $err{NotForLoanStatusUpdated} = $messages->{NotForLoanStatusUpdated};
544     }
545     else {
546         die "Unknown error code $code";    # note we need all the (empty) elsif's above, or we die.
547         # This forces the issue of staying in sync w/ Circulation.pm
548     }
549     if (%err) {
550         push( @errmsgloop, \%err );
551     }
552     last if $exit_required_p;
553 }
554 $template->param( errmsgloop => \@errmsgloop );
555
556 #set up so only the last 8 returned items display (make for faster loading pages)
557 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
558 my $count = 0;
559 my @riloop;
560 my $shelflocations = GetKohaAuthorisedValues('items.location','');
561 foreach ( sort { $a <=> $b } keys %returneditems ) {
562     my %ri;
563     if ( $count++ < $returned_counter ) {
564         my $bar_code = $returneditems{$_};
565         if ($riduedate{$_}) {
566             my $duedate = dt_from_string( $riduedate{$_}, 'sql');
567             $ri{year}  = $duedate->year();
568             $ri{month} = $duedate->month();
569             $ri{day}   = $duedate->day();
570             $ri{hour}   = $duedate->hour();
571             $ri{minute}   = $duedate->minute();
572             $ri{duedate} = output_pref($duedate);
573             my $b      = C4::Members::GetMember( borrowernumber => $riborrowernumber{$_} );
574             unless ( $dropboxmode ) {
575                 $ri{return_overdue} = 1 if (DateTime->compare($duedate, DateTime->now()) == -1);
576             } else {
577                 $ri{return_overdue} = 1 if (DateTime->compare($duedate, $dropboxdate) == -1);
578             }
579             $ri{borrowernumber} = $b->{'borrowernumber'};
580             $ri{borcnum}        = $b->{'cardnumber'};
581             $ri{borfirstname}   = $b->{'firstname'};
582             $ri{borsurname}     = $b->{'surname'};
583             $ri{bortitle}       = $b->{'title'};
584             $ri{bornote}        = $b->{'borrowernotes'};
585             $ri{borcategorycode}= $b->{'categorycode'};
586         }
587         else {
588             $ri{borrowernumber} = $riborrowernumber{$_};
589         }
590
591         #        my %ri;
592         my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($bar_code));
593         my $item   = GetItem( GetItemnumberFromBarcode($bar_code) );
594         # fix up item type for display
595         $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
596         $ri{itembiblionumber}    = $biblio->{'biblionumber'};
597         $ri{itemtitle}           = $biblio->{'title'};
598         $ri{itemauthor}          = $biblio->{'author'};
599         $ri{itemcallnumber}      = $biblio->{'itemcallnumber'};
600         $ri{dateaccessioned}     = $item->{dateaccessioned};
601         $ri{itemtype}            = $biblio->{'itemtype'};
602         $ri{itemnote}            = $biblio->{'itemnotes'};
603         $ri{itemnotes_nonpublic} = $item->{'itemnotes_nonpublic'};
604         $ri{ccode}               = $biblio->{'ccode'};
605         $ri{itemnumber}          = $biblio->{'itemnumber'};
606         $ri{barcode}             = $bar_code;
607         $ri{homebranch}          = $item->{'homebranch'};
608         $ri{holdingbranch}       = $item->{'holdingbranch'};
609
610         $ri{location}         = $biblio->{'location'};
611         my $shelfcode = $ri{'location'};
612         $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
613
614     }
615     else {
616         last;
617     }
618     push @riloop, \%ri;
619 }
620 my ($genbrname, $genprname);
621 if (my $b = $branches->{$userenv_branch}) {
622     $genbrname = $b->{'branchname'};
623 }
624 if (my $p = $printers->{$printer}) {
625     $genprname = $p->{'printername'};
626 }
627 $template->param(
628     riloop         => \@riloop,
629     genbrname      => $genbrname,
630     genprname      => $genprname,
631     branchname     => $genbrname,
632     printer        => $printer,
633     errmsgloop     => \@errmsgloop,
634     exemptfine     => $exemptfine,
635     dropboxmode    => $dropboxmode,
636     dropboxdate    => output_pref($dropboxdate),
637     forgivemanualholdsexpire => $forgivemanualholdsexpire,
638     overduecharges => $overduecharges,
639     AudioAlerts        => C4::Context->preference("AudioAlerts"),
640     BlockReturnOfWithdrawnItems => C4::Context->preference("BlockReturnOfWithdrawnItems"),
641 );
642
643 $itemnumber = GetItemnumberFromBarcode( $barcode );
644 if ( $itemnumber ) {
645     my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
646     if ( $holdingBranch and $collectionBranch ) {
647         $holdingBranch //= '';
648         $collectionBranch //= $returnbranch;
649         if ( ! ( $holdingBranch eq $collectionBranch ) ) {
650             $template->param(
651               collectionItemNeedsTransferred => 1,
652               collectionBranchName => GetBranchName($collectionBranch),
653               collectionBranch => $collectionBranch,
654               itemnumber => $itemnumber,
655             );
656         }
657     }
658 }
659
660 # actually print the page!
661 output_html_with_http_headers $query, $cookie, $template->output;