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);
75 # Print a reserve slip on this page
76 if ( $query->param('print_slip') ) {
79 reserve_id
=> scalar $query->param('reserve_id'),
85 my $userenv = C4
::Context
->userenv;
86 my $userenv_branch = $userenv->{'branch'} // '';
87 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
89 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
91 # Set up the item stack ....
96 foreach ( $query->param ) {
109 my $barcode = $query->param("ri-$counter");
110 my $duedate = $query->param("dd-$counter");
111 my $borrowernumber = $query->param("bn-$counter");
114 # decode barcode ## Didn't we already decode them before passing them back last time??
115 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
116 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
118 ######################
119 #Are these lines still useful ?
120 $returneditems{$counter} = $barcode;
121 $riduedate{$counter} = $duedate;
122 $riborrowernumber{$counter} = $borrowernumber;
124 #######################
125 $input{counter
} = $counter;
126 $input{barcode
} = $barcode;
127 $input{duedate
} = $duedate;
128 $input{borrowernumber
} = $borrowernumber;
129 push( @inputloop, \
%input );
133 # Deal with the requests....
135 if ($query->param('WT-itemNumber')){
136 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
139 if ( $query->param('reserve_id') ) {
140 my $itemnumber = $query->param('itemnumber');
141 my $borrowernumber = $query->param('borrowernumber');
142 my $reserve_id = $query->param('reserve_id');
143 my $diffBranchReturned = $query->param('diffBranch');
144 my $cancel_reserve = $query->param('cancel_reserve');
145 # fix up item type for display
146 my $item = Koha
::Items
->find( $itemnumber );
147 my $biblio = $item->biblio;
149 if ( $cancel_reserve ) {
150 my $hold = Koha
::Holds
->find( $reserve_id );
152 $hold->cancel( { charge_cancel_fee
=> !$forgivemanualholdsexpire } );
155 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ?
$diffBranchReturned : undef;
156 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
157 # i.e., whether to apply waiting status
158 ModReserveAffect
( $itemnumber, $borrowernumber, $diffBranchSend, $reserve_id );
160 # check if we have other reserves for this document, if we have a return send the message of transfer
161 my ( $messages, $nextreservinfo ) = GetOtherReserves
($itemnumber);
163 my $patron = Koha
::Patrons
->find( $nextreservinfo );
164 my $name = $patron ?
$patron->surname . ", " . $patron->title . " " . $patron->firstname : '';
165 if ( $messages->{'transfert'} ) {
167 itemtitle
=> $biblio->title,
168 itemnumber
=> $item->itemnumber,
169 itembiblionumber
=> $biblio->biblionumber,
170 iteminfo
=> $biblio->author,
183 my $barcode = $query->param('barcode');
184 my $exemptfine = $query->param('exemptfine');
187 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
189 # silently prevent unauthorized operator from forgiving overdue
190 # fines by manually tweaking form parameters
193 my $dropboxmode = $query->param('dropboxmode');
194 my $dotransfer = $query->param('dotransfer');
195 my $canceltransfer = $query->param('canceltransfer');
196 my $dest = $query->param('dest');
197 #dropbox: get last open day (today - 1)
198 my $dropboxdate = Koha
::Checkouts
::calculate_dropbox_date
();
200 my $return_date_override = $query->param('return_date_override');
201 my $return_date_override_dt;
202 my $return_date_override_remember =
203 $query->param('return_date_override_remember');
204 if ($return_date_override) {
205 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
206 $return_date_override_dt = eval {dt_from_string
( $return_date_override ) };
207 if ( $return_date_override_dt ) {
208 # note that we've overriden the return date
209 $template->param( return_date_was_overriden
=> 1);
210 # Save the original format if we are remembering for this series
212 return_date_override
=> $return_date_override,
213 return_date_override_remember
=> 1
214 ) if ($return_date_override_remember);
216 $return_date_override =
217 DateTime
::Format
::MySQL
->format_datetime( $return_date_override_dt );
221 $return_date_override = q{};
226 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
227 my $transferitem = $query->param('transferitem');
228 my $tobranch = $query->param('tobranch');
229 my $trigger = $query->param('trigger');
230 ModItemTransfer
($transferitem, $userenv_branch, $tobranch, $trigger);
233 if ($canceltransfer){
234 $itemnumber=$query->param('itemnumber');
235 DeleteTransfer
($itemnumber);
237 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
240 $template->param( transfercancelled
=> 1);
244 # actually return book and prepare item table.....
247 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
248 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
249 my $item = Koha
::Items
->find({ barcode
=> $barcode });
252 $itemnumber = $item->itemnumber;
253 # Check if we should display a checkin message, based on the the item
254 # type of the checked in item
255 my $itemtype = Koha
::ItemTypes
->find( $item->effective_itemtype );
256 if ( $itemtype && $itemtype->checkinmsg ) {
258 checkinmsg
=> $itemtype->checkinmsg,
259 checkinmsgtype
=> $itemtype->checkinmsgtype,
263 # make sure return branch respects home branch circulation rules, default to homebranch
264 my $hbr = GetBranchItemRule
($item->homebranch, $itemtype ?
$itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
265 $returnbranch = $hbr ne 'noreturn' ?
$item->$hbr : $userenv_branch; # can be noreturn, homebranch or holdingbranch
267 my $materials = $item->materials;
268 my $descriptions = Koha
::AuthorisedValues
->get_description_by_koha_field({frameworkcode
=> '', kohafield
=>'items.materials', authorised_value
=> $materials });
269 $materials = $descriptions->{lib
} // $materials;
271 my $checkout = $item->checkout;
272 my $biblio = $item->biblio;
274 title
=> $biblio->title,
275 homebranch
=> $item->homebranch,
276 holdingbranch
=> $item->holdingbranch,
277 returnbranch
=> $returnbranch,
278 author
=> $biblio->author,
279 itembarcode
=> $item->barcode,
280 itemtype
=> $item->effective_itemtype,
281 ccode
=> $item->ccode,
282 itembiblionumber
=> $biblio->biblionumber,
283 biblionumber
=> $biblio->biblionumber,
284 additional_materials
=> $materials,
287 } # FIXME else we should not call AddReturn but set BadBarcode directly instead
295 my $return_date = $dropboxmode ?
$dropboxdate : $return_date_override_dt;
298 ( $returned, $messages, $issue, $borrower ) =
299 AddReturn
( $barcode, $userenv_branch, $exemptfine, $return_date );
302 my $time_now = dt_from_string
()->truncate( to
=> 'minute');
303 my $date_due_dt = dt_from_string
( $issue->date_due, 'sql' );
304 my $duedate = $date_due_dt->strftime('%Y-%m-%d %H:%M');
305 $returneditems{0} = $barcode;
306 $riborrowernumber{0} = $borrower->{'borrowernumber'};
307 $riduedate{0} = $duedate;
308 $input{borrowernumber
} = $borrower->{'borrowernumber'};
309 $input{duedate
} = $duedate;
310 unless ( $dropboxmode ) {
311 $input{return_overdue
} = 1 if (DateTime
->compare($date_due_dt, dt_from_string
()) == -1);
313 $input{return_overdue
} = 1 if (DateTime
->compare($date_due_dt, $dropboxdate) == -1);
315 push( @inputloop, \
%input );
317 if ( C4
::Context
->preference("FineNotifyAtCheckin") ) {
318 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
319 my $balance = $patron->account->balance;
322 $template->param( fines
=> sprintf("%.2f", $balance) );
323 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
327 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
328 #Check for waiting holds
329 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
330 my $waiting_holds = $patron->holds->search({ found
=> 'W', branchcode
=> $userenv_branch })->count;
331 if ($waiting_holds > 0) {
333 waiting_holds
=> $waiting_holds,
334 holdsborrowernumber
=> $borrower->{'borrowernumber'},
335 holdsfirstname
=> $borrower->{'firstname'},
336 holdssurname
=> $borrower->{'surname'},
340 } elsif ( C4
::Context
->preference('ShowAllCheckins') and !$messages->{'BadBarcode'} ) {
342 $returneditems{0} = $barcode;
344 push( @inputloop, \
%input );
346 $template->param( privacy
=> $borrower->{privacy
} );
348 $template->param( inputloop
=> \
@inputloop );
355 # new op dev : we check if the document must be returned to his homebranch directly,
356 # if the document is transferred, we have warning message .
358 if ( $messages->{'WasTransfered'} ) {
362 itemnumber
=> $itemnumber,
366 if ( $messages->{'NeedsTransfer'} ){
369 needstransfer
=> $messages->{'NeedsTransfer'},
370 trigger
=> $messages->{'TransferTrigger'},
371 itemnumber
=> $itemnumber,
375 if ( $messages->{'Wrongbranch'} ){
378 rightbranch
=> $messages->{'Wrongbranch'}->{'Rightbranch'},
382 # case of wrong transfert, if the document wasn't transferred to the right library (according to branchtransfer (tobranch) BDD)
384 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
387 TransferWaitingAt
=> $messages->{'WrongTransfer'},
388 WrongTransferItem
=> $messages->{'WrongTransferItem'},
389 itemnumber
=> $itemnumber,
392 my $reserve = $messages->{'ResFound'};
394 my $patron = Koha
::Patrons
->find( $reserve->{'borrowernumber'} );
395 my $name = $patron->surname . ", " . $patron->title . " " . $patron->firstname;
401 wtransfertFrom
=> $userenv_branch,
406 # reserve found and item arrived at the expected branch
408 if ( $messages->{'ResFound'}) {
409 my $reserve = $messages->{'ResFound'};
410 my $patron = Koha
::Patrons
->find( $reserve->{borrowernumber
} );
411 my $holdmsgpreferences = C4
::Members
::Messaging
::GetMessagingPreferences
( { borrowernumber
=> $reserve->{'borrowernumber'}, message_name
=> 'Hold_Filled' } );
412 my $branchCheck = ( $userenv_branch eq $reserve->{branchcode
} );
413 if ( $reserve->{'ResFound'} eq "Reserved" && C4
::Context
->preference('HoldsAutoFill') ) {
414 my $item = Koha
::Items
->find( $itemnumber );
415 my $biblio = $item->biblio;
417 my $diffBranchSend = !$branchCheck ?
$reserve->{branchcode
} : undef;
418 ModReserveAffect
( $reserve->{itemnumber
}, $reserve->{borrowernumber
}, $diffBranchSend, $reserve->{reserve_id
} );
419 my ( $messages, $nextreservinfo ) = GetOtherReserves
($reserve->{itemnumber
});
422 hold_auto_filled
=> 1,
423 print_slip
=> C4
::Context
->preference('HoldsAutoFillPrintSlip'),
424 reserve_id
=> $nextreservinfo->{reserve_id
},
425 itemnumber
=> $itemnumber,
428 if ( $messages->{'transfert'} ) {
430 itemtitle
=> $biblio->title,
431 itembiblionumber
=> $biblio->biblionumber,
432 iteminfo
=> $biblio->author,
437 elsif ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
438 if ( $reserve->{'ResFound'} eq "Waiting" ) {
440 waiting
=> $branchCheck ?
1 : undef,
442 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
444 intransit
=> $branchCheck ?
undef : 1,
445 transfertodo
=> $branchCheck ?
undef : 1,
446 reserve_id
=> $reserve->{reserve_id
},
448 itemnumber
=> $itemnumber,
452 } # else { ; } # error?
454 # same params for Waiting or Reserved
459 destbranch
=> $reserve->{'branchcode'},
460 reservenotes
=> $reserve->{'reservenotes'},
461 reserve_id
=> $reserve->{reserve_id
},
462 bormessagepref
=> $holdmsgpreferences->{'transports'},
468 foreach my $code ( keys %$messages ) {
470 my $exit_required_p = 0;
471 if ( $code eq 'BadBarcode' ) {
472 $err{badbarcode
} = 1;
473 $err{msg
} = $messages->{'BadBarcode'};
475 elsif ( $code eq 'NotIssued' ) {
479 elsif ( $code eq 'LocalUse' ) {
482 elsif ( $code eq 'WasLost' ) {
484 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfLostItems");
486 elsif ( $code eq 'LostItemFeeRefunded' ) {
487 $template->param( LostItemFeeRefunded
=> 1 );
489 elsif ( $code eq 'ResFound' ) {
490 ; # FIXME... anything to do here?
492 elsif ( $code eq 'WasReturned' ) {
493 ; # FIXME... anything to do here?
495 elsif ( $code eq 'WasTransfered' ) {
496 ; # FIXME... anything to do here?
498 elsif ( $code eq 'withdrawn' ) {
500 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
502 elsif ( $code eq 'WrongTransfer' ) {
503 ; # FIXME... anything to do here?
505 elsif ( $code eq 'WrongTransferItem' ) {
506 ; # FIXME... anything to do here?
508 elsif ( $code eq 'NeedsTransfer' ) {
510 elsif ( $code eq 'TransferTrigger' ) {
511 ; # Handled alongside NeedsTransfer
513 elsif ( $code eq 'Wrongbranch' ) {
515 elsif ( $code eq 'Debarred' ) {
516 $err{debarred
} = $messages->{'Debarred'};
517 $err{debarcardnumber
} = $borrower->{cardnumber
};
518 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
519 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
521 elsif ( $code eq 'PrevDebarred' ) {
522 $err{prevdebarred
} = $messages->{'PrevDebarred'};
524 elsif ( $code eq 'ForeverDebarred' ) {
525 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
527 elsif ( $code eq 'ItemLocationUpdated' ) {
528 $err{ItemLocationUpdated
} = $messages->{ItemLocationUpdated
};
530 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
531 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
533 elsif ( $code eq 'DataCorrupted' ) {
534 $err{data_corrupted
} = 1;
536 elsif ( $code eq 'ReturnClaims' ) {
537 $template->param( ReturnClaims
=> $messages->{ReturnClaims
} );
539 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
540 # This forces the issue of staying in sync w/ Circulation.pm
543 push( @errmsgloop, \
%err );
545 last if $exit_required_p;
547 $template->param( errmsgloop
=> \
@errmsgloop );
549 #set up so only the last 8 returned items display (make for faster loading pages)
550 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
554 { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.location' } ) };
555 foreach ( sort { $a <=> $b } keys %returneditems ) {
557 if ( $count++ < $returned_counter ) {
558 my $bar_code = $returneditems{$_};
559 if ($riduedate{$_}) {
560 my $duedate = dt_from_string
( $riduedate{$_}, 'sql');
561 $ri{year
} = $duedate->year();
562 $ri{month
} = $duedate->month();
563 $ri{day
} = $duedate->day();
564 $ri{hour
} = $duedate->hour();
565 $ri{minute
} = $duedate->minute();
566 $ri{duedate
} = output_pref
($duedate);
567 my $patron = Koha
::Patrons
->find( $riborrowernumber{$_} );
568 unless ( $dropboxmode ) {
569 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, dt_from_string
()) == -1);
571 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, $dropboxdate) == -1);
573 $ri{patron
} = $patron,
574 $ri{borissuescount
} = $patron->checkouts->count;
577 $ri{borrowernumber
} = $riborrowernumber{$_};
580 my $item = Koha
::Items
->find({ barcode
=> $bar_code });
581 next unless $item; # FIXME The item has been deleted in the meantime,
582 # we could handle that better displaying a message in the template
584 my $biblio = $item->biblio;
585 # FIXME pass $item to the template and we are done here...
586 $ri{itembiblionumber
} = $biblio->biblionumber;
587 $ri{itemtitle
} = $biblio->title;
588 $ri{subtitle
} = $biblio->subtitle;
589 $ri{part_name
} = $biblio->part_name;
590 $ri{part_number
} = $biblio->part_number;
591 $ri{itemauthor
} = $biblio->author;
592 $ri{itemcallnumber
} = $item->itemcallnumber;
593 $ri{dateaccessioned
} = $item->dateaccessioned;
594 $ri{recordtype
} = $biblio->itemtype;
595 $ri{itemtype
} = $item->itype;
596 $ri{itemnote
} = $item->itemnotes;
597 $ri{itemnotes_nonpublic
} = $item->itemnotes_nonpublic;
598 $ri{ccode
} = $item->ccode;
599 $ri{enumchron
} = $item->enumchron;
600 $ri{itemnumber
} = $item->itemnumber;
601 $ri{barcode
} = $bar_code;
602 $ri{homebranch
} = $item->homebranch;
603 $ri{holdingbranch
} = $item->holdingbranch;
605 $ri{location
} = $item->location;
606 my $shelfcode = $ri{'location'};
607 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
618 errmsgloop
=> \
@errmsgloop,
619 exemptfine
=> $exemptfine,
620 dropboxmode
=> $dropboxmode,
621 dropboxdate
=> $dropboxdate,
622 forgivemanualholdsexpire
=> $forgivemanualholdsexpire,
623 overduecharges
=> $overduecharges,
624 AudioAlerts
=> C4
::Context
->preference("AudioAlerts"),
628 my $item_from_barcode = Koha
::Items
->find({barcode
=> $barcode }); # How many times do we fetch this item?!?
629 if ( $item_from_barcode ) {
630 $itemnumber = $item_from_barcode->itemnumber;
631 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
632 if ( $holdingBranch and $collectionBranch ) {
633 $holdingBranch //= '';
634 $collectionBranch //= $returnbranch;
635 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
637 collectionItemNeedsTransferred
=> 1,
638 collectionBranch
=> $collectionBranch,
639 itemnumber
=> $itemnumber,
646 # Checking if there is a Fast Cataloging Framework
647 $template->param( fast_cataloging
=> 1 ) if Koha
::BiblioFrameworks
->find( 'FA' );
649 # actually print the page!
650 output_html_with_http_headers
$query, $cookie, $template->output;