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
32 # FIXME There are weird things going on with $patron and $borrowernumber in this script
37 use C4
::Auth qw
/:DEFAULT get_session/;
45 use C4
::Koha
; # FIXME : is it still useful ?
46 use C4
::Members
::Messaging
;
50 use C4
::RotatingCollections
;
51 use Koha
::AuthorisedValues
;
52 use Koha
::BiblioFrameworks
;
63 my ( $template, $librarian, $cookie, $flags ) = get_template_and_user
(
65 template_name
=> "circ/returns.tt",
68 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
72 my $sessionID = $query->cookie("CGISESSID");
73 my $session = get_session
($sessionID);
74 my $desk_id = C4
::Context
->userenv->{"desk_id"} || '';
76 # Print a reserve slip on this page
77 if ( $query->param('print_slip') ) {
80 reserve_id
=> scalar $query->param('reserve_id'),
86 my $userenv = C4
::Context
->userenv;
87 my $userenv_branch = $userenv->{'branch'} // '';
88 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
90 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
92 # Set up the item stack ....
97 foreach ( $query->param ) {
110 my $barcode = $query->param("ri-$counter");
111 my $duedate = $query->param("dd-$counter");
112 my $borrowernumber = $query->param("bn-$counter");
115 # decode barcode ## Didn't we already decode them before passing them back last time??
116 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
117 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
119 ######################
120 #Are these lines still useful ?
121 $returneditems{$counter} = $barcode;
122 $riduedate{$counter} = $duedate;
123 $riborrowernumber{$counter} = $borrowernumber;
125 #######################
126 $input{counter
} = $counter;
127 $input{barcode
} = $barcode;
128 $input{duedate
} = $duedate;
129 $input{borrowernumber
} = $borrowernumber;
130 push( @inputloop, \
%input );
134 # Deal with the requests....
136 if ($query->param('WT-itemNumber')){
137 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
140 my $itemnumber = $query->param('itemnumber');
141 if ( $query->param('reserve_id') ) {
142 my $borrowernumber = $query->param('borrowernumber');
143 my $reserve_id = $query->param('reserve_id');
144 my $diffBranchReturned = $query->param('diffBranch');
145 my $cancel_reserve = $query->param('cancel_reserve');
146 # fix up item type for display
147 my $item = Koha
::Items
->find( $itemnumber );
148 my $biblio = $item->biblio;
150 if ( $cancel_reserve ) {
151 my $hold = Koha
::Holds
->find( $reserve_id );
153 $hold->cancel( { charge_cancel_fee
=> !$forgivemanualholdsexpire } );
156 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ?
$diffBranchReturned : undef;
157 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
158 # i.e., whether to apply waiting status
159 ModReserveAffect
( $itemnumber, $borrowernumber, $diffBranchSend, $reserve_id, $desk_id );
161 # check if we have other reserves for this document, if we have a return send the message of transfer
162 my ( $messages, $nextreservinfo ) = GetOtherReserves
($itemnumber);
164 my $patron = Koha
::Patrons
->find( $nextreservinfo );
165 my $name = $patron ?
$patron->surname . ", " . $patron->title . " " . $patron->firstname : '';
166 if ( $messages->{'transfert'} ) {
168 itemtitle
=> $biblio->title,
169 itembiblionumber
=> $biblio->biblionumber,
170 iteminfo
=> $biblio->author,
182 my $barcode = $query->param('barcode');
183 my $exemptfine = $query->param('exemptfine');
186 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
188 # silently prevent unauthorized operator from forgiving overdue
189 # fines by manually tweaking form parameters
192 my $dropboxmode = $query->param('dropboxmode');
193 my $dotransfer = $query->param('dotransfer');
194 my $canceltransfer = $query->param('canceltransfer');
195 my $dest = $query->param('dest');
196 #dropbox: get last open day (today - 1)
197 my $dropboxdate = Koha
::Checkouts
::calculate_dropbox_date
();
199 my $return_date_override = $query->param('return_date_override');
200 my $return_date_override_dt;
201 my $return_date_override_remember =
202 $query->param('return_date_override_remember');
203 if ($return_date_override) {
204 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
205 $return_date_override_dt = eval {dt_from_string
( $return_date_override ) };
206 if ( $return_date_override_dt ) {
207 # note that we've overriden the return date
208 $template->param( return_date_was_overriden
=> 1);
209 # Save the original format if we are remembering for this series
211 return_date_override
=> $return_date_override,
212 return_date_override_remember
=> 1
213 ) if ($return_date_override_remember);
215 $return_date_override =
216 DateTime
::Format
::MySQL
->format_datetime( $return_date_override_dt );
220 $return_date_override = q{};
225 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
226 my $transferitem = $query->param('transferitem');
227 my $tobranch = $query->param('tobranch');
228 my $trigger = $query->param('trigger');
229 ModItemTransfer
($transferitem, $userenv_branch, $tobranch, $trigger);
232 if ($canceltransfer){
233 DeleteTransfer
($itemnumber);
235 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
238 $template->param( transfercancelled
=> 1);
242 # actually return book and prepare item table.....
245 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
246 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
247 my $item = Koha
::Items
->find({ barcode
=> $barcode });
250 $itemnumber = $item->itemnumber;
251 # Check if we should display a checkin message, based on the the item
252 # type of the checked in item
253 my $itemtype = Koha
::ItemTypes
->find( $item->effective_itemtype );
254 if ( $itemtype && $itemtype->checkinmsg ) {
256 checkinmsg
=> $itemtype->checkinmsg,
257 checkinmsgtype
=> $itemtype->checkinmsgtype,
261 # make sure return branch respects home branch circulation rules, default to homebranch
262 my $hbr = GetBranchItemRule
($item->homebranch, $itemtype ?
$itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
263 $returnbranch = $hbr ne 'noreturn' ?
$item->$hbr : $userenv_branch; # can be noreturn, homebranch or holdingbranch
265 my $materials = $item->materials;
266 my $descriptions = Koha
::AuthorisedValues
->get_description_by_koha_field({frameworkcode
=> '', kohafield
=>'items.materials', authorised_value
=> $materials });
267 $materials = $descriptions->{lib
} // $materials;
269 my $checkout = $item->checkout;
270 my $biblio = $item->biblio;
272 title
=> $biblio->title,
273 homebranch
=> $item->homebranch,
274 holdingbranch
=> $item->holdingbranch,
275 returnbranch
=> $returnbranch,
276 author
=> $biblio->author,
277 itembarcode
=> $item->barcode,
278 itemtype
=> $item->effective_itemtype,
279 ccode
=> $item->ccode,
280 itembiblionumber
=> $biblio->biblionumber,
281 biblionumber
=> $biblio->biblionumber,
282 additional_materials
=> $materials,
285 } # FIXME else we should not call AddReturn but set BadBarcode directly instead
293 my $return_date = $dropboxmode ?
$dropboxdate : $return_date_override_dt;
295 # Block return if multi-part and confirm has not been received
297 C4
::Context
->preference("CircConfirmItemParts")
300 && !$query->param('multiple_confirm');
301 $template->param( 'multiple_confirmed' => 1 )
302 if $query->param('multiple_confirm');
305 ( $returned, $messages, $issue, $borrower ) =
306 AddReturn
( $barcode, $userenv_branch, $exemptfine, $return_date )
307 unless $needs_confirm;
310 my $time_now = dt_from_string
()->truncate( to
=> 'minute');
311 my $date_due_dt = dt_from_string
( $issue->date_due, 'sql' );
312 my $duedate = $date_due_dt->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($date_due_dt, dt_from_string
()) == -1);
321 $input{return_overdue
} = 1 if (DateTime
->compare($date_due_dt, $dropboxdate) == -1);
323 push( @inputloop, \
%input );
325 if ( C4
::Context
->preference("FineNotifyAtCheckin") ) {
326 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
327 my $balance = $patron->account->balance;
330 $template->param( fines
=> sprintf("%.2f", $balance) );
331 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
335 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
336 #Check for waiting holds
337 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
338 my $waiting_holds = $patron->holds->search({ found
=> 'W', branchcode
=> $userenv_branch })->count;
339 if ($waiting_holds > 0) {
341 waiting_holds
=> $waiting_holds,
342 holdsborrowernumber
=> $borrower->{'borrowernumber'},
343 holdsfirstname
=> $borrower->{'firstname'},
344 holdssurname
=> $borrower->{'surname'},
348 } elsif ( C4
::Context
->preference('ShowAllCheckins') and !$messages->{'BadBarcode'} and !$needs_confirm ) {
350 $returneditems{0} = $barcode;
352 push( @inputloop, \
%input );
354 $template->param( privacy
=> $borrower->{privacy
} );
356 if ( $needs_confirm ) {
357 $template->param( needs_confirm
=> $needs_confirm );
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 transferred, we have warning message .
369 if ( $messages->{'WasTransfered'} ) {
376 if ( $messages->{'NeedsTransfer'} ){
379 needstransfer
=> $messages->{'NeedsTransfer'},
380 trigger
=> $messages->{'TransferTrigger'},
384 if ( $messages->{'Wrongbranch'} ){
387 rightbranch
=> $messages->{'Wrongbranch'}->{'Rightbranch'},
391 # case of wrong transfert, if the document wasn't transferred 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'},
400 my $reserve = $messages->{'ResFound'};
402 my $patron = Koha
::Patrons
->find( $reserve->{'borrowernumber'} );
403 my $name = $patron->surname . ", " . $patron->title . " " . $patron->firstname;
409 wtransfertFrom
=> $userenv_branch,
414 # reserve found and item arrived at the expected branch
416 if ( $messages->{'ResFound'}) {
417 my $reserve = $messages->{'ResFound'};
418 my $patron = Koha
::Patrons
->find( $reserve->{borrowernumber
} );
419 my $holdmsgpreferences = C4
::Members
::Messaging
::GetMessagingPreferences
( { borrowernumber
=> $reserve->{'borrowernumber'}, message_name
=> 'Hold_Filled' } );
420 my $branchCheck = ( $userenv_branch eq $reserve->{branchcode
} );
421 if ( $reserve->{'ResFound'} eq "Reserved" && C4
::Context
->preference('HoldsAutoFill') ) {
422 my $item = Koha
::Items
->find( $itemnumber );
423 my $biblio = $item->biblio;
425 my $diffBranchSend = !$branchCheck ?
$reserve->{branchcode
} : undef;
426 ModReserveAffect
( $reserve->{itemnumber
}, $reserve->{borrowernumber
}, $diffBranchSend, $reserve->{reserve_id
}, $desk_id );
427 my ( $messages, $nextreservinfo ) = GetOtherReserves
($reserve->{itemnumber
});
430 hold_auto_filled
=> 1,
431 print_slip
=> C4
::Context
->preference('HoldsAutoFillPrintSlip'),
432 reserve_id
=> $nextreservinfo->{reserve_id
},
435 if ( $messages->{'transfert'} ) {
437 itemtitle
=> $biblio->title,
438 itembiblionumber
=> $biblio->biblionumber,
439 iteminfo
=> $biblio->author,
443 } elsif ( $reserve->{'ResFound'} eq "Waiting" ) {
445 waiting
=> $branchCheck ?
1 : undef,
447 } elsif ( $reserve->{'ResFound'} eq "Reserved" || $reserve->{'ResFound'} eq "Processing" ) {
449 intransit
=> $branchCheck ?
undef : 1,
450 transfertodo
=> $branchCheck ?
undef : 1,
451 reserve_id
=> $reserve->{reserve_id
},
454 } # else { ; } # error?
456 # same params for Waiting or Reserved
461 destbranch
=> $reserve->{'branchcode'},
462 reservenotes
=> $reserve->{'reservenotes'},
463 reserve_id
=> $reserve->{reserve_id
},
464 bormessagepref
=> $holdmsgpreferences->{'transports'},
470 foreach my $code ( keys %$messages ) {
472 my $exit_required_p = 0;
473 if ( $code eq 'BadBarcode' ) {
474 $err{badbarcode
} = 1;
475 $err{msg
} = $messages->{'BadBarcode'};
477 elsif ( $code eq 'NotIssued' ) {
481 elsif ( $code eq 'LocalUse' ) {
484 elsif ( $code eq 'WasLost' ) {
486 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfLostItems");
488 elsif ( $code eq 'LostItemFeeRefunded' ) {
489 $template->param( LostItemFeeRefunded
=> 1 );
491 elsif ( $code eq 'LostItemFeeCharged' ) {
492 $template->param( LostItemFeeCharged
=> 1 );
494 elsif ( $code eq 'LostItemFeeRestored' ) {
495 $template->param( LostItemFeeRestored
=> 1 );
497 elsif ( $code eq 'ResFound' ) {
498 ; # FIXME... anything to do here?
500 elsif ( $code eq 'WasReturned' ) {
501 ; # FIXME... anything to do here?
503 elsif ( $code eq 'WasTransfered' ) {
504 ; # FIXME... anything to do here?
506 elsif ( $code eq 'withdrawn' ) {
508 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
510 elsif ( $code eq 'WrongTransfer' ) {
511 ; # FIXME... anything to do here?
513 elsif ( $code eq 'WrongTransferItem' ) {
514 ; # FIXME... anything to do here?
516 elsif ( $code eq 'NeedsTransfer' ) {
518 elsif ( $code eq 'TransferTrigger' ) {
519 ; # Handled alongside NeedsTransfer
521 elsif ( $code eq 'TransferArrived' ) {
522 $err{transferred
} = $messages->{'TransferArrived'};
524 elsif ( $code eq 'Wrongbranch' ) {
526 elsif ( $code eq 'Debarred' ) {
527 $err{debarred
} = $messages->{'Debarred'};
528 $err{debarcardnumber
} = $borrower->{cardnumber
};
529 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
530 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
532 elsif ( $code eq 'PrevDebarred' ) {
533 $err{prevdebarred
} = $messages->{'PrevDebarred'};
535 elsif ( $code eq 'ForeverDebarred' ) {
536 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
538 elsif ( $code eq 'ItemLocationUpdated' ) {
539 $err{ItemLocationUpdated
} = $messages->{ItemLocationUpdated
};
541 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
542 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
544 elsif ( $code eq 'DataCorrupted' ) {
545 $err{data_corrupted
} = 1;
547 elsif ( $code eq 'ReturnClaims' ) {
548 $template->param( ReturnClaims
=> $messages->{ReturnClaims
} );
550 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
551 # This forces the issue of staying in sync w/ Circulation.pm
554 push( @errmsgloop, \
%err );
556 last if $exit_required_p;
558 $template->param( errmsgloop
=> \
@errmsgloop );
560 #set up so only the last 8 returned items display (make for faster loading pages)
561 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
565 { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.location' } ) };
566 foreach ( sort { $a <=> $b } keys %returneditems ) {
568 if ( $count++ < $returned_counter ) {
569 my $bar_code = $returneditems{$_};
570 if ($riduedate{$_}) {
571 my $duedate = dt_from_string
( $riduedate{$_}, 'sql');
572 $ri{year
} = $duedate->year();
573 $ri{month
} = $duedate->month();
574 $ri{day
} = $duedate->day();
575 $ri{hour
} = $duedate->hour();
576 $ri{minute
} = $duedate->minute();
577 $ri{duedate
} = output_pref
($duedate);
578 my $patron = Koha
::Patrons
->find( $riborrowernumber{$_} );
579 unless ( $dropboxmode ) {
580 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, dt_from_string
()) == -1);
582 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, $dropboxdate) == -1);
584 $ri{patron
} = $patron,
585 $ri{borissuescount
} = $patron->checkouts->count;
588 $ri{borrowernumber
} = $riborrowernumber{$_};
591 my $item = Koha
::Items
->find({ barcode
=> $bar_code });
592 next unless $item; # FIXME The item has been deleted in the meantime,
593 # we could handle that better displaying a message in the template
595 my $biblio = $item->biblio;
596 # FIXME pass $item to the template and we are done here...
597 $ri{itembiblionumber
} = $biblio->biblionumber;
598 $ri{itemtitle
} = $biblio->title;
599 $ri{subtitle
} = $biblio->subtitle;
600 $ri{part_name
} = $biblio->part_name;
601 $ri{part_number
} = $biblio->part_number;
602 $ri{itemauthor
} = $biblio->author;
603 $ri{itemcallnumber
} = $item->itemcallnumber;
604 $ri{dateaccessioned
} = $item->dateaccessioned;
605 $ri{recordtype
} = $biblio->itemtype;
606 $ri{itemtype
} = $item->itype;
607 $ri{itemnote
} = $item->itemnotes;
608 $ri{itemnotes_nonpublic
} = $item->itemnotes_nonpublic;
609 $ri{ccode
} = $item->ccode;
610 $ri{enumchron
} = $item->enumchron;
611 $ri{itemnumber
} = $item->itemnumber;
612 $ri{barcode
} = $bar_code;
613 $ri{homebranch
} = $item->homebranch;
614 $ri{holdingbranch
} = $item->holdingbranch;
615 $ri{damaged
} = $item->damaged;
617 $ri{location
} = $item->location;
618 my $shelfcode = $ri{'location'};
619 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
630 errmsgloop
=> \
@errmsgloop,
631 exemptfine
=> $exemptfine,
632 dropboxmode
=> $dropboxmode,
633 dropboxdate
=> $dropboxdate,
634 forgivemanualholdsexpire
=> $forgivemanualholdsexpire,
635 overduecharges
=> $overduecharges,
636 AudioAlerts
=> C4
::Context
->preference("AudioAlerts"),
640 my $item_from_barcode = Koha
::Items
->find({barcode
=> $barcode }); # How many times do we fetch this item?!?
641 if ( $item_from_barcode ) {
642 $itemnumber = $item_from_barcode->itemnumber;
643 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
644 if ( $holdingBranch and $collectionBranch ) {
645 $holdingBranch //= '';
646 $collectionBranch //= $returnbranch;
647 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
649 collectionItemNeedsTransferred
=> 1,
650 collectionBranch
=> $collectionBranch,
657 $template->param( itemnumber
=> $itemnumber );
659 # Checking if there is a Fast Cataloging Framework
660 $template->param( fast_cataloging
=> 1 ) if Koha
::BiblioFrameworks
->find( 'FA' );
662 # actually print the page!
663 output_html_with_http_headers
$query, $cookie, $template->output;