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
;
57 my ( $template, $librarian, $cookie ) = get_template_and_user
(
59 template_name
=> "circ/returns.tt",
63 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
67 my $sessionID = $query->cookie("CGISESSID");
68 my $session = get_session
($sessionID);
69 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
70 # no branch set we can't return
71 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
75 # Print a reserve slip on this page
76 if ( $query->param('print_slip') ) {
79 borrowernumber
=> scalar $query->param('borrowernumber'),
80 biblionumber
=> scalar $query->param('biblionumber'),
86 my $printers = GetPrinters
();
87 my $userenv = C4
::Context
->userenv;
88 my $userenv_branch = $userenv->{'branch'} // '';
89 my $printer = $userenv->{'branchprinter'} // '';
90 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
92 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
94 # Some code to handle the error if there is no branch or printer setting.....
97 # Set up the item stack ....
100 my %riborrowernumber;
102 foreach ( $query->param ) {
115 my $barcode = $query->param("ri-$counter");
116 my $duedate = $query->param("dd-$counter");
117 my $borrowernumber = $query->param("bn-$counter");
120 # decode barcode ## Didn't we already decode them before passing them back last time??
121 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
122 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
124 ######################
125 #Are these lines still useful ?
126 $returneditems{$counter} = $barcode;
127 $riduedate{$counter} = $duedate;
128 $riborrowernumber{$counter} = $borrowernumber;
130 #######################
131 $input{counter
} = $counter;
132 $input{barcode
} = $barcode;
133 $input{duedate
} = $duedate;
134 $input{borrowernumber
} = $borrowernumber;
135 push( @inputloop, \
%input );
139 # Deal with the requests....
141 if ($query->param('WT-itemNumber')){
142 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
145 if ( $query->param('reserve_id') ) {
146 my $item = $query->param('itemnumber');
147 my $borrowernumber = $query->param('borrowernumber');
148 my $reserve_id = $query->param('reserve_id');
149 my $diffBranchReturned = $query->param('diffBranch');
150 my $iteminfo = GetBiblioFromItemNumber
($item);
151 my $cancel_reserve = $query->param('cancel_reserve');
152 # fix up item type for display
153 $iteminfo->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$iteminfo->{'itype'} : $iteminfo->{'itemtype'};
155 if ( $cancel_reserve ) {
156 CancelReserve
({ reserve_id
=> $reserve_id, charge_cancel_fee
=> !$forgivemanualholdsexpire });
158 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ?
$diffBranchReturned : undef;
159 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
160 # i.e., whether to apply waiting status
161 ModReserveAffect
( $item, $borrowernumber, $diffBranchSend, $reserve_id );
163 # check if we have other reserves for this document, if we have a return send the message of transfer
164 my ( $messages, $nextreservinfo ) = GetOtherReserves
($item);
166 my $borr = GetMember
( borrowernumber
=> $nextreservinfo );
167 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
168 if ( $messages->{'transfert'} ) {
170 itemtitle
=> $iteminfo->{'title'},
171 itemnumber
=> $iteminfo->{'itemnumber'},
172 itembiblionumber
=> $iteminfo->{'biblionumber'},
173 iteminfo
=> $iteminfo->{'author'},
175 borrowernumber
=> $borrowernumber,
176 borcnum
=> $borr->{'cardnumber'},
177 borfirstname
=> $borr->{'firstname'},
178 borsurname
=> $borr->{'surname'},
179 borcategory
=> $borr->{'description'},
188 my $issueinformation;
190 my $barcode = $query->param('barcode');
191 my $exemptfine = $query->param('exemptfine');
194 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
196 # silently prevent unauthorized operator from forgiving overdue
197 # fines by manually tweaking form parameters
200 my $dropboxmode = $query->param('dropboxmode');
201 my $dotransfer = $query->param('dotransfer');
202 my $canceltransfer = $query->param('canceltransfer');
203 my $dest = $query->param('dest');
204 my $calendar = Koha
::Calendar
->new( branchcode
=> $userenv_branch );
205 #dropbox: get last open day (today - 1)
206 my $today = DateTime
->now( time_zone
=> C4
::Context
->tz());
207 my $dropboxdate = $calendar->addDate($today, -1);
209 my $return_date_override = $query->param('return_date_override');
210 my $return_date_override_remember =
211 $query->param('return_date_override_remember');
212 if ($return_date_override) {
213 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
214 my $return_date_override_dt = eval {dt_from_string
( $return_date_override ) };
215 if ( $return_date_override_dt ) {
216 # note that we've overriden the return date
217 $template->param( return_date_was_overriden
=> 1);
218 # Save the original format if we are remembering for this series
220 return_date_override
=> $return_date_override,
221 return_date_override_remember
=> 1
222 ) if ($return_date_override_remember);
224 $return_date_override =
225 DateTime
::Format
::MySQL
->format_datetime( $return_date_override_dt );
229 $return_date_override = q{};
234 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
235 my $transferitem = $query->param('transferitem');
236 my $tobranch = $query->param('tobranch');
237 ModItemTransfer
($transferitem, $userenv_branch, $tobranch);
240 if ($canceltransfer){
241 $itemnumber=$query->param('itemnumber');
242 DeleteTransfer
($itemnumber);
244 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
247 $template->param( transfercancelled
=> 1);
251 # actually return book and prepare item table.....
254 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
255 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
256 $itemnumber = GetItemnumberFromBarcode
($barcode);
262 # get biblio description
263 my $biblio = GetBiblioFromItemNumber
($itemnumber);
264 # fix up item type for display
265 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
267 # Check if we should display a checkin message, based on the the item
268 # type of the checked in item
269 my $itemtype = Koha
::ItemTypes
->find( $biblio->{'itemtype'} );
270 if ( $itemtype && $itemtype->checkinmsg ) {
272 checkinmsg
=> $itemtype->checkinmsg,
273 checkinmsgtype
=> $itemtype->checkinmsgtype,
277 # make sure return branch respects home branch circulation rules, default to homebranch
278 my $hbr = GetBranchItemRule
($biblio->{'homebranch'}, $itemtype ?
$itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
279 $returnbranch = $biblio->{$hbr};
281 my $materials = $biblio->{'materials'};
282 my $descriptions = Koha
::AuthorisedValues
->get_description_by_koha_field({frameworkcode
=> '', kohafield
=>'items.materials', authorised_value
=> $materials });
283 $materials = $descriptions->{lib
} // '';
286 title
=> $biblio->{'title'},
287 homebranch
=> $biblio->{'homebranch'},
288 holdingbranch
=> $biblio->{'holdingbranch'},
289 returnbranch
=> $returnbranch,
290 author
=> $biblio->{'author'},
291 itembarcode
=> $biblio->{'barcode'},
292 itemtype
=> $biblio->{'itemtype'},
293 ccode
=> $biblio->{'ccode'},
294 itembiblionumber
=> $biblio->{'biblionumber'},
295 biblionumber
=> $biblio->{'biblionumber'},
296 borrower
=> $borrower,
297 additional_materials
=> $materials,
307 ( $returned, $messages, $issueinformation, $borrower ) =
308 AddReturn
( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override, $dropboxdate );
311 my $time_now = DateTime
->now( time_zone
=> C4
::Context
->tz )->truncate( to
=> 'minute');
312 my $duedate = $issueinformation->{date_due
}->strftime('%Y-%m-%d %H:%M');
313 $returneditems{0} = $barcode;
314 $riborrowernumber{0} = $borrower->{'borrowernumber'};
315 $riduedate{0} = $duedate;
316 $input{borrowernumber
} = $borrower->{'borrowernumber'};
317 $input{duedate
} = $duedate;
318 unless ( $dropboxmode ) {
319 $input{return_overdue
} = 1 if (DateTime
->compare($issueinformation->{date_due
}, DateTime
->now()) == -1);
321 $input{return_overdue
} = 1 if (DateTime
->compare($issueinformation->{date_due
}, $dropboxdate) == -1);
323 push( @inputloop, \
%input );
325 if ( C4
::Context
->preference("FineNotifyAtCheckin") ) {
326 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines
( $borrower->{'borrowernumber'} );
327 if ($fines && $fines > 0) {
328 $template->param( fines
=> sprintf("%.2f",$fines) );
329 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
333 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
334 #Check for waiting holds
335 my @reserves = GetReservesFromBorrowernumber
($borrower->{'borrowernumber'});
336 my $waiting_holds = 0;
337 foreach my $num_res (@reserves) {
338 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
342 if ($waiting_holds > 0) {
344 waiting_holds
=> $waiting_holds,
345 holdsborrowernumber
=> $borrower->{'borrowernumber'},
346 holdsfirstname
=> $borrower->{'firstname'},
347 holdssurname
=> $borrower->{'surname'},
351 } elsif ( C4
::Context
->preference('ShowAllCheckins') and !$messages->{'BadBarcode'} ) {
353 $returneditems{0} = $barcode;
355 push( @inputloop, \
%input );
357 $template->param( privacy
=> $borrower->{privacy
} );
359 $template->param( inputloop
=> \
@inputloop );
365 # new op dev : we check if the document must be returned to his homebranch directly,
366 # if the document is transfered, we have warning message .
368 if ( $messages->{'WasTransfered'} ) {
372 itemnumber
=> $itemnumber,
376 if ( $messages->{'NeedsTransfer'} ){
379 needstransfer
=> $messages->{'NeedsTransfer'},
380 itemnumber
=> $itemnumber,
384 if ( $messages->{'Wrongbranch'} ){
387 rightbranch
=> $messages->{'Wrongbranch'}->{'Rightbranch'},
391 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
393 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
396 TransferWaitingAt
=> $messages->{'WrongTransfer'},
397 WrongTransferItem
=> $messages->{'WrongTransferItem'},
398 itemnumber
=> $itemnumber,
401 my $reserve = $messages->{'ResFound'};
402 my $borr = C4
::Members
::GetMember
( borrowernumber
=> $reserve->{'borrowernumber'} );
403 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
406 wborfirstname
=> $borr->{'firstname'},
407 wborsurname
=> $borr->{'surname'},
408 wborcategory
=> $borr->{'description'},
409 wbortitle
=> $borr->{'title'},
410 wborphone
=> $borr->{'phone'},
411 wboremail
=> $borr->{'email'},
412 wborstnum
=> $borr->{streetnumber
},
413 wboraddress
=> $borr->{'address'},
414 wboraddress2
=> $borr->{'address2'},
415 wborcity
=> $borr->{'city'},
416 wborzip
=> $borr->{'zipcode'},
417 wborrowernumber
=> $reserve->{'borrowernumber'},
418 wborcnum
=> $borr->{'cardnumber'},
419 wtransfertFrom
=> $userenv_branch,
424 # reserve found and item arrived at the expected branch
426 if ( $messages->{'ResFound'}) {
427 my $reserve = $messages->{'ResFound'};
428 my $borr = C4
::Members
::GetMember
( borrowernumber
=> $reserve->{'borrowernumber'} );
429 my $holdmsgpreferences = C4
::Members
::Messaging
::GetMessagingPreferences
( { borrowernumber
=> $reserve->{'borrowernumber'}, message_name
=> 'Hold_Filled' } );
430 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
431 if ( $reserve->{'ResFound'} eq "Waiting" ) {
433 waiting
=> ($userenv_branch eq $reserve->{'branchcode'} ?
1 : 0 ),
435 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
437 intransit
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
438 transfertodo
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
439 reserve_id
=> $reserve->{reserve_id
},
444 # same params for Waiting or Reserved
447 name
=> $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
448 borfirstname
=> $borr->{'firstname'},
449 borsurname
=> $borr->{'surname'},
450 borcategory
=> $borr->{'description'},
451 bortitle
=> $borr->{'title'},
452 borphone
=> $borr->{'phone'},
453 boremail
=> $borr->{'email'},
454 boraddress
=> $borr->{'address'},
455 boraddress2
=> $borr->{'address2'},
456 borstnum
=> $borr->{streetnumber
},
457 borcity
=> $borr->{'city'},
458 borzip
=> $borr->{'zipcode'},
459 borcnum
=> $borr->{'cardnumber'},
460 debarred
=> $borr->{'debarred'},
461 gonenoaddress
=> $borr->{'gonenoaddress'},
463 destbranch
=> $reserve->{'branchcode'},
464 borrowernumber
=> $reserve->{'borrowernumber'},
465 itemnumber
=> $reserve->{'itemnumber'},
466 reservenotes
=> $reserve->{'reservenotes'},
467 bormessagepref
=> $holdmsgpreferences->{'transports'},
469 } # else { ; } # error?
474 foreach my $code ( keys %$messages ) {
476 my $exit_required_p = 0;
477 if ( $code eq 'BadBarcode' ) {
478 $err{badbarcode
} = 1;
479 $err{msg
} = $messages->{'BadBarcode'};
481 elsif ( $code eq 'NotIssued' ) {
484 $err{msg
} = $messages->{'IsPermanent'} if $messages->{'IsPermanent'};
486 elsif ( $code eq 'LocalUse' ) {
489 elsif ( $code eq 'WasLost' ) {
492 elsif ( $code eq 'LostItemFeeRefunded' ) {
493 $template->param( LostItemFeeRefunded
=> 1 );
495 elsif ( $code eq 'ResFound' ) {
496 ; # FIXME... anything to do here?
498 elsif ( $code eq 'WasReturned' ) {
499 ; # FIXME... anything to do here?
501 elsif ( $code eq 'WasTransfered' ) {
502 ; # FIXME... anything to do here?
504 elsif ( $code eq 'withdrawn' ) {
506 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
508 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
509 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
510 $err{ispermanent
} = 1;
511 $err{msg
} = $messages->{'IsPermanent'};
514 elsif ( $code eq 'WrongTransfer' ) {
515 ; # FIXME... anything to do here?
517 elsif ( $code eq 'WrongTransferItem' ) {
518 ; # FIXME... anything to do here?
520 elsif ( $code eq 'NeedsTransfer' ) {
522 elsif ( $code eq 'Wrongbranch' ) {
524 elsif ( $code eq 'Debarred' ) {
525 $err{debarred
} = $messages->{'Debarred'};
526 $err{debarcardnumber
} = $borrower->{cardnumber
};
527 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
528 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
530 elsif ( $code eq 'PrevDebarred' ) {
531 $err{prevdebarred
} = $messages->{'PrevDebarred'};
533 elsif ( $code eq 'ForeverDebarred' ) {
534 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
536 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
537 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
540 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
541 # This forces the issue of staying in sync w/ Circulation.pm
544 push( @errmsgloop, \
%err );
546 last if $exit_required_p;
548 $template->param( errmsgloop
=> \
@errmsgloop );
550 #set up so only the last 8 returned items display (make for faster loading pages)
551 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
555 { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> '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;