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
38 use C4
::Auth qw
/:DEFAULT get_session/;
46 use C4
::Members
::Messaging
;
47 use C4
::Koha
; # FIXME : is it still useful ?
48 use C4
::RotatingCollections
;
49 use Koha
::AuthorisedValues
;
52 use Koha
::BiblioFrameworks
;
61 my ( $template, $librarian, $cookie, $flags ) = get_template_and_user
(
63 template_name
=> "circ/returns.tt",
67 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
71 my $sessionID = $query->cookie("CGISESSID");
72 my $session = get_session
($sessionID);
73 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
74 # no branch set we can't return
75 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
79 # Print a reserve slip on this page
80 if ( $query->param('print_slip') ) {
83 borrowernumber
=> scalar $query->param('borrowernumber'),
84 biblionumber
=> scalar $query->param('biblionumber'),
90 my $printers = GetPrinters
();
91 my $userenv = C4
::Context
->userenv;
92 my $userenv_branch = $userenv->{'branch'} // '';
93 my $printer = $userenv->{'branchprinter'} // '';
94 my $forgivemanualholdsexpire = $query->param('forgivemanualholdsexpire');
96 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
98 # Some code to handle the error if there is no branch or printer setting.....
101 # Set up the item stack ....
104 my %riborrowernumber;
106 foreach ( $query->param ) {
119 my $barcode = $query->param("ri-$counter");
120 my $duedate = $query->param("dd-$counter");
121 my $borrowernumber = $query->param("bn-$counter");
124 # decode barcode ## Didn't we already decode them before passing them back last time??
125 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
126 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
128 ######################
129 #Are these lines still useful ?
130 $returneditems{$counter} = $barcode;
131 $riduedate{$counter} = $duedate;
132 $riborrowernumber{$counter} = $borrowernumber;
134 #######################
135 $input{counter
} = $counter;
136 $input{barcode
} = $barcode;
137 $input{duedate
} = $duedate;
138 $input{borrowernumber
} = $borrowernumber;
139 push( @inputloop, \
%input );
143 # Deal with the requests....
145 if ($query->param('WT-itemNumber')){
146 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
149 if ( $query->param('reserve_id') ) {
150 my $itemnumber = $query->param('itemnumber');
151 my $borrowernumber = $query->param('borrowernumber');
152 my $reserve_id = $query->param('reserve_id');
153 my $diffBranchReturned = $query->param('diffBranch');
154 my $cancel_reserve = $query->param('cancel_reserve');
155 # fix up item type for display
156 my $item = Koha
::Items
->find( $itemnumber );
157 my $biblio = $item->biblio;
159 if ( $cancel_reserve ) {
160 my $hold = Koha
::Holds
->find( $reserve_id );
162 $hold->cancel( { charge_cancel_fee
=> !$forgivemanualholdsexpire } );
165 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ?
$diffBranchReturned : undef;
166 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
167 # i.e., whether to apply waiting status
168 ModReserveAffect
( $itemnumber, $borrowernumber, $diffBranchSend, $reserve_id );
170 # check if we have other reserves for this document, if we have a return send the message of transfer
171 my ( $messages, $nextreservinfo ) = GetOtherReserves
($itemnumber);
173 my $patron = Koha
::Patrons
->find( $nextreservinfo );
174 my $name = $patron ?
$patron->surname . ", " . $patron->title . " " . $patron->firstname : '';
175 if ( $messages->{'transfert'} ) {
177 itemtitle
=> $biblio->title,
178 itemnumber
=> $item->itemnumber,
179 itembiblionumber
=> $biblio->biblionumber,
180 iteminfo
=> $biblio->author,
182 borrowernumber
=> $borrowernumber,
183 borcnum
=> $patron->cardnumber,
184 borfirstname
=> $patron->firstname,
185 borsurname
=> $patron->surname,
186 borcategory
=> $patron->category->description,
197 my $barcode = $query->param('barcode');
198 my $exemptfine = $query->param('exemptfine');
201 !C4
::Auth
::haspermission
(C4
::Context
->userenv->{'id'}, {'updatecharges' => 'writeoff'})
203 # silently prevent unauthorized operator from forgiving overdue
204 # fines by manually tweaking form parameters
207 my $dropboxmode = $query->param('dropboxmode');
208 my $dotransfer = $query->param('dotransfer');
209 my $canceltransfer = $query->param('canceltransfer');
210 my $dest = $query->param('dest');
211 my $calendar = Koha
::Calendar
->new( branchcode
=> $userenv_branch );
212 #dropbox: get last open day (today - 1)
213 my $today = DateTime
->now( time_zone
=> C4
::Context
->tz());
214 my $dropboxdate = $calendar->addDate($today, -1);
216 my $return_date_override = $query->param('return_date_override');
217 my $return_date_override_remember =
218 $query->param('return_date_override_remember');
219 if ($return_date_override) {
220 if ( C4
::Context
->preference('SpecifyReturnDate') ) {
221 my $return_date_override_dt = eval {dt_from_string
( $return_date_override ) };
222 if ( $return_date_override_dt ) {
223 # note that we've overriden the return date
224 $template->param( return_date_was_overriden
=> 1);
225 # Save the original format if we are remembering for this series
227 return_date_override
=> $return_date_override,
228 return_date_override_remember
=> 1
229 ) if ($return_date_override_remember);
231 $return_date_override =
232 DateTime
::Format
::MySQL
->format_datetime( $return_date_override_dt );
236 $return_date_override = q{};
241 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
242 my $transferitem = $query->param('transferitem');
243 my $tobranch = $query->param('tobranch');
244 ModItemTransfer
($transferitem, $userenv_branch, $tobranch);
247 if ($canceltransfer){
248 $itemnumber=$query->param('itemnumber');
249 DeleteTransfer
($itemnumber);
251 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
254 $template->param( transfercancelled
=> 1);
258 # actually return book and prepare item table.....
261 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
262 $barcode = barcodedecode
($barcode) if C4
::Context
->preference('itemBarcodeInputFilter');
263 my $item = Koha
::Items
->find({ barcode
=> $barcode });
266 # Check if we should display a checkin message, based on the the item
267 # type of the checked in item
268 my $itemtype = Koha
::ItemTypes
->find( $item->effective_itemtype );
269 if ( $itemtype && $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
($item->homebranch, $itemtype ?
$itemtype->itemtype : undef )->{'returnbranch'} || "homebranch";
278 $returnbranch = $item->$hbr;
280 my $materials = $item->materials;
281 my $descriptions = Koha
::AuthorisedValues
->get_description_by_koha_field({frameworkcode
=> '', kohafield
=>'items.materials', authorised_value
=> $materials });
282 $materials = $descriptions->{lib
} // $materials;
284 my $issue = Koha
::Checkouts
->find( { itemnumber
=> $itemnumber } );
286 my $biblio = $item->biblio;
288 title
=> $biblio->title,
289 homebranch
=> $item->homebranch,
290 holdingbranch
=> $item->holdingbranch,
291 returnbranch
=> $returnbranch,
292 author
=> $biblio->author,
293 itembarcode
=> $item->barcode,
294 itemtype
=> $item->effective_itemtype,
295 ccode
=> $item->ccode,
296 itembiblionumber
=> $biblio->biblionumber,
297 biblionumber
=> $biblio->biblionumber,
298 borrower
=> $borrower,
299 additional_materials
=> $materials,
302 } # FIXME else we should not call AddReturn but set BadBarcode directly instead
312 ( $returned, $messages, $issue, $borrower ) =
313 AddReturn
( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override, $dropboxdate );
316 my $time_now = DateTime
->now( time_zone
=> C4
::Context
->tz )->truncate( to
=> 'minute');
317 my $date_due_dt = dt_from_string
( $issue->date_due, 'sql' );
318 my $duedate = $date_due_dt->strftime('%Y-%m-%d %H:%M');
319 $returneditems{0} = $barcode;
320 $riborrowernumber{0} = $borrower->{'borrowernumber'};
321 $riduedate{0} = $duedate;
322 $input{borrowernumber
} = $borrower->{'borrowernumber'};
323 $input{duedate
} = $duedate;
324 unless ( $dropboxmode ) {
325 $input{return_overdue
} = 1 if (DateTime
->compare($date_due_dt, DateTime
->now()) == -1);
327 $input{return_overdue
} = 1 if (DateTime
->compare($date_due_dt, $dropboxdate) == -1);
329 push( @inputloop, \
%input );
331 if ( C4
::Context
->preference("FineNotifyAtCheckin") ) {
332 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
333 my $balance = $patron->account->balance;
336 $template->param( fines
=> sprintf("%.2f", $balance) );
337 $template->param( fineborrowernumber
=> $borrower->{'borrowernumber'} );
341 if (C4
::Context
->preference("WaitingNotifyAtCheckin") ) {
342 #Check for waiting holds
343 my $patron = Koha
::Patrons
->find( $borrower->{borrowernumber
} );
344 my $waiting_holds = $patron->holds->search({ found
=> 'W', branchcode
=> $userenv_branch })->count;
345 if ($waiting_holds > 0) {
347 waiting_holds
=> $waiting_holds,
348 holdsborrowernumber
=> $borrower->{'borrowernumber'},
349 holdsfirstname
=> $borrower->{'firstname'},
350 holdssurname
=> $borrower->{'surname'},
354 } elsif ( C4
::Context
->preference('ShowAllCheckins') and !$messages->{'BadBarcode'} ) {
356 $returneditems{0} = $barcode;
358 push( @inputloop, \
%input );
360 $template->param( privacy
=> $borrower->{privacy
} );
362 $template->param( inputloop
=> \
@inputloop );
368 # new op dev : we check if the document must be returned to his homebranch directly,
369 # if the document is transfered, we have warning message .
371 if ( $messages->{'WasTransfered'} ) {
375 itemnumber
=> $itemnumber,
379 if ( $messages->{'NeedsTransfer'} ){
382 needstransfer
=> $messages->{'NeedsTransfer'},
383 itemnumber
=> $itemnumber,
387 if ( $messages->{'Wrongbranch'} ){
390 rightbranch
=> $messages->{'Wrongbranch'}->{'Rightbranch'},
394 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
396 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
399 TransferWaitingAt
=> $messages->{'WrongTransfer'},
400 WrongTransferItem
=> $messages->{'WrongTransferItem'},
401 itemnumber
=> $itemnumber,
404 my $reserve = $messages->{'ResFound'};
406 my $patron = Koha
::Patrons
->find( $reserve->{'borrowernumber'} );
407 my $name = $patron->surname . ", " . $patron->title . " " . $patron->firstname;
409 # FIXME The full patron object should be passed to the template
411 wborfirstname
=> $patron->firstname,
412 wborsurname
=> $patron->surname,
413 wborcategory
=> $patron->category->description,
414 wbortitle
=> $patron->title,
415 wborphone
=> $patron->phone,
416 wboremail
=> $patron->email,
417 streetnumber
=> $patron->streetnumber,
418 address
=> $patron->address,
419 address2
=> $patron->address2,
420 city
=> $patron->city,
421 zipcode
=> $patron->zipcode,
422 state => $patron->state,
423 country
=> $patron->country,
424 wborrowernumber
=> $reserve->{'borrowernumber'},
425 wborcnum
=> $patron->cardnumber,
429 wtransfertFrom
=> $userenv_branch,
434 # reserve found and item arrived at the expected branch
436 if ( $messages->{'ResFound'}) {
437 my $reserve = $messages->{'ResFound'};
438 my $patron = Koha
::Patrons
->find( $reserve->{borrowernumber
} );
439 my $holdmsgpreferences = C4
::Members
::Messaging
::GetMessagingPreferences
( { borrowernumber
=> $reserve->{'borrowernumber'}, message_name
=> 'Hold_Filled' } );
440 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
441 if ( $reserve->{'ResFound'} eq "Waiting" ) {
443 waiting
=> ($userenv_branch eq $reserve->{'branchcode'} ?
1 : 0 ),
445 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
447 intransit
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
448 transfertodo
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
449 reserve_id
=> $reserve->{reserve_id
},
454 # same params for Waiting or Reserved
456 # FIXME The full patron object should be passed to the template
458 name
=> $patron->surname . ", " . $patron->title . " " . $patron->firstname,
459 borfirstname
=> $patron->firstname,
460 borsurname
=> $patron->surname,
461 borcategory
=> $patron->category->description,
462 bortitle
=> $patron->title,
463 borphone
=> $patron->phone,
464 boremail
=> $patron->email,
465 boraddress
=> $patron->address,
466 boraddress2
=> $patron->address2,
467 streetnumber
=> $patron->streetnumber,
468 city
=> $patron->city,
469 zipcode
=> $patron->zipcode,
470 state => $patron->state,
471 country
=> $patron->country,
472 borcnum
=> $patron->cardnumber,
473 debarred
=> $patron->debarred,
474 gonenoaddress
=> $patron->gonenoaddress,
476 destbranch
=> $reserve->{'branchcode'},
477 borrowernumber
=> $reserve->{'borrowernumber'},
478 itemnumber
=> $reserve->{'itemnumber'},
479 reservenotes
=> $reserve->{'reservenotes'},
480 reserve_id
=> $reserve->{reserve_id
},
481 bormessagepref
=> $holdmsgpreferences->{'transports'},
483 } # else { ; } # error?
488 foreach my $code ( keys %$messages ) {
490 my $exit_required_p = 0;
491 if ( $code eq 'BadBarcode' ) {
492 $err{badbarcode
} = 1;
493 $err{msg
} = $messages->{'BadBarcode'};
495 elsif ( $code eq 'NotIssued' ) {
498 $err{msg
} = $messages->{'IsPermanent'} if $messages->{'IsPermanent'};
500 elsif ( $code eq 'LocalUse' ) {
503 elsif ( $code eq 'WasLost' ) {
505 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfLostItems");
507 elsif ( $code eq 'LostItemFeeRefunded' ) {
508 $template->param( LostItemFeeRefunded
=> 1 );
510 elsif ( $code eq 'ResFound' ) {
511 ; # FIXME... anything to do here?
513 elsif ( $code eq 'WasReturned' ) {
514 ; # FIXME... anything to do here?
516 elsif ( $code eq 'WasTransfered' ) {
517 ; # FIXME... anything to do here?
519 elsif ( $code eq 'withdrawn' ) {
521 $exit_required_p = 1 if C4
::Context
->preference("BlockReturnOfWithdrawnItems");
523 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
524 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
525 $err{ispermanent
} = 1;
526 $err{msg
} = $messages->{'IsPermanent'};
529 elsif ( $code eq 'WrongTransfer' ) {
530 ; # FIXME... anything to do here?
532 elsif ( $code eq 'WrongTransferItem' ) {
533 ; # FIXME... anything to do here?
535 elsif ( $code eq 'NeedsTransfer' ) {
537 elsif ( $code eq 'Wrongbranch' ) {
539 elsif ( $code eq 'Debarred' ) {
540 $err{debarred
} = $messages->{'Debarred'};
541 $err{debarcardnumber
} = $borrower->{cardnumber
};
542 $err{debarborrowernumber
} = $borrower->{borrowernumber
};
543 $err{debarname
} = "$borrower->{firstname} $borrower->{surname}";
545 elsif ( $code eq 'PrevDebarred' ) {
546 $err{prevdebarred
} = $messages->{'PrevDebarred'};
548 elsif ( $code eq 'ForeverDebarred' ) {
549 $err{foreverdebarred
} = $messages->{'ForeverDebarred'};
551 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
552 $err{NotForLoanStatusUpdated
} = $messages->{NotForLoanStatusUpdated
};
554 elsif ( $code eq 'DataCorrupted' ) {
555 $err{data_corrupted
} = 1;
558 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
559 # This forces the issue of staying in sync w/ Circulation.pm
562 push( @errmsgloop, \
%err );
564 last if $exit_required_p;
566 $template->param( errmsgloop
=> \
@errmsgloop );
568 #set up so only the last 8 returned items display (make for faster loading pages)
569 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
573 { map { $_->{authorised_value
} => $_->{lib
} } Koha
::AuthorisedValues
->get_descriptions_by_koha_field( { frameworkcode
=> '', kohafield
=> 'items.location' } ) };
574 foreach ( sort { $a <=> $b } keys %returneditems ) {
576 if ( $count++ < $returned_counter ) {
577 my $bar_code = $returneditems{$_};
578 if ($riduedate{$_}) {
579 my $duedate = dt_from_string
( $riduedate{$_}, 'sql');
580 $ri{year
} = $duedate->year();
581 $ri{month
} = $duedate->month();
582 $ri{day
} = $duedate->day();
583 $ri{hour
} = $duedate->hour();
584 $ri{minute
} = $duedate->minute();
585 $ri{duedate
} = output_pref
($duedate);
586 my $patron = Koha
::Patrons
->find( $riborrowernumber{$_} );
587 unless ( $dropboxmode ) {
588 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, DateTime
->now()) == -1);
590 $ri{return_overdue
} = 1 if (DateTime
->compare($duedate, $dropboxdate) == -1);
592 $ri{borrowernumber
} = $patron->borrowernumber;
593 $ri{borcnum
} = $patron->cardnumber;
594 $ri{borfirstname
} = $patron->firstname;
595 $ri{borsurname
} = $patron->surname;
596 $ri{bortitle
} = $patron->title;
597 $ri{bornote
} = $patron->borrowernotes;
598 $ri{borcategorycode
}= $patron->categorycode;
599 $ri{borissuescount
} = Koha
::Checkouts
->count( { borrowernumber
=> $b->{'borrowernumber'} } );
602 $ri{borrowernumber
} = $riborrowernumber{$_};
605 my $item = Koha
::Items
->find({ barcode
=> $bar_code });
606 my $biblio = $item->biblio;
607 # FIXME pass $item to the template and we are done here...
608 $ri{itembiblionumber
} = $biblio->biblionumber;
609 $ri{itemtitle
} = $biblio->title;
610 $ri{itemauthor
} = $biblio->author;
611 $ri{itemcallnumber
} = $item->itemcallnumber;
612 $ri{dateaccessioned
} = $item->dateaccessioned;
613 $ri{itemtype
} = $item->effective_itemtype;
614 $ri{itemnote
} = $item->itemnotes;
615 $ri{itemnotes_nonpublic
} = $item->itemnotes_nonpublic;
616 $ri{ccode
} = $item->ccode;
617 $ri{enumchron
} = $item->enumchron;
618 $ri{itemnumber
} = $item->itemnumber;
619 $ri{barcode
} = $bar_code;
620 $ri{homebranch
} = $item->homebranch;
621 $ri{holdingbranch
} = $item->holdingbranch;
623 $ri{location
} = $biblio->{'location'};
624 my $shelfcode = $ri{'location'};
625 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
637 errmsgloop
=> \
@errmsgloop,
638 exemptfine
=> $exemptfine,
639 dropboxmode
=> $dropboxmode,
640 dropboxdate
=> output_pref
($dropboxdate),
641 forgivemanualholdsexpire
=> $forgivemanualholdsexpire,
642 overduecharges
=> $overduecharges,
643 AudioAlerts
=> C4
::Context
->preference("AudioAlerts"),
646 $itemnumber = GetItemnumberFromBarcode
( $barcode );
648 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches
( $itemnumber );
649 if ( $holdingBranch and $collectionBranch ) {
650 $holdingBranch //= '';
651 $collectionBranch //= $returnbranch;
652 if ( ! ( $holdingBranch eq $collectionBranch ) ) {
654 collectionItemNeedsTransferred
=> 1,
655 collectionBranch
=> $collectionBranch,
656 itemnumber
=> $itemnumber,
662 # Checking if there is a Fast Cataloging Framework
663 $template->param( fast_cataloging
=> 1 ) if Koha
::BiblioFrameworks
->find( 'FA' );
665 # actually print the page!
666 output_html_with_http_headers
$query, $cookie, $template->output;