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
;
63 my ( $template, $librarian, $cookie, $flags ) = get_template_and_user
(
65 template_name
=> "circ/returns.tt",
69 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
73 my $sessionID = $query->cookie("CGISESSID");
74 my $session = get_session
($sessionID);
75 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
76 # no branch set we can't return
77 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
81 # Print a reserve slip on this page
82 if ( $query->param('print_slip') ) {
85 borrowernumber
=> scalar $query->param('borrowernumber'),
86 biblionumber
=> scalar $query->param('biblionumber'),
92 my $printers = GetPrinters
();
93 my $userenv = C4
::Context
->userenv;
94 my $userenv_branch = $userenv->{'branch'} // '';
95 my $printer = $userenv->{'branchprinter'} // '';
96 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
98 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
100 # Some code to handle the error if there is no branch or printer setting.....
103 # Set up the item stack ....
106 my %riborrowernumber;
108 foreach ( $query->param ) {
121 my $barcode = $query->param("ri-$counter");
122 my $duedate = $query->param("dd-$counter");
123 my $borrowernumber = $query->param("bn-$counter");
126 # decode barcode ## Didn't we already decode them before passing them back last time??
127 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
128 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
130 ######################
131 #Are these lines still useful ?
132 $returneditems{$counter} = $barcode;
133 $riduedate{$counter} = $duedate;
134 $riborrowernumber{$counter} = $borrowernumber;
136 #######################
137 $input{counter
} = $counter;
138 $input{barcode
} = $barcode;
139 $input{duedate
} = $duedate;
140 $input{borrowernumber
} = $borrowernumber;
141 push( @inputloop, \
%input );
145 # Deal with the requests....
147 if ($query->param('WT-itemNumber')){
148 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
151 if ( $query->param('reserve_id') ) {
152 my $itemnumber = $query->param('itemnumber');
153 my $borrowernumber = $query->param('borrowernumber');
154 my $reserve_id = $query->param('reserve_id');
155 my $diffBranchReturned = $query->param('diffBranch');
156 my $cancel_reserve = $query->param('cancel_reserve');
157 # fix up item type for display
158 my $item = Koha
::Items
->find( $itemnumber );
159 my $biblio = $item->biblio;
161 if ( $cancel_reserve ) {
162 CancelReserve
({ reserve_id
=> $reserve_id, charge_cancel_fee
=> !$forgivemanualholdsexpire });
164 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ?
$diffBranchReturned : undef;
165 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
166 # i.e., whether to apply waiting status
167 ModReserveAffect
( $itemnumber, $borrowernumber, $diffBranchSend, $reserve_id );
169 # check if we have other reserves for this document, if we have a return send the message of transfer
170 my ( $messages, $nextreservinfo ) = GetOtherReserves
($itemnumber);
172 my $patron = Koha
::Patrons
->find( $nextreservinfo );
173 my $name = $patron->surname . ", " . $patron->title . " " . $patron->firstname;
174 if ( $messages->{'transfert'} ) {
176 itemtitle
=> $biblio->title,
177 itemnumber
=> $item->itemnumber,
178 itembiblionumber
=> $biblio->biblionumber,
179 iteminfo
=> $biblio->author,
181 borrowernumber
=> $borrowernumber,
182 borcnum
=> $patron->cardnumber,
183 borfirstname
=> $patron->firstname,
184 borsurname
=> $patron->surname,
185 borcategory
=> $patron->category->description,
196 my $barcode = $query->param('barcode');
197 my $exemptfine = $query->param('exemptfine');
200 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
202 # silently prevent unauthorized operator from forgiving overdue
203 # fines by manually tweaking form parameters
206 my $dropboxmode = $query->param('dropboxmode');
207 my $dotransfer = $query->param('dotransfer');
208 my $canceltransfer = $query->param('canceltransfer');
209 my $dest = $query->param('dest');
210 my $calendar = Koha
::Calendar
->new( branchcode
=> $userenv_branch );
211 #dropbox: get last open day (today - 1)
212 my $today = DateTime
->now( time_zone
=> C4
::Context
->tz());
213 my $dropboxdate = $calendar->addDate($today, -1);
215 my $return_date_override = $query->param('return_date_override');
216 my $return_date_override_remember =
217 $query->param('return_date_override_remember');
218 if ($return_date_override) {
219 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
220 my $return_date_override_dt = eval {dt_from_string
( $return_date_override ) };
221 if ( $return_date_override_dt ) {
222 # note that we've overriden the return date
223 $template->param( return_date_was_overriden
=> 1);
224 # Save the original format if we are remembering for this series
226 return_date_override
=> $return_date_override,
227 return_date_override_remember
=> 1
228 ) if ($return_date_override_remember);
230 $return_date_override =
231 DateTime
::Format
::MySQL
->format_datetime( $return_date_override_dt );
235 $return_date_override = q{};
240 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
241 my $transferitem = $query->param('transferitem');
242 my $tobranch = $query->param('tobranch');
243 ModItemTransfer
($transferitem, $userenv_branch, $tobranch);
246 if ($canceltransfer){
247 $itemnumber=$query->param('itemnumber');
248 DeleteTransfer
($itemnumber);
250 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
253 $template->param( transfercancelled
=> 1);
257 # actually return book and prepare item table.....
260 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
261 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
262 my $item = Koha
::Items
->find({ barcode
=> $barcode });
265 # Check if we should display a checkin message, based on the the item
266 # type of the checked in item
267 my $itemtype = Koha
::ItemTypes
->find( $item->effective_itemtype );
268 if ( $itemtype && $itemtype->checkinmsg ) {
270 checkinmsg
=> $itemtype->checkinmsg,
271 checkinmsgtype
=> $itemtype->checkinmsgtype,
275 # make sure return branch respects home branch circulation rules, default to homebranch
276 my $hbr = GetBranchItemRule
($item->homebranch, $itemtype ?
$itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
277 $returnbranch = $item->$hbr;
279 my $materials = $item->materials;
280 my $descriptions = Koha
::AuthorisedValues
->get_description_by_koha_field({frameworkcode
=> '', kohafield
=>'items.materials', authorised_value
=> $materials });
281 $materials = $descriptions->{lib
} // $materials;
283 my $issue = Koha
::Checkouts
->find( { itemnumber
=> $itemnumber } );
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' ) {
505 elsif ( $code eq 'LostItemFeeRefunded' ) {
506 $template->param( LostItemFeeRefunded
=> 1 );
508 elsif ( $code eq 'ResFound' ) {
509 ; # FIXME... anything to do here?
511 elsif ( $code eq 'WasReturned' ) {
512 ; # FIXME... anything to do here?
514 elsif ( $code eq 'WasTransfered' ) {
515 ; # FIXME... anything to do here?
517 elsif ( $code eq 'withdrawn' ) {
519 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
521 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
522 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
523 $err{ispermanent
} = 1;
524 $err{msg
} = $messages->{'IsPermanent'};
527 elsif ( $code eq 'WrongTransfer' ) {
528 ; # FIXME... anything to do here?
530 elsif ( $code eq 'WrongTransferItem' ) {
531 ; # FIXME... anything to do here?
533 elsif ( $code eq 'NeedsTransfer' ) {
535 elsif ( $code eq 'Wrongbranch' ) {
537 elsif ( $code eq 'Debarred' ) {
538 $err{debarred
} = $messages->{'Debarred'};
539 $err{debarcardnumber
} = $borrower->{cardnumber
};
540 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
541 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
543 elsif ( $code eq 'PrevDebarred' ) {
544 $err{prevdebarred
} = $messages->{'PrevDebarred'};
546 elsif ( $code eq 'ForeverDebarred' ) {
547 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
549 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
550 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
553 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
554 # This forces the issue of staying in sync w/ Circulation.pm
557 push( @errmsgloop, \
%err );
559 last if $exit_required_p;
561 $template->param( errmsgloop
=> \
@errmsgloop );
563 #set up so only the last 8 returned items display (make for faster loading pages)
564 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
568 { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.location' } ) };
569 foreach ( sort { $a <=> $b } keys %returneditems ) {
571 if ( $count++ < $returned_counter ) {
572 my $bar_code = $returneditems{$_};
573 if ($riduedate{$_}) {
574 my $duedate = dt_from_string
( $riduedate{$_}, 'sql');
575 $ri{year
} = $duedate->year();
576 $ri{month
} = $duedate->month();
577 $ri{day
} = $duedate->day();
578 $ri{hour
} = $duedate->hour();
579 $ri{minute
} = $duedate->minute();
580 $ri{duedate
} = output_pref
($duedate);
581 my $patron = Koha
::Patrons
->find( $riborrowernumber{$_} );
582 unless ( $dropboxmode ) {
583 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, DateTime
->now()) == -1);
585 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, $dropboxdate) == -1);
587 $ri{borrowernumber
} = $patron->borrowernumber;
588 $ri{borcnum
} = $patron->cardnumber;
589 $ri{borfirstname
} = $patron->firstname;
590 $ri{borsurname
} = $patron->surname;
591 $ri{bortitle
} = $patron->title;
592 $ri{bornote
} = $patron->borrowernotes;
593 $ri{borcategorycode
}= $patron->categorycode;
594 $ri{borissuescount
} = Koha
::Checkouts
->count( { borrowernumber
=> $b->{'borrowernumber'} } );
597 $ri{borrowernumber
} = $riborrowernumber{$_};
600 my $item = Koha
::Items
->find({ barcode
=> $bar_code });
601 my $biblio = $item->biblio;
602 # FIXME pass $item to the template and we are done here...
603 $ri{itembiblionumber
} = $biblio->biblionumber;
604 $ri{itemtitle
} = $biblio->title;
605 $ri{itemauthor
} = $biblio->author;
606 $ri{itemcallnumber
} = $item->itemcallnumber;
607 $ri{dateaccessioned
} = $item->dateaccessioned;
608 $ri{itemtype
} = $item->effective_itemtype;
609 $ri{itemnote
} = $item->itemnotes;
610 $ri{itemnotes_nonpublic
} = $item->itemnotes_nonpublic;
611 $ri{ccode
} = $item->ccode;
612 $ri{enumchron
} = $item->enumchron;
613 $ri{itemnumber
} = $item->itemnumber;
614 $ri{barcode
} = $bar_code;
615 $ri{homebranch
} = $item->homebranch;
616 $ri{holdingbranch
} = $item->holdingbranch;
618 $ri{location
} = $biblio->{'location'};
619 my $shelfcode = $ri{'location'};
620 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
632 errmsgloop
=> \
@errmsgloop,
633 exemptfine
=> $exemptfine,
634 dropboxmode
=> $dropboxmode,
635 dropboxdate
=> output_pref
($dropboxdate),
636 forgivemanualholdsexpire
=> $forgivemanualholdsexpire,
637 overduecharges
=> $overduecharges,
638 AudioAlerts
=> C4
::Context
->preference("AudioAlerts"),
639 BlockReturnOfWithdrawnItems
=> C4
::Context
->preference("BlockReturnOfWithdrawnItems"),
642 $itemnumber = GetItemnumberFromBarcode
( $barcode );
644 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
645 if ( $holdingBranch and $collectionBranch ) {
646 $holdingBranch //= '';
647 $collectionBranch //= $returnbranch;
648 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
650 collectionItemNeedsTransferred
=> 1,
651 collectionBranch
=> $collectionBranch,
652 itemnumber
=> $itemnumber,
658 # Checking if there is a Fast Cataloging Framework
659 $template->param( fast_cataloging
=> 1 ) if Koha
::BiblioFrameworks
->find( 'FA' );
661 # actually print the page!
662 output_html_with_http_headers
$query, $cookie, $template->output;