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
36 use C4
::Auth qw
/:DEFAULT get_session/;
44 use C4
::Branch
; # GetBranches GetBranchName
45 use C4
::Koha
; # FIXME : is it still useful ?
46 use C4
::RotatingCollections
;
53 my ( $template, $librarian, $cookie ) = get_template_and_user
(
55 template_name
=> "circ/returns.tt",
59 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
63 my $sessionID = $query->cookie("CGISESSID");
64 my $session = get_session
($sessionID);
65 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
66 # no branch set we can't return
67 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
71 # Print a reserve slip on this page
72 if ( $query->param('print_slip') ) {
75 borrowernumber
=> $query->param('borrowernumber'),
76 biblionumber
=> $query->param('biblionumber'),
82 my $branches = GetBranches
();
83 my $printers = GetPrinters
();
84 my $userenv = C4
::Context
->userenv;
85 my $userenv_branch = $userenv->{'branch'} // '';
86 my $printer = $userenv->{'branchprinter'} // '';
88 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
90 # Some code to handle the error if there is no branch or printer setting.....
93 # Set up the item stack ....
98 foreach ( $query->param ) {
111 my $barcode = $query->param("ri-$counter");
112 my $duedate = $query->param("dd-$counter");
113 my $borrowernumber = $query->param("bn-$counter");
116 # decode barcode ## Didn't we already decode them before passing them back last time??
117 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
118 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
120 ######################
121 #Are these lines still useful ?
122 $returneditems{$counter} = $barcode;
123 $riduedate{$counter} = $duedate;
124 $riborrowernumber{$counter} = $borrowernumber;
126 #######################
127 $input{counter
} = $counter;
128 $input{barcode
} = $barcode;
129 $input{duedate
} = $duedate;
130 $input{borrowernumber
} = $borrowernumber;
131 push( @inputloop, \
%input );
135 # Deal with the requests....
137 if ($query->param('WT-itemNumber')){
138 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
141 if ( $query->param('resbarcode') ) {
142 my $item = $query->param('itemnumber');
143 my $borrowernumber = $query->param('borrowernumber');
144 my $resbarcode = $query->param('resbarcode');
145 my $diffBranchReturned = $query->param('diffBranch');
146 my $iteminfo = GetBiblioFromItemNumber
($item);
147 # fix up item type for display
148 $iteminfo->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$iteminfo->{'itype'} : $iteminfo->{'itemtype'};
149 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ?
$diffBranchReturned : undef;
150 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
151 # i.e., whether to apply waiting status
152 ModReserveAffect
( $item, $borrowernumber, $diffBranchSend);
153 # check if we have other reserves for this document, if we have a return send the message of transfer
154 my ( $messages, $nextreservinfo ) = GetOtherReserves
($item);
156 my ($borr) = GetMemberDetails
( $nextreservinfo, 0 );
157 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
158 if ( $messages->{'transfert'} ) {
160 itemtitle
=> $iteminfo->{'title'},
161 itemnumber
=> $iteminfo->{'itemnumber'},
162 itembiblionumber
=> $iteminfo->{'biblionumber'},
163 iteminfo
=> $iteminfo->{'author'},
164 tobranchname
=> GetBranchName
($messages->{'transfert'}),
166 borrowernumber
=> $borrowernumber,
167 borcnum
=> $borr->{'cardnumber'},
168 borfirstname
=> $borr->{'firstname'},
169 borsurname
=> $borr->{'surname'},
178 my $issueinformation;
180 my $barcode = $query->param('barcode');
181 my $exemptfine = $query->param('exemptfine');
184 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
186 # silently prevent unauthorized operator from forgiving overdue
187 # fines by manually tweaking form parameters
190 my $dropboxmode = $query->param('dropboxmode');
191 my $dotransfer = $query->param('dotransfer');
192 my $canceltransfer = $query->param('canceltransfer');
193 my $dest = $query->param('dest');
194 my $calendar = Koha
::Calendar
->new( branchcode
=> $userenv_branch );
195 #dropbox: get last open day (today - 1)
196 my $today = DateTime
->now( time_zone
=> C4
::Context
->tz());
197 my $dropboxdate = $calendar->addDate($today, -1);
199 my $return_date_override = $query->param('return_date_override');
200 my $return_date_override_remember =
201 $query->param('return_date_override_remember');
202 if ($return_date_override) {
203 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
204 # FIXME we really need to stop adding more uses of C4::Dates
205 if ( $return_date_override =~ C4
::Dates
->regexp('syspref') ) {
207 # note that we've overriden the return date
208 $template->param( return_date_was_overriden
=> 1);
209 # Save the original format if we are remembering for this series
211 return_date_override
=> $return_date_override,
212 return_date_override_remember
=> 1
213 ) if ($return_date_override_remember);
215 my $dt = dt_from_string
($return_date_override);
216 $return_date_override =
217 DateTime
::Format
::MySQL
->format_datetime($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 ModItemTransfer
($transferitem, $userenv_branch, $tobranch);
232 if ($canceltransfer){
233 $itemnumber=$query->param('itemnumber');
234 DeleteTransfer
($itemnumber);
236 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
239 $template->param( transfercancelled
=> 1);
243 # actually return book and prepare item table.....
245 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
246 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
247 $itemnumber = GetItemnumberFromBarcode
($barcode);
252 ( $returned, $messages, $issueinformation, $borrower ) =
253 AddReturn
( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override, $dropboxdate );
254 my $homeorholdingbranchreturn = C4
::Context
->preference('HomeOrHoldingBranchReturn');
255 $homeorholdingbranchreturn ||= 'homebranch';
257 # get biblio description
258 my $biblio = GetBiblioFromItemNumber
($itemnumber);
259 # fix up item type for display
260 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
262 # Check if we should display a checkin message, based on the the item
263 # type of the checked in item
264 my $itemtype = C4
::ItemType
->get( $biblio->{'itemtype'} );
265 if ( $itemtype->{'checkinmsg'} ) {
267 checkinmsg
=> $itemtype->{'checkinmsg'},
268 checkinmsgtype
=> $itemtype->{'checkinmsgtype'},
273 title
=> $biblio->{'title'},
274 homebranch
=> $biblio->{'homebranch'},
275 homebranchname
=> GetBranchName
( $biblio->{$homeorholdingbranchreturn} ),
276 author
=> $biblio->{'author'},
277 itembarcode
=> $biblio->{'barcode'},
278 itemtype
=> $biblio->{'itemtype'},
279 ccode
=> $biblio->{'ccode'},
280 itembiblionumber
=> $biblio->{'biblionumber'},
281 biblionumber
=> $biblio->{'biblionumber'},
282 borrower
=> $borrower,
283 additional_materials
=> $biblio->{'materials'},
293 my $time_now = DateTime
->now( time_zone
=> C4
::Context
->tz )->truncate( to
=> 'minute');
294 my $duedate = $issueinformation->{date_due
}->strftime('%Y-%m-%d %H:%M');
295 $returneditems{0} = $barcode;
296 $riborrowernumber{0} = $borrower->{'borrowernumber'};
297 $riduedate{0} = $duedate;
298 $input{borrowernumber
} = $borrower->{'borrowernumber'};
299 $input{duedate
} = $duedate;
300 unless ( $dropboxmode ) {
301 $input{return_overdue
} = 1 if (DateTime
->compare($issueinformation->{date_due
}, DateTime
->now()) == -1);
303 $input{return_overdue
} = 1 if (DateTime
->compare($issueinformation->{date_due
}, $dropboxdate) == -1);
305 push( @inputloop, \
%input );
307 if ( C4
::Context
->preference("FineNotifyAtCheckin") ) {
308 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines
( $borrower->{'borrowernumber'} );
309 if ($fines && $fines > 0) {
310 $template->param( fines
=> sprintf("%.2f",$fines) );
311 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
315 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
316 #Check for waiting holds
317 my @reserves = GetReservesFromBorrowernumber
($borrower->{'borrowernumber'});
318 my $waiting_holds = 0;
319 foreach my $num_res (@reserves) {
320 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
324 if ($waiting_holds > 0) {
326 waiting_holds
=> $waiting_holds,
327 holdsborrowernumber
=> $borrower->{'borrowernumber'},
328 holdsfirstname
=> $borrower->{'firstname'},
329 holdssurname
=> $borrower->{'surname'},
334 elsif ( !$messages->{'BadBarcode'} ) {
336 $returneditems{0} = $barcode;
338 push( @inputloop, \
%input );
340 $template->param( privacy
=> $borrower->{privacy
} );
342 $template->param( inputloop
=> \
@inputloop );
348 # new op dev : we check if the document must be returned to his homebranch directly,
349 # if the document is transfered, we have warning message .
351 if ( $messages->{'WasTransfered'} ) {
355 itemnumber
=> $itemnumber,
359 if ( $messages->{'NeedsTransfer'} ){
363 itemnumber
=> $itemnumber,
367 if ( $messages->{'Wrongbranch'} ){
373 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
375 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
378 TransferWaitingAt
=> $messages->{'WrongTransfer'},
379 WrongTransferItem
=> $messages->{'WrongTransferItem'},
380 itemnumber
=> $itemnumber,
383 my $reserve = $messages->{'ResFound'};
384 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
385 my ($borr) = GetMemberDetails
( $reserve->{'borrowernumber'}, 0 );
386 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
389 wborfirstname
=> $borr->{'firstname'},
390 wborsurname
=> $borr->{'surname'},
391 wbortitle
=> $borr->{'title'},
392 wborphone
=> $borr->{'phone'},
393 wboremail
=> $borr->{'email'},
394 wborstnum
=> $borr->{streetnumber
},
395 wboraddress
=> $borr->{'address'},
396 wboraddress2
=> $borr->{'address2'},
397 wborcity
=> $borr->{'city'},
398 wborzip
=> $borr->{'zipcode'},
399 wborrowernumber
=> $reserve->{'borrowernumber'},
400 wborcnum
=> $borr->{'cardnumber'},
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 $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
411 my ($borr) = GetMemberDetails
( $reserve->{'borrowernumber'}, 0 );
413 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
414 if ( $reserve->{'ResFound'} eq "Waiting" ) {
416 waiting
=> ($userenv_branch eq $reserve->{'branchcode'} ?
1 : 0 ),
418 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
420 intransit
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
421 transfertodo
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
422 resbarcode
=> $barcode,
427 # same params for Waiting or Reserved
430 currentbranch
=> $branches->{$userenv_branch}->{'branchname'},
431 destbranchname
=> $branches->{ $reserve->{'branchcode'} }->{'branchname'},
432 name
=> $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
433 borfirstname
=> $borr->{'firstname'},
434 borsurname
=> $borr->{'surname'},
435 bortitle
=> $borr->{'title'},
436 borphone
=> $borr->{'phone'},
437 boremail
=> $borr->{'email'},
438 boraddress
=> $borr->{'address'},
439 boraddress2
=> $borr->{'address2'},
440 borstnum
=> $borr->{streetnumber
},
441 borcity
=> $borr->{'city'},
442 borzip
=> $borr->{'zipcode'},
443 borcnum
=> $borr->{'cardnumber'},
444 debarred
=> $borr->{'debarred'},
445 gonenoaddress
=> $borr->{'gonenoaddress'},
447 destbranch
=> $reserve->{'branchcode'},
448 borrowernumber
=> $reserve->{'borrowernumber'},
449 itemnumber
=> $reserve->{'itemnumber'},
450 reservenotes
=> $reserve->{'reservenotes'},
452 } # else { ; } # error?
457 foreach my $code ( keys %$messages ) {
459 my $exit_required_p = 0;
460 if ( $code eq 'BadBarcode' ) {
461 $err{badbarcode
} = 1;
462 $err{msg
} = $messages->{'BadBarcode'};
464 elsif ( $code eq 'NotIssued' ) {
467 $err{msg
} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'} if $messages->{'IsPermanent'};
469 elsif ( $code eq 'LocalUse' ) {
472 elsif ( $code eq 'WasLost' ) {
475 elsif ( $code eq 'LostItemFeeRefunded' ) {
476 $template->param( LostItemFeeRefunded
=> 1 );
478 elsif ( $code eq 'ResFound' ) {
479 ; # FIXME... anything to do here?
481 elsif ( $code eq 'WasReturned' ) {
482 ; # FIXME... anything to do here?
484 elsif ( $code eq 'WasTransfered' ) {
485 ; # FIXME... anything to do here?
487 elsif ( $code eq 'withdrawn' ) {
489 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
491 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
492 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
493 $err{ispermanent
} = 1;
495 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
498 elsif ( $code eq 'WrongTransfer' ) {
499 ; # FIXME... anything to do here?
501 elsif ( $code eq 'WrongTransferItem' ) {
502 ; # FIXME... anything to do here?
504 elsif ( $code eq 'NeedsTransfer' ) {
506 elsif ( $code eq 'Wrongbranch' ) {
508 elsif ( $code eq 'Debarred' ) {
509 $err{debarred
} = $messages->{'Debarred'};
510 $err{debarcardnumber
} = $borrower->{cardnumber
};
511 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
512 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
514 elsif ( $code eq 'PrevDebarred' ) {
515 $err{prevdebarred
} = $messages->{'PrevDebarred'};
517 elsif ( $code eq 'ForeverDebarred' ) {
518 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
520 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
521 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
524 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
525 # This forces the issue of staying in sync w/ Circulation.pm
528 push( @errmsgloop, \
%err );
530 last if $exit_required_p;
532 $template->param( errmsgloop
=> \
@errmsgloop );
534 #set up so only the last 8 returned items display (make for faster loading pages)
535 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
538 my $shelflocations = GetKohaAuthorisedValues
('items.location','');
539 foreach ( sort { $a <=> $b } keys %returneditems ) {
541 if ( $count++ < $returned_counter ) {
542 my $bar_code = $returneditems{$_};
543 if ($riduedate{$_}) {
544 my $duedate = dt_from_string
( $riduedate{$_}, 'sql');
545 $ri{year
} = $duedate->year();
546 $ri{month
} = $duedate->month();
547 $ri{day
} = $duedate->day();
548 $ri{hour
} = $duedate->hour();
549 $ri{minute
} = $duedate->minute();
550 $ri{duedate
} = output_pref
($duedate);
551 my ($b) = GetMemberDetails
( $riborrowernumber{$_}, 0 );
552 unless ( $dropboxmode ) {
553 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, DateTime
->now()) == -1);
555 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, $dropboxdate) == -1);
557 $ri{borrowernumber
} = $b->{'borrowernumber'};
558 $ri{borcnum
} = $b->{'cardnumber'};
559 $ri{borfirstname
} = $b->{'firstname'};
560 $ri{borsurname
} = $b->{'surname'};
561 $ri{bortitle
} = $b->{'title'};
562 $ri{bornote
} = $b->{'borrowernotes'};
563 $ri{borcategorycode
}= $b->{'categorycode'};
566 $ri{borrowernumber
} = $riborrowernumber{$_};
570 my $biblio = GetBiblioFromItemNumber
(GetItemnumberFromBarcode
($bar_code));
571 my $item = GetItem
( GetItemnumberFromBarcode
($bar_code) );
572 # fix up item type for display
573 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
574 $ri{itembiblionumber
} = $biblio->{'biblionumber'};
575 $ri{itemtitle
} = $biblio->{'title'};
576 $ri{itemauthor
} = $biblio->{'author'};
577 $ri{itemcallnumber
} = $biblio->{'itemcallnumber'};
578 $ri{itemtype
} = $biblio->{'itemtype'};
579 $ri{itemnote
} = $biblio->{'itemnotes'};
580 $ri{ccode
} = $biblio->{'ccode'};
581 $ri{itemnumber
} = $biblio->{'itemnumber'};
582 $ri{barcode
} = $bar_code;
583 $ri{homebranch
} = $item->{'homebranch'};
584 $ri{holdingbranch
} = $item->{'holdingbranch'};
586 $ri{location
} = $biblio->{'location'};
587 my $shelfcode = $ri{'location'};
588 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
596 my ($genbrname, $genprname);
597 if (my $b = $branches->{$userenv_branch}) {
598 $genbrname = $b->{'branchname'};
600 if (my $p = $printers->{$printer}) {
601 $genprname = $p->{'printername'};
605 genbrname
=> $genbrname,
606 genprname
=> $genprname,
607 branchname
=> $genbrname,
609 errmsgloop
=> \
@errmsgloop,
610 exemptfine
=> $exemptfine,
611 dropboxmode
=> $dropboxmode,
612 dropboxdate
=> output_pref
($dropboxdate),
613 overduecharges
=> $overduecharges,
614 soundon
=> C4
::Context
->preference("SoundOn"),
615 BlockReturnOfWithdrawnItems
=> C4
::Context
->preference("BlockReturnOfWithdrawnItems"),
618 $itemnumber = GetItemnumberFromBarcode
( $barcode );
620 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
621 $holdingBranch //= '';
622 $collectionBranch //= '';
623 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
625 collectionItemNeedsTransferred
=> 1,
626 collectionBranch
=> GetBranchName
($collectionBranch),
631 # actually print the page!
632 output_html_with_http_headers
$query, $cookie, $template->output;