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
36 $SIG{ __DIE__
} = sub { Carp
::confess
( @_ ) };
41 use C4
::Auth qw
/:DEFAULT get_session/;
49 use C4
::Members
::Messaging
;
50 use C4
::Koha
; # FIXME : is it still useful ?
51 use C4
::RotatingCollections
;
52 use Koha
::AuthorisedValues
;
55 use Koha
::BiblioFrameworks
;
64 my ( $template, $librarian, $cookie, $flags ) = get_template_and_user
(
66 template_name
=> "circ/returns.tt",
70 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
74 my $sessionID = $query->cookie("CGISESSID");
75 my $session = get_session
($sessionID);
76 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
77 # no branch set we can't return
78 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
82 # Print a reserve slip on this page
83 if ( $query->param('print_slip') ) {
86 borrowernumber
=> scalar $query->param('borrowernumber'),
87 biblionumber
=> scalar $query->param('biblionumber'),
93 my $printers = GetPrinters
();
94 my $userenv = C4
::Context
->userenv;
95 my $userenv_branch = $userenv->{'branch'} // '';
96 my $printer = $userenv->{'branchprinter'} // '';
97 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
99 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
101 # Some code to handle the error if there is no branch or printer setting.....
104 # Set up the item stack ....
107 my %riborrowernumber;
109 foreach ( $query->param ) {
122 my $barcode = $query->param("ri-$counter");
123 my $duedate = $query->param("dd-$counter");
124 my $borrowernumber = $query->param("bn-$counter");
127 # decode barcode ## Didn't we already decode them before passing them back last time??
128 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
129 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
131 ######################
132 #Are these lines still useful ?
133 $returneditems{$counter} = $barcode;
134 $riduedate{$counter} = $duedate;
135 $riborrowernumber{$counter} = $borrowernumber;
137 #######################
138 $input{counter
} = $counter;
139 $input{barcode
} = $barcode;
140 $input{duedate
} = $duedate;
141 $input{borrowernumber
} = $borrowernumber;
142 push( @inputloop, \
%input );
146 # Deal with the requests....
148 if ($query->param('WT-itemNumber')){
149 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
152 if ( $query->param('reserve_id') ) {
153 my $itemnumber = $query->param('itemnumber');
154 my $borrowernumber = $query->param('borrowernumber');
155 my $reserve_id = $query->param('reserve_id');
156 my $diffBranchReturned = $query->param('diffBranch');
157 my $cancel_reserve = $query->param('cancel_reserve');
158 # fix up item type for display
159 my $item = Koha
::Items
->find( $itemnumber );
160 my $biblio = $item->biblio;
162 if ( $cancel_reserve ) {
163 my $hold = Koha
::Holds
->find( $reserve_id );
165 $hold->cancel( { charge_cancel_fee
=> !$forgivemanualholdsexpire } );
168 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ?
$diffBranchReturned : undef;
169 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
170 # i.e., whether to apply waiting status
171 ModReserveAffect
( $itemnumber, $borrowernumber, $diffBranchSend, $reserve_id );
173 # check if we have other reserves for this document, if we have a return send the message of transfer
174 my ( $messages, $nextreservinfo ) = GetOtherReserves
($itemnumber);
176 my $patron = Koha
::Patrons
->find( $nextreservinfo );
177 my $name = $patron ?
$patron->surname . ", " . $patron->title . " " . $patron->firstname : '';
178 if ( $messages->{'transfert'} ) {
180 itemtitle
=> $biblio->title,
181 itemnumber
=> $item->itemnumber,
182 itembiblionumber
=> $biblio->biblionumber,
183 iteminfo
=> $biblio->author,
185 borrowernumber
=> $borrowernumber,
186 borcnum
=> $patron->cardnumber,
187 borfirstname
=> $patron->firstname,
188 borsurname
=> $patron->surname,
189 borcategory
=> $patron->category->description,
200 my $barcode = $query->param('barcode');
201 my $exemptfine = $query->param('exemptfine');
204 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
206 # silently prevent unauthorized operator from forgiving overdue
207 # fines by manually tweaking form parameters
210 my $dropboxmode = $query->param('dropboxmode');
211 my $dotransfer = $query->param('dotransfer');
212 my $canceltransfer = $query->param('canceltransfer');
213 my $dest = $query->param('dest');
214 my $calendar = Koha
::Calendar
->new( branchcode
=> $userenv_branch );
215 #dropbox: get last open day (today - 1)
216 my $today = DateTime
->now( time_zone
=> C4
::Context
->tz());
217 my $dropboxdate = $calendar->addDate($today, -1);
219 my $return_date_override = $query->param('return_date_override');
220 my $return_date_override_remember =
221 $query->param('return_date_override_remember');
222 if ($return_date_override) {
223 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
224 my $return_date_override_dt = eval {dt_from_string
( $return_date_override ) };
225 if ( $return_date_override_dt ) {
226 # note that we've overriden the return date
227 $template->param( return_date_was_overriden
=> 1);
228 # Save the original format if we are remembering for this series
230 return_date_override
=> $return_date_override,
231 return_date_override_remember
=> 1
232 ) if ($return_date_override_remember);
234 $return_date_override =
235 DateTime
::Format
::MySQL
->format_datetime( $return_date_override_dt );
239 $return_date_override = q{};
244 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
245 my $transferitem = $query->param('transferitem');
246 my $tobranch = $query->param('tobranch');
247 ModItemTransfer
($transferitem, $userenv_branch, $tobranch);
250 if ($canceltransfer){
251 $itemnumber=$query->param('itemnumber');
252 DeleteTransfer
($itemnumber);
254 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
257 $template->param( transfercancelled
=> 1);
261 # actually return book and prepare item table.....
264 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
265 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
266 my $item = Koha
::Items
->find({ barcode
=> $barcode });
269 # Check if we should display a checkin message, based on the the item
270 # type of the checked in item
271 my $itemtype = Koha
::ItemTypes
->find( $item->effective_itemtype );
272 if ( $itemtype && $itemtype->checkinmsg ) {
274 checkinmsg
=> $itemtype->checkinmsg,
275 checkinmsgtype
=> $itemtype->checkinmsgtype,
279 # make sure return branch respects home branch circulation rules, default to homebranch
280 my $hbr = GetBranchItemRule
($item->homebranch, $itemtype ?
$itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
281 $returnbranch = $item->$hbr;
283 my $materials = $item->materials;
284 my $descriptions = Koha
::AuthorisedValues
->get_description_by_koha_field({frameworkcode
=> '', kohafield
=>'items.materials', authorised_value
=> $materials });
285 $materials = $descriptions->{lib
} // $materials;
287 my $issue = Koha
::Checkouts
->find( { itemnumber
=> $itemnumber } );
289 my $biblio = $item->biblio;
291 title
=> $biblio->title,
292 homebranch
=> $item->homebranch,
293 holdingbranch
=> $item->holdingbranch,
294 returnbranch
=> $returnbranch,
295 author
=> $biblio->author,
296 itembarcode
=> $item->barcode,
297 itemtype
=> $item->effective_itemtype,
298 ccode
=> $item->ccode,
299 itembiblionumber
=> $biblio->biblionumber,
300 biblionumber
=> $biblio->biblionumber,
301 borrower
=> $borrower,
302 additional_materials
=> $materials,
305 } # FIXME else we should not call AddReturn but set BadBarcode directly instead
315 ( $returned, $messages, $issue, $borrower ) =
316 AddReturn
( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override, $dropboxdate );
319 my $time_now = DateTime
->now( time_zone
=> C4
::Context
->tz )->truncate( to
=> 'minute');
320 my $date_due_dt = dt_from_string
( $issue->date_due, 'sql' );
321 my $duedate = $date_due_dt->strftime('%Y-%m-%d %H:%M');
322 $returneditems{0} = $barcode;
323 $riborrowernumber{0} = $borrower->{'borrowernumber'};
324 $riduedate{0} = $duedate;
325 $input{borrowernumber
} = $borrower->{'borrowernumber'};
326 $input{duedate
} = $duedate;
327 unless ( $dropboxmode ) {
328 $input{return_overdue
} = 1 if (DateTime
->compare($date_due_dt, DateTime
->now()) == -1);
330 $input{return_overdue
} = 1 if (DateTime
->compare($date_due_dt, $dropboxdate) == -1);
332 push( @inputloop, \
%input );
334 if ( C4
::Context
->preference("FineNotifyAtCheckin") ) {
335 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
336 my $balance = $patron->account->balance;
339 $template->param( fines
=> sprintf("%.2f", $balance) );
340 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
344 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
345 #Check for waiting holds
346 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
347 my $waiting_holds = $patron->holds->search({ found
=> 'W', branchcode
=> $userenv_branch })->count;
348 if ($waiting_holds > 0) {
350 waiting_holds
=> $waiting_holds,
351 holdsborrowernumber
=> $borrower->{'borrowernumber'},
352 holdsfirstname
=> $borrower->{'firstname'},
353 holdssurname
=> $borrower->{'surname'},
357 } elsif ( C4
::Context
->preference('ShowAllCheckins') and !$messages->{'BadBarcode'} ) {
359 $returneditems{0} = $barcode;
361 push( @inputloop, \
%input );
363 $template->param( privacy
=> $borrower->{privacy
} );
365 $template->param( inputloop
=> \
@inputloop );
371 # new op dev : we check if the document must be returned to his homebranch directly,
372 # if the document is transfered, we have warning message .
374 if ( $messages->{'WasTransfered'} ) {
378 itemnumber
=> $itemnumber,
382 if ( $messages->{'NeedsTransfer'} ){
385 needstransfer
=> $messages->{'NeedsTransfer'},
386 itemnumber
=> $itemnumber,
390 if ( $messages->{'Wrongbranch'} ){
393 rightbranch
=> $messages->{'Wrongbranch'}->{'Rightbranch'},
397 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
399 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
402 TransferWaitingAt
=> $messages->{'WrongTransfer'},
403 WrongTransferItem
=> $messages->{'WrongTransferItem'},
404 itemnumber
=> $itemnumber,
407 my $reserve = $messages->{'ResFound'};
409 my $patron = Koha
::Patrons
->find( $reserve->{'borrowernumber'} );
410 my $name = $patron->surname . ", " . $patron->title . " " . $patron->firstname;
412 # FIXME The full patron object should be passed to the template
414 wborfirstname
=> $patron->firstname,
415 wborsurname
=> $patron->surname,
416 wborcategory
=> $patron->category->description,
417 wbortitle
=> $patron->title,
418 wborphone
=> $patron->phone,
419 wboremail
=> $patron->email,
420 streetnumber
=> $patron->streetnumber,
421 address
=> $patron->address,
422 address2
=> $patron->address2,
423 city
=> $patron->city,
424 zipcode
=> $patron->zipcode,
425 state => $patron->state,
426 country
=> $patron->country,
427 wborrowernumber
=> $reserve->{'borrowernumber'},
428 wborcnum
=> $patron->cardnumber,
432 wtransfertFrom
=> $userenv_branch,
437 # reserve found and item arrived at the expected branch
439 if ( $messages->{'ResFound'}) {
440 my $reserve = $messages->{'ResFound'};
441 my $patron = Koha
::Patrons
->find( $reserve->{borrowernumber
} );
442 my $holdmsgpreferences = C4
::Members
::Messaging
::GetMessagingPreferences
( { borrowernumber
=> $reserve->{'borrowernumber'}, message_name
=> 'Hold_Filled' } );
443 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
444 if ( $reserve->{'ResFound'} eq "Waiting" ) {
446 waiting
=> ($userenv_branch eq $reserve->{'branchcode'} ?
1 : 0 ),
448 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
450 intransit
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
451 transfertodo
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
452 reserve_id
=> $reserve->{reserve_id
},
457 # same params for Waiting or Reserved
459 # FIXME The full patron object should be passed to the template
461 name
=> $patron->surname . ", " . $patron->title . " " . $patron->firstname,
462 borfirstname
=> $patron->firstname,
463 borsurname
=> $patron->surname,
464 borcategory
=> $patron->category->description,
465 bortitle
=> $patron->title,
466 borphone
=> $patron->phone,
467 boremail
=> $patron->email,
468 boraddress
=> $patron->address,
469 boraddress2
=> $patron->address2,
470 streetnumber
=> $patron->streetnumber,
471 city
=> $patron->city,
472 zipcode
=> $patron->zipcode,
473 state => $patron->state,
474 country
=> $patron->country,
475 borcnum
=> $patron->cardnumber,
476 debarred
=> $patron->debarred,
477 gonenoaddress
=> $patron->gonenoaddress,
479 destbranch
=> $reserve->{'branchcode'},
480 borrowernumber
=> $reserve->{'borrowernumber'},
481 itemnumber
=> $reserve->{'itemnumber'},
482 reservenotes
=> $reserve->{'reservenotes'},
483 reserve_id
=> $reserve->{reserve_id
},
484 bormessagepref
=> $holdmsgpreferences->{'transports'},
486 } # else { ; } # error?
491 foreach my $code ( keys %$messages ) {
493 my $exit_required_p = 0;
494 if ( $code eq 'BadBarcode' ) {
495 $err{badbarcode
} = 1;
496 $err{msg
} = $messages->{'BadBarcode'};
498 elsif ( $code eq 'NotIssued' ) {
501 $err{msg
} = $messages->{'IsPermanent'} if $messages->{'IsPermanent'};
503 elsif ( $code eq 'LocalUse' ) {
506 elsif ( $code eq 'WasLost' ) {
509 elsif ( $code eq 'LostItemFeeRefunded' ) {
510 $template->param( LostItemFeeRefunded
=> 1 );
512 elsif ( $code eq 'ResFound' ) {
513 ; # FIXME... anything to do here?
515 elsif ( $code eq 'WasReturned' ) {
516 ; # FIXME... anything to do here?
518 elsif ( $code eq 'WasTransfered' ) {
519 ; # FIXME... anything to do here?
521 elsif ( $code eq 'withdrawn' ) {
523 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
525 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
526 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
527 $err{ispermanent
} = 1;
528 $err{msg
} = $messages->{'IsPermanent'};
531 elsif ( $code eq 'WrongTransfer' ) {
532 ; # FIXME... anything to do here?
534 elsif ( $code eq 'WrongTransferItem' ) {
535 ; # FIXME... anything to do here?
537 elsif ( $code eq 'NeedsTransfer' ) {
539 elsif ( $code eq 'Wrongbranch' ) {
541 elsif ( $code eq 'Debarred' ) {
542 $err{debarred
} = $messages->{'Debarred'};
543 $err{debarcardnumber
} = $borrower->{cardnumber
};
544 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
545 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
547 elsif ( $code eq 'PrevDebarred' ) {
548 $err{prevdebarred
} = $messages->{'PrevDebarred'};
550 elsif ( $code eq 'ForeverDebarred' ) {
551 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
553 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
554 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
556 elsif ( $code eq 'DataCorrupted' ) {
557 $err{data_corrupted
} = 1;
560 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
561 # This forces the issue of staying in sync w/ Circulation.pm
564 push( @errmsgloop, \
%err );
566 last if $exit_required_p;
568 $template->param( errmsgloop
=> \
@errmsgloop );
570 #set up so only the last 8 returned items display (make for faster loading pages)
571 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
575 { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.location' } ) };
576 foreach ( sort { $a <=> $b } keys %returneditems ) {
578 if ( $count++ < $returned_counter ) {
579 my $bar_code = $returneditems{$_};
580 if ($riduedate{$_}) {
581 my $duedate = dt_from_string
( $riduedate{$_}, 'sql');
582 $ri{year
} = $duedate->year();
583 $ri{month
} = $duedate->month();
584 $ri{day
} = $duedate->day();
585 $ri{hour
} = $duedate->hour();
586 $ri{minute
} = $duedate->minute();
587 $ri{duedate
} = output_pref
($duedate);
588 my $patron = Koha
::Patrons
->find( $riborrowernumber{$_} );
589 unless ( $dropboxmode ) {
590 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, DateTime
->now()) == -1);
592 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, $dropboxdate) == -1);
594 $ri{borrowernumber
} = $patron->borrowernumber;
595 $ri{borcnum
} = $patron->cardnumber;
596 $ri{borfirstname
} = $patron->firstname;
597 $ri{borsurname
} = $patron->surname;
598 $ri{bortitle
} = $patron->title;
599 $ri{bornote
} = $patron->borrowernotes;
600 $ri{borcategorycode
}= $patron->categorycode;
601 $ri{borissuescount
} = Koha
::Checkouts
->count( { borrowernumber
=> $b->{'borrowernumber'} } );
604 $ri{borrowernumber
} = $riborrowernumber{$_};
607 my $item = Koha
::Items
->find({ barcode
=> $bar_code });
608 my $biblio = $item->biblio;
609 # FIXME pass $item to the template and we are done here...
610 $ri{itembiblionumber
} = $biblio->biblionumber;
611 $ri{itemtitle
} = $biblio->title;
612 $ri{itemauthor
} = $biblio->author;
613 $ri{itemcallnumber
} = $item->itemcallnumber;
614 $ri{dateaccessioned
} = $item->dateaccessioned;
615 $ri{itemtype
} = $item->effective_itemtype;
616 $ri{itemnote
} = $item->itemnotes;
617 $ri{itemnotes_nonpublic
} = $item->itemnotes_nonpublic;
618 $ri{ccode
} = $item->ccode;
619 $ri{enumchron
} = $item->enumchron;
620 $ri{itemnumber
} = $item->itemnumber;
621 $ri{barcode
} = $bar_code;
622 $ri{homebranch
} = $item->homebranch;
623 $ri{holdingbranch
} = $item->holdingbranch;
625 $ri{location
} = $biblio->{'location'};
626 my $shelfcode = $ri{'location'};
627 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
639 errmsgloop
=> \
@errmsgloop,
640 exemptfine
=> $exemptfine,
641 dropboxmode
=> $dropboxmode,
642 dropboxdate
=> output_pref
($dropboxdate),
643 forgivemanualholdsexpire
=> $forgivemanualholdsexpire,
644 overduecharges
=> $overduecharges,
645 AudioAlerts
=> C4
::Context
->preference("AudioAlerts"),
646 BlockReturnOfWithdrawnItems
=> C4
::Context
->preference("BlockReturnOfWithdrawnItems"),
649 $itemnumber = GetItemnumberFromBarcode
( $barcode );
651 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
652 if ( $holdingBranch and $collectionBranch ) {
653 $holdingBranch //= '';
654 $collectionBranch //= $returnbranch;
655 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
657 collectionItemNeedsTransferred
=> 1,
658 collectionBranch
=> $collectionBranch,
659 itemnumber
=> $itemnumber,
665 # Checking if there is a Fast Cataloging Framework
666 $template->param( fast_cataloging
=> 1 ) if Koha
::BiblioFrameworks
->find( 'FA' );
668 # actually print the page!
669 output_html_with_http_headers
$query, $cookie, $template->output;