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",
69 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
73 my $sessionID = $query->cookie("CGISESSID");
74 my $session = get_session
($sessionID);
76 # Print a reserve slip on this page
77 if ( $query->param('print_slip') ) {
80 borrowernumber
=> scalar $query->param('borrowernumber'), # FIXME We should send a Koha::Patron and raise an error if not exist.
81 biblionumber
=> scalar $query->param('biblionumber'),
82 itemnumber
=> scalar $query->param('itemnumber'),
88 my $userenv = C4
::Context
->userenv;
89 my $userenv_branch = $userenv->{'branch'} // '';
90 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
92 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
94 # Set up the item stack ....
99 foreach ( $query->param ) {
112 my $barcode = $query->param("ri-$counter");
113 my $duedate = $query->param("dd-$counter");
114 my $borrowernumber = $query->param("bn-$counter");
117 # decode barcode ## Didn't we already decode them before passing them back last time??
118 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
119 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
121 ######################
122 #Are these lines still useful ?
123 $returneditems{$counter} = $barcode;
124 $riduedate{$counter} = $duedate;
125 $riborrowernumber{$counter} = $borrowernumber;
127 #######################
128 $input{counter
} = $counter;
129 $input{barcode
} = $barcode;
130 $input{duedate
} = $duedate;
131 $input{borrowernumber
} = $borrowernumber;
132 push( @inputloop, \
%input );
136 # Deal with the requests....
138 if ($query->param('WT-itemNumber')){
139 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
142 if ( $query->param('reserve_id') ) {
143 my $itemnumber = $query->param('itemnumber');
144 my $borrowernumber = $query->param('borrowernumber');
145 my $reserve_id = $query->param('reserve_id');
146 my $diffBranchReturned = $query->param('diffBranch');
147 my $cancel_reserve = $query->param('cancel_reserve');
148 # fix up item type for display
149 my $item = Koha
::Items
->find( $itemnumber );
150 my $biblio = $item->biblio;
152 if ( $cancel_reserve ) {
153 my $hold = Koha
::Holds
->find( $reserve_id );
155 $hold->cancel( { 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
( $itemnumber, $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
($itemnumber);
166 my $patron = Koha
::Patrons
->find( $nextreservinfo );
167 my $name = $patron ?
$patron->surname . ", " . $patron->title . " " . $patron->firstname : '';
168 if ( $messages->{'transfert'} ) {
170 itemtitle
=> $biblio->title,
171 itemnumber
=> $item->itemnumber,
172 itembiblionumber
=> $biblio->biblionumber,
173 iteminfo
=> $biblio->author,
186 my $barcode = $query->param('barcode');
187 my $exemptfine = $query->param('exemptfine');
190 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
192 # silently prevent unauthorized operator from forgiving overdue
193 # fines by manually tweaking form parameters
196 my $dropboxmode = $query->param('dropboxmode');
197 my $dotransfer = $query->param('dotransfer');
198 my $canceltransfer = $query->param('canceltransfer');
199 my $dest = $query->param('dest');
200 #dropbox: get last open day (today - 1)
201 my $dropboxdate = Koha
::Checkouts
::calculate_dropbox_date
();
203 my $return_date_override = $query->param('return_date_override');
204 my $return_date_override_dt;
205 my $return_date_override_remember =
206 $query->param('return_date_override_remember');
207 if ($return_date_override) {
208 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
209 $return_date_override_dt = eval {dt_from_string
( $return_date_override ) };
210 if ( $return_date_override_dt ) {
211 # note that we've overriden the return date
212 $template->param( return_date_was_overriden
=> 1);
213 # Save the original format if we are remembering for this series
215 return_date_override
=> $return_date_override,
216 return_date_override_remember
=> 1
217 ) if ($return_date_override_remember);
219 $return_date_override =
220 DateTime
::Format
::MySQL
->format_datetime( $return_date_override_dt );
224 $return_date_override = q{};
229 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
230 my $transferitem = $query->param('transferitem');
231 my $tobranch = $query->param('tobranch');
232 my $trigger = $query->param('trigger');
233 ModItemTransfer
($transferitem, $userenv_branch, $tobranch, $trigger);
236 if ($canceltransfer){
237 $itemnumber=$query->param('itemnumber');
238 DeleteTransfer
($itemnumber);
240 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
243 $template->param( transfercancelled
=> 1);
247 # actually return book and prepare item table.....
250 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
251 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
252 my $item = Koha
::Items
->find({ barcode
=> $barcode });
255 $itemnumber = $item->itemnumber;
256 # Check if we should display a checkin message, based on the the item
257 # type of the checked in item
258 my $itemtype = Koha
::ItemTypes
->find( $item->effective_itemtype );
259 if ( $itemtype && $itemtype->checkinmsg ) {
261 checkinmsg
=> $itemtype->checkinmsg,
262 checkinmsgtype
=> $itemtype->checkinmsgtype,
266 # make sure return branch respects home branch circulation rules, default to homebranch
267 my $hbr = GetBranchItemRule
($item->homebranch, $itemtype ?
$itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
268 $returnbranch = $hbr ne 'noreturn' ?
$item->$hbr : $userenv_branch; # can be noreturn, homebranch or holdingbranch
270 my $materials = $item->materials;
271 my $descriptions = Koha
::AuthorisedValues
->get_description_by_koha_field({frameworkcode
=> '', kohafield
=>'items.materials', authorised_value
=> $materials });
272 $materials = $descriptions->{lib
} // $materials;
274 my $checkout = $item->checkout;
275 my $biblio = $item->biblio;
277 title
=> $biblio->title,
278 homebranch
=> $item->homebranch,
279 holdingbranch
=> $item->holdingbranch,
280 returnbranch
=> $returnbranch,
281 author
=> $biblio->author,
282 itembarcode
=> $item->barcode,
283 itemtype
=> $item->effective_itemtype,
284 ccode
=> $item->ccode,
285 itembiblionumber
=> $biblio->biblionumber,
286 biblionumber
=> $biblio->biblionumber,
287 additional_materials
=> $materials,
290 } # FIXME else we should not call AddReturn but set BadBarcode directly instead
298 my $return_date = $dropboxmode ?
$dropboxdate : $return_date_override_dt;
301 ( $returned, $messages, $issue, $borrower ) =
302 AddReturn
( $barcode, $userenv_branch, $exemptfine, $return_date );
305 my $time_now = dt_from_string
()->truncate( to
=> 'minute');
306 my $date_due_dt = dt_from_string
( $issue->date_due, 'sql' );
307 my $duedate = $date_due_dt->strftime('%Y-%m-%d %H:%M');
308 $returneditems{0} = $barcode;
309 $riborrowernumber{0} = $borrower->{'borrowernumber'};
310 $riduedate{0} = $duedate;
311 $input{borrowernumber
} = $borrower->{'borrowernumber'};
312 $input{duedate
} = $duedate;
313 unless ( $dropboxmode ) {
314 $input{return_overdue
} = 1 if (DateTime
->compare($date_due_dt, dt_from_string
()) == -1);
316 $input{return_overdue
} = 1 if (DateTime
->compare($date_due_dt, $dropboxdate) == -1);
318 push( @inputloop, \
%input );
320 if ( C4
::Context
->preference("FineNotifyAtCheckin") ) {
321 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
322 my $balance = $patron->account->balance;
325 $template->param( fines
=> sprintf("%.2f", $balance) );
326 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
330 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
331 #Check for waiting holds
332 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
333 my $waiting_holds = $patron->holds->search({ found
=> 'W', branchcode
=> $userenv_branch })->count;
334 if ($waiting_holds > 0) {
336 waiting_holds
=> $waiting_holds,
337 holdsborrowernumber
=> $borrower->{'borrowernumber'},
338 holdsfirstname
=> $borrower->{'firstname'},
339 holdssurname
=> $borrower->{'surname'},
343 } elsif ( C4
::Context
->preference('ShowAllCheckins') and !$messages->{'BadBarcode'} ) {
345 $returneditems{0} = $barcode;
347 push( @inputloop, \
%input );
349 $template->param( privacy
=> $borrower->{privacy
} );
351 $template->param( inputloop
=> \
@inputloop );
357 # new op dev : we check if the document must be returned to his homebranch directly,
358 # if the document is transferred, we have warning message .
360 if ( $messages->{'WasTransfered'} ) {
364 itemnumber
=> $itemnumber,
368 if ( $messages->{'NeedsTransfer'} ){
371 needstransfer
=> $messages->{'NeedsTransfer'},
372 trigger
=> $messages->{'TransferTrigger'},
373 itemnumber
=> $itemnumber,
377 if ( $messages->{'Wrongbranch'} ){
380 rightbranch
=> $messages->{'Wrongbranch'}->{'Rightbranch'},
384 # case of wrong transfert, if the document wasn't transferred to the right library (according to branchtransfer (tobranch) BDD)
386 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
389 TransferWaitingAt
=> $messages->{'WrongTransfer'},
390 WrongTransferItem
=> $messages->{'WrongTransferItem'},
391 itemnumber
=> $itemnumber,
394 my $reserve = $messages->{'ResFound'};
396 my $patron = Koha
::Patrons
->find( $reserve->{'borrowernumber'} );
397 my $name = $patron->surname . ", " . $patron->title . " " . $patron->firstname;
403 wtransfertFrom
=> $userenv_branch,
408 # reserve found and item arrived at the expected branch
410 if ( $messages->{'ResFound'}) {
411 my $reserve = $messages->{'ResFound'};
412 my $patron = Koha
::Patrons
->find( $reserve->{borrowernumber
} );
413 my $holdmsgpreferences = C4
::Members
::Messaging
::GetMessagingPreferences
( { borrowernumber
=> $reserve->{'borrowernumber'}, message_name
=> 'Hold_Filled' } );
414 my $branchCheck = ( $userenv_branch eq $reserve->{branchcode
} );
415 if ( $reserve->{'ResFound'} eq "Reserved" && C4
::Context
->preference('HoldsAutoFill') ) {
416 my $item = Koha
::Items
->find( $itemnumber );
417 my $biblio = $item->biblio;
419 my $diffBranchSend = !$branchCheck ?
$reserve->{branchcode
} : undef;
420 ModReserveAffect
( $reserve->{itemnumber
}, $reserve->{borrowernumber
}, $diffBranchSend, $reserve->{reserve_id
} );
421 my ( $messages, $nextreservinfo ) = GetOtherReserves
($reserve->{itemnumber
});
423 my $patron = Koha
::Patrons
->find( $nextreservinfo );
426 hold_auto_filled
=> 1,
427 print_slip
=> C4
::Context
->preference('HoldsAutoFillPrintSlip'),
429 borrowernumber
=> $patron->id,
430 biblionumber
=> $biblio->id,
433 if ( $messages->{'transfert'} ) {
435 itemtitle
=> $biblio->title,
436 itemnumber
=> $item->itemnumber,
437 itembiblionumber
=> $biblio->biblionumber,
438 iteminfo
=> $biblio->author,
443 elsif ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
444 if ( $reserve->{'ResFound'} eq "Waiting" ) {
446 waiting
=> $branchCheck ?
1 : undef,
448 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
450 intransit
=> $branchCheck ?
undef : 1,
451 transfertodo
=> $branchCheck ?
undef : 1,
452 reserve_id
=> $reserve->{reserve_id
},
457 } # else { ; } # error?
459 # same params for Waiting or Reserved
464 destbranch
=> $reserve->{'branchcode'},
465 itemnumber
=> $reserve->{'itemnumber'},
466 reservenotes
=> $reserve->{'reservenotes'},
467 reserve_id
=> $reserve->{reserve_id
},
468 bormessagepref
=> $holdmsgpreferences->{'transports'},
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' ) {
485 elsif ( $code eq 'LocalUse' ) {
488 elsif ( $code eq 'WasLost' ) {
490 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfLostItems");
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 'WrongTransfer' ) {
509 ; # FIXME... anything to do here?
511 elsif ( $code eq 'WrongTransferItem' ) {
512 ; # FIXME... anything to do here?
514 elsif ( $code eq 'NeedsTransfer' ) {
516 elsif ( $code eq 'TransferTrigger' ) {
517 ; # Handled alongside NeedsTransfer
519 elsif ( $code eq 'Wrongbranch' ) {
521 elsif ( $code eq 'Debarred' ) {
522 $err{debarred
} = $messages->{'Debarred'};
523 $err{debarcardnumber
} = $borrower->{cardnumber
};
524 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
525 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
527 elsif ( $code eq 'PrevDebarred' ) {
528 $err{prevdebarred
} = $messages->{'PrevDebarred'};
530 elsif ( $code eq 'ForeverDebarred' ) {
531 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
533 elsif ( $code eq 'ItemLocationUpdated' ) {
534 $err{ItemLocationUpdated
} = $messages->{ItemLocationUpdated
};
536 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
537 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
539 elsif ( $code eq 'DataCorrupted' ) {
540 $err{data_corrupted
} = 1;
542 elsif ( $code eq 'ReturnClaims' ) {
543 $template->param( ReturnClaims
=> $messages->{ReturnClaims
} );
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 $patron = Koha
::Patrons
->find( $riborrowernumber{$_} );
574 unless ( $dropboxmode ) {
575 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, dt_from_string
()) == -1);
577 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, $dropboxdate) == -1);
579 $ri{patron
} = $patron,
580 $ri{borissuescount
} = $patron->checkouts->count;
583 $ri{borrowernumber
} = $riborrowernumber{$_};
586 my $item = Koha
::Items
->find({ barcode
=> $bar_code });
587 next unless $item; # FIXME The item has been deleted in the meantime,
588 # we could handle that better displaying a message in the template
590 my $biblio = $item->biblio;
591 # FIXME pass $item to the template and we are done here...
592 $ri{itembiblionumber
} = $biblio->biblionumber;
593 $ri{itemtitle
} = $biblio->title;
594 $ri{subtitle
} = $biblio->subtitle;
595 $ri{part_name
} = $biblio->part_name;
596 $ri{part_number
} = $biblio->part_number;
597 $ri{itemauthor
} = $biblio->author;
598 $ri{itemcallnumber
} = $item->itemcallnumber;
599 $ri{dateaccessioned
} = $item->dateaccessioned;
600 $ri{recordtype
} = $biblio->itemtype;
601 $ri{itemtype
} = $item->itype;
602 $ri{itemnote
} = $item->itemnotes;
603 $ri{itemnotes_nonpublic
} = $item->itemnotes_nonpublic;
604 $ri{ccode
} = $item->ccode;
605 $ri{enumchron
} = $item->enumchron;
606 $ri{itemnumber
} = $item->itemnumber;
607 $ri{barcode
} = $bar_code;
608 $ri{homebranch
} = $item->homebranch;
609 $ri{holdingbranch
} = $item->holdingbranch;
611 $ri{location
} = $item->location;
612 my $shelfcode = $ri{'location'};
613 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
624 errmsgloop
=> \
@errmsgloop,
625 exemptfine
=> $exemptfine,
626 dropboxmode
=> $dropboxmode,
627 dropboxdate
=> $dropboxdate,
628 forgivemanualholdsexpire
=> $forgivemanualholdsexpire,
629 overduecharges
=> $overduecharges,
630 AudioAlerts
=> C4
::Context
->preference("AudioAlerts"),
634 my $item_from_barcode = Koha
::Items
->find({barcode
=> $barcode }); # How many times do we fetch this item?!?
635 if ( $item_from_barcode ) {
636 $itemnumber = $item_from_barcode->itemnumber;
637 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
638 if ( $holdingBranch and $collectionBranch ) {
639 $holdingBranch //= '';
640 $collectionBranch //= $returnbranch;
641 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
643 collectionItemNeedsTransferred
=> 1,
644 collectionBranch
=> $collectionBranch,
645 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;