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
33 # FIXME There are weird things going on with $patron and $borrowernumber in this script
38 use C4
::Auth qw
/:DEFAULT get_session/;
46 use C4
::Members
::Messaging
;
47 use C4
::Koha
; # FIXME : is it still useful ?
48 use C4
::RotatingCollections
;
49 use Koha
::AuthorisedValues
;
52 use Koha
::BiblioFrameworks
;
61 my ( $template, $librarian, $cookie, $flags ) = get_template_and_user
(
63 template_name
=> "circ/returns.tt",
67 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
71 my $sessionID = $query->cookie("CGISESSID");
72 my $session = get_session
($sessionID);
73 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
74 # no branch set we can't return
75 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
79 # Print a reserve slip on this page
80 if ( $query->param('print_slip') ) {
83 borrowernumber
=> scalar $query->param('borrowernumber'),
84 biblionumber
=> scalar $query->param('biblionumber'),
90 my $printers = GetPrinters
();
91 my $userenv = C4
::Context
->userenv;
92 my $userenv_branch = $userenv->{'branch'} // '';
93 my $printer = $userenv->{'branchprinter'} // '';
94 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
96 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
98 # Some code to handle the error if there is no branch or printer setting.....
101 # Set up the item stack ....
104 my %riborrowernumber;
106 foreach ( $query->param ) {
119 my $barcode = $query->param("ri-$counter");
120 my $duedate = $query->param("dd-$counter");
121 my $borrowernumber = $query->param("bn-$counter");
124 # decode barcode ## Didn't we already decode them before passing them back last time??
125 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
126 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
128 ######################
129 #Are these lines still useful ?
130 $returneditems{$counter} = $barcode;
131 $riduedate{$counter} = $duedate;
132 $riborrowernumber{$counter} = $borrowernumber;
134 #######################
135 $input{counter
} = $counter;
136 $input{barcode
} = $barcode;
137 $input{duedate
} = $duedate;
138 $input{borrowernumber
} = $borrowernumber;
139 push( @inputloop, \
%input );
143 # Deal with the requests....
145 if ($query->param('WT-itemNumber')){
146 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
149 if ( $query->param('reserve_id') ) {
150 my $itemnumber = $query->param('itemnumber');
151 my $borrowernumber = $query->param('borrowernumber');
152 my $reserve_id = $query->param('reserve_id');
153 my $diffBranchReturned = $query->param('diffBranch');
154 my $cancel_reserve = $query->param('cancel_reserve');
155 # fix up item type for display
156 my $item = Koha
::Items
->find( $itemnumber );
157 my $biblio = $item->biblio;
159 if ( $cancel_reserve ) {
160 my $hold = Koha
::Holds
->find( $reserve_id );
162 $hold->cancel( { charge_cancel_fee
=> !$forgivemanualholdsexpire } );
165 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ?
$diffBranchReturned : undef;
166 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
167 # i.e., whether to apply waiting status
168 ModReserveAffect
( $itemnumber, $borrowernumber, $diffBranchSend, $reserve_id );
170 # check if we have other reserves for this document, if we have a return send the message of transfer
171 my ( $messages, $nextreservinfo ) = GetOtherReserves
($itemnumber);
173 my $patron = Koha
::Patrons
->find( $nextreservinfo );
174 my $name = $patron ?
$patron->surname . ", " . $patron->title . " " . $patron->firstname : '';
175 if ( $messages->{'transfert'} ) {
177 itemtitle
=> $biblio->title,
178 itemnumber
=> $item->itemnumber,
179 itembiblionumber
=> $biblio->biblionumber,
180 iteminfo
=> $biblio->author,
182 borrowernumber
=> $borrowernumber,
183 borcnum
=> $patron->cardnumber,
184 borfirstname
=> $patron->firstname,
185 borsurname
=> $patron->surname,
186 borcategory
=> $patron->category->description,
197 my $barcode = $query->param('barcode');
198 my $exemptfine = $query->param('exemptfine');
201 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
203 # silently prevent unauthorized operator from forgiving overdue
204 # fines by manually tweaking form parameters
207 my $dropboxmode = $query->param('dropboxmode');
208 my $dotransfer = $query->param('dotransfer');
209 my $canceltransfer = $query->param('canceltransfer');
210 my $dest = $query->param('dest');
211 my $calendar = Koha
::Calendar
->new( branchcode
=> $userenv_branch );
212 #dropbox: get last open day (today - 1)
213 my $today = DateTime
->now( time_zone
=> C4
::Context
->tz());
214 my $dropboxdate = $calendar->addDate($today, -1);
216 my $return_date_override = $query->param('return_date_override');
217 my $return_date_override_remember =
218 $query->param('return_date_override_remember');
219 if ($return_date_override) {
220 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
221 my $return_date_override_dt = eval {dt_from_string
( $return_date_override ) };
222 if ( $return_date_override_dt ) {
223 # note that we've overriden the return date
224 $template->param( return_date_was_overriden
=> 1);
225 # Save the original format if we are remembering for this series
227 return_date_override
=> $return_date_override,
228 return_date_override_remember
=> 1
229 ) if ($return_date_override_remember);
231 $return_date_override =
232 DateTime
::Format
::MySQL
->format_datetime( $return_date_override_dt );
236 $return_date_override = q{};
241 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
242 my $transferitem = $query->param('transferitem');
243 my $tobranch = $query->param('tobranch');
244 ModItemTransfer
($transferitem, $userenv_branch, $tobranch);
247 if ($canceltransfer){
248 $itemnumber=$query->param('itemnumber');
249 DeleteTransfer
($itemnumber);
251 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
254 $template->param( transfercancelled
=> 1);
258 # actually return book and prepare item table.....
261 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
262 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
263 my $item = Koha
::Items
->find({ barcode
=> $barcode });
266 # Check if we should display a checkin message, based on the the item
267 # type of the checked in item
268 my $itemtype = Koha
::ItemTypes
->find( $item->effective_itemtype );
269 if ( $itemtype && $itemtype->checkinmsg ) {
271 checkinmsg
=> $itemtype->checkinmsg,
272 checkinmsgtype
=> $itemtype->checkinmsgtype,
276 # make sure return branch respects home branch circulation rules, default to homebranch
277 my $hbr = GetBranchItemRule
($item->homebranch, $itemtype ?
$itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
278 $returnbranch = $item->$hbr;
280 my $materials = $item->materials;
281 my $descriptions = Koha
::AuthorisedValues
->get_description_by_koha_field({frameworkcode
=> '', kohafield
=>'items.materials', authorised_value
=> $materials });
282 $materials = $descriptions->{lib
} // $materials;
284 my $checkout = $item->checkout;
285 my $biblio = $item->biblio;
287 title
=> $biblio->title,
288 homebranch
=> $item->homebranch,
289 holdingbranch
=> $item->holdingbranch,
290 returnbranch
=> $returnbranch,
291 author
=> $biblio->author,
292 itembarcode
=> $item->barcode,
293 itemtype
=> $item->effective_itemtype,
294 ccode
=> $item->ccode,
295 itembiblionumber
=> $biblio->biblionumber,
296 biblionumber
=> $biblio->biblionumber,
297 borrower
=> $borrower,
298 additional_materials
=> $materials,
301 } # FIXME else we should not call AddReturn but set BadBarcode directly instead
311 ( $returned, $messages, $issue, $borrower ) =
312 AddReturn
( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override, $dropboxdate );
315 my $time_now = DateTime
->now( time_zone
=> C4
::Context
->tz )->truncate( to
=> 'minute');
316 my $date_due_dt = dt_from_string
( $issue->date_due, 'sql' );
317 my $duedate = $date_due_dt->strftime('%Y-%m-%d %H:%M');
318 $returneditems{0} = $barcode;
319 $riborrowernumber{0} = $borrower->{'borrowernumber'};
320 $riduedate{0} = $duedate;
321 $input{borrowernumber
} = $borrower->{'borrowernumber'};
322 $input{duedate
} = $duedate;
323 unless ( $dropboxmode ) {
324 $input{return_overdue
} = 1 if (DateTime
->compare($date_due_dt, DateTime
->now()) == -1);
326 $input{return_overdue
} = 1 if (DateTime
->compare($date_due_dt, $dropboxdate) == -1);
328 push( @inputloop, \
%input );
330 if ( C4
::Context
->preference("FineNotifyAtCheckin") ) {
331 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
332 my $balance = $patron->account->balance;
335 $template->param( fines
=> sprintf("%.2f", $balance) );
336 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
340 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
341 #Check for waiting holds
342 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
343 my $waiting_holds = $patron->holds->search({ found
=> 'W', branchcode
=> $userenv_branch })->count;
344 if ($waiting_holds > 0) {
346 waiting_holds
=> $waiting_holds,
347 holdsborrowernumber
=> $borrower->{'borrowernumber'},
348 holdsfirstname
=> $borrower->{'firstname'},
349 holdssurname
=> $borrower->{'surname'},
353 } elsif ( C4
::Context
->preference('ShowAllCheckins') and !$messages->{'BadBarcode'} ) {
355 $returneditems{0} = $barcode;
357 push( @inputloop, \
%input );
359 $template->param( privacy
=> $borrower->{privacy
} );
361 $template->param( inputloop
=> \
@inputloop );
367 # new op dev : we check if the document must be returned to his homebranch directly,
368 # if the document is transfered, we have warning message .
370 if ( $messages->{'WasTransfered'} ) {
374 itemnumber
=> $itemnumber,
378 if ( $messages->{'NeedsTransfer'} ){
381 needstransfer
=> $messages->{'NeedsTransfer'},
382 itemnumber
=> $itemnumber,
386 if ( $messages->{'Wrongbranch'} ){
389 rightbranch
=> $messages->{'Wrongbranch'}->{'Rightbranch'},
393 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
395 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
398 TransferWaitingAt
=> $messages->{'WrongTransfer'},
399 WrongTransferItem
=> $messages->{'WrongTransferItem'},
400 itemnumber
=> $itemnumber,
403 my $reserve = $messages->{'ResFound'};
405 my $patron = Koha
::Patrons
->find( $reserve->{'borrowernumber'} );
406 my $name = $patron->surname . ", " . $patron->title . " " . $patron->firstname;
408 # FIXME The full patron object should be passed to the template
410 wborfirstname
=> $patron->firstname,
411 wborsurname
=> $patron->surname,
412 wborcategory
=> $patron->category->description,
413 wbortitle
=> $patron->title,
414 wborphone
=> $patron->phone,
415 wboremail
=> $patron->email,
416 streetnumber
=> $patron->streetnumber,
417 address
=> $patron->address,
418 address2
=> $patron->address2,
419 city
=> $patron->city,
420 zipcode
=> $patron->zipcode,
421 state => $patron->state,
422 country
=> $patron->country,
423 wborrowernumber
=> $reserve->{'borrowernumber'},
424 wborcnum
=> $patron->cardnumber,
428 wtransfertFrom
=> $userenv_branch,
433 # reserve found and item arrived at the expected branch
435 if ( $messages->{'ResFound'}) {
436 my $reserve = $messages->{'ResFound'};
437 my $patron = Koha
::Patrons
->find( $reserve->{borrowernumber
} );
438 my $holdmsgpreferences = C4
::Members
::Messaging
::GetMessagingPreferences
( { borrowernumber
=> $reserve->{'borrowernumber'}, message_name
=> 'Hold_Filled' } );
439 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
440 if ( $reserve->{'ResFound'} eq "Waiting" ) {
442 waiting
=> ($userenv_branch eq $reserve->{'branchcode'} ?
1 : 0 ),
444 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
446 intransit
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
447 transfertodo
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
448 reserve_id
=> $reserve->{reserve_id
},
453 # same params for Waiting or Reserved
455 # FIXME The full patron object should be passed to the template
457 name
=> $patron->surname . ", " . $patron->title . " " . $patron->firstname,
458 borfirstname
=> $patron->firstname,
459 borsurname
=> $patron->surname,
460 borcategory
=> $patron->category->description,
461 bortitle
=> $patron->title,
462 borphone
=> $patron->phone,
463 boremail
=> $patron->email,
464 boraddress
=> $patron->address,
465 boraddress2
=> $patron->address2,
466 streetnumber
=> $patron->streetnumber,
467 city
=> $patron->city,
468 zipcode
=> $patron->zipcode,
469 state => $patron->state,
470 country
=> $patron->country,
471 borcnum
=> $patron->cardnumber,
472 debarred
=> $patron->debarred,
473 gonenoaddress
=> $patron->gonenoaddress,
475 destbranch
=> $reserve->{'branchcode'},
476 borrowernumber
=> $reserve->{'borrowernumber'},
477 itemnumber
=> $reserve->{'itemnumber'},
478 reservenotes
=> $reserve->{'reservenotes'},
479 reserve_id
=> $reserve->{reserve_id
},
480 bormessagepref
=> $holdmsgpreferences->{'transports'},
482 } # else { ; } # error?
487 foreach my $code ( keys %$messages ) {
489 my $exit_required_p = 0;
490 if ( $code eq 'BadBarcode' ) {
491 $err{badbarcode
} = 1;
492 $err{msg
} = $messages->{'BadBarcode'};
494 elsif ( $code eq 'NotIssued' ) {
497 $err{msg
} = $messages->{'IsPermanent'} if $messages->{'IsPermanent'};
499 elsif ( $code eq 'LocalUse' ) {
502 elsif ( $code eq 'WasLost' ) {
504 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfLostItems");
506 elsif ( $code eq 'LostItemFeeRefunded' ) {
507 $template->param( LostItemFeeRefunded
=> 1 );
509 elsif ( $code eq 'ResFound' ) {
510 ; # FIXME... anything to do here?
512 elsif ( $code eq 'WasReturned' ) {
513 ; # FIXME... anything to do here?
515 elsif ( $code eq 'WasTransfered' ) {
516 ; # FIXME... anything to do here?
518 elsif ( $code eq 'withdrawn' ) {
520 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
522 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
523 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
524 $err{ispermanent
} = 1;
525 $err{msg
} = $messages->{'IsPermanent'};
528 elsif ( $code eq 'WrongTransfer' ) {
529 ; # FIXME... anything to do here?
531 elsif ( $code eq 'WrongTransferItem' ) {
532 ; # FIXME... anything to do here?
534 elsif ( $code eq 'NeedsTransfer' ) {
536 elsif ( $code eq 'Wrongbranch' ) {
538 elsif ( $code eq 'Debarred' ) {
539 $err{debarred
} = $messages->{'Debarred'};
540 $err{debarcardnumber
} = $borrower->{cardnumber
};
541 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
542 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
544 elsif ( $code eq 'PrevDebarred' ) {
545 $err{prevdebarred
} = $messages->{'PrevDebarred'};
547 elsif ( $code eq 'ForeverDebarred' ) {
548 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
550 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
551 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
553 elsif ( $code eq 'DataCorrupted' ) {
554 $err{data_corrupted
} = 1;
557 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
558 # This forces the issue of staying in sync w/ Circulation.pm
561 push( @errmsgloop, \
%err );
563 last if $exit_required_p;
565 $template->param( errmsgloop
=> \
@errmsgloop );
567 #set up so only the last 8 returned items display (make for faster loading pages)
568 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
572 { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.location' } ) };
573 foreach ( sort { $a <=> $b } keys %returneditems ) {
575 if ( $count++ < $returned_counter ) {
576 my $bar_code = $returneditems{$_};
577 if ($riduedate{$_}) {
578 my $duedate = dt_from_string
( $riduedate{$_}, 'sql');
579 $ri{year
} = $duedate->year();
580 $ri{month
} = $duedate->month();
581 $ri{day
} = $duedate->day();
582 $ri{hour
} = $duedate->hour();
583 $ri{minute
} = $duedate->minute();
584 $ri{duedate
} = output_pref
($duedate);
585 my $patron = Koha
::Patrons
->find( $riborrowernumber{$_} );
586 unless ( $dropboxmode ) {
587 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, DateTime
->now()) == -1);
589 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, $dropboxdate) == -1);
591 $ri{borrowernumber
} = $patron->borrowernumber;
592 $ri{borcnum
} = $patron->cardnumber;
593 $ri{borfirstname
} = $patron->firstname;
594 $ri{borsurname
} = $patron->surname;
595 $ri{bortitle
} = $patron->title;
596 $ri{bornote
} = $patron->borrowernotes;
597 $ri{borcategorycode
}= $patron->categorycode;
598 $ri{borissuescount
} = Koha
::Checkouts
->count( { borrowernumber
=> $b->{'borrowernumber'} } );
601 $ri{borrowernumber
} = $riborrowernumber{$_};
604 my $item = Koha
::Items
->find({ barcode
=> $bar_code });
605 my $biblio = $item->biblio;
606 # FIXME pass $item to the template and we are done here...
607 $ri{itembiblionumber
} = $biblio->biblionumber;
608 $ri{itemtitle
} = $biblio->title;
609 $ri{itemauthor
} = $biblio->author;
610 $ri{itemcallnumber
} = $item->itemcallnumber;
611 $ri{dateaccessioned
} = $item->dateaccessioned;
612 $ri{itemtype
} = $item->effective_itemtype;
613 $ri{itemnote
} = $item->itemnotes;
614 $ri{itemnotes_nonpublic
} = $item->itemnotes_nonpublic;
615 $ri{ccode
} = $item->ccode;
616 $ri{enumchron
} = $item->enumchron;
617 $ri{itemnumber
} = $item->itemnumber;
618 $ri{barcode
} = $bar_code;
619 $ri{homebranch
} = $item->homebranch;
620 $ri{holdingbranch
} = $item->holdingbranch;
622 $ri{location
} = $biblio->{'location'};
623 my $shelfcode = $ri{'location'};
624 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
636 errmsgloop
=> \
@errmsgloop,
637 exemptfine
=> $exemptfine,
638 dropboxmode
=> $dropboxmode,
639 dropboxdate
=> output_pref
($dropboxdate),
640 forgivemanualholdsexpire
=> $forgivemanualholdsexpire,
641 overduecharges
=> $overduecharges,
642 AudioAlerts
=> C4
::Context
->preference("AudioAlerts"),
645 $itemnumber = GetItemnumberFromBarcode
( $barcode );
647 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
648 if ( $holdingBranch and $collectionBranch ) {
649 $holdingBranch //= '';
650 $collectionBranch //= $returnbranch;
651 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
653 collectionItemNeedsTransferred
=> 1,
654 collectionBranch
=> $collectionBranch,
655 itemnumber
=> $itemnumber,
661 # Checking if there is a Fast Cataloging Framework
662 $template->param( fast_cataloging
=> 1 ) if Koha
::BiblioFrameworks
->find( 'FA' );
664 # actually print the page!
665 output_html_with_http_headers
$query, $cookie, $template->output;