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 itembiblionumber => $iteminfo->{'biblionumber'},
154 iteminfo => $iteminfo->{'author'},
155 tobranchname => GetBranchName($messages->{'transfert'}),
157 borrowernumber => $borrowernumber,
158 borcnum => $borr->{'cardnumber'},
159 borfirstname => $borr->{'firstname'},
160 borsurname => $borr->{'surname'},
169 my $issueinformation;
171 my $barcode = $query->param('barcode');
172 my $exemptfine = $query->param('exemptfine');
173 my $dropboxmode = $query->param('dropboxmode');
174 my $dotransfer = $query->param('dotransfer');
175 my $canceltransfer = $query->param('canceltransfer');
176 my $dest = $query->param('dest');
177 my $calendar = C4::Calendar->new( branchcode => $userenv_branch );
178 #dropbox: get last open day (today - 1)
179 my $today = C4::Dates->new();
180 my $today_iso = $today->output('iso');
181 my $dropboxdate = $calendar->addDate($today, -1);
183 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
184 my $transferitem = $query->param('transferitem');
185 my $tobranch = $query->param('tobranch');
186 ModItemTransfer($transferitem, $userenv_branch, $tobranch);
189 if ($canceltransfer){
190 $itemnumber=$query->param('itemnumber');
191 DeleteTransfer($itemnumber);
193 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
196 $template->param( transfercancelled => 1);
200 # actually return book and prepare item table.....
202 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
203 $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
204 $itemnumber = GetItemnumberFromBarcode($barcode);
206 if ( C4::Context->preference("InProcessingToShelvingCart") ) {
207 my $item = GetItem( $itemnumber );
208 if ( $item->{'location'} eq 'PROC' ) {
209 $item->{'location'} = 'CART';
210 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
214 if ( C4::Context->preference("ReturnToShelvingCart") ) {
215 my $item = GetItem( $itemnumber );
216 $item->{'location'} = 'CART';
217 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
223 ( $returned, $messages, $issueinformation, $borrower ) =
224 AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode); # do the return
225 my $homeorholdingbranchreturn = C4::Context->preference('HomeOrHoldingBranchReturn') or 'homebranch';
227 # get biblio description
228 my $biblio = GetBiblioFromItemNumber($itemnumber);
229 # fix up item type for display
230 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
233 title => $biblio->{'title'},
234 homebranch => $biblio->{'homebranch'},
235 homebranchname => GetBranchName( $biblio->{$homeorholdingbranchreturn} ),
236 author => $biblio->{'author'},
237 itembarcode => $biblio->{'barcode'},
238 itemtype => $biblio->{'itemtype'},
239 ccode => $biblio->{'ccode'},
240 itembiblionumber => $biblio->{'biblionumber'},
250 my $duedate = $issueinformation->{'date_due'};
251 $returneditems{0} = $barcode;
252 $riborrowernumber{0} = $borrower->{'borrowernumber'};
253 $riduedate{0} = $duedate;
254 $input{borrowernumber} = $borrower->{'borrowernumber'};
255 $input{duedate} = $duedate;
256 $input{return_overdue} = 1 if ($duedate and $duedate lt $today->output('iso'));
257 push( @inputloop, \%input );
259 if ( C4::Context->preference("FineNotifyAtCheckin") ) {
260 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrower->{'borrowernumber'} );
262 $template->param( fines => sprintf("%.2f",$fines) );
263 $template->param( fineborrowernumber => $borrower->{'borrowernumber'} );
267 if (C4::Context->preference("WaitingNotifyAtCheckin") ) {
268 #Check for waiting holds
269 my @reserves = GetReservesFromBorrowernumber($borrower->{'borrowernumber'});
271 foreach my $num_res (@reserves) {
272 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
276 if ($waiting_holds > 0) {
278 waiting_holds => $waiting_holds,
279 holdsborrowernumber => $borrower->{'borrowernumber'},
284 elsif ( !$messages->{'BadBarcode'} ) {
286 $returneditems{0} = $barcode;
288 if ( $messages->{'wthdrawn'} ) {
289 $input{withdrawn} = 1;
290 $input{borrowernumber} = 'Item Cancelled'; # FIXME: should be in display layer ?
291 $riborrowernumber{0} = 'Item Cancelled';
294 $input{borrowernumber} = ' '; # This seems clearly bogus.
295 $riborrowernumber{0} = ' ';
297 push( @inputloop, \%input );
300 $template->param( inputloop => \@inputloop );
306 # new op dev : we check if the document must be returned to his homebranch directly,
307 # if the document is transfered, we have warning message .
309 if ( $messages->{'WasTransfered'} ) {
316 if ( $messages->{'NeedsTransfer'} ){
320 itemnumber => $itemnumber,
324 if ( $messages->{'Wrongbranch'} ){
330 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
332 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
333 $messages->{'WrongTransfer'} = GetBranchName( $messages->{'WrongTransfer'} );
336 TransferWaitingAt => $messages->{'WrongTransfer'},
337 WrongTransferItem => $messages->{'WrongTransferItem'},
338 itemnumber => $itemnumber,
341 my $reserve = $messages->{'ResFound'};
342 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
343 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
344 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
347 wborfirstname => $borr->{'firstname'},
348 wborsurname => $borr->{'surname'},
349 wbortitle => $borr->{'title'},
350 wborphone => $borr->{'phone'},
351 wboremail => $borr->{'email'},
352 wboraddress => $borr->{'address'},
353 wboraddress2 => $borr->{'address2'},
354 wborcity => $borr->{'city'},
355 wborzip => $borr->{'zipcode'},
356 wborrowernumber => $reserve->{'borrowernumber'},
357 wborcnum => $borr->{'cardnumber'},
358 wtransfertFrom => $userenv_branch,
363 # reserve found and item arrived at the expected branch
365 if ( $messages->{'ResFound'}) {
366 my $reserve = $messages->{'ResFound'};
367 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
368 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
370 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
371 if ( $reserve->{'ResFound'} eq "Waiting" ) {
373 waiting => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
375 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
377 intransit => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
378 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
379 resbarcode => $barcode,
384 # same params for Waiting or Reserved
387 currentbranch => $branches->{$userenv_branch}->{'branchname'},
388 destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
389 name => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
390 borfirstname => $borr->{'firstname'},
391 borsurname => $borr->{'surname'},
392 bortitle => $borr->{'title'},
393 borphone => $borr->{'phone'},
394 boremail => $borr->{'email'},
395 boraddress => $borr->{'address'},
396 boraddress2 => $borr->{'address2'},
397 borcity => $borr->{'city'},
398 borzip => $borr->{'zipcode'},
399 borcnum => $borr->{'cardnumber'},
400 debarred => $borr->{'debarred'},
401 gonenoaddress => $borr->{'gonenoaddress'},
403 destbranch => $reserve->{'branchcode'},
404 borrowernumber => $reserve->{'borrowernumber'},
405 itemnumber => $reserve->{'itemnumber'},
406 reservenotes => $reserve->{'reservenotes'},
408 } # else { ; } # error?
413 foreach my $code ( keys %$messages ) {
415 my $exit_required_p = 0;
416 if ( $code eq 'BadBarcode' ) {
417 $err{badbarcode} = 1;
418 $err{msg} = $messages->{'BadBarcode'};
420 elsif ( $code eq 'NotIssued' ) {
422 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
424 elsif ( $code eq 'WasLost' ) {
427 elsif ( $code eq 'ResFound' ) {
428 ; # FIXME... anything to do here?
430 elsif ( $code eq 'WasReturned' ) {
431 ; # FIXME... anything to do here?
433 elsif ( $code eq 'WasTransfered' ) {
434 ; # FIXME... anything to do here?
436 elsif ( $code eq 'wthdrawn' ) {
438 $exit_required_p = 1;
440 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
441 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
442 $err{ispermanent} = 1;
444 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
447 elsif ( $code eq 'WrongTransfer' ) {
448 ; # FIXME... anything to do here?
450 elsif ( $code eq 'WrongTransferItem' ) {
451 ; # FIXME... anything to do here?
453 elsif ( $code eq 'NeedsTransfer' ) {
455 elsif ( $code eq 'Wrongbranch' ) {
459 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
460 # This forces the issue of staying in sync w/ Circulation.pm
463 push( @errmsgloop, \%err );
465 last if $exit_required_p;
467 $template->param( errmsgloop => \@errmsgloop );
471 my $flags = $borrower->{'flags'};
474 foreach my $flag ( sort keys %$flags ) {
476 unless ($flagset) { $flagset = 1; }
477 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
478 $flaginfo{flag} = $flag;
479 if ( $flag eq 'CHARGES' ) {
480 $flaginfo{msg} = $flag;
481 $flaginfo{charges} = 1;
482 $flaginfo{chargeamount} = $flags->{$flag}->{amount};
483 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
485 elsif ( $flag eq 'WAITING' ) {
486 $flaginfo{msg} = $flag;
487 $flaginfo{waiting} = 1;
489 my $items = $flags->{$flag}->{'itemlist'};
490 foreach my $item (@$items) {
491 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
492 push @waitingitemloop, {
493 biblionum => $biblio->{'biblionumber'},
494 barcode => $biblio->{'barcode'},
495 title => $biblio->{'title'},
496 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
499 $flaginfo{itemloop} = \@waitingitemloop;
501 elsif ( $flag eq 'ODUES' ) {
502 my $items = $flags->{$flag}->{'itemlist'};
504 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
507 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
509 duedate => format_date($item->{'date_due'}),
510 biblionum => $biblio->{'biblionumber'},
511 barcode => $biblio->{'barcode'},
512 title => $biblio->{'title'},
513 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
516 $flaginfo{itemloop} = \@itemloop;
517 $flaginfo{overdue} = 1;
520 $flaginfo{other} = 1;
521 $flaginfo{msg} = $flags->{$flag}->{'message'};
523 push( @flagloop, \%flaginfo );
527 flagloop => \@flagloop,
528 riborrowernumber => $borrower->{'borrowernumber'},
529 riborcnum => $borrower->{'cardnumber'},
530 riborsurname => $borrower->{'surname'},
531 ribortitle => $borrower->{'title'},
532 riborfirstname => $borrower->{'firstname'}
536 #set up so only the last 8 returned items display (make for faster loading pages)
537 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
540 my $shelflocations = GetKohaAuthorisedValues('items.location','');
541 foreach ( sort { $a <=> $b } keys %returneditems ) {
543 if ( $count++ < $returned_counter ) {
544 my $bar_code = $returneditems{$_};
545 my $duedate = $riduedate{$_};
547 my @tempdate = split( /-/, $duedate );
548 $ri{year} = $tempdate[0];
549 $ri{month} = $tempdate[1];
550 $ri{day} = $tempdate[2];
551 $ri{duedate} = format_date($duedate);
552 my ($b) = GetMemberDetails( $riborrowernumber{$_}, 0 );
553 $ri{return_overdue} = 1 if ($duedate lt $today->output('iso'));
554 $ri{borrowernumber} = $b->{'borrowernumber'};
555 $ri{borcnum} = $b->{'cardnumber'};
556 $ri{borfirstname} = $b->{'firstname'};
557 $ri{borsurname} = $b->{'surname'};
558 $ri{bortitle} = $b->{'title'};
559 $ri{bornote} = $b->{'borrowernotes'};
560 $ri{borcategorycode}= $b->{'categorycode'};
563 $ri{borrowernumber} = $riborrowernumber{$_};
567 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($bar_code));
568 # fix up item type for display
569 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
570 $ri{itembiblionumber} = $biblio->{'biblionumber'};
571 $ri{itemtitle} = $biblio->{'title'};
572 $ri{itemauthor} = $biblio->{'author'};
573 $ri{itemcallnumber} = $biblio->{'itemcallnumber'};
574 $ri{itemtype} = $biblio->{'itemtype'};
575 $ri{itemnote} = $biblio->{'itemnotes'};
576 $ri{ccode} = $biblio->{'ccode'};
577 $ri{itemnumber} = $biblio->{'itemnumber'};
578 $ri{barcode} = $bar_code;
580 $ri{location} = $biblio->{'location'};
581 my $shelfcode = $ri{'location'};
582 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
593 genbrname => $branches->{$userenv_branch}->{'branchname'},
594 genprname => $printers->{$printer}->{'printername'},
595 branchname => $branches->{$userenv_branch}->{'branchname'},
597 errmsgloop => \@errmsgloop,
598 exemptfine => $exemptfine,
599 dropboxmode => $dropboxmode,
600 dropboxdate => $dropboxdate->output(),
601 overduecharges => $overduecharges,
602 soundon => C4::Context->preference("SoundOn"),
605 ### Comment out rotating collections for now to allow it a little more time to bake
606 ### for 3.4; in particular, must ensure that it doesn't fight with transfers required
607 ### to fill hold requests
608 ### -- Galen Charlton 2010-10-06
609 #my $itemnumber = GetItemnumberFromBarcode( $query->param('barcode') );
610 #if ( $itemnumber ) {
611 # my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
612 # if ( ! ( $holdingBranch eq $collectionBranch ) ) {
614 # collectionItemNeedsTransferred => 1,
615 # collectionBranch => GetBranchName($collectionBranch),
620 # actually print the page!
621 output_html_with_http_headers $query, $cookie, $template->output;