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 under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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");
73 my $branches = GetBranches
();
74 my $printers = GetPrinters
();
75 my $userenv = C4
::Context
->userenv;
76 my $userenv_branch = $userenv->{'branch'} // '';
77 my $printer = $userenv->{'branchprinter'} // '';
79 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
81 # Some code to handle the error if there is no branch or printer setting.....
84 # Set up the item stack ....
89 foreach ( $query->param ) {
102 my $barcode = $query->param("ri-$counter");
103 my $duedate = $query->param("dd-$counter");
104 my $borrowernumber = $query->param("bn-$counter");
107 # decode barcode ## Didn't we already decode them before passing them back last time??
108 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
109 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
111 ######################
112 #Are these lines still useful ?
113 $returneditems{$counter} = $barcode;
114 $riduedate{$counter} = $duedate;
115 $riborrowernumber{$counter} = $borrowernumber;
117 #######################
118 $input{counter
} = $counter;
119 $input{barcode
} = $barcode;
120 $input{duedate
} = $duedate;
121 $input{borrowernumber
} = $borrowernumber;
122 push( @inputloop, \
%input );
126 # Deal with the requests....
128 if ($query->param('WT-itemNumber')){
129 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
132 if ( $query->param('resbarcode') ) {
133 my $item = $query->param('itemnumber');
134 my $borrowernumber = $query->param('borrowernumber');
135 my $resbarcode = $query->param('resbarcode');
136 my $diffBranchReturned = $query->param('diffBranch');
137 my $iteminfo = GetBiblioFromItemNumber
($item);
138 # fix up item type for display
139 $iteminfo->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$iteminfo->{'itype'} : $iteminfo->{'itemtype'};
140 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ?
$diffBranchReturned : undef;
141 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
142 # i.e., whether to apply waiting status
143 ModReserveAffect
( $item, $borrowernumber, $diffBranchSend);
144 # check if we have other reserves for this document, if we have a return send the message of transfer
145 my ( $messages, $nextreservinfo ) = GetOtherReserves
($item);
147 my ($borr) = GetMemberDetails
( $nextreservinfo, 0 );
148 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
149 if ( $messages->{'transfert'} ) {
151 itemtitle
=> $iteminfo->{'title'},
152 itemnumber
=> $iteminfo->{'itemnumber'},
153 itembiblionumber
=> $iteminfo->{'biblionumber'},
154 iteminfo
=> $iteminfo->{'author'},
155 tobranchname
=> GetBranchName
($messages->{'transfert'}),
157 borrowernumber
=> $borrowernumber,
158 borcnum
=> $borr->{'cardnumber'},
159 borfirstname
=> $borr->{'firstname'},
160 borsurname
=> $borr->{'surname'},
169 my $issueinformation;
171 my $barcode = $query->param('barcode');
172 my $exemptfine = $query->param('exemptfine');
175 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
177 # silently prevent unauthorized operator from forgiving overdue
178 # fines by manually tweaking form parameters
181 my $dropboxmode = $query->param('dropboxmode');
182 my $dotransfer = $query->param('dotransfer');
183 my $canceltransfer = $query->param('canceltransfer');
184 my $dest = $query->param('dest');
185 my $calendar = Koha
::Calendar
->new( branchcode
=> $userenv_branch );
186 #dropbox: get last open day (today - 1)
187 my $today = DateTime
->now( time_zone
=> C4
::Context
->tz());
188 my $dropboxdate = $calendar->addDate($today, -1);
190 my $return_date_override = $query->param('return_date_override');
191 my $return_date_override_remember =
192 $query->param('return_date_override_remember');
193 if ($return_date_override) {
194 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
195 # FIXME we really need to stop adding more uses of C4::Dates
196 if ( $return_date_override =~ C4
::Dates
->regexp('syspref') ) {
198 # note that we've overriden the return date
199 $template->param( return_date_was_overriden
=> 1);
200 # Save the original format if we are remembering for this series
202 return_date_override
=> $return_date_override,
203 return_date_override_remember
=> 1
204 ) if ($return_date_override_remember);
206 my $dt = dt_from_string
($return_date_override);
207 $return_date_override =
208 DateTime
::Format
::MySQL
->format_datetime($dt);
212 $return_date_override = q{};
217 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
218 my $transferitem = $query->param('transferitem');
219 my $tobranch = $query->param('tobranch');
220 ModItemTransfer
($transferitem, $userenv_branch, $tobranch);
223 if ($canceltransfer){
224 $itemnumber=$query->param('itemnumber');
225 DeleteTransfer
($itemnumber);
227 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
230 $template->param( transfercancelled
=> 1);
234 # actually return book and prepare item table.....
236 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
237 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
238 $itemnumber = GetItemnumberFromBarcode
($barcode);
243 ( $returned, $messages, $issueinformation, $borrower ) =
244 AddReturn
( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override );
245 my $homeorholdingbranchreturn = C4
::Context
->preference('HomeOrHoldingBranchReturn');
246 $homeorholdingbranchreturn ||= 'homebranch';
248 # get biblio description
249 my $biblio = GetBiblioFromItemNumber
($itemnumber);
250 # fix up item type for display
251 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
253 # Check if we should display a checkin message, based on the the item
254 # type of the checked in item
255 my $itemtype = C4
::ItemType
->get( $biblio->{'itemtype'} );
256 if ( $itemtype->{'checkinmsg'} ) {
258 checkinmsg
=> $itemtype->{'checkinmsg'},
259 checkinmsgtype
=> $itemtype->{'checkinmsgtype'},
264 title
=> $biblio->{'title'},
265 homebranch
=> $biblio->{'homebranch'},
266 homebranchname
=> GetBranchName
( $biblio->{$homeorholdingbranchreturn} ),
267 author
=> $biblio->{'author'},
268 itembarcode
=> $biblio->{'barcode'},
269 itemtype
=> $biblio->{'itemtype'},
270 ccode
=> $biblio->{'ccode'},
271 itembiblionumber
=> $biblio->{'biblionumber'},
272 borrower
=> $borrower,
273 additional_materials
=> $biblio->{'materials'},
283 my $time_now = DateTime
->now( time_zone
=> C4
::Context
->tz )->truncate( to
=> 'minute');
284 my $duedate = $issueinformation->{date_due
}->strftime('%Y-%m-%d %H:%M');
285 $returneditems{0} = $barcode;
286 $riborrowernumber{0} = $borrower->{'borrowernumber'};
287 $riduedate{0} = $duedate;
288 $input{borrowernumber
} = $borrower->{'borrowernumber'};
289 $input{duedate
} = $duedate;
290 $input{return_overdue
} = 1 if (DateTime
->compare($issueinformation->{date_due
}, $time_now) == -1);
291 push( @inputloop, \
%input );
293 if ( C4
::Context
->preference("FineNotifyAtCheckin") ) {
294 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines
( $borrower->{'borrowernumber'} );
295 if ($fines && $fines > 0) {
296 $template->param( fines
=> sprintf("%.2f",$fines) );
297 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
301 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
302 #Check for waiting holds
303 my @reserves = GetReservesFromBorrowernumber
($borrower->{'borrowernumber'});
304 my $waiting_holds = 0;
305 foreach my $num_res (@reserves) {
306 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
310 if ($waiting_holds > 0) {
312 waiting_holds
=> $waiting_holds,
313 holdsborrowernumber
=> $borrower->{'borrowernumber'},
314 holdsfirstname
=> $borrower->{'firstname'},
315 holdssurname
=> $borrower->{'surname'},
320 elsif ( !$messages->{'BadBarcode'} ) {
322 $returneditems{0} = $barcode;
324 push( @inputloop, \
%input );
327 $template->param( inputloop
=> \
@inputloop );
333 # new op dev : we check if the document must be returned to his homebranch directly,
334 # if the document is transfered, we have warning message .
336 if ( $messages->{'WasTransfered'} ) {
340 itemnumber
=> $itemnumber,
344 if ( $messages->{'NeedsTransfer'} ){
348 itemnumber
=> $itemnumber,
352 if ( $messages->{'Wrongbranch'} ){
358 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
360 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
363 TransferWaitingAt
=> $messages->{'WrongTransfer'},
364 WrongTransferItem
=> $messages->{'WrongTransferItem'},
365 itemnumber
=> $itemnumber,
368 my $reserve = $messages->{'ResFound'};
369 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
370 my ($borr) = GetMemberDetails
( $reserve->{'borrowernumber'}, 0 );
371 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
374 wborfirstname
=> $borr->{'firstname'},
375 wborsurname
=> $borr->{'surname'},
376 wbortitle
=> $borr->{'title'},
377 wborphone
=> $borr->{'phone'},
378 wboremail
=> $borr->{'email'},
379 wborstnum
=> $borr->{streetnumber
},
380 wboraddress
=> $borr->{'address'},
381 wboraddress2
=> $borr->{'address2'},
382 wborcity
=> $borr->{'city'},
383 wborzip
=> $borr->{'zipcode'},
384 wborrowernumber
=> $reserve->{'borrowernumber'},
385 wborcnum
=> $borr->{'cardnumber'},
386 wtransfertFrom
=> $userenv_branch,
391 # reserve found and item arrived at the expected branch
393 if ( $messages->{'ResFound'}) {
394 my $reserve = $messages->{'ResFound'};
395 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
396 my ($borr) = GetMemberDetails
( $reserve->{'borrowernumber'}, 0 );
398 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
399 if ( $reserve->{'ResFound'} eq "Waiting" ) {
401 waiting
=> ($userenv_branch eq $reserve->{'branchcode'} ?
1 : 0 ),
403 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
405 intransit
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
406 transfertodo
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
407 resbarcode
=> $barcode,
412 # same params for Waiting or Reserved
415 currentbranch
=> $branches->{$userenv_branch}->{'branchname'},
416 destbranchname
=> $branches->{ $reserve->{'branchcode'} }->{'branchname'},
417 name
=> $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
418 borfirstname
=> $borr->{'firstname'},
419 borsurname
=> $borr->{'surname'},
420 bortitle
=> $borr->{'title'},
421 borphone
=> $borr->{'phone'},
422 boremail
=> $borr->{'email'},
423 boraddress
=> $borr->{'address'},
424 boraddress2
=> $borr->{'address2'},
425 borstnum
=> $borr->{streetnumber
},
426 borcity
=> $borr->{'city'},
427 borzip
=> $borr->{'zipcode'},
428 borcnum
=> $borr->{'cardnumber'},
429 debarred
=> $borr->{'debarred'},
430 gonenoaddress
=> $borr->{'gonenoaddress'},
432 destbranch
=> $reserve->{'branchcode'},
433 borrowernumber
=> $reserve->{'borrowernumber'},
434 itemnumber
=> $reserve->{'itemnumber'},
435 reservenotes
=> $reserve->{'reservenotes'},
437 } # else { ; } # error?
442 foreach my $code ( keys %$messages ) {
444 my $exit_required_p = 0;
445 if ( $code eq 'BadBarcode' ) {
446 $err{badbarcode
} = 1;
447 $err{msg
} = $messages->{'BadBarcode'};
449 elsif ( $code eq 'NotIssued' ) {
451 $err{msg
} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
453 elsif ( $code eq 'LocalUse' ) {
456 elsif ( $code eq 'WasLost' ) {
459 elsif ( $code eq 'LostItemFeeRefunded' ) {
460 $template->param( LostItemFeeRefunded
=> 1 );
462 elsif ( $code eq 'ResFound' ) {
463 ; # FIXME... anything to do here?
465 elsif ( $code eq 'WasReturned' ) {
466 ; # FIXME... anything to do here?
468 elsif ( $code eq 'WasTransfered' ) {
469 ; # FIXME... anything to do here?
471 elsif ( $code eq 'withdrawn' ) {
473 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
475 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
476 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
477 $err{ispermanent
} = 1;
479 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
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
};
508 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
509 # This forces the issue of staying in sync w/ Circulation.pm
512 push( @errmsgloop, \
%err );
514 last if $exit_required_p;
516 $template->param( errmsgloop
=> \
@errmsgloop );
518 #set up so only the last 8 returned items display (make for faster loading pages)
519 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
522 my $shelflocations = GetKohaAuthorisedValues
('items.location','');
523 foreach ( sort { $a <=> $b } keys %returneditems ) {
525 if ( $count++ < $returned_counter ) {
526 my $bar_code = $returneditems{$_};
527 if ($riduedate{$_}) {
528 my $duedate = dt_from_string
( $riduedate{$_}, 'sql');
529 $ri{year
} = $duedate->year();
530 $ri{month
} = $duedate->month();
531 $ri{day
} = $duedate->day();
532 $ri{hour
} = $duedate->hour();
533 $ri{minute
} = $duedate->minute();
534 $ri{duedate
} = output_pref
($duedate);
535 my ($b) = GetMemberDetails
( $riborrowernumber{$_}, 0 );
536 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, DateTime
->now()) == -1 );
537 $ri{borrowernumber
} = $b->{'borrowernumber'};
538 $ri{borcnum
} = $b->{'cardnumber'};
539 $ri{borfirstname
} = $b->{'firstname'};
540 $ri{borsurname
} = $b->{'surname'};
541 $ri{bortitle
} = $b->{'title'};
542 $ri{bornote
} = $b->{'borrowernotes'};
543 $ri{borcategorycode
}= $b->{'categorycode'};
546 $ri{borrowernumber
} = $riborrowernumber{$_};
550 my $biblio = GetBiblioFromItemNumber
(GetItemnumberFromBarcode
($bar_code));
551 my $item = GetItem
( GetItemnumberFromBarcode
($bar_code) );
552 # fix up item type for display
553 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
554 $ri{itembiblionumber
} = $biblio->{'biblionumber'};
555 $ri{itemtitle
} = $biblio->{'title'};
556 $ri{itemauthor
} = $biblio->{'author'};
557 $ri{itemcallnumber
} = $biblio->{'itemcallnumber'};
558 $ri{itemtype
} = $biblio->{'itemtype'};
559 $ri{itemnote
} = $biblio->{'itemnotes'};
560 $ri{ccode
} = $biblio->{'ccode'};
561 $ri{itemnumber
} = $biblio->{'itemnumber'};
562 $ri{barcode
} = $bar_code;
563 $ri{homebranch
} = $item->{'homebranch'};
564 $ri{holdingbranch
} = $item->{'holdingbranch'};
566 $ri{location
} = $biblio->{'location'};
567 my $shelfcode = $ri{'location'};
568 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
576 my ($genbrname, $genprname);
577 if (my $b = $branches->{$userenv_branch}) {
578 $genbrname = $b->{'branchname'};
580 if (my $p = $printers->{$printer}) {
581 $genprname = $p->{'printername'};
585 genbrname
=> $genbrname,
586 genprname
=> $genprname,
587 branchname
=> $genbrname,
589 errmsgloop
=> \
@errmsgloop,
590 exemptfine
=> $exemptfine,
591 dropboxmode
=> $dropboxmode,
592 dropboxdate
=> output_pref
($dropboxdate),
593 overduecharges
=> $overduecharges,
594 soundon
=> C4
::Context
->preference("SoundOn"),
595 BlockReturnOfWithdrawnItems
=> C4
::Context
->preference("BlockReturnOfWithdrawnItems"),
598 $itemnumber = GetItemnumberFromBarcode
( $query->param('barcode') );
600 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
601 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
603 collectionItemNeedsTransferred
=> 1,
604 collectionBranch
=> GetBranchName
($collectionBranch),
609 # actually print the page!
610 output_html_with_http_headers
$query, $cookie, $template->output;