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
::Members
::Messaging
;
46 use C4
::Koha
; # FIXME : is it still useful ?
47 use C4
::RotatingCollections
;
48 use Koha
::AuthorisedValues
;
51 use Koha
::BiblioFrameworks
;
59 my ( $template, $librarian, $cookie, $flags ) = get_template_and_user
(
61 template_name
=> "circ/returns.tt",
65 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
69 my $sessionID = $query->cookie("CGISESSID");
70 my $session = get_session
($sessionID);
71 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
72 # no branch set we can't return
73 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
77 # Print a reserve slip on this page
78 if ( $query->param('print_slip') ) {
81 borrowernumber
=> scalar $query->param('borrowernumber'),
82 biblionumber
=> scalar $query->param('biblionumber'),
88 my $printers = GetPrinters
();
89 my $userenv = C4
::Context
->userenv;
90 my $userenv_branch = $userenv->{'branch'} // '';
91 my $printer = $userenv->{'branchprinter'} // '';
92 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
94 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
96 # Some code to handle the error if there is no branch or printer setting.....
99 # Set up the item stack ....
102 my %riborrowernumber;
104 foreach ( $query->param ) {
117 my $barcode = $query->param("ri-$counter");
118 my $duedate = $query->param("dd-$counter");
119 my $borrowernumber = $query->param("bn-$counter");
122 # decode barcode ## Didn't we already decode them before passing them back last time??
123 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
124 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
126 ######################
127 #Are these lines still useful ?
128 $returneditems{$counter} = $barcode;
129 $riduedate{$counter} = $duedate;
130 $riborrowernumber{$counter} = $borrowernumber;
132 #######################
133 $input{counter
} = $counter;
134 $input{barcode
} = $barcode;
135 $input{duedate
} = $duedate;
136 $input{borrowernumber
} = $borrowernumber;
137 push( @inputloop, \
%input );
141 # Deal with the requests....
143 if ($query->param('WT-itemNumber')){
144 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
147 if ( $query->param('reserve_id') ) {
148 my $itemnumber = $query->param('itemnumber');
149 my $borrowernumber = $query->param('borrowernumber');
150 my $reserve_id = $query->param('reserve_id');
151 my $diffBranchReturned = $query->param('diffBranch');
152 my $cancel_reserve = $query->param('cancel_reserve');
153 # fix up item type for display
154 my $item = Koha
::Items
->find( $itemnumber );
155 my $biblio = $item->biblio;
157 if ( $cancel_reserve ) {
158 my $hold = Koha
::Holds
->find( $reserve_id );
160 $hold->cancel( { charge_cancel_fee
=> !$forgivemanualholdsexpire } );
163 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ?
$diffBranchReturned : undef;
164 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
165 # i.e., whether to apply waiting status
166 ModReserveAffect
( $itemnumber, $borrowernumber, $diffBranchSend, $reserve_id );
168 # check if we have other reserves for this document, if we have a return send the message of transfer
169 my ( $messages, $nextreservinfo ) = GetOtherReserves
($itemnumber);
171 my $patron = Koha
::Patrons
->find( $nextreservinfo );
172 my $name = $patron ?
$patron->surname . ", " . $patron->title . " " . $patron->firstname : '';
173 if ( $messages->{'transfert'} ) {
175 itemtitle
=> $biblio->title,
176 itemnumber
=> $item->itemnumber,
177 itembiblionumber
=> $biblio->biblionumber,
178 iteminfo
=> $biblio->author,
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 my $item = Koha
::Items
->find({ barcode
=> $barcode });
260 $itemnumber = $item->itemnumber;
261 # Check if we should display a checkin message, based on the the item
262 # type of the checked in item
263 my $itemtype = Koha
::ItemTypes
->find( $item->effective_itemtype );
264 if ( $itemtype && $itemtype->checkinmsg ) {
266 checkinmsg
=> $itemtype->checkinmsg,
267 checkinmsgtype
=> $itemtype->checkinmsgtype,
271 # make sure return branch respects home branch circulation rules, default to homebranch
272 my $hbr = GetBranchItemRule
($item->homebranch, $itemtype ?
$itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
273 $returnbranch = $hbr ne 'noreturn' ?
$item->$hbr : $userenv_branch; # can be noreturn, homebranch or holdingbranch
275 my $materials = $item->materials;
276 my $descriptions = Koha
::AuthorisedValues
->get_description_by_koha_field({frameworkcode
=> '', kohafield
=>'items.materials', authorised_value
=> $materials });
277 $materials = $descriptions->{lib
} // $materials;
279 my $checkout = $item->checkout;
280 my $biblio = $item->biblio;
282 title
=> $biblio->title,
283 homebranch
=> $item->homebranch,
284 holdingbranch
=> $item->holdingbranch,
285 returnbranch
=> $returnbranch,
286 author
=> $biblio->author,
287 itembarcode
=> $item->barcode,
288 itemtype
=> $item->effective_itemtype,
289 ccode
=> $item->ccode,
290 itembiblionumber
=> $biblio->biblionumber,
291 biblionumber
=> $biblio->biblionumber,
292 additional_materials
=> $materials,
295 } # FIXME else we should not call AddReturn but set BadBarcode directly instead
305 ( $returned, $messages, $issue, $borrower ) =
306 AddReturn
( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override, $dropboxdate );
309 my $time_now = DateTime
->now( time_zone
=> C4
::Context
->tz )->truncate( to
=> 'minute');
310 my $date_due_dt = dt_from_string
( $issue->date_due, 'sql' );
311 my $duedate = $date_due_dt->strftime('%Y-%m-%d %H:%M');
312 $returneditems{0} = $barcode;
313 $riborrowernumber{0} = $borrower->{'borrowernumber'};
314 $riduedate{0} = $duedate;
315 $input{borrowernumber
} = $borrower->{'borrowernumber'};
316 $input{duedate
} = $duedate;
317 unless ( $dropboxmode ) {
318 $input{return_overdue
} = 1 if (DateTime
->compare($date_due_dt, DateTime
->now()) == -1);
320 $input{return_overdue
} = 1 if (DateTime
->compare($date_due_dt, $dropboxdate) == -1);
322 push( @inputloop, \
%input );
324 if ( C4
::Context
->preference("FineNotifyAtCheckin") ) {
325 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
326 my $balance = $patron->account->balance;
329 $template->param( fines
=> sprintf("%.2f", $balance) );
330 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
334 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
335 #Check for waiting holds
336 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
337 my $waiting_holds = $patron->holds->search({ found
=> 'W', branchcode
=> $userenv_branch })->count;
338 if ($waiting_holds > 0) {
340 waiting_holds
=> $waiting_holds,
341 holdsborrowernumber
=> $borrower->{'borrowernumber'},
342 holdsfirstname
=> $borrower->{'firstname'},
343 holdssurname
=> $borrower->{'surname'},
347 } elsif ( C4
::Context
->preference('ShowAllCheckins') and !$messages->{'BadBarcode'} ) {
349 $returneditems{0} = $barcode;
351 push( @inputloop, \
%input );
353 $template->param( privacy
=> $borrower->{privacy
} );
355 $template->param( inputloop
=> \
@inputloop );
361 # new op dev : we check if the document must be returned to his homebranch directly,
362 # if the document is transfered, we have warning message .
364 if ( $messages->{'WasTransfered'} ) {
368 itemnumber
=> $itemnumber,
372 if ( $messages->{'NeedsTransfer'} ){
375 needstransfer
=> $messages->{'NeedsTransfer'},
376 itemnumber
=> $itemnumber,
380 if ( $messages->{'Wrongbranch'} ){
383 rightbranch
=> $messages->{'Wrongbranch'}->{'Rightbranch'},
387 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
389 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
392 TransferWaitingAt
=> $messages->{'WrongTransfer'},
393 WrongTransferItem
=> $messages->{'WrongTransferItem'},
394 itemnumber
=> $itemnumber,
397 my $reserve = $messages->{'ResFound'};
399 my $patron = Koha
::Patrons
->find( $reserve->{'borrowernumber'} );
400 my $name = $patron->surname . ", " . $patron->title . " " . $patron->firstname;
406 wtransfertFrom
=> $userenv_branch,
411 # reserve found and item arrived at the expected branch
413 if ( $messages->{'ResFound'}) {
414 my $reserve = $messages->{'ResFound'};
415 my $patron = Koha
::Patrons
->find( $reserve->{borrowernumber
} );
416 my $holdmsgpreferences = C4
::Members
::Messaging
::GetMessagingPreferences
( { borrowernumber
=> $reserve->{'borrowernumber'}, message_name
=> 'Hold_Filled' } );
417 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
418 if ( $reserve->{'ResFound'} eq "Waiting" ) {
420 waiting
=> ($userenv_branch eq $reserve->{'branchcode'} ?
1 : 0 ),
422 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
424 intransit
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
425 transfertodo
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
426 reserve_id
=> $reserve->{reserve_id
},
431 # same params for Waiting or Reserved
433 # FIXME The full patron object should be passed to the template
437 destbranch
=> $reserve->{'branchcode'},
438 itemnumber
=> $reserve->{'itemnumber'},
439 reservenotes
=> $reserve->{'reservenotes'},
440 reserve_id
=> $reserve->{reserve_id
},
441 bormessagepref
=> $holdmsgpreferences->{'transports'},
443 } # else { ; } # error?
448 foreach my $code ( keys %$messages ) {
450 my $exit_required_p = 0;
451 if ( $code eq 'BadBarcode' ) {
452 $err{badbarcode
} = 1;
453 $err{msg
} = $messages->{'BadBarcode'};
455 elsif ( $code eq 'NotIssued' ) {
459 elsif ( $code eq 'LocalUse' ) {
462 elsif ( $code eq 'WasLost' ) {
464 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfLostItems");
466 elsif ( $code eq 'LostItemFeeRefunded' ) {
467 $template->param( LostItemFeeRefunded
=> 1 );
469 elsif ( $code eq 'ResFound' ) {
470 ; # FIXME... anything to do here?
472 elsif ( $code eq 'WasReturned' ) {
473 ; # FIXME... anything to do here?
475 elsif ( $code eq 'WasTransfered' ) {
476 ; # FIXME... anything to do here?
478 elsif ( $code eq 'withdrawn' ) {
480 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
482 elsif ( $code eq 'WrongTransfer' ) {
483 ; # FIXME... anything to do here?
485 elsif ( $code eq 'WrongTransferItem' ) {
486 ; # FIXME... anything to do here?
488 elsif ( $code eq 'NeedsTransfer' ) {
490 elsif ( $code eq 'Wrongbranch' ) {
492 elsif ( $code eq 'Debarred' ) {
493 $err{debarred
} = $messages->{'Debarred'};
494 $err{debarcardnumber
} = $borrower->{cardnumber
};
495 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
496 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
498 elsif ( $code eq 'PrevDebarred' ) {
499 $err{prevdebarred
} = $messages->{'PrevDebarred'};
501 elsif ( $code eq 'ForeverDebarred' ) {
502 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
504 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
505 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
507 elsif ( $code eq 'DataCorrupted' ) {
508 $err{data_corrupted
} = 1;
511 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
512 # This forces the issue of staying in sync w/ Circulation.pm
515 push( @errmsgloop, \
%err );
517 last if $exit_required_p;
519 $template->param( errmsgloop
=> \
@errmsgloop );
521 #set up so only the last 8 returned items display (make for faster loading pages)
522 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
526 { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.location' } ) };
527 foreach ( sort { $a <=> $b } keys %returneditems ) {
529 if ( $count++ < $returned_counter ) {
530 my $bar_code = $returneditems{$_};
531 if ($riduedate{$_}) {
532 my $duedate = dt_from_string
( $riduedate{$_}, 'sql');
533 $ri{year
} = $duedate->year();
534 $ri{month
} = $duedate->month();
535 $ri{day
} = $duedate->day();
536 $ri{hour
} = $duedate->hour();
537 $ri{minute
} = $duedate->minute();
538 $ri{duedate
} = output_pref
($duedate);
539 my $patron = Koha
::Patrons
->find( $riborrowernumber{$_} );
540 unless ( $dropboxmode ) {
541 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, DateTime
->now()) == -1);
543 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, $dropboxdate) == -1);
545 $ri{patron
} = $patron,
546 $ri{borissuescount
} = $patron->checkouts->count;
549 $ri{borrowernumber
} = $riborrowernumber{$_};
552 my $item = Koha
::Items
->find({ barcode
=> $bar_code });
553 my $biblio = $item->biblio;
554 # FIXME pass $item to the template and we are done here...
555 $ri{itembiblionumber
} = $biblio->biblionumber;
556 $ri{itemtitle
} = $biblio->title;
557 $ri{itemauthor
} = $biblio->author;
558 $ri{itemcallnumber
} = $item->itemcallnumber;
559 $ri{dateaccessioned
} = $item->dateaccessioned;
560 $ri{itemtype
} = $item->effective_itemtype;
561 $ri{itemnote
} = $item->itemnotes;
562 $ri{itemnotes_nonpublic
} = $item->itemnotes_nonpublic;
563 $ri{ccode
} = $item->ccode;
564 $ri{enumchron
} = $item->enumchron;
565 $ri{itemnumber
} = $item->itemnumber;
566 $ri{barcode
} = $bar_code;
567 $ri{homebranch
} = $item->homebranch;
568 $ri{holdingbranch
} = $item->holdingbranch;
570 $ri{location
} = $biblio->{'location'};
571 my $shelfcode = $ri{'location'};
572 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
584 errmsgloop
=> \
@errmsgloop,
585 exemptfine
=> $exemptfine,
586 dropboxmode
=> $dropboxmode,
587 dropboxdate
=> output_pref
($dropboxdate),
588 forgivemanualholdsexpire
=> $forgivemanualholdsexpire,
589 overduecharges
=> $overduecharges,
590 AudioAlerts
=> C4
::Context
->preference("AudioAlerts"),
593 $itemnumber = GetItemnumberFromBarcode
( $barcode );
595 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
596 if ( $holdingBranch and $collectionBranch ) {
597 $holdingBranch //= '';
598 $collectionBranch //= $returnbranch;
599 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
601 collectionItemNeedsTransferred
=> 1,
602 collectionBranch
=> $collectionBranch,
603 itemnumber
=> $itemnumber,
609 # Checking if there is a Fast Cataloging Framework
610 $template->param( fast_cataloging
=> 1 ) if Koha
::BiblioFrameworks
->find( 'FA' );
612 # actually print the page!
613 output_html_with_http_headers
$query, $cookie, $template->output;