3 # Copyright 2000-2002 Katipo Communications
5 # 2007-2010 BibLibre, Paul POULAIN
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 script to execute returns of books
30 #use warnings; FIXME - Bug 2505
34 use C4::Auth qw/:DEFAULT get_session/;
37 use C4::Dates qw/format_date/;
38 use Date::Calc qw/Add_Delta_Days/;
45 use C4::Branch; # GetBranches GetBranchName
46 use C4::Koha; # FIXME : is it still useful ?
47 use C4::RotatingCollections;
51 if (!C4::Context->userenv){
52 my $sessionID = $query->cookie("CGISESSID");
53 my $session = get_session($sessionID);
54 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
55 # no branch set we can't return
56 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
62 my ( $template, $librarian, $cookie ) = get_template_and_user(
64 template_name => "circ/returns.tmpl",
68 flagsrequired => { circulate => "circulate_remaining_permissions" },
74 my $branches = GetBranches();
75 my $printers = GetPrinters();
77 my $printer = C4::Context->userenv ? C4::Context->userenv->{'branchprinter'} : "";
78 my $overduecharges = (C4::Context->preference('finesMode') && C4::Context->preference('finesMode') ne 'off');
80 my $userenv_branch = C4::Context->userenv->{'branch'} || '';
82 # Some code to handle the error if there is no branch or printer setting.....
85 # Set up the item stack ....
90 foreach ( $query->param ) {
103 my $barcode = $query->param("ri-$counter");
104 my $duedate = $query->param("dd-$counter");
105 my $borrowernumber = $query->param("bn-$counter");
108 # decode barcode ## Didn't we already decode them before passing them back last time??
109 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
110 $barcode = barcodedecode($barcode) if(C4::Context->preference('itemBarcodeInputFilter'));
112 ######################
113 #Are these lines still useful ?
114 $returneditems{$counter} = $barcode;
115 $riduedate{$counter} = $duedate;
116 $riborrowernumber{$counter} = $borrowernumber;
118 #######################
119 $input{counter} = $counter;
120 $input{barcode} = $barcode;
121 $input{duedate} = $duedate;
122 $input{borrowernumber} = $borrowernumber;
123 push( @inputloop, \%input );
127 # Deal with the requests....
129 if ($query->param('WT-itemNumber')){
130 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
133 if ( $query->param('resbarcode') ) {
134 my $item = $query->param('itemnumber');
135 my $borrowernumber = $query->param('borrowernumber');
136 my $resbarcode = $query->param('resbarcode');
137 my $diffBranchReturned = $query->param('diffBranch');
138 my $iteminfo = GetBiblioFromItemNumber($item);
139 # fix up item type for display
140 $iteminfo->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $iteminfo->{'itype'} : $iteminfo->{'itemtype'};
141 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ? $diffBranchReturned : undef;
142 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
143 # i.e., whether to apply waiting status
144 ModReserveAffect( $item, $borrowernumber, $diffBranchSend);
145 # check if we have other reserves for this document, if we have a return send the message of transfer
146 my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
148 my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
149 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
150 if ( $messages->{'transfert'} ) {
152 itemtitle => $iteminfo->{'title'},
153 itemnumber => $iteminfo->{'itemnumber'},
154 itembiblionumber => $iteminfo->{'biblionumber'},
155 iteminfo => $iteminfo->{'author'},
156 tobranchname => GetBranchName($messages->{'transfert'}),
158 borrowernumber => $borrowernumber,
159 borcnum => $borr->{'cardnumber'},
160 borfirstname => $borr->{'firstname'},
161 borsurname => $borr->{'surname'},
170 my $issueinformation;
172 my $barcode = $query->param('barcode');
173 my $exemptfine = $query->param('exemptfine');
174 my $dropboxmode = $query->param('dropboxmode');
175 my $dotransfer = $query->param('dotransfer');
176 my $canceltransfer = $query->param('canceltransfer');
177 my $dest = $query->param('dest');
178 my $calendar = C4::Calendar->new( branchcode => $userenv_branch );
179 #dropbox: get last open day (today - 1)
180 my $today = C4::Dates->new();
181 my $today_iso = $today->output('iso');
182 my $dropboxdate = $calendar->addDate($today, -1);
184 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
185 my $transferitem = $query->param('transferitem');
186 my $tobranch = $query->param('tobranch');
187 ModItemTransfer($transferitem, $userenv_branch, $tobranch);
190 if ($canceltransfer){
191 $itemnumber=$query->param('itemnumber');
192 DeleteTransfer($itemnumber);
194 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
197 $template->param( transfercancelled => 1);
201 # actually return book and prepare item table.....
203 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
204 $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
205 $itemnumber = GetItemnumberFromBarcode($barcode);
207 if ( C4::Context->preference("InProcessingToShelvingCart") ) {
208 my $item = GetItem( $itemnumber );
209 if ( $item->{'location'} eq 'PROC' ) {
210 $item->{'location'} = 'CART';
211 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
215 if ( C4::Context->preference("ReturnToShelvingCart") ) {
216 my $item = GetItem( $itemnumber );
217 $item->{'location'} = 'CART';
218 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
224 ( $returned, $messages, $issueinformation, $borrower ) =
225 AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode); # do the return
226 my $homeorholdingbranchreturn = C4::Context->preference('HomeOrHoldingBranchReturn') or 'homebranch';
228 # get biblio description
229 my $biblio = GetBiblioFromItemNumber($itemnumber);
230 # fix up item type for display
231 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
234 title => $biblio->{'title'},
235 homebranch => $biblio->{'homebranch'},
236 homebranchname => GetBranchName( $biblio->{$homeorholdingbranchreturn} ),
237 author => $biblio->{'author'},
238 itembarcode => $biblio->{'barcode'},
239 itemtype => $biblio->{'itemtype'},
240 ccode => $biblio->{'ccode'},
241 itembiblionumber => $biblio->{'biblionumber'},
242 additional_materials => $biblio->{'materials'}
252 my $duedate = $issueinformation->{'date_due'};
253 $returneditems{0} = $barcode;
254 $riborrowernumber{0} = $borrower->{'borrowernumber'};
255 $riduedate{0} = $duedate;
256 $input{borrowernumber} = $borrower->{'borrowernumber'};
257 $input{duedate} = $duedate;
258 $input{return_overdue} = 1 if ($duedate and $duedate lt $today->output('iso'));
259 push( @inputloop, \%input );
261 if ( C4::Context->preference("FineNotifyAtCheckin") ) {
262 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrower->{'borrowernumber'} );
264 $template->param( fines => sprintf("%.2f",$fines) );
265 $template->param( fineborrowernumber => $borrower->{'borrowernumber'} );
269 if (C4::Context->preference("WaitingNotifyAtCheckin") ) {
270 #Check for waiting holds
271 my @reserves = GetReservesFromBorrowernumber($borrower->{'borrowernumber'});
273 foreach my $num_res (@reserves) {
274 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
278 if ($waiting_holds > 0) {
280 waiting_holds => $waiting_holds,
281 holdsborrowernumber => $borrower->{'borrowernumber'},
282 holdsfirstname => $borrower->{'firstname'},
283 holdssurname => $borrower->{'surname'},
288 elsif ( !$messages->{'BadBarcode'} ) {
290 $returneditems{0} = $barcode;
292 if ( $messages->{'wthdrawn'} ) {
293 $input{withdrawn} = 1;
294 $input{borrowernumber} = 'Item Cancelled'; # FIXME: should be in display layer ?
295 $riborrowernumber{0} = 'Item Cancelled';
298 $input{borrowernumber} = ' '; # This seems clearly bogus.
299 $riborrowernumber{0} = ' ';
301 push( @inputloop, \%input );
304 $template->param( inputloop => \@inputloop );
310 # new op dev : we check if the document must be returned to his homebranch directly,
311 # if the document is transfered, we have warning message .
313 if ( $messages->{'WasTransfered'} ) {
317 itemnumber => $itemnumber,
321 if ( $messages->{'NeedsTransfer'} ){
325 itemnumber => $itemnumber,
329 if ( $messages->{'Wrongbranch'} ){
335 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
337 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
338 $messages->{'WrongTransfer'} = GetBranchName( $messages->{'WrongTransfer'} );
341 TransferWaitingAt => $messages->{'WrongTransfer'},
342 WrongTransferItem => $messages->{'WrongTransferItem'},
343 itemnumber => $itemnumber,
346 my $reserve = $messages->{'ResFound'};
347 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
348 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
349 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
352 wborfirstname => $borr->{'firstname'},
353 wborsurname => $borr->{'surname'},
354 wbortitle => $borr->{'title'},
355 wborphone => $borr->{'phone'},
356 wboremail => $borr->{'email'},
357 wboraddress => $borr->{'address'},
358 wboraddress2 => $borr->{'address2'},
359 wborcity => $borr->{'city'},
360 wborzip => $borr->{'zipcode'},
361 wborrowernumber => $reserve->{'borrowernumber'},
362 wborcnum => $borr->{'cardnumber'},
363 wtransfertFrom => $userenv_branch,
368 # reserve found and item arrived at the expected branch
370 if ( $messages->{'ResFound'}) {
371 my $reserve = $messages->{'ResFound'};
372 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
373 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
375 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
376 if ( $reserve->{'ResFound'} eq "Waiting" ) {
378 waiting => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
380 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
382 intransit => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
383 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
384 resbarcode => $barcode,
389 # same params for Waiting or Reserved
392 currentbranch => $branches->{$userenv_branch}->{'branchname'},
393 destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
394 name => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
395 borfirstname => $borr->{'firstname'},
396 borsurname => $borr->{'surname'},
397 bortitle => $borr->{'title'},
398 borphone => $borr->{'phone'},
399 boremail => $borr->{'email'},
400 boraddress => $borr->{'address'},
401 boraddress2 => $borr->{'address2'},
402 borcity => $borr->{'city'},
403 borzip => $borr->{'zipcode'},
404 borcnum => $borr->{'cardnumber'},
405 debarred => $borr->{'debarred'},
406 gonenoaddress => $borr->{'gonenoaddress'},
408 destbranch => $reserve->{'branchcode'},
409 borrowernumber => $reserve->{'borrowernumber'},
410 itemnumber => $reserve->{'itemnumber'},
411 reservenotes => $reserve->{'reservenotes'},
413 } # else { ; } # error?
418 foreach my $code ( keys %$messages ) {
420 my $exit_required_p = 0;
421 if ( $code eq 'BadBarcode' ) {
422 $err{badbarcode} = 1;
423 $err{msg} = $messages->{'BadBarcode'};
425 elsif ( $code eq 'NotIssued' ) {
427 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
429 elsif ( $code eq 'LocalUse' ) {
432 elsif ( $code eq 'WasLost' ) {
435 elsif ( $code eq 'ResFound' ) {
436 ; # FIXME... anything to do here?
438 elsif ( $code eq 'WasReturned' ) {
439 ; # FIXME... anything to do here?
441 elsif ( $code eq 'WasTransfered' ) {
442 ; # FIXME... anything to do here?
444 elsif ( $code eq 'wthdrawn' ) {
446 $exit_required_p = 1;
448 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
449 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
450 $err{ispermanent} = 1;
452 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
455 elsif ( $code eq 'WrongTransfer' ) {
456 ; # FIXME... anything to do here?
458 elsif ( $code eq 'WrongTransferItem' ) {
459 ; # FIXME... anything to do here?
461 elsif ( $code eq 'NeedsTransfer' ) {
463 elsif ( $code eq 'Wrongbranch' ) {
465 elsif ( $code eq 'Debarred' ) {
466 $err{debarred} = format_date( $messages->{'Debarred'} );
467 $err{debarcardnumber} = $borrower->{cardnumber};
468 $err{debarborrowernumber} = $borrower->{borrowernumber};
469 $err{debarname} = "$borrower->{firstname} $borrower->{surname}";
472 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
473 # This forces the issue of staying in sync w/ Circulation.pm
476 push( @errmsgloop, \%err );
478 last if $exit_required_p;
480 $template->param( errmsgloop => \@errmsgloop );
484 my $flags = $borrower->{'flags'};
487 foreach my $flag ( sort keys %$flags ) {
489 unless ($flagset) { $flagset = 1; }
490 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
491 $flaginfo{flag} = $flag;
492 if ( $flag eq 'CHARGES' ) {
493 $flaginfo{msg} = $flag;
494 $flaginfo{charges} = 1;
495 $flaginfo{chargeamount} = $flags->{$flag}->{amount};
496 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
498 elsif ( $flag eq 'WAITING' ) {
499 $flaginfo{msg} = $flag;
500 $flaginfo{waiting} = 1;
502 my $items = $flags->{$flag}->{'itemlist'};
503 foreach my $item (@$items) {
504 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
505 push @waitingitemloop, {
506 biblionum => $biblio->{'biblionumber'},
507 barcode => $biblio->{'barcode'},
508 title => $biblio->{'title'},
509 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
512 $flaginfo{itemloop} = \@waitingitemloop;
514 elsif ( $flag eq 'ODUES' ) {
515 my $items = $flags->{$flag}->{'itemlist'};
517 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
520 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
522 duedate => format_date($item->{'date_due'}),
523 biblionum => $biblio->{'biblionumber'},
524 barcode => $biblio->{'barcode'},
525 title => $biblio->{'title'},
526 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
529 $flaginfo{itemloop} = \@itemloop;
530 $flaginfo{overdue} = 1;
533 $flaginfo{other} = 1;
534 $flaginfo{msg} = $flags->{$flag}->{'message'};
536 push( @flagloop, \%flaginfo );
540 flagloop => \@flagloop,
541 riborrowernumber => $borrower->{'borrowernumber'},
542 riborcnum => $borrower->{'cardnumber'},
543 riborsurname => $borrower->{'surname'},
544 ribortitle => $borrower->{'title'},
545 riborfirstname => $borrower->{'firstname'}
549 #set up so only the last 8 returned items display (make for faster loading pages)
550 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
553 my $shelflocations = GetKohaAuthorisedValues('items.location','');
554 foreach ( sort { $a <=> $b } keys %returneditems ) {
556 if ( $count++ < $returned_counter ) {
557 my $bar_code = $returneditems{$_};
558 my $duedate = $riduedate{$_};
560 my @tempdate = split( /-/, $duedate );
561 $ri{year} = $tempdate[0];
562 $ri{month} = $tempdate[1];
563 $ri{day} = $tempdate[2];
564 $ri{duedate} = format_date($duedate);
565 my ($b) = GetMemberDetails( $riborrowernumber{$_}, 0 );
566 $ri{return_overdue} = 1 if ($duedate lt $today->output('iso'));
567 $ri{borrowernumber} = $b->{'borrowernumber'};
568 $ri{borcnum} = $b->{'cardnumber'};
569 $ri{borfirstname} = $b->{'firstname'};
570 $ri{borsurname} = $b->{'surname'};
571 $ri{bortitle} = $b->{'title'};
572 $ri{bornote} = $b->{'borrowernotes'};
573 $ri{borcategorycode}= $b->{'categorycode'};
576 $ri{borrowernumber} = $riborrowernumber{$_};
580 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($bar_code));
581 # fix up item type for display
582 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
583 $ri{itembiblionumber} = $biblio->{'biblionumber'};
584 $ri{itemtitle} = $biblio->{'title'};
585 $ri{itemauthor} = $biblio->{'author'};
586 $ri{itemcallnumber} = $biblio->{'itemcallnumber'};
587 $ri{itemtype} = $biblio->{'itemtype'};
588 $ri{itemnote} = $biblio->{'itemnotes'};
589 $ri{ccode} = $biblio->{'ccode'};
590 $ri{itemnumber} = $biblio->{'itemnumber'};
591 $ri{barcode} = $bar_code;
593 $ri{location} = $biblio->{'location'};
594 my $shelfcode = $ri{'location'};
595 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
606 genbrname => $branches->{$userenv_branch}->{'branchname'},
607 genprname => $printers->{$printer}->{'printername'},
608 branchname => $branches->{$userenv_branch}->{'branchname'},
610 errmsgloop => \@errmsgloop,
611 exemptfine => $exemptfine,
612 dropboxmode => $dropboxmode,
613 dropboxdate => $dropboxdate->output(),
614 overduecharges => $overduecharges,
615 soundon => C4::Context->preference("SoundOn"),
618 ### Comment out rotating collections for now to allow it a little more time to bake
619 ### for 3.4; in particular, must ensure that it doesn't fight with transfers required
620 ### to fill hold requests
621 ### -- Galen Charlton 2010-10-06
622 #my $itemnumber = GetItemnumberFromBarcode( $query->param('barcode') );
623 #if ( $itemnumber ) {
624 # my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
625 # if ( ! ( $holdingBranch eq $collectionBranch ) ) {
627 # collectionItemNeedsTransferred => 1,
628 # collectionBranch => GetBranchName($collectionBranch),
633 # actually print the page!
634 output_html_with_http_headers $query, $cookie, $template->output;