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
34 $SIG{ __DIE__
} = sub { Carp
::confess
( @_ ) };
39 use C4
::Auth qw
/:DEFAULT get_session/;
47 use C4
::Branch
; # GetBranches GetBranchName
48 use C4
::Koha
; # FIXME : is it still useful ?
49 use C4
::RotatingCollections
;
56 my ( $template, $librarian, $cookie ) = get_template_and_user
(
58 template_name
=> "circ/returns.tt",
62 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
66 my $sessionID = $query->cookie("CGISESSID");
67 my $session = get_session
($sessionID);
68 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
69 # no branch set we can't return
70 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
74 # Print a reserve slip on this page
75 if ( $query->param('print_slip') ) {
78 borrowernumber
=> $query->param('borrowernumber'),
79 biblionumber
=> $query->param('biblionumber'),
85 my $branches = GetBranches
();
86 my $printers = GetPrinters
();
87 my $userenv = C4
::Context
->userenv;
88 my $userenv_branch = $userenv->{'branch'} // '';
89 my $printer = $userenv->{'branchprinter'} // '';
90 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
92 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
94 # Some code to handle the error if there is no branch or printer setting.....
97 # Set up the item stack ....
100 my %riborrowernumber;
102 foreach ( $query->param ) {
115 my $barcode = $query->param("ri-$counter");
116 my $duedate = $query->param("dd-$counter");
117 my $borrowernumber = $query->param("bn-$counter");
120 # decode barcode ## Didn't we already decode them before passing them back last time??
121 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
122 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
124 ######################
125 #Are these lines still useful ?
126 $returneditems{$counter} = $barcode;
127 $riduedate{$counter} = $duedate;
128 $riborrowernumber{$counter} = $borrowernumber;
130 #######################
131 $input{counter
} = $counter;
132 $input{barcode
} = $barcode;
133 $input{duedate
} = $duedate;
134 $input{borrowernumber
} = $borrowernumber;
135 push( @inputloop, \
%input );
139 # Deal with the requests....
141 if ($query->param('WT-itemNumber')){
142 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
145 if ( $query->param('resbarcode') ) {
146 my $item = $query->param('itemnumber');
147 my $borrowernumber = $query->param('borrowernumber');
148 my $resbarcode = $query->param('resbarcode');
149 my $diffBranchReturned = $query->param('diffBranch');
150 my $iteminfo = GetBiblioFromItemNumber
($item);
151 my $cancel_reserve = $query->param('cancel_reserve');
152 # fix up item type for display
153 $iteminfo->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$iteminfo->{'itype'} : $iteminfo->{'itemtype'};
155 if ( $cancel_reserve ) {
156 CancelReserve
({ borrowernumber
=> $borrowernumber, itemnumber
=> $item, 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
( $item, $borrowernumber, $diffBranchSend);
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
($item);
166 my ($borr) = GetMemberDetails
( $nextreservinfo, 0 );
167 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
168 if ( $messages->{'transfert'} ) {
170 itemtitle
=> $iteminfo->{'title'},
171 itemnumber
=> $iteminfo->{'itemnumber'},
172 itembiblionumber
=> $iteminfo->{'biblionumber'},
173 iteminfo
=> $iteminfo->{'author'},
174 tobranchname
=> GetBranchName
($messages->{'transfert'}),
176 borrowernumber
=> $borrowernumber,
177 borcnum
=> $borr->{'cardnumber'},
178 borfirstname
=> $borr->{'firstname'},
179 borsurname
=> $borr->{'surname'},
188 my $issueinformation;
190 my $barcode = $query->param('barcode');
191 my $exemptfine = $query->param('exemptfine');
194 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
196 # silently prevent unauthorized operator from forgiving overdue
197 # fines by manually tweaking form parameters
200 my $dropboxmode = $query->param('dropboxmode');
201 my $dotransfer = $query->param('dotransfer');
202 my $canceltransfer = $query->param('canceltransfer');
203 my $dest = $query->param('dest');
204 my $calendar = Koha
::Calendar
->new( branchcode
=> $userenv_branch );
205 #dropbox: get last open day (today - 1)
206 my $today = DateTime
->now( time_zone
=> C4
::Context
->tz());
207 my $dropboxdate = $calendar->addDate($today, -1);
209 my $return_date_override = $query->param('return_date_override');
210 my $return_date_override_remember =
211 $query->param('return_date_override_remember');
212 if ($return_date_override) {
213 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
214 my $return_date_override_dt = eval {dt_from_string
( $return_date_override ) };
215 if ( $return_date_override_dt ) {
216 # note that we've overriden the return date
217 $template->param( return_date_was_overriden
=> 1);
218 # Save the original format if we are remembering for this series
220 return_date_override
=> $return_date_override,
221 return_date_override_remember
=> 1
222 ) if ($return_date_override_remember);
224 $return_date_override =
225 DateTime
::Format
::MySQL
->format_datetime( $return_date_override_dt );
229 $return_date_override = q{};
234 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
235 my $transferitem = $query->param('transferitem');
236 my $tobranch = $query->param('tobranch');
237 ModItemTransfer
($transferitem, $userenv_branch, $tobranch);
240 if ($canceltransfer){
241 $itemnumber=$query->param('itemnumber');
242 DeleteTransfer
($itemnumber);
244 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
247 $template->param( transfercancelled
=> 1);
251 # actually return book and prepare item table.....
253 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
254 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
255 $itemnumber = GetItemnumberFromBarcode
($barcode);
261 # get biblio description
262 my $biblio = GetBiblioFromItemNumber
($itemnumber);
263 # fix up item type for display
264 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
266 # Check if we should display a checkin message, based on the the item
267 # type of the checked in item
268 my $itemtype = C4
::ItemType
->get( $biblio->{'itemtype'} );
269 if ( $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
($biblio->{'homebranch'}, $itemtype->{itemtype
})->{'returnbranch'} || "homebranch";
278 my $returnbranch = $biblio->{$hbr} ;
281 title
=> $biblio->{'title'},
282 homebranch
=> $biblio->{'homebranch'},
283 returnbranch
=> $returnbranch,
284 author
=> $biblio->{'author'},
285 itembarcode
=> $biblio->{'barcode'},
286 itemtype
=> $biblio->{'itemtype'},
287 ccode
=> $biblio->{'ccode'},
288 itembiblionumber
=> $biblio->{'biblionumber'},
289 biblionumber
=> $biblio->{'biblionumber'},
290 borrower
=> $borrower,
291 additional_materials
=> $biblio->{'materials'},
301 ( $returned, $messages, $issueinformation, $borrower ) =
302 AddReturn
( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override, $dropboxdate );
305 my $time_now = DateTime
->now( time_zone
=> C4
::Context
->tz )->truncate( to
=> 'minute');
306 my $duedate = $issueinformation->{date_due
}->strftime('%Y-%m-%d %H:%M');
307 $returneditems{0} = $barcode;
308 $riborrowernumber{0} = $borrower->{'borrowernumber'};
309 $riduedate{0} = $duedate;
310 $input{borrowernumber
} = $borrower->{'borrowernumber'};
311 $input{duedate
} = $duedate;
312 unless ( $dropboxmode ) {
313 $input{return_overdue
} = 1 if (DateTime
->compare($issueinformation->{date_due
}, DateTime
->now()) == -1);
315 $input{return_overdue
} = 1 if (DateTime
->compare($issueinformation->{date_due
}, $dropboxdate) == -1);
317 push( @inputloop, \
%input );
319 if ( C4
::Context
->preference("FineNotifyAtCheckin") ) {
320 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines
( $borrower->{'borrowernumber'} );
321 if ($fines && $fines > 0) {
322 $template->param( fines
=> sprintf("%.2f",$fines) );
323 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
327 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
328 #Check for waiting holds
329 my @reserves = GetReservesFromBorrowernumber
($borrower->{'borrowernumber'});
330 my $waiting_holds = 0;
331 foreach my $num_res (@reserves) {
332 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
336 if ($waiting_holds > 0) {
338 waiting_holds
=> $waiting_holds,
339 holdsborrowernumber
=> $borrower->{'borrowernumber'},
340 holdsfirstname
=> $borrower->{'firstname'},
341 holdssurname
=> $borrower->{'surname'},
346 $template->param( privacy
=> $borrower->{privacy
} );
348 $template->param( inputloop
=> \
@inputloop );
354 # new op dev : we check if the document must be returned to his homebranch directly,
355 # if the document is transfered, we have warning message .
357 if ( $messages->{'WasTransfered'} ) {
361 itemnumber
=> $itemnumber,
365 if ( $messages->{'NeedsTransfer'} ){
368 needstransfer
=> $messages->{'NeedsTransfer'},
369 itemnumber
=> $itemnumber,
373 if ( $messages->{'Wrongbranch'} ){
379 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
381 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
384 TransferWaitingAt
=> $messages->{'WrongTransfer'},
385 WrongTransferItem
=> $messages->{'WrongTransferItem'},
386 itemnumber
=> $itemnumber,
389 my $reserve = $messages->{'ResFound'};
390 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
391 my ($borr) = GetMemberDetails
( $reserve->{'borrowernumber'}, 0 );
392 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
395 wborfirstname
=> $borr->{'firstname'},
396 wborsurname
=> $borr->{'surname'},
397 wbortitle
=> $borr->{'title'},
398 wborphone
=> $borr->{'phone'},
399 wboremail
=> $borr->{'email'},
400 wborstnum
=> $borr->{streetnumber
},
401 wboraddress
=> $borr->{'address'},
402 wboraddress2
=> $borr->{'address2'},
403 wborcity
=> $borr->{'city'},
404 wborzip
=> $borr->{'zipcode'},
405 wborrowernumber
=> $reserve->{'borrowernumber'},
406 wborcnum
=> $borr->{'cardnumber'},
407 wtransfertFrom
=> $userenv_branch,
412 # reserve found and item arrived at the expected branch
414 if ( $messages->{'ResFound'}) {
415 my $reserve = $messages->{'ResFound'};
416 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
417 my ($borr) = GetMemberDetails
( $reserve->{'borrowernumber'}, 0 );
419 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
420 if ( $reserve->{'ResFound'} eq "Waiting" ) {
422 waiting
=> ($userenv_branch eq $reserve->{'branchcode'} ?
1 : 0 ),
424 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
426 intransit
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
427 transfertodo
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
428 resbarcode
=> $barcode,
433 # same params for Waiting or Reserved
436 currentbranch
=> $branches->{$userenv_branch}->{'branchname'},
437 destbranchname
=> $branches->{ $reserve->{'branchcode'} }->{'branchname'},
438 name
=> $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
439 borfirstname
=> $borr->{'firstname'},
440 borsurname
=> $borr->{'surname'},
441 bortitle
=> $borr->{'title'},
442 borphone
=> $borr->{'phone'},
443 boremail
=> $borr->{'email'},
444 boraddress
=> $borr->{'address'},
445 boraddress2
=> $borr->{'address2'},
446 borstnum
=> $borr->{streetnumber
},
447 borcity
=> $borr->{'city'},
448 borzip
=> $borr->{'zipcode'},
449 borcnum
=> $borr->{'cardnumber'},
450 debarred
=> $borr->{'debarred'},
451 gonenoaddress
=> $borr->{'gonenoaddress'},
453 destbranch
=> $reserve->{'branchcode'},
454 borrowernumber
=> $reserve->{'borrowernumber'},
455 itemnumber
=> $reserve->{'itemnumber'},
456 reservenotes
=> $reserve->{'reservenotes'},
458 } # else { ; } # error?
463 foreach my $code ( keys %$messages ) {
465 my $exit_required_p = 0;
466 if ( $code eq 'BadBarcode' ) {
467 $err{badbarcode
} = 1;
468 $err{msg
} = $messages->{'BadBarcode'};
470 elsif ( $code eq 'NotIssued' ) {
473 $err{msg
} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'} if $messages->{'IsPermanent'};
475 elsif ( $code eq 'LocalUse' ) {
478 elsif ( $code eq 'WasLost' ) {
481 elsif ( $code eq 'LostItemFeeRefunded' ) {
482 $template->param( LostItemFeeRefunded
=> 1 );
484 elsif ( $code eq 'ResFound' ) {
485 ; # FIXME... anything to do here?
487 elsif ( $code eq 'WasReturned' ) {
488 ; # FIXME... anything to do here?
490 elsif ( $code eq 'WasTransfered' ) {
491 ; # FIXME... anything to do here?
493 elsif ( $code eq 'withdrawn' ) {
495 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
497 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
498 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
499 $err{ispermanent
} = 1;
501 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
504 elsif ( $code eq 'WrongTransfer' ) {
505 ; # FIXME... anything to do here?
507 elsif ( $code eq 'WrongTransferItem' ) {
508 ; # FIXME... anything to do here?
510 elsif ( $code eq 'NeedsTransfer' ) {
512 elsif ( $code eq 'Wrongbranch' ) {
514 elsif ( $code eq 'Debarred' ) {
515 $err{debarred
} = $messages->{'Debarred'};
516 $err{debarcardnumber
} = $borrower->{cardnumber
};
517 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
518 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
520 elsif ( $code eq 'PrevDebarred' ) {
521 $err{prevdebarred
} = $messages->{'PrevDebarred'};
523 elsif ( $code eq 'ForeverDebarred' ) {
524 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
526 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
527 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
530 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
531 # This forces the issue of staying in sync w/ Circulation.pm
534 push( @errmsgloop, \
%err );
536 last if $exit_required_p;
538 $template->param( errmsgloop
=> \
@errmsgloop );
540 #set up so only the last 8 returned items display (make for faster loading pages)
541 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
544 my $shelflocations = GetKohaAuthorisedValues
('items.location','');
545 foreach ( sort { $a <=> $b } keys %returneditems ) {
547 if ( $count++ < $returned_counter ) {
548 my $bar_code = $returneditems{$_};
549 if ($riduedate{$_}) {
550 my $duedate = dt_from_string
( $riduedate{$_}, 'sql');
551 $ri{year
} = $duedate->year();
552 $ri{month
} = $duedate->month();
553 $ri{day
} = $duedate->day();
554 $ri{hour
} = $duedate->hour();
555 $ri{minute
} = $duedate->minute();
556 $ri{duedate
} = output_pref
($duedate);
557 my ($b) = GetMemberDetails
( $riborrowernumber{$_}, 0 );
558 unless ( $dropboxmode ) {
559 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, DateTime
->now()) == -1);
561 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, $dropboxdate) == -1);
563 $ri{borrowernumber
} = $b->{'borrowernumber'};
564 $ri{borcnum
} = $b->{'cardnumber'};
565 $ri{borfirstname
} = $b->{'firstname'};
566 $ri{borsurname
} = $b->{'surname'};
567 $ri{bortitle
} = $b->{'title'};
568 $ri{bornote
} = $b->{'borrowernotes'};
569 $ri{borcategorycode
}= $b->{'categorycode'};
572 $ri{borrowernumber
} = $riborrowernumber{$_};
576 my $biblio = GetBiblioFromItemNumber
(GetItemnumberFromBarcode
($bar_code));
577 my $item = GetItem
( GetItemnumberFromBarcode
($bar_code) );
578 # fix up item type for display
579 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
580 $ri{itembiblionumber
} = $biblio->{'biblionumber'};
581 $ri{itemtitle
} = $biblio->{'title'};
582 $ri{itemauthor
} = $biblio->{'author'};
583 $ri{itemcallnumber
} = $biblio->{'itemcallnumber'};
584 $ri{itemtype
} = $biblio->{'itemtype'};
585 $ri{itemnote
} = $biblio->{'itemnotes'};
586 $ri{itemnotes_nonpublic
} = $item->{'itemnotes_nonpublic'};
587 $ri{ccode
} = $biblio->{'ccode'};
588 $ri{itemnumber
} = $biblio->{'itemnumber'};
589 $ri{barcode
} = $bar_code;
590 $ri{homebranch
} = $item->{'homebranch'};
591 $ri{holdingbranch
} = $item->{'holdingbranch'};
593 $ri{location
} = $biblio->{'location'};
594 my $shelfcode = $ri{'location'};
595 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
603 my ($genbrname, $genprname);
604 if (my $b = $branches->{$userenv_branch}) {
605 $genbrname = $b->{'branchname'};
607 if (my $p = $printers->{$printer}) {
608 $genprname = $p->{'printername'};
612 genbrname
=> $genbrname,
613 genprname
=> $genprname,
614 branchname
=> $genbrname,
616 errmsgloop
=> \
@errmsgloop,
617 exemptfine
=> $exemptfine,
618 dropboxmode
=> $dropboxmode,
619 dropboxdate
=> output_pref
($dropboxdate),
620 forgivemanualholdsexpire
=> $forgivemanualholdsexpire,
621 overduecharges
=> $overduecharges,
622 AudioAlerts
=> C4
::Context
->preference("AudioAlerts"),
623 BlockReturnOfWithdrawnItems
=> C4
::Context
->preference("BlockReturnOfWithdrawnItems"),
626 $itemnumber = GetItemnumberFromBarcode
( $barcode );
628 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
629 $holdingBranch //= '';
630 $collectionBranch //= '';
631 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
633 collectionItemNeedsTransferred
=> 1,
634 collectionBranch
=> GetBranchName
($collectionBranch),
639 # actually print the page!
640 output_html_with_http_headers
$query, $cookie, $template->output;