3 # Copyright 2000-2002 Katipo Communications
5 # 2007-2010 BibLibre, Paul POULAIN
7 # 2011 PTFS-Europe Ltd.
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 script to execute returns of books
36 use C4::Auth qw/:DEFAULT get_session/;
44 use C4::Branch; # GetBranches GetBranchName
45 use C4::Koha; # FIXME : is it still useful ?
46 use C4::RotatingCollections;
53 my ( $template, $librarian, $cookie ) = get_template_and_user(
55 template_name => "circ/returns.tmpl",
59 flagsrequired => { circulate => "circulate_remaining_permissions" },
63 my $sessionID = $query->cookie("CGISESSID");
64 my $session = get_session($sessionID);
65 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
66 # no branch set we can't return
67 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
73 my $branches = GetBranches();
74 my $printers = GetPrinters();
75 my $userenv = C4::Context->userenv;
76 my $userenv_branch = $userenv->{'branch'} // '';
77 my $printer = $userenv->{'branchprinter'} // '';
79 my $overduecharges = (C4::Context->preference('finesMode') && C4::Context->preference('finesMode') ne 'off');
81 # Some code to handle the error if there is no branch or printer setting.....
84 # Set up the item stack ....
89 foreach ( $query->param ) {
102 my $barcode = $query->param("ri-$counter");
103 my $duedate = $query->param("dd-$counter");
104 my $borrowernumber = $query->param("bn-$counter");
107 # decode barcode ## Didn't we already decode them before passing them back last time??
108 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
109 $barcode = barcodedecode($barcode) if(C4::Context->preference('itemBarcodeInputFilter'));
111 ######################
112 #Are these lines still useful ?
113 $returneditems{$counter} = $barcode;
114 $riduedate{$counter} = $duedate;
115 $riborrowernumber{$counter} = $borrowernumber;
117 #######################
118 $input{counter} = $counter;
119 $input{barcode} = $barcode;
120 $input{duedate} = $duedate;
121 $input{borrowernumber} = $borrowernumber;
122 push( @inputloop, \%input );
126 # Deal with the requests....
128 if ($query->param('WT-itemNumber')){
129 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
132 if ( $query->param('resbarcode') ) {
133 my $item = $query->param('itemnumber');
134 my $borrowernumber = $query->param('borrowernumber');
135 my $resbarcode = $query->param('resbarcode');
136 my $diffBranchReturned = $query->param('diffBranch');
137 my $iteminfo = GetBiblioFromItemNumber($item);
138 # fix up item type for display
139 $iteminfo->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $iteminfo->{'itype'} : $iteminfo->{'itemtype'};
140 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ? $diffBranchReturned : undef;
141 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
142 # i.e., whether to apply waiting status
143 ModReserveAffect( $item, $borrowernumber, $diffBranchSend);
144 # check if we have other reserves for this document, if we have a return send the message of transfer
145 my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
147 my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
148 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
149 if ( $messages->{'transfert'} ) {
151 itemtitle => $iteminfo->{'title'},
152 itemnumber => $iteminfo->{'itemnumber'},
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');
175 !C4::Auth::haspermission(C4::Context->userenv->{'id'}, {'updatecharges' => 'writeoff'})
177 # silently prevent unauthorized operator from forgiving overdue
178 # fines by manually tweaking form parameters
181 my $dropboxmode = $query->param('dropboxmode');
182 my $dotransfer = $query->param('dotransfer');
183 my $canceltransfer = $query->param('canceltransfer');
184 my $dest = $query->param('dest');
185 my $calendar = Koha::Calendar->new( branchcode => $userenv_branch );
186 #dropbox: get last open day (today - 1)
187 my $today = DateTime->now( time_zone => C4::Context->tz());
188 my $dropboxdate = $calendar->addDate($today, -1);
190 my $return_date_override = $query->param('return_date_override');
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 if ( $return_date_override =~ C4::Dates->regexp('syspref') ) {
197 # Save the original format if we are remembering for this series
199 return_date_override => $return_date_override,
200 return_date_override_remember => 1
201 ) if ($return_date_override_remember);
203 my $dt = dt_from_string($return_date_override);
204 $return_date_override =
205 DateTime::Format::MySQL->format_datetime($dt);
209 $return_date_override = q{};
214 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
215 my $transferitem = $query->param('transferitem');
216 my $tobranch = $query->param('tobranch');
217 ModItemTransfer($transferitem, $userenv_branch, $tobranch);
220 if ($canceltransfer){
221 $itemnumber=$query->param('itemnumber');
222 DeleteTransfer($itemnumber);
224 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
227 $template->param( transfercancelled => 1);
231 # actually return book and prepare item table.....
233 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
234 $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
235 $itemnumber = GetItemnumberFromBarcode($barcode);
237 if ( C4::Context->preference("InProcessingToShelvingCart") ) {
238 my $item = GetItem( $itemnumber );
239 if ( $item->{'location'} eq 'PROC' ) {
240 $item->{'location'} = 'CART';
241 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
248 ( $returned, $messages, $issueinformation, $borrower ) =
249 AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override );
250 my $homeorholdingbranchreturn = C4::Context->preference('HomeOrHoldingBranchReturn');
251 $homeorholdingbranchreturn ||= 'homebranch';
253 # get biblio description
254 my $biblio = GetBiblioFromItemNumber($itemnumber);
255 # fix up item type for display
256 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
258 # Check if we should display a checkin message, based on the the item
259 # type of the checked in item
260 my $itemtype = C4::ItemType->get( $biblio->{'itemtype'} );
261 if ( $itemtype->{'checkinmsg'} ) {
263 checkinmsg => $itemtype->{'checkinmsg'},
264 checkinmsgtype => $itemtype->{'checkinmsgtype'},
269 title => $biblio->{'title'},
270 homebranch => $biblio->{'homebranch'},
271 homebranchname => GetBranchName( $biblio->{$homeorholdingbranchreturn} ),
272 author => $biblio->{'author'},
273 itembarcode => $biblio->{'barcode'},
274 itemtype => $biblio->{'itemtype'},
275 ccode => $biblio->{'ccode'},
276 itembiblionumber => $biblio->{'biblionumber'},
277 borrower => $borrower,
278 additional_materials => $biblio->{'materials'},
288 my $time_now = DateTime->now( time_zone => C4::Context->tz )->truncate( to => 'minute');
289 my $duedate = $issueinformation->{date_due}->strftime('%Y-%m-%d %H:%M');
290 $returneditems{0} = $barcode;
291 $riborrowernumber{0} = $borrower->{'borrowernumber'};
292 $riduedate{0} = $duedate;
293 $input{borrowernumber} = $borrower->{'borrowernumber'};
294 $input{duedate} = $duedate;
295 $input{return_overdue} = 1 if (DateTime->compare($issueinformation->{date_due}, $time_now) == -1);
296 push( @inputloop, \%input );
298 if ( C4::Context->preference("FineNotifyAtCheckin") ) {
299 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrower->{'borrowernumber'} );
300 if ($fines && $fines > 0) {
301 $template->param( fines => sprintf("%.2f",$fines) );
302 $template->param( fineborrowernumber => $borrower->{'borrowernumber'} );
306 if (C4::Context->preference("WaitingNotifyAtCheckin") ) {
307 #Check for waiting holds
308 my @reserves = GetReservesFromBorrowernumber($borrower->{'borrowernumber'});
309 my $waiting_holds = 0;
310 foreach my $num_res (@reserves) {
311 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
315 if ($waiting_holds > 0) {
317 waiting_holds => $waiting_holds,
318 holdsborrowernumber => $borrower->{'borrowernumber'},
319 holdsfirstname => $borrower->{'firstname'},
320 holdssurname => $borrower->{'surname'},
325 elsif ( !$messages->{'BadBarcode'} ) {
327 $returneditems{0} = $barcode;
329 push( @inputloop, \%input );
332 $template->param( inputloop => \@inputloop );
338 # new op dev : we check if the document must be returned to his homebranch directly,
339 # if the document is transfered, we have warning message .
341 if ( $messages->{'WasTransfered'} ) {
345 itemnumber => $itemnumber,
349 if ( $messages->{'NeedsTransfer'} ){
353 itemnumber => $itemnumber,
357 if ( $messages->{'Wrongbranch'} ){
363 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
365 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
368 TransferWaitingAt => $messages->{'WrongTransfer'},
369 WrongTransferItem => $messages->{'WrongTransferItem'},
370 itemnumber => $itemnumber,
373 my $reserve = $messages->{'ResFound'};
374 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
375 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
376 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
379 wborfirstname => $borr->{'firstname'},
380 wborsurname => $borr->{'surname'},
381 wbortitle => $borr->{'title'},
382 wborphone => $borr->{'phone'},
383 wboremail => $borr->{'email'},
384 wboraddress => $borr->{'address'},
385 wboraddress2 => $borr->{'address2'},
386 wborcity => $borr->{'city'},
387 wborzip => $borr->{'zipcode'},
388 wborrowernumber => $reserve->{'borrowernumber'},
389 wborcnum => $borr->{'cardnumber'},
390 wtransfertFrom => $userenv_branch,
395 # reserve found and item arrived at the expected branch
397 if ( $messages->{'ResFound'}) {
398 my $reserve = $messages->{'ResFound'};
399 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
400 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
402 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
403 if ( $reserve->{'ResFound'} eq "Waiting" ) {
405 waiting => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
407 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
409 intransit => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
410 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
411 resbarcode => $barcode,
416 # same params for Waiting or Reserved
419 currentbranch => $branches->{$userenv_branch}->{'branchname'},
420 destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
421 name => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
422 borfirstname => $borr->{'firstname'},
423 borsurname => $borr->{'surname'},
424 bortitle => $borr->{'title'},
425 borphone => $borr->{'phone'},
426 boremail => $borr->{'email'},
427 boraddress => $borr->{'address'},
428 boraddress2 => $borr->{'address2'},
429 borcity => $borr->{'city'},
430 borzip => $borr->{'zipcode'},
431 borcnum => $borr->{'cardnumber'},
432 debarred => $borr->{'debarred'},
433 gonenoaddress => $borr->{'gonenoaddress'},
435 destbranch => $reserve->{'branchcode'},
436 borrowernumber => $reserve->{'borrowernumber'},
437 itemnumber => $reserve->{'itemnumber'},
438 reservenotes => $reserve->{'reservenotes'},
440 } # else { ; } # error?
445 foreach my $code ( keys %$messages ) {
447 my $exit_required_p = 0;
448 if ( $code eq 'BadBarcode' ) {
449 $err{badbarcode} = 1;
450 $err{msg} = $messages->{'BadBarcode'};
452 elsif ( $code eq 'NotIssued' ) {
454 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
456 elsif ( $code eq 'LocalUse' ) {
459 elsif ( $code eq 'WasLost' ) {
462 elsif ( $code eq 'LostItemFeeRefunded' ) {
463 $template->param( LostItemFeeRefunded => 1 );
465 elsif ( $code eq 'ResFound' ) {
466 ; # FIXME... anything to do here?
468 elsif ( $code eq 'WasReturned' ) {
469 ; # FIXME... anything to do here?
471 elsif ( $code eq 'WasTransfered' ) {
472 ; # FIXME... anything to do here?
474 elsif ( $code eq 'withdrawn' ) {
476 $exit_required_p = 1 if C4::Context->preference("BlockReturnOfWithdrawnItems");
478 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
479 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
480 $err{ispermanent} = 1;
482 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
485 elsif ( $code eq 'WrongTransfer' ) {
486 ; # FIXME... anything to do here?
488 elsif ( $code eq 'WrongTransferItem' ) {
489 ; # FIXME... anything to do here?
491 elsif ( $code eq 'NeedsTransfer' ) {
493 elsif ( $code eq 'Wrongbranch' ) {
495 elsif ( $code eq 'Debarred' ) {
496 $err{debarred} = $messages->{'Debarred'};
497 $err{debarcardnumber} = $borrower->{cardnumber};
498 $err{debarborrowernumber} = $borrower->{borrowernumber};
499 $err{debarname} = "$borrower->{firstname} $borrower->{surname}";
502 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
503 # This forces the issue of staying in sync w/ Circulation.pm
506 push( @errmsgloop, \%err );
508 last if $exit_required_p;
510 $template->param( errmsgloop => \@errmsgloop );
512 #set up so only the last 8 returned items display (make for faster loading pages)
513 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
516 my $shelflocations = GetKohaAuthorisedValues('items.location','');
517 foreach ( sort { $a <=> $b } keys %returneditems ) {
519 if ( $count++ < $returned_counter ) {
520 my $bar_code = $returneditems{$_};
521 if ($riduedate{$_}) {
522 my $duedate = dt_from_string( $riduedate{$_}, 'sql');
523 $ri{year} = $duedate->year();
524 $ri{month} = $duedate->month();
525 $ri{day} = $duedate->day();
526 $ri{hour} = $duedate->hour();
527 $ri{minute} = $duedate->minute();
528 $ri{duedate} = output_pref($duedate);
529 my ($b) = GetMemberDetails( $riborrowernumber{$_}, 0 );
530 $ri{return_overdue} = 1 if (DateTime->compare($duedate, DateTime->now()) == -1 );
531 $ri{borrowernumber} = $b->{'borrowernumber'};
532 $ri{borcnum} = $b->{'cardnumber'};
533 $ri{borfirstname} = $b->{'firstname'};
534 $ri{borsurname} = $b->{'surname'};
535 $ri{bortitle} = $b->{'title'};
536 $ri{bornote} = $b->{'borrowernotes'};
537 $ri{borcategorycode}= $b->{'categorycode'};
540 $ri{borrowernumber} = $riborrowernumber{$_};
544 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($bar_code));
545 my $item = GetItem( GetItemnumberFromBarcode($bar_code) );
546 # fix up item type for display
547 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
548 $ri{itembiblionumber} = $biblio->{'biblionumber'};
549 $ri{itemtitle} = $biblio->{'title'};
550 $ri{itemauthor} = $biblio->{'author'};
551 $ri{itemcallnumber} = $biblio->{'itemcallnumber'};
552 $ri{itemtype} = $biblio->{'itemtype'};
553 $ri{itemnote} = $biblio->{'itemnotes'};
554 $ri{ccode} = $biblio->{'ccode'};
555 $ri{itemnumber} = $biblio->{'itemnumber'};
556 $ri{barcode} = $bar_code;
557 $ri{homebranch} = $item->{'homebranch'};
558 $ri{holdingbranch} = $item->{'holdingbranch'};
560 $ri{location} = $biblio->{'location'};
561 my $shelfcode = $ri{'location'};
562 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
570 my ($genbrname, $genprname);
571 if (my $b = $branches->{$userenv_branch}) {
572 $genbrname = $b->{'branchname'};
574 if (my $p = $printers->{$printer}) {
575 $genprname = $p->{'printername'};
579 genbrname => $genbrname,
580 genprname => $genprname,
581 branchname => $genbrname,
583 errmsgloop => \@errmsgloop,
584 exemptfine => $exemptfine,
585 dropboxmode => $dropboxmode,
586 dropboxdate => output_pref($dropboxdate),
587 overduecharges => $overduecharges,
588 soundon => C4::Context->preference("SoundOn"),
589 BlockReturnOfWithdrawnItems => C4::Context->preference("BlockReturnOfWithdrawnItems"),
592 ### Comment out rotating collections for now to allow it a little more time to bake
593 ### for 3.4; in particular, must ensure that it doesn't fight with transfers required
594 ### to fill hold requests
595 ### -- Galen Charlton 2010-10-06
596 #my $itemnumber = GetItemnumberFromBarcode( $query->param('barcode') );
597 #if ( $itemnumber ) {
598 # my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
599 # if ( ! ( $holdingBranch eq $collectionBranch ) ) {
601 # collectionItemNeedsTransferred => 1,
602 # collectionBranch => GetBranchName($collectionBranch),
607 # actually print the page!
608 output_html_with_http_headers $query, $cookie, $template->output;