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
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.
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.
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>.
26 script to execute returns of books
34 $SIG{ __DIE__
} = sub { Carp
::confess
( @_ ) };
39 use C4
::Auth qw
/:DEFAULT get_session/;
47 use C4
::Members
::Messaging
;
48 use C4
::Koha
; # FIXME : is it still useful ?
49 use C4
::RotatingCollections
;
50 use Koha
::AuthorisedValues
;
54 use Koha
::BiblioFrameworks
;
59 my ( $template, $librarian, $cookie, $flags ) = get_template_and_user
(
61 template_name
=> "circ/returns.tt",
65 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
69 my $sessionID = $query->cookie("CGISESSID");
70 my $session = get_session
($sessionID);
71 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
72 # no branch set we can't return
73 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
77 # Print a reserve slip on this page
78 if ( $query->param('print_slip') ) {
81 borrowernumber
=> scalar $query->param('borrowernumber'),
82 biblionumber
=> scalar $query->param('biblionumber'),
88 my $printers = GetPrinters
();
89 my $userenv = C4
::Context
->userenv;
90 my $userenv_branch = $userenv->{'branch'} // '';
91 my $printer = $userenv->{'branchprinter'} // '';
92 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
94 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
96 # Some code to handle the error if there is no branch or printer setting.....
99 # Set up the item stack ....
102 my %riborrowernumber;
104 foreach ( $query->param ) {
117 my $barcode = $query->param("ri-$counter");
118 my $duedate = $query->param("dd-$counter");
119 my $borrowernumber = $query->param("bn-$counter");
122 # decode barcode ## Didn't we already decode them before passing them back last time??
123 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
124 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
126 ######################
127 #Are these lines still useful ?
128 $returneditems{$counter} = $barcode;
129 $riduedate{$counter} = $duedate;
130 $riborrowernumber{$counter} = $borrowernumber;
132 #######################
133 $input{counter
} = $counter;
134 $input{barcode
} = $barcode;
135 $input{duedate
} = $duedate;
136 $input{borrowernumber
} = $borrowernumber;
137 push( @inputloop, \
%input );
141 # Deal with the requests....
143 if ($query->param('WT-itemNumber')){
144 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
147 if ( $query->param('reserve_id') ) {
148 my $item = $query->param('itemnumber');
149 my $borrowernumber = $query->param('borrowernumber');
150 my $reserve_id = $query->param('reserve_id');
151 my $diffBranchReturned = $query->param('diffBranch');
152 my $iteminfo = GetBiblioFromItemNumber
($item);
153 my $cancel_reserve = $query->param('cancel_reserve');
154 # fix up item type for display
155 $iteminfo->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$iteminfo->{'itype'} : $iteminfo->{'itemtype'};
157 if ( $cancel_reserve ) {
158 CancelReserve
({ reserve_id
=> $reserve_id, charge_cancel_fee
=> !$forgivemanualholdsexpire });
160 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ?
$diffBranchReturned : undef;
161 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
162 # i.e., whether to apply waiting status
163 ModReserveAffect
( $item, $borrowernumber, $diffBranchSend, $reserve_id );
165 # check if we have other reserves for this document, if we have a return send the message of transfer
166 my ( $messages, $nextreservinfo ) = GetOtherReserves
($item);
168 my $borr = GetMember
( borrowernumber
=> $nextreservinfo );
169 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
170 if ( $messages->{'transfert'} ) {
172 itemtitle
=> $iteminfo->{'title'},
173 itemnumber
=> $iteminfo->{'itemnumber'},
174 itembiblionumber
=> $iteminfo->{'biblionumber'},
175 iteminfo
=> $iteminfo->{'author'},
177 borrowernumber
=> $borrowernumber,
178 borcnum
=> $borr->{'cardnumber'},
179 borfirstname
=> $borr->{'firstname'},
180 borsurname
=> $borr->{'surname'},
181 borcategory
=> $borr->{'description'},
190 my $issueinformation;
192 my $barcode = $query->param('barcode');
193 my $exemptfine = $query->param('exemptfine');
196 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
198 # silently prevent unauthorized operator from forgiving overdue
199 # fines by manually tweaking form parameters
202 my $dropboxmode = $query->param('dropboxmode');
203 my $dotransfer = $query->param('dotransfer');
204 my $canceltransfer = $query->param('canceltransfer');
205 my $dest = $query->param('dest');
206 my $calendar = Koha
::Calendar
->new( branchcode
=> $userenv_branch );
207 #dropbox: get last open day (today - 1)
208 my $today = DateTime
->now( time_zone
=> C4
::Context
->tz());
209 my $dropboxdate = $calendar->addDate($today, -1);
211 my $return_date_override = $query->param('return_date_override');
212 my $return_date_override_remember =
213 $query->param('return_date_override_remember');
214 if ($return_date_override) {
215 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
216 my $return_date_override_dt = eval {dt_from_string
( $return_date_override ) };
217 if ( $return_date_override_dt ) {
218 # note that we've overriden the return date
219 $template->param( return_date_was_overriden
=> 1);
220 # Save the original format if we are remembering for this series
222 return_date_override
=> $return_date_override,
223 return_date_override_remember
=> 1
224 ) if ($return_date_override_remember);
226 $return_date_override =
227 DateTime
::Format
::MySQL
->format_datetime( $return_date_override_dt );
231 $return_date_override = q{};
236 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
237 my $transferitem = $query->param('transferitem');
238 my $tobranch = $query->param('tobranch');
239 ModItemTransfer
($transferitem, $userenv_branch, $tobranch);
242 if ($canceltransfer){
243 $itemnumber=$query->param('itemnumber');
244 DeleteTransfer
($itemnumber);
246 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
249 $template->param( transfercancelled
=> 1);
253 # actually return book and prepare item table.....
256 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
257 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
258 $itemnumber = GetItemnumberFromBarcode
($barcode);
264 # get biblio description
265 my $biblio = GetBiblioFromItemNumber
($itemnumber);
266 # fix up item type for display
267 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
269 # Check if we should display a checkin message, based on the the item
270 # type of the checked in item
271 my $itemtype = Koha
::ItemTypes
->find( $biblio->{'itemtype'} );
272 if ( $itemtype && $itemtype->checkinmsg ) {
274 checkinmsg
=> $itemtype->checkinmsg,
275 checkinmsgtype
=> $itemtype->checkinmsgtype,
279 # make sure return branch respects home branch circulation rules, default to homebranch
280 my $hbr = GetBranchItemRule
($biblio->{'homebranch'}, $itemtype ?
$itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
281 $returnbranch = $biblio->{$hbr};
283 my $materials = $biblio->{'materials'};
284 my $descriptions = Koha
::AuthorisedValues
->get_description_by_koha_field({frameworkcode
=> '', kohafield
=>'items.materials', authorised_value
=> $materials });
285 $materials = $descriptions->{lib
} // '';
288 title
=> $biblio->{'title'},
289 homebranch
=> $biblio->{'homebranch'},
290 holdingbranch
=> $biblio->{'holdingbranch'},
291 returnbranch
=> $returnbranch,
292 author
=> $biblio->{'author'},
293 itembarcode
=> $biblio->{'barcode'},
294 itemtype
=> $biblio->{'itemtype'},
295 ccode
=> $biblio->{'ccode'},
296 itembiblionumber
=> $biblio->{'biblionumber'},
297 biblionumber
=> $biblio->{'biblionumber'},
298 borrower
=> $borrower,
299 additional_materials
=> $materials,
309 ( $returned, $messages, $issueinformation, $borrower ) =
310 AddReturn
( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override, $dropboxdate );
313 my $time_now = DateTime
->now( time_zone
=> C4
::Context
->tz )->truncate( to
=> 'minute');
314 my $duedate = $issueinformation->{date_due
}->strftime('%Y-%m-%d %H:%M');
315 $returneditems{0} = $barcode;
316 $riborrowernumber{0} = $borrower->{'borrowernumber'};
317 $riduedate{0} = $duedate;
318 $input{borrowernumber
} = $borrower->{'borrowernumber'};
319 $input{duedate
} = $duedate;
320 unless ( $dropboxmode ) {
321 $input{return_overdue
} = 1 if (DateTime
->compare($issueinformation->{date_due
}, DateTime
->now()) == -1);
323 $input{return_overdue
} = 1 if (DateTime
->compare($issueinformation->{date_due
}, $dropboxdate) == -1);
325 push( @inputloop, \
%input );
327 if ( C4
::Context
->preference("FineNotifyAtCheckin") ) {
328 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
329 my $balance = $patron->account->balance;
332 $template->param( fines
=> sprintf("%.2f", $balance) );
333 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
337 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
338 #Check for waiting holds
339 my @reserves = GetReservesFromBorrowernumber
($borrower->{'borrowernumber'});
340 my $waiting_holds = 0;
341 foreach my $num_res (@reserves) {
342 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
346 if ($waiting_holds > 0) {
348 waiting_holds
=> $waiting_holds,
349 holdsborrowernumber
=> $borrower->{'borrowernumber'},
350 holdsfirstname
=> $borrower->{'firstname'},
351 holdssurname
=> $borrower->{'surname'},
355 } elsif ( C4
::Context
->preference('ShowAllCheckins') and !$messages->{'BadBarcode'} ) {
357 $returneditems{0} = $barcode;
359 push( @inputloop, \
%input );
361 $template->param( privacy
=> $borrower->{privacy
} );
363 $template->param( inputloop
=> \
@inputloop );
369 # new op dev : we check if the document must be returned to his homebranch directly,
370 # if the document is transfered, we have warning message .
372 if ( $messages->{'WasTransfered'} ) {
376 itemnumber
=> $itemnumber,
380 if ( $messages->{'NeedsTransfer'} ){
383 needstransfer
=> $messages->{'NeedsTransfer'},
384 itemnumber
=> $itemnumber,
388 if ( $messages->{'Wrongbranch'} ){
391 rightbranch
=> $messages->{'Wrongbranch'}->{'Rightbranch'},
395 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
397 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
400 TransferWaitingAt
=> $messages->{'WrongTransfer'},
401 WrongTransferItem
=> $messages->{'WrongTransferItem'},
402 itemnumber
=> $itemnumber,
405 my $reserve = $messages->{'ResFound'};
406 my $borr = C4
::Members
::GetMember
( borrowernumber
=> $reserve->{'borrowernumber'} );
407 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
410 wborfirstname
=> $borr->{'firstname'},
411 wborsurname
=> $borr->{'surname'},
412 wborcategory
=> $borr->{'description'},
413 wbortitle
=> $borr->{'title'},
414 wborphone
=> $borr->{'phone'},
415 wboremail
=> $borr->{'email'},
416 wborstnum
=> $borr->{streetnumber
},
417 wboraddress
=> $borr->{'address'},
418 wboraddress2
=> $borr->{'address2'},
419 wborcity
=> $borr->{'city'},
420 wborzip
=> $borr->{'zipcode'},
421 wborrowernumber
=> $reserve->{'borrowernumber'},
422 wborcnum
=> $borr->{'cardnumber'},
423 wtransfertFrom
=> $userenv_branch,
428 # reserve found and item arrived at the expected branch
430 if ( $messages->{'ResFound'}) {
431 my $reserve = $messages->{'ResFound'};
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" ) {
437 waiting
=> ($userenv_branch eq $reserve->{'branchcode'} ?
1 : 0 ),
439 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
441 intransit
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
442 transfertodo
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
443 reserve_id
=> $reserve->{reserve_id
},
448 # same params for Waiting or Reserved
451 name
=> $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
452 borfirstname
=> $borr->{'firstname'},
453 borsurname
=> $borr->{'surname'},
454 borcategory
=> $borr->{'description'},
455 bortitle
=> $borr->{'title'},
456 borphone
=> $borr->{'phone'},
457 boremail
=> $borr->{'email'},
458 boraddress
=> $borr->{'address'},
459 boraddress2
=> $borr->{'address2'},
460 borstnum
=> $borr->{streetnumber
},
461 borcity
=> $borr->{'city'},
462 borzip
=> $borr->{'zipcode'},
463 borcnum
=> $borr->{'cardnumber'},
464 debarred
=> $borr->{'debarred'},
465 gonenoaddress
=> $borr->{'gonenoaddress'},
467 destbranch
=> $reserve->{'branchcode'},
468 borrowernumber
=> $reserve->{'borrowernumber'},
469 itemnumber
=> $reserve->{'itemnumber'},
470 reservenotes
=> $reserve->{'reservenotes'},
471 reserve_id
=> $reserve->{reserve_id
},
472 bormessagepref
=> $holdmsgpreferences->{'transports'},
474 } # else { ; } # error?
479 foreach my $code ( keys %$messages ) {
481 my $exit_required_p = 0;
482 if ( $code eq 'BadBarcode' ) {
483 $err{badbarcode
} = 1;
484 $err{msg
} = $messages->{'BadBarcode'};
486 elsif ( $code eq 'NotIssued' ) {
489 $err{msg
} = $messages->{'IsPermanent'} if $messages->{'IsPermanent'};
491 elsif ( $code eq 'LocalUse' ) {
494 elsif ( $code eq 'WasLost' ) {
497 elsif ( $code eq 'LostItemFeeRefunded' ) {
498 $template->param( LostItemFeeRefunded
=> 1 );
500 elsif ( $code eq 'ResFound' ) {
501 ; # FIXME... anything to do here?
503 elsif ( $code eq 'WasReturned' ) {
504 ; # FIXME... anything to do here?
506 elsif ( $code eq 'WasTransfered' ) {
507 ; # FIXME... anything to do here?
509 elsif ( $code eq 'withdrawn' ) {
511 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
513 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
514 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
515 $err{ispermanent
} = 1;
516 $err{msg
} = $messages->{'IsPermanent'};
519 elsif ( $code eq 'WrongTransfer' ) {
520 ; # FIXME... anything to do here?
522 elsif ( $code eq 'WrongTransferItem' ) {
523 ; # FIXME... anything to do here?
525 elsif ( $code eq 'NeedsTransfer' ) {
527 elsif ( $code eq 'Wrongbranch' ) {
529 elsif ( $code eq 'Debarred' ) {
530 $err{debarred
} = $messages->{'Debarred'};
531 $err{debarcardnumber
} = $borrower->{cardnumber
};
532 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
533 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
535 elsif ( $code eq 'PrevDebarred' ) {
536 $err{prevdebarred
} = $messages->{'PrevDebarred'};
538 elsif ( $code eq 'ForeverDebarred' ) {
539 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
541 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
542 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
545 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
546 # This forces the issue of staying in sync w/ Circulation.pm
549 push( @errmsgloop, \
%err );
551 last if $exit_required_p;
553 $template->param( errmsgloop
=> \
@errmsgloop );
555 #set up so only the last 8 returned items display (make for faster loading pages)
556 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
560 { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.location' } ) };
561 foreach ( sort { $a <=> $b } keys %returneditems ) {
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);
577 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, $dropboxdate) == -1);
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 $ri{borissuescount
} = Koha
::Checkouts
->count( { borrowernumber
=> $b->{'borrowernumber'} } );
589 $ri{borrowernumber
} = $riborrowernumber{$_};
593 my $biblio = GetBiblioFromItemNumber
(GetItemnumberFromBarcode
($bar_code));
594 my $item = GetItem
( GetItemnumberFromBarcode
($bar_code) );
595 # fix up item type for display
596 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
597 $ri{itembiblionumber
} = $biblio->{'biblionumber'};
598 $ri{itemtitle
} = $biblio->{'title'};
599 $ri{itemauthor
} = $biblio->{'author'};
600 $ri{itemcallnumber
} = $biblio->{'itemcallnumber'};
601 $ri{dateaccessioned
} = $item->{dateaccessioned
};
602 $ri{itemtype
} = $biblio->{'itemtype'};
603 $ri{itemnote
} = $biblio->{'itemnotes'};
604 $ri{itemnotes_nonpublic
} = $item->{'itemnotes_nonpublic'};
605 $ri{ccode
} = $biblio->{'ccode'};
606 $ri{enumchron
} = $biblio->{'enumchron'};
607 $ri{itemnumber
} = $biblio->{'itemnumber'};
608 $ri{barcode
} = $bar_code;
609 $ri{homebranch
} = $item->{'homebranch'};
610 $ri{holdingbranch
} = $item->{'holdingbranch'};
612 $ri{location
} = $biblio->{'location'};
613 my $shelfcode = $ri{'location'};
614 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
626 errmsgloop
=> \
@errmsgloop,
627 exemptfine
=> $exemptfine,
628 dropboxmode
=> $dropboxmode,
629 dropboxdate
=> output_pref
($dropboxdate),
630 forgivemanualholdsexpire
=> $forgivemanualholdsexpire,
631 overduecharges
=> $overduecharges,
632 AudioAlerts
=> C4
::Context
->preference("AudioAlerts"),
633 BlockReturnOfWithdrawnItems
=> C4
::Context
->preference("BlockReturnOfWithdrawnItems"),
636 $itemnumber = GetItemnumberFromBarcode
( $barcode );
638 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
639 if ( $holdingBranch and $collectionBranch ) {
640 $holdingBranch //= '';
641 $collectionBranch //= $returnbranch;
642 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
644 collectionItemNeedsTransferred
=> 1,
645 collectionBranch
=> $collectionBranch,
646 itemnumber
=> $itemnumber,
652 # Checking if there is a Fast Cataloging Framework
653 $template->param( fast_cataloging
=> 1 ) if Koha
::BiblioFrameworks
->find( 'FA' );
655 # actually print the page!
656 output_html_with_http_headers
$query, $cookie, $template->output;