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 additional_materials => $biblio->{'materials'}
287 my $time_now = DateTime->now( time_zone => C4::Context->tz )->truncate( to => 'minute');
288 my $duedate = $issueinformation->{date_due}->strftime('%Y-%m-%d %H:%M');
289 $returneditems{0} = $barcode;
290 $riborrowernumber{0} = $borrower->{'borrowernumber'};
291 $riduedate{0} = $duedate;
292 $input{borrowernumber} = $borrower->{'borrowernumber'};
293 $input{duedate} = $duedate;
294 $input{return_overdue} = 1 if (DateTime->compare($issueinformation->{date_due}, $time_now) == -1);
295 push( @inputloop, \%input );
297 if ( C4::Context->preference("FineNotifyAtCheckin") ) {
298 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrower->{'borrowernumber'} );
299 if ($fines && $fines > 0) {
300 $template->param( fines => sprintf("%.2f",$fines) );
301 $template->param( fineborrowernumber => $borrower->{'borrowernumber'} );
305 if (C4::Context->preference("WaitingNotifyAtCheckin") ) {
306 #Check for waiting holds
307 my @reserves = GetReservesFromBorrowernumber($borrower->{'borrowernumber'});
308 my $waiting_holds = 0;
309 foreach my $num_res (@reserves) {
310 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
314 if ($waiting_holds > 0) {
316 waiting_holds => $waiting_holds,
317 holdsborrowernumber => $borrower->{'borrowernumber'},
318 holdsfirstname => $borrower->{'firstname'},
319 holdssurname => $borrower->{'surname'},
324 elsif ( !$messages->{'BadBarcode'} ) {
326 $returneditems{0} = $barcode;
328 push( @inputloop, \%input );
331 $template->param( inputloop => \@inputloop );
337 # new op dev : we check if the document must be returned to his homebranch directly,
338 # if the document is transfered, we have warning message .
340 if ( $messages->{'WasTransfered'} ) {
344 itemnumber => $itemnumber,
348 if ( $messages->{'NeedsTransfer'} ){
352 itemnumber => $itemnumber,
356 if ( $messages->{'Wrongbranch'} ){
362 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
364 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
367 TransferWaitingAt => $messages->{'WrongTransfer'},
368 WrongTransferItem => $messages->{'WrongTransferItem'},
369 itemnumber => $itemnumber,
372 my $reserve = $messages->{'ResFound'};
373 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
374 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
375 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
378 wborfirstname => $borr->{'firstname'},
379 wborsurname => $borr->{'surname'},
380 wbortitle => $borr->{'title'},
381 wborphone => $borr->{'phone'},
382 wboremail => $borr->{'email'},
383 wboraddress => $borr->{'address'},
384 wboraddress2 => $borr->{'address2'},
385 wborcity => $borr->{'city'},
386 wborzip => $borr->{'zipcode'},
387 wborrowernumber => $reserve->{'borrowernumber'},
388 wborcnum => $borr->{'cardnumber'},
389 wtransfertFrom => $userenv_branch,
394 # reserve found and item arrived at the expected branch
396 if ( $messages->{'ResFound'}) {
397 my $reserve = $messages->{'ResFound'};
398 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
399 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
401 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
402 if ( $reserve->{'ResFound'} eq "Waiting" ) {
404 waiting => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
406 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
408 intransit => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
409 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
410 resbarcode => $barcode,
415 # same params for Waiting or Reserved
418 currentbranch => $branches->{$userenv_branch}->{'branchname'},
419 destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
420 name => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
421 borfirstname => $borr->{'firstname'},
422 borsurname => $borr->{'surname'},
423 bortitle => $borr->{'title'},
424 borphone => $borr->{'phone'},
425 boremail => $borr->{'email'},
426 boraddress => $borr->{'address'},
427 boraddress2 => $borr->{'address2'},
428 borcity => $borr->{'city'},
429 borzip => $borr->{'zipcode'},
430 borcnum => $borr->{'cardnumber'},
431 debarred => $borr->{'debarred'},
432 gonenoaddress => $borr->{'gonenoaddress'},
434 destbranch => $reserve->{'branchcode'},
435 borrowernumber => $reserve->{'borrowernumber'},
436 itemnumber => $reserve->{'itemnumber'},
437 reservenotes => $reserve->{'reservenotes'},
439 } # else { ; } # error?
444 foreach my $code ( keys %$messages ) {
446 my $exit_required_p = 0;
447 if ( $code eq 'BadBarcode' ) {
448 $err{badbarcode} = 1;
449 $err{msg} = $messages->{'BadBarcode'};
451 elsif ( $code eq 'NotIssued' ) {
453 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
455 elsif ( $code eq 'LocalUse' ) {
458 elsif ( $code eq 'WasLost' ) {
461 elsif ( $code eq 'LostItemFeeRefunded' ) {
462 $template->param( LostItemFeeRefunded => 1 );
464 elsif ( $code eq 'ResFound' ) {
465 ; # FIXME... anything to do here?
467 elsif ( $code eq 'WasReturned' ) {
468 ; # FIXME... anything to do here?
470 elsif ( $code eq 'WasTransfered' ) {
471 ; # FIXME... anything to do here?
473 elsif ( $code eq 'withdrawn' ) {
475 $exit_required_p = 1 if C4::Context->preference("BlockReturnOfWithdrawnItems");
477 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
478 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
479 $err{ispermanent} = 1;
481 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
484 elsif ( $code eq 'WrongTransfer' ) {
485 ; # FIXME... anything to do here?
487 elsif ( $code eq 'WrongTransferItem' ) {
488 ; # FIXME... anything to do here?
490 elsif ( $code eq 'NeedsTransfer' ) {
492 elsif ( $code eq 'Wrongbranch' ) {
494 elsif ( $code eq 'Debarred' ) {
495 $err{debarred} = $messages->{'Debarred'};
496 $err{debarcardnumber} = $borrower->{cardnumber};
497 $err{debarborrowernumber} = $borrower->{borrowernumber};
498 $err{debarname} = "$borrower->{firstname} $borrower->{surname}";
501 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
502 # This forces the issue of staying in sync w/ Circulation.pm
505 push( @errmsgloop, \%err );
507 last if $exit_required_p;
509 $template->param( errmsgloop => \@errmsgloop );
511 #set up so only the last 8 returned items display (make for faster loading pages)
512 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
515 my $shelflocations = GetKohaAuthorisedValues('items.location','');
516 foreach ( sort { $a <=> $b } keys %returneditems ) {
518 if ( $count++ < $returned_counter ) {
519 my $bar_code = $returneditems{$_};
520 if ($riduedate{$_}) {
521 my $duedate = dt_from_string( $riduedate{$_}, 'sql');
522 $ri{year} = $duedate->year();
523 $ri{month} = $duedate->month();
524 $ri{day} = $duedate->day();
525 $ri{hour} = $duedate->hour();
526 $ri{minute} = $duedate->minute();
527 $ri{duedate} = output_pref($duedate);
528 my ($b) = GetMemberDetails( $riborrowernumber{$_}, 0 );
529 $ri{return_overdue} = 1 if (DateTime->compare($duedate, DateTime->now()) == -1 );
530 $ri{borrowernumber} = $b->{'borrowernumber'};
531 $ri{borcnum} = $b->{'cardnumber'};
532 $ri{borfirstname} = $b->{'firstname'};
533 $ri{borsurname} = $b->{'surname'};
534 $ri{bortitle} = $b->{'title'};
535 $ri{bornote} = $b->{'borrowernotes'};
536 $ri{borcategorycode}= $b->{'categorycode'};
539 $ri{borrowernumber} = $riborrowernumber{$_};
543 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($bar_code));
544 my $item = GetItem( GetItemnumberFromBarcode($bar_code) );
545 # fix up item type for display
546 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
547 $ri{itembiblionumber} = $biblio->{'biblionumber'};
548 $ri{itemtitle} = $biblio->{'title'};
549 $ri{itemauthor} = $biblio->{'author'};
550 $ri{itemcallnumber} = $biblio->{'itemcallnumber'};
551 $ri{itemtype} = $biblio->{'itemtype'};
552 $ri{itemnote} = $biblio->{'itemnotes'};
553 $ri{ccode} = $biblio->{'ccode'};
554 $ri{itemnumber} = $biblio->{'itemnumber'};
555 $ri{barcode} = $bar_code;
556 $ri{homebranch} = $item->{'homebranch'};
557 $ri{holdingbranch} = $item->{'holdingbranch'};
559 $ri{location} = $biblio->{'location'};
560 my $shelfcode = $ri{'location'};
561 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
569 my ($genbrname, $genprname);
570 if (my $b = $branches->{$userenv_branch}) {
571 $genbrname = $b->{'branchname'};
573 if (my $p = $printers->{$printer}) {
574 $genprname = $p->{'printername'};
578 genbrname => $genbrname,
579 genprname => $genprname,
580 branchname => $genbrname,
582 errmsgloop => \@errmsgloop,
583 exemptfine => $exemptfine,
584 dropboxmode => $dropboxmode,
585 dropboxdate => output_pref($dropboxdate),
586 overduecharges => $overduecharges,
587 soundon => C4::Context->preference("SoundOn"),
588 BlockReturnOfWithdrawnItems => C4::Context->preference("BlockReturnOfWithdrawnItems"),
591 ### Comment out rotating collections for now to allow it a little more time to bake
592 ### for 3.4; in particular, must ensure that it doesn't fight with transfers required
593 ### to fill hold requests
594 ### -- Galen Charlton 2010-10-06
595 #my $itemnumber = GetItemnumberFromBarcode( $query->param('barcode') );
596 #if ( $itemnumber ) {
597 # my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
598 # if ( ! ( $holdingBranch eq $collectionBranch ) ) {
600 # collectionItemNeedsTransferred => 1,
601 # collectionBranch => GetBranchName($collectionBranch),
606 # actually print the page!
607 output_html_with_http_headers $query, $cookie, $template->output;