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
;
58 my ( $template, $librarian, $cookie ) = get_template_and_user
(
60 template_name
=> "circ/returns.tt",
64 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
68 my $sessionID = $query->cookie("CGISESSID");
69 my $session = get_session
($sessionID);
70 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
71 # no branch set we can't return
72 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
76 # Print a reserve slip on this page
77 if ( $query->param('print_slip') ) {
80 borrowernumber
=> scalar $query->param('borrowernumber'),
81 biblionumber
=> scalar $query->param('biblionumber'),
87 my $printers = GetPrinters
();
88 my $userenv = C4
::Context
->userenv;
89 my $userenv_branch = $userenv->{'branch'} // '';
90 my $printer = $userenv->{'branchprinter'} // '';
91 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
93 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
95 # Some code to handle the error if there is no branch or printer setting.....
98 # Set up the item stack ....
101 my %riborrowernumber;
103 foreach ( $query->param ) {
116 my $barcode = $query->param("ri-$counter");
117 my $duedate = $query->param("dd-$counter");
118 my $borrowernumber = $query->param("bn-$counter");
121 # decode barcode ## Didn't we already decode them before passing them back last time??
122 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
123 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
125 ######################
126 #Are these lines still useful ?
127 $returneditems{$counter} = $barcode;
128 $riduedate{$counter} = $duedate;
129 $riborrowernumber{$counter} = $borrowernumber;
131 #######################
132 $input{counter
} = $counter;
133 $input{barcode
} = $barcode;
134 $input{duedate
} = $duedate;
135 $input{borrowernumber
} = $borrowernumber;
136 push( @inputloop, \
%input );
140 # Deal with the requests....
142 if ($query->param('WT-itemNumber')){
143 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
146 if ( $query->param('reserve_id') ) {
147 my $item = $query->param('itemnumber');
148 my $borrowernumber = $query->param('borrowernumber');
149 my $reserve_id = $query->param('reserve_id');
150 my $diffBranchReturned = $query->param('diffBranch');
151 my $iteminfo = GetBiblioFromItemNumber
($item);
152 my $cancel_reserve = $query->param('cancel_reserve');
153 # fix up item type for display
154 $iteminfo->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$iteminfo->{'itype'} : $iteminfo->{'itemtype'};
156 if ( $cancel_reserve ) {
157 CancelReserve
({ reserve_id
=> $reserve_id, charge_cancel_fee
=> !$forgivemanualholdsexpire });
159 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ?
$diffBranchReturned : undef;
160 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
161 # i.e., whether to apply waiting status
162 ModReserveAffect
( $item, $borrowernumber, $diffBranchSend, $reserve_id );
164 # check if we have other reserves for this document, if we have a return send the message of transfer
165 my ( $messages, $nextreservinfo ) = GetOtherReserves
($item);
167 my $borr = GetMember
( borrowernumber
=> $nextreservinfo );
168 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
169 if ( $messages->{'transfert'} ) {
171 itemtitle
=> $iteminfo->{'title'},
172 itemnumber
=> $iteminfo->{'itemnumber'},
173 itembiblionumber
=> $iteminfo->{'biblionumber'},
174 iteminfo
=> $iteminfo->{'author'},
176 borrowernumber
=> $borrowernumber,
177 borcnum
=> $borr->{'cardnumber'},
178 borfirstname
=> $borr->{'firstname'},
179 borsurname
=> $borr->{'surname'},
180 borcategory
=> $borr->{'description'},
189 my $issueinformation;
191 my $barcode = $query->param('barcode');
192 my $exemptfine = $query->param('exemptfine');
195 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
197 # silently prevent unauthorized operator from forgiving overdue
198 # fines by manually tweaking form parameters
201 my $dropboxmode = $query->param('dropboxmode');
202 my $dotransfer = $query->param('dotransfer');
203 my $canceltransfer = $query->param('canceltransfer');
204 my $dest = $query->param('dest');
205 my $calendar = Koha
::Calendar
->new( branchcode
=> $userenv_branch );
206 #dropbox: get last open day (today - 1)
207 my $today = DateTime
->now( time_zone
=> C4
::Context
->tz());
208 my $dropboxdate = $calendar->addDate($today, -1);
210 my $return_date_override = $query->param('return_date_override');
211 my $return_date_override_remember =
212 $query->param('return_date_override_remember');
213 if ($return_date_override) {
214 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
215 my $return_date_override_dt = eval {dt_from_string
( $return_date_override ) };
216 if ( $return_date_override_dt ) {
217 # note that we've overriden the return date
218 $template->param( return_date_was_overriden
=> 1);
219 # Save the original format if we are remembering for this series
221 return_date_override
=> $return_date_override,
222 return_date_override_remember
=> 1
223 ) if ($return_date_override_remember);
225 $return_date_override =
226 DateTime
::Format
::MySQL
->format_datetime( $return_date_override_dt );
230 $return_date_override = q{};
235 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
236 my $transferitem = $query->param('transferitem');
237 my $tobranch = $query->param('tobranch');
238 ModItemTransfer
($transferitem, $userenv_branch, $tobranch);
241 if ($canceltransfer){
242 $itemnumber=$query->param('itemnumber');
243 DeleteTransfer
($itemnumber);
245 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
248 $template->param( transfercancelled
=> 1);
252 # actually return book and prepare item table.....
255 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
256 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
257 $itemnumber = GetItemnumberFromBarcode
($barcode);
263 # get biblio description
264 my $biblio = GetBiblioFromItemNumber
($itemnumber);
265 # fix up item type for display
266 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
268 # Check if we should display a checkin message, based on the the item
269 # type of the checked in item
270 my $itemtype = Koha
::ItemTypes
->find( $biblio->{'itemtype'} );
271 if ( $itemtype && $itemtype->checkinmsg ) {
273 checkinmsg
=> $itemtype->checkinmsg,
274 checkinmsgtype
=> $itemtype->checkinmsgtype,
278 # make sure return branch respects home branch circulation rules, default to homebranch
279 my $hbr = GetBranchItemRule
($biblio->{'homebranch'}, $itemtype ?
$itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
280 $returnbranch = $biblio->{$hbr};
282 my $materials = $biblio->{'materials'};
283 my $descriptions = Koha
::AuthorisedValues
->get_description_by_koha_field({frameworkcode
=> '', kohafield
=>'items.materials', authorised_value
=> $materials });
284 $materials = $descriptions->{lib
} // '';
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 $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
328 my $balance = $patron->account->balance;
331 $template->param( fines
=> sprintf("%.2f", $balance) );
332 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
336 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
337 #Check for waiting holds
338 my @reserves = GetReservesFromBorrowernumber
($borrower->{'borrowernumber'});
339 my $waiting_holds = 0;
340 foreach my $num_res (@reserves) {
341 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
345 if ($waiting_holds > 0) {
347 waiting_holds
=> $waiting_holds,
348 holdsborrowernumber
=> $borrower->{'borrowernumber'},
349 holdsfirstname
=> $borrower->{'firstname'},
350 holdssurname
=> $borrower->{'surname'},
354 } elsif ( C4
::Context
->preference('ShowAllCheckins') and !$messages->{'BadBarcode'} ) {
356 $returneditems{0} = $barcode;
358 push( @inputloop, \
%input );
360 $template->param( privacy
=> $borrower->{privacy
} );
362 $template->param( inputloop
=> \
@inputloop );
368 # new op dev : we check if the document must be returned to his homebranch directly,
369 # if the document is transfered, we have warning message .
371 if ( $messages->{'WasTransfered'} ) {
375 itemnumber
=> $itemnumber,
379 if ( $messages->{'NeedsTransfer'} ){
382 needstransfer
=> $messages->{'NeedsTransfer'},
383 itemnumber
=> $itemnumber,
387 if ( $messages->{'Wrongbranch'} ){
390 rightbranch
=> $messages->{'Wrongbranch'}->{'Rightbranch'},
394 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
396 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
399 TransferWaitingAt
=> $messages->{'WrongTransfer'},
400 WrongTransferItem
=> $messages->{'WrongTransferItem'},
401 itemnumber
=> $itemnumber,
404 my $reserve = $messages->{'ResFound'};
405 my $borr = C4
::Members
::GetMember
( borrowernumber
=> $reserve->{'borrowernumber'} );
406 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
409 wborfirstname
=> $borr->{'firstname'},
410 wborsurname
=> $borr->{'surname'},
411 wborcategory
=> $borr->{'description'},
412 wbortitle
=> $borr->{'title'},
413 wborphone
=> $borr->{'phone'},
414 wboremail
=> $borr->{'email'},
415 wborstnum
=> $borr->{streetnumber
},
416 wboraddress
=> $borr->{'address'},
417 wboraddress2
=> $borr->{'address2'},
418 wborcity
=> $borr->{'city'},
419 wborzip
=> $borr->{'zipcode'},
420 wborrowernumber
=> $reserve->{'borrowernumber'},
421 wborcnum
=> $borr->{'cardnumber'},
422 wtransfertFrom
=> $userenv_branch,
427 # reserve found and item arrived at the expected branch
429 if ( $messages->{'ResFound'}) {
430 my $reserve = $messages->{'ResFound'};
431 my $borr = C4
::Members
::GetMember
( borrowernumber
=> $reserve->{'borrowernumber'} );
432 my $holdmsgpreferences = C4
::Members
::Messaging
::GetMessagingPreferences
( { borrowernumber
=> $reserve->{'borrowernumber'}, message_name
=> 'Hold_Filled' } );
433 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
434 if ( $reserve->{'ResFound'} eq "Waiting" ) {
436 waiting
=> ($userenv_branch eq $reserve->{'branchcode'} ?
1 : 0 ),
438 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
440 intransit
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
441 transfertodo
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
442 reserve_id
=> $reserve->{reserve_id
},
447 # same params for Waiting or Reserved
450 name
=> $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
451 borfirstname
=> $borr->{'firstname'},
452 borsurname
=> $borr->{'surname'},
453 borcategory
=> $borr->{'description'},
454 bortitle
=> $borr->{'title'},
455 borphone
=> $borr->{'phone'},
456 boremail
=> $borr->{'email'},
457 boraddress
=> $borr->{'address'},
458 boraddress2
=> $borr->{'address2'},
459 borstnum
=> $borr->{streetnumber
},
460 borcity
=> $borr->{'city'},
461 borzip
=> $borr->{'zipcode'},
462 borcnum
=> $borr->{'cardnumber'},
463 debarred
=> $borr->{'debarred'},
464 gonenoaddress
=> $borr->{'gonenoaddress'},
466 destbranch
=> $reserve->{'branchcode'},
467 borrowernumber
=> $reserve->{'borrowernumber'},
468 itemnumber
=> $reserve->{'itemnumber'},
469 reservenotes
=> $reserve->{'reservenotes'},
470 bormessagepref
=> $holdmsgpreferences->{'transports'},
472 } # else { ; } # error?
477 foreach my $code ( keys %$messages ) {
479 my $exit_required_p = 0;
480 if ( $code eq 'BadBarcode' ) {
481 $err{badbarcode
} = 1;
482 $err{msg
} = $messages->{'BadBarcode'};
484 elsif ( $code eq 'NotIssued' ) {
487 $err{msg
} = $messages->{'IsPermanent'} if $messages->{'IsPermanent'};
489 elsif ( $code eq 'LocalUse' ) {
492 elsif ( $code eq 'WasLost' ) {
495 elsif ( $code eq 'LostItemFeeRefunded' ) {
496 $template->param( LostItemFeeRefunded
=> 1 );
498 elsif ( $code eq 'ResFound' ) {
499 ; # FIXME... anything to do here?
501 elsif ( $code eq 'WasReturned' ) {
502 ; # FIXME... anything to do here?
504 elsif ( $code eq 'WasTransfered' ) {
505 ; # FIXME... anything to do here?
507 elsif ( $code eq 'withdrawn' ) {
509 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
511 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
512 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
513 $err{ispermanent
} = 1;
514 $err{msg
} = $messages->{'IsPermanent'};
517 elsif ( $code eq 'WrongTransfer' ) {
518 ; # FIXME... anything to do here?
520 elsif ( $code eq 'WrongTransferItem' ) {
521 ; # FIXME... anything to do here?
523 elsif ( $code eq 'NeedsTransfer' ) {
525 elsif ( $code eq 'Wrongbranch' ) {
527 elsif ( $code eq 'Debarred' ) {
528 $err{debarred
} = $messages->{'Debarred'};
529 $err{debarcardnumber
} = $borrower->{cardnumber
};
530 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
531 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
533 elsif ( $code eq 'PrevDebarred' ) {
534 $err{prevdebarred
} = $messages->{'PrevDebarred'};
536 elsif ( $code eq 'ForeverDebarred' ) {
537 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
539 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
540 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
543 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
544 # This forces the issue of staying in sync w/ Circulation.pm
547 push( @errmsgloop, \
%err );
549 last if $exit_required_p;
551 $template->param( errmsgloop
=> \
@errmsgloop );
553 #set up so only the last 8 returned items display (make for faster loading pages)
554 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
558 { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.location' } ) };
559 foreach ( sort { $a <=> $b } keys %returneditems ) {
561 if ( $count++ < $returned_counter ) {
562 my $bar_code = $returneditems{$_};
563 if ($riduedate{$_}) {
564 my $duedate = dt_from_string
( $riduedate{$_}, 'sql');
565 $ri{year
} = $duedate->year();
566 $ri{month
} = $duedate->month();
567 $ri{day
} = $duedate->day();
568 $ri{hour
} = $duedate->hour();
569 $ri{minute
} = $duedate->minute();
570 $ri{duedate
} = output_pref
($duedate);
571 my $b = C4
::Members
::GetMember
( borrowernumber
=> $riborrowernumber{$_} );
572 unless ( $dropboxmode ) {
573 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, DateTime
->now()) == -1);
575 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, $dropboxdate) == -1);
577 $ri{borrowernumber
} = $b->{'borrowernumber'};
578 $ri{borcnum
} = $b->{'cardnumber'};
579 $ri{borfirstname
} = $b->{'firstname'};
580 $ri{borsurname
} = $b->{'surname'};
581 $ri{bortitle
} = $b->{'title'};
582 $ri{bornote
} = $b->{'borrowernotes'};
583 $ri{borcategorycode
}= $b->{'categorycode'};
584 $ri{borissuescount
} = Koha
::Checkouts
->count( { borrowernumber
=> $b->{'borrowernumber'} } );
587 $ri{borrowernumber
} = $riborrowernumber{$_};
591 my $biblio = GetBiblioFromItemNumber
(GetItemnumberFromBarcode
($bar_code));
592 my $item = GetItem
( GetItemnumberFromBarcode
($bar_code) );
593 # fix up item type for display
594 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
595 $ri{itembiblionumber
} = $biblio->{'biblionumber'};
596 $ri{itemtitle
} = $biblio->{'title'};
597 $ri{itemauthor
} = $biblio->{'author'};
598 $ri{itemcallnumber
} = $biblio->{'itemcallnumber'};
599 $ri{dateaccessioned
} = $item->{dateaccessioned
};
600 $ri{itemtype
} = $biblio->{'itemtype'};
601 $ri{itemnote
} = $biblio->{'itemnotes'};
602 $ri{itemnotes_nonpublic
} = $item->{'itemnotes_nonpublic'};
603 $ri{ccode
} = $biblio->{'ccode'};
604 $ri{enumchron
} = $biblio->{'enumchron'};
605 $ri{itemnumber
} = $biblio->{'itemnumber'};
606 $ri{barcode
} = $bar_code;
607 $ri{homebranch
} = $item->{'homebranch'};
608 $ri{holdingbranch
} = $item->{'holdingbranch'};
610 $ri{location
} = $biblio->{'location'};
611 my $shelfcode = $ri{'location'};
612 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
624 errmsgloop
=> \
@errmsgloop,
625 exemptfine
=> $exemptfine,
626 dropboxmode
=> $dropboxmode,
627 dropboxdate
=> output_pref
($dropboxdate),
628 forgivemanualholdsexpire
=> $forgivemanualholdsexpire,
629 overduecharges
=> $overduecharges,
630 AudioAlerts
=> C4
::Context
->preference("AudioAlerts"),
631 BlockReturnOfWithdrawnItems
=> C4
::Context
->preference("BlockReturnOfWithdrawnItems"),
634 $itemnumber = GetItemnumberFromBarcode
( $barcode );
636 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
637 if ( $holdingBranch and $collectionBranch ) {
638 $holdingBranch //= '';
639 $collectionBranch //= $returnbranch;
640 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
642 collectionItemNeedsTransferred
=> 1,
643 collectionBranch
=> $collectionBranch,
644 itemnumber
=> $itemnumber,
650 # actually print the page!
651 output_html_with_http_headers
$query, $cookie, $template->output;