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
;
56 my ( $template, $librarian, $cookie ) = get_template_and_user
(
58 template_name
=> "circ/returns.tt",
62 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
66 my $sessionID = $query->cookie("CGISESSID");
67 my $session = get_session
($sessionID);
68 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
69 # no branch set we can't return
70 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
74 # Print a reserve slip on this page
75 if ( $query->param('print_slip') ) {
78 borrowernumber
=> scalar $query->param('borrowernumber'),
79 biblionumber
=> scalar $query->param('biblionumber'),
85 my $printers = GetPrinters
();
86 my $userenv = C4
::Context
->userenv;
87 my $userenv_branch = $userenv->{'branch'} // '';
88 my $printer = $userenv->{'branchprinter'} // '';
89 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
91 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
93 # Some code to handle the error if there is no branch or printer setting.....
96 # Set up the item stack ....
101 foreach ( $query->param ) {
114 my $barcode = $query->param("ri-$counter");
115 my $duedate = $query->param("dd-$counter");
116 my $borrowernumber = $query->param("bn-$counter");
119 # decode barcode ## Didn't we already decode them before passing them back last time??
120 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
121 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
123 ######################
124 #Are these lines still useful ?
125 $returneditems{$counter} = $barcode;
126 $riduedate{$counter} = $duedate;
127 $riborrowernumber{$counter} = $borrowernumber;
129 #######################
130 $input{counter
} = $counter;
131 $input{barcode
} = $barcode;
132 $input{duedate
} = $duedate;
133 $input{borrowernumber
} = $borrowernumber;
134 push( @inputloop, \
%input );
138 # Deal with the requests....
140 if ($query->param('WT-itemNumber')){
141 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
144 if ( $query->param('reserve_id') ) {
145 my $item = $query->param('itemnumber');
146 my $borrowernumber = $query->param('borrowernumber');
147 my $reserve_id = $query->param('reserve_id');
148 my $diffBranchReturned = $query->param('diffBranch');
149 my $iteminfo = GetBiblioFromItemNumber
($item);
150 my $cancel_reserve = $query->param('cancel_reserve');
151 # fix up item type for display
152 $iteminfo->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$iteminfo->{'itype'} : $iteminfo->{'itemtype'};
154 if ( $cancel_reserve ) {
155 CancelReserve
({ reserve_id
=> $reserve_id, charge_cancel_fee
=> !$forgivemanualholdsexpire });
157 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ?
$diffBranchReturned : undef;
158 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
159 # i.e., whether to apply waiting status
160 ModReserveAffect
( $item, $borrowernumber, $diffBranchSend, $reserve_id );
162 # check if we have other reserves for this document, if we have a return send the message of transfer
163 my ( $messages, $nextreservinfo ) = GetOtherReserves
($item);
165 my $borr = GetMember
( borrowernumber
=> $nextreservinfo );
166 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
167 if ( $messages->{'transfert'} ) {
169 itemtitle
=> $iteminfo->{'title'},
170 itemnumber
=> $iteminfo->{'itemnumber'},
171 itembiblionumber
=> $iteminfo->{'biblionumber'},
172 iteminfo
=> $iteminfo->{'author'},
174 borrowernumber
=> $borrowernumber,
175 borcnum
=> $borr->{'cardnumber'},
176 borfirstname
=> $borr->{'firstname'},
177 borsurname
=> $borr->{'surname'},
178 borcategory
=> $borr->{'description'},
187 my $issueinformation;
189 my $barcode = $query->param('barcode');
190 my $exemptfine = $query->param('exemptfine');
193 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
195 # silently prevent unauthorized operator from forgiving overdue
196 # fines by manually tweaking form parameters
199 my $dropboxmode = $query->param('dropboxmode');
200 my $dotransfer = $query->param('dotransfer');
201 my $canceltransfer = $query->param('canceltransfer');
202 my $dest = $query->param('dest');
203 my $calendar = Koha
::Calendar
->new( branchcode
=> $userenv_branch );
204 #dropbox: get last open day (today - 1)
205 my $today = DateTime
->now( time_zone
=> C4
::Context
->tz());
206 my $dropboxdate = $calendar->addDate($today, -1);
208 my $return_date_override = $query->param('return_date_override');
209 my $return_date_override_remember =
210 $query->param('return_date_override_remember');
211 if ($return_date_override) {
212 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
213 my $return_date_override_dt = eval {dt_from_string
( $return_date_override ) };
214 if ( $return_date_override_dt ) {
215 # note that we've overriden the return date
216 $template->param( return_date_was_overriden
=> 1);
217 # Save the original format if we are remembering for this series
219 return_date_override
=> $return_date_override,
220 return_date_override_remember
=> 1
221 ) if ($return_date_override_remember);
223 $return_date_override =
224 DateTime
::Format
::MySQL
->format_datetime( $return_date_override_dt );
228 $return_date_override = q{};
233 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
234 my $transferitem = $query->param('transferitem');
235 my $tobranch = $query->param('tobranch');
236 ModItemTransfer
($transferitem, $userenv_branch, $tobranch);
239 if ($canceltransfer){
240 $itemnumber=$query->param('itemnumber');
241 DeleteTransfer
($itemnumber);
243 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
246 $template->param( transfercancelled
=> 1);
250 # actually return book and prepare item table.....
253 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
254 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
255 $itemnumber = GetItemnumberFromBarcode
($barcode);
261 # get biblio description
262 my $biblio = GetBiblioFromItemNumber
($itemnumber);
263 # fix up item type for display
264 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
266 # Check if we should display a checkin message, based on the the item
267 # type of the checked in item
268 my $itemtype = Koha
::ItemTypes
->find( $biblio->{'itemtype'} );
269 if ( $itemtype && $itemtype->checkinmsg ) {
271 checkinmsg
=> $itemtype->checkinmsg,
272 checkinmsgtype
=> $itemtype->checkinmsgtype,
276 # make sure return branch respects home branch circulation rules, default to homebranch
277 my $hbr = GetBranchItemRule
($biblio->{'homebranch'}, $itemtype ?
$itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
278 $returnbranch = $biblio->{$hbr};
280 my $materials = $biblio->{'materials'};
281 my $avcode = GetAuthValCode
('items.materials');
283 $materials = GetKohaAuthorisedValueLib
($avcode, $materials);
287 title
=> $biblio->{'title'},
288 homebranch
=> $biblio->{'homebranch'},
289 holdingbranch
=> $biblio->{'holdingbranch'},
290 returnbranch
=> $returnbranch,
291 author
=> $biblio->{'author'},
292 itembarcode
=> $biblio->{'barcode'},
293 itemtype
=> $biblio->{'itemtype'},
294 ccode
=> $biblio->{'ccode'},
295 itembiblionumber
=> $biblio->{'biblionumber'},
296 biblionumber
=> $biblio->{'biblionumber'},
297 borrower
=> $borrower,
298 additional_materials
=> $materials,
308 ( $returned, $messages, $issueinformation, $borrower ) =
309 AddReturn
( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override, $dropboxdate );
312 my $time_now = DateTime
->now( time_zone
=> C4
::Context
->tz )->truncate( to
=> 'minute');
313 my $duedate = $issueinformation->{date_due
}->strftime('%Y-%m-%d %H:%M');
314 $returneditems{0} = $barcode;
315 $riborrowernumber{0} = $borrower->{'borrowernumber'};
316 $riduedate{0} = $duedate;
317 $input{borrowernumber
} = $borrower->{'borrowernumber'};
318 $input{duedate
} = $duedate;
319 unless ( $dropboxmode ) {
320 $input{return_overdue
} = 1 if (DateTime
->compare($issueinformation->{date_due
}, DateTime
->now()) == -1);
322 $input{return_overdue
} = 1 if (DateTime
->compare($issueinformation->{date_due
}, $dropboxdate) == -1);
324 push( @inputloop, \
%input );
326 if ( C4
::Context
->preference("FineNotifyAtCheckin") ) {
327 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines
( $borrower->{'borrowernumber'} );
328 if ($fines && $fines > 0) {
329 $template->param( fines
=> sprintf("%.2f",$fines) );
330 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
334 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
335 #Check for waiting holds
336 my @reserves = GetReservesFromBorrowernumber
($borrower->{'borrowernumber'});
337 my $waiting_holds = 0;
338 foreach my $num_res (@reserves) {
339 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
343 if ($waiting_holds > 0) {
345 waiting_holds
=> $waiting_holds,
346 holdsborrowernumber
=> $borrower->{'borrowernumber'},
347 holdsfirstname
=> $borrower->{'firstname'},
348 holdssurname
=> $borrower->{'surname'},
352 } elsif ( C4
::Context
->preference('ShowAllCheckins') and !$messages->{'BadBarcode'} ) {
354 $returneditems{0} = $barcode;
356 push( @inputloop, \
%input );
358 $template->param( privacy
=> $borrower->{privacy
} );
360 $template->param( inputloop
=> \
@inputloop );
366 # new op dev : we check if the document must be returned to his homebranch directly,
367 # if the document is transfered, we have warning message .
369 if ( $messages->{'WasTransfered'} ) {
373 itemnumber
=> $itemnumber,
377 if ( $messages->{'NeedsTransfer'} ){
380 needstransfer
=> $messages->{'NeedsTransfer'},
381 itemnumber
=> $itemnumber,
385 if ( $messages->{'Wrongbranch'} ){
388 rightbranch
=> $messages->{'Wrongbranch'}->{'Rightbranch'},
392 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
394 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
397 TransferWaitingAt
=> $messages->{'WrongTransfer'},
398 WrongTransferItem
=> $messages->{'WrongTransferItem'},
399 itemnumber
=> $itemnumber,
402 my $reserve = $messages->{'ResFound'};
403 my $borr = C4
::Members
::GetMember
( borrowernumber
=> $reserve->{'borrowernumber'} );
404 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
407 wborfirstname
=> $borr->{'firstname'},
408 wborsurname
=> $borr->{'surname'},
409 wborcategory
=> $borr->{'description'},
410 wbortitle
=> $borr->{'title'},
411 wborphone
=> $borr->{'phone'},
412 wboremail
=> $borr->{'email'},
413 wborstnum
=> $borr->{streetnumber
},
414 wboraddress
=> $borr->{'address'},
415 wboraddress2
=> $borr->{'address2'},
416 wborcity
=> $borr->{'city'},
417 wborzip
=> $borr->{'zipcode'},
418 wborrowernumber
=> $reserve->{'borrowernumber'},
419 wborcnum
=> $borr->{'cardnumber'},
420 wtransfertFrom
=> $userenv_branch,
425 # reserve found and item arrived at the expected branch
427 if ( $messages->{'ResFound'}) {
428 my $reserve = $messages->{'ResFound'};
429 my $borr = C4
::Members
::GetMember
( borrowernumber
=> $reserve->{'borrowernumber'} );
430 my $holdmsgpreferences = C4
::Members
::Messaging
::GetMessagingPreferences
( { borrowernumber
=> $reserve->{'borrowernumber'}, message_name
=> 'Hold_Filled' } );
431 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
432 if ( $reserve->{'ResFound'} eq "Waiting" ) {
434 waiting
=> ($userenv_branch eq $reserve->{'branchcode'} ?
1 : 0 ),
436 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
438 intransit
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
439 transfertodo
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
440 reserve_id
=> $reserve->{reserve_id
},
445 # same params for Waiting or Reserved
448 name
=> $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
449 borfirstname
=> $borr->{'firstname'},
450 borsurname
=> $borr->{'surname'},
451 borcategory
=> $borr->{'description'},
452 bortitle
=> $borr->{'title'},
453 borphone
=> $borr->{'phone'},
454 boremail
=> $borr->{'email'},
455 boraddress
=> $borr->{'address'},
456 boraddress2
=> $borr->{'address2'},
457 borstnum
=> $borr->{streetnumber
},
458 borcity
=> $borr->{'city'},
459 borzip
=> $borr->{'zipcode'},
460 borcnum
=> $borr->{'cardnumber'},
461 debarred
=> $borr->{'debarred'},
462 gonenoaddress
=> $borr->{'gonenoaddress'},
464 destbranch
=> $reserve->{'branchcode'},
465 borrowernumber
=> $reserve->{'borrowernumber'},
466 itemnumber
=> $reserve->{'itemnumber'},
467 reservenotes
=> $reserve->{'reservenotes'},
468 bormessagepref
=> $holdmsgpreferences->{'transports'},
470 } # else { ; } # error?
475 foreach my $code ( keys %$messages ) {
477 my $exit_required_p = 0;
478 if ( $code eq 'BadBarcode' ) {
479 $err{badbarcode
} = 1;
480 $err{msg
} = $messages->{'BadBarcode'};
482 elsif ( $code eq 'NotIssued' ) {
485 $err{msg
} = $messages->{'IsPermanent'} if $messages->{'IsPermanent'};
487 elsif ( $code eq 'LocalUse' ) {
490 elsif ( $code eq 'WasLost' ) {
493 elsif ( $code eq 'LostItemFeeRefunded' ) {
494 $template->param( LostItemFeeRefunded
=> 1 );
496 elsif ( $code eq 'ResFound' ) {
497 ; # FIXME... anything to do here?
499 elsif ( $code eq 'WasReturned' ) {
500 ; # FIXME... anything to do here?
502 elsif ( $code eq 'WasTransfered' ) {
503 ; # FIXME... anything to do here?
505 elsif ( $code eq 'withdrawn' ) {
507 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
509 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
510 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
511 $err{ispermanent
} = 1;
512 $err{msg
} = $messages->{'IsPermanent'};
515 elsif ( $code eq 'WrongTransfer' ) {
516 ; # FIXME... anything to do here?
518 elsif ( $code eq 'WrongTransferItem' ) {
519 ; # FIXME... anything to do here?
521 elsif ( $code eq 'NeedsTransfer' ) {
523 elsif ( $code eq 'Wrongbranch' ) {
525 elsif ( $code eq 'Debarred' ) {
526 $err{debarred
} = $messages->{'Debarred'};
527 $err{debarcardnumber
} = $borrower->{cardnumber
};
528 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
529 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
531 elsif ( $code eq 'PrevDebarred' ) {
532 $err{prevdebarred
} = $messages->{'PrevDebarred'};
534 elsif ( $code eq 'ForeverDebarred' ) {
535 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
537 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
538 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
541 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
542 # This forces the issue of staying in sync w/ Circulation.pm
545 push( @errmsgloop, \
%err );
547 last if $exit_required_p;
549 $template->param( errmsgloop
=> \
@errmsgloop );
551 #set up so only the last 8 returned items display (make for faster loading pages)
552 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
555 my $shelflocations = GetKohaAuthorisedValues
('items.location','');
556 foreach ( sort { $a <=> $b } keys %returneditems ) {
558 if ( $count++ < $returned_counter ) {
559 my $bar_code = $returneditems{$_};
560 if ($riduedate{$_}) {
561 my $duedate = dt_from_string
( $riduedate{$_}, 'sql');
562 $ri{year
} = $duedate->year();
563 $ri{month
} = $duedate->month();
564 $ri{day
} = $duedate->day();
565 $ri{hour
} = $duedate->hour();
566 $ri{minute
} = $duedate->minute();
567 $ri{duedate
} = output_pref
($duedate);
568 my $b = C4
::Members
::GetMember
( borrowernumber
=> $riborrowernumber{$_} );
569 unless ( $dropboxmode ) {
570 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, DateTime
->now()) == -1);
572 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, $dropboxdate) == -1);
574 $ri{borrowernumber
} = $b->{'borrowernumber'};
575 $ri{borcnum
} = $b->{'cardnumber'};
576 $ri{borfirstname
} = $b->{'firstname'};
577 $ri{borsurname
} = $b->{'surname'};
578 $ri{bortitle
} = $b->{'title'};
579 $ri{bornote
} = $b->{'borrowernotes'};
580 $ri{borcategorycode
}= $b->{'categorycode'};
583 $ri{borrowernumber
} = $riborrowernumber{$_};
587 my $biblio = GetBiblioFromItemNumber
(GetItemnumberFromBarcode
($bar_code));
588 my $item = GetItem
( GetItemnumberFromBarcode
($bar_code) );
589 # fix up item type for display
590 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
591 $ri{itembiblionumber
} = $biblio->{'biblionumber'};
592 $ri{itemtitle
} = $biblio->{'title'};
593 $ri{itemauthor
} = $biblio->{'author'};
594 $ri{itemcallnumber
} = $biblio->{'itemcallnumber'};
595 $ri{dateaccessioned
} = $item->{dateaccessioned
};
596 $ri{itemtype
} = $biblio->{'itemtype'};
597 $ri{itemnote
} = $biblio->{'itemnotes'};
598 $ri{itemnotes_nonpublic
} = $item->{'itemnotes_nonpublic'};
599 $ri{ccode
} = $biblio->{'ccode'};
600 $ri{enumchron
} = $biblio->{'enumchron'};
601 $ri{itemnumber
} = $biblio->{'itemnumber'};
602 $ri{barcode
} = $bar_code;
603 $ri{homebranch
} = $item->{'homebranch'};
604 $ri{holdingbranch
} = $item->{'holdingbranch'};
606 $ri{location
} = $biblio->{'location'};
607 my $shelfcode = $ri{'location'};
608 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
620 errmsgloop
=> \
@errmsgloop,
621 exemptfine
=> $exemptfine,
622 dropboxmode
=> $dropboxmode,
623 dropboxdate
=> output_pref
($dropboxdate),
624 forgivemanualholdsexpire
=> $forgivemanualholdsexpire,
625 overduecharges
=> $overduecharges,
626 AudioAlerts
=> C4
::Context
->preference("AudioAlerts"),
627 BlockReturnOfWithdrawnItems
=> C4
::Context
->preference("BlockReturnOfWithdrawnItems"),
630 $itemnumber = GetItemnumberFromBarcode
( $barcode );
632 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
633 if ( $holdingBranch and $collectionBranch ) {
634 $holdingBranch //= '';
635 $collectionBranch //= $returnbranch;
636 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
638 collectionItemNeedsTransferred
=> 1,
639 collectionBranch
=> $collectionBranch,
640 itemnumber
=> $itemnumber,
646 # actually print the page!
647 output_html_with_http_headers
$query, $cookie, $template->output;