Bug 12612: Remove CGI::scrolling_list from auth_tag_structure.pl
[koha.git] / circ / returns.pl
blobd99f58c0e11d4e6ffbbd867385d0c71a406cda0d
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # 2006 SAN-OP
5 # 2007-2010 BibLibre, Paul POULAIN
6 # 2010 Catalyst IT
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
14 # version.
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.
24 =head1 returns.pl
26 script to execute returns of books
28 =cut
30 use strict;
31 use warnings;
33 use CGI;
34 use DateTime;
35 use C4::Context;
36 use C4::Auth qw/:DEFAULT get_session/;
37 use C4::Output;
38 use C4::Circulation;
39 use C4::Print;
40 use C4::Reserves;
41 use C4::Biblio;
42 use C4::Items;
43 use C4::Members;
44 use C4::Branch; # GetBranches GetBranchName
45 use C4::Koha; # FIXME : is it still useful ?
46 use C4::RotatingCollections;
47 use Koha::DateUtils;
48 use Koha::Calendar;
50 my $query = new CGI;
52 #getting the template
53 my ( $template, $librarian, $cookie ) = get_template_and_user(
55 template_name => "circ/returns.tt",
56 query => $query,
57 type => "intranet",
58 authnotrequired => 0,
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");
68 exit;
71 #####################
72 #Global vars
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 ....
85 my %returneditems;
86 my %riduedate;
87 my %riborrowernumber;
88 my @inputloop;
89 foreach ( $query->param ) {
90 my $counter;
91 if (/ri-(\d*)/) {
92 $counter = $1;
93 if ($counter > 20) {
94 next;
97 else {
98 next;
101 my %input;
102 my $barcode = $query->param("ri-$counter");
103 my $duedate = $query->param("dd-$counter");
104 my $borrowernumber = $query->param("bn-$counter");
105 $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 );
125 ############
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'} ) {
150 $template->param(
151 itemtitle => $iteminfo->{'title'},
152 itemnumber => $iteminfo->{'itemnumber'},
153 itembiblionumber => $iteminfo->{'biblionumber'},
154 iteminfo => $iteminfo->{'author'},
155 tobranchname => GetBranchName($messages->{'transfert'}),
156 name => $name,
157 borrowernumber => $borrowernumber,
158 borcnum => $borr->{'cardnumber'},
159 borfirstname => $borr->{'firstname'},
160 borsurname => $borr->{'surname'},
161 diffbranch => 1,
166 my $borrower;
167 my $returned = 0;
168 my $messages;
169 my $issueinformation;
170 my $itemnumber;
171 my $barcode = $query->param('barcode');
172 my $exemptfine = $query->param('exemptfine');
173 if (
174 $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
179 undef $exemptfine;
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
201 $template->param(
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);
211 else {
212 $return_date_override = q{};
216 if ($dotransfer){
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);
226 if($dest eq "ttr"){
227 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
228 exit;
229 } else {
230 $template->param( transfercancelled => 1);
234 # actually return book and prepare item table.....
235 if ($barcode) {
236 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
237 $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
238 $itemnumber = GetItemnumberFromBarcode($barcode);
240 if ( C4::Context->preference("InProcessingToShelvingCart") ) {
241 my $item = GetItem( $itemnumber );
242 if ( $item->{'location'} eq 'PROC' ) {
243 $item->{'location'} = 'CART';
244 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
249 # save the return
251 ( $returned, $messages, $issueinformation, $borrower ) =
252 AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode, $return_date_override );
253 my $homeorholdingbranchreturn = C4::Context->preference('HomeOrHoldingBranchReturn');
254 $homeorholdingbranchreturn ||= 'homebranch';
256 # get biblio description
257 my $biblio = GetBiblioFromItemNumber($itemnumber);
258 # fix up item type for display
259 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
261 # Check if we should display a checkin message, based on the the item
262 # type of the checked in item
263 my $itemtype = C4::ItemType->get( $biblio->{'itemtype'} );
264 if ( $itemtype->{'checkinmsg'} ) {
265 $template->param(
266 checkinmsg => $itemtype->{'checkinmsg'},
267 checkinmsgtype => $itemtype->{'checkinmsgtype'},
271 $template->param(
272 title => $biblio->{'title'},
273 homebranch => $biblio->{'homebranch'},
274 homebranchname => GetBranchName( $biblio->{$homeorholdingbranchreturn} ),
275 author => $biblio->{'author'},
276 itembarcode => $biblio->{'barcode'},
277 itemtype => $biblio->{'itemtype'},
278 ccode => $biblio->{'ccode'},
279 itembiblionumber => $biblio->{'biblionumber'},
280 borrower => $borrower,
281 additional_materials => $biblio->{'materials'},
284 my %input = (
285 counter => 0,
286 first => 1,
287 barcode => $barcode,
290 if ($returned) {
291 my $time_now = DateTime->now( time_zone => C4::Context->tz )->truncate( to => 'minute');
292 my $duedate = $issueinformation->{date_due}->strftime('%Y-%m-%d %H:%M');
293 $returneditems{0} = $barcode;
294 $riborrowernumber{0} = $borrower->{'borrowernumber'};
295 $riduedate{0} = $duedate;
296 $input{borrowernumber} = $borrower->{'borrowernumber'};
297 $input{duedate} = $duedate;
298 $input{return_overdue} = 1 if (DateTime->compare($issueinformation->{date_due}, $time_now) == -1);
299 push( @inputloop, \%input );
301 if ( C4::Context->preference("FineNotifyAtCheckin") ) {
302 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrower->{'borrowernumber'} );
303 if ($fines && $fines > 0) {
304 $template->param( fines => sprintf("%.2f",$fines) );
305 $template->param( fineborrowernumber => $borrower->{'borrowernumber'} );
309 if (C4::Context->preference("WaitingNotifyAtCheckin") ) {
310 #Check for waiting holds
311 my @reserves = GetReservesFromBorrowernumber($borrower->{'borrowernumber'});
312 my $waiting_holds = 0;
313 foreach my $num_res (@reserves) {
314 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
315 $waiting_holds++;
318 if ($waiting_holds > 0) {
319 $template->param(
320 waiting_holds => $waiting_holds,
321 holdsborrowernumber => $borrower->{'borrowernumber'},
322 holdsfirstname => $borrower->{'firstname'},
323 holdssurname => $borrower->{'surname'},
328 elsif ( !$messages->{'BadBarcode'} ) {
329 $input{duedate} = 0;
330 $returneditems{0} = $barcode;
331 $riduedate{0} = 0;
332 push( @inputloop, \%input );
335 $template->param( inputloop => \@inputloop );
337 my $found = 0;
338 my $waiting = 0;
339 my $reserved = 0;
341 # new op dev : we check if the document must be returned to his homebranch directly,
342 # if the document is transfered, we have warning message .
344 if ( $messages->{'WasTransfered'} ) {
345 $template->param(
346 found => 1,
347 transfer => 1,
348 itemnumber => $itemnumber,
352 if ( $messages->{'NeedsTransfer'} ){
353 $template->param(
354 found => 1,
355 needstransfer => 1,
356 itemnumber => $itemnumber,
360 if ( $messages->{'Wrongbranch'} ){
361 $template->param(
362 wrongbranch => 1,
366 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
368 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
369 $template->param(
370 WrongTransfer => 1,
371 TransferWaitingAt => $messages->{'WrongTransfer'},
372 WrongTransferItem => $messages->{'WrongTransferItem'},
373 itemnumber => $itemnumber,
376 my $reserve = $messages->{'ResFound'};
377 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
378 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
379 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
380 $template->param(
381 wname => $name,
382 wborfirstname => $borr->{'firstname'},
383 wborsurname => $borr->{'surname'},
384 wbortitle => $borr->{'title'},
385 wborphone => $borr->{'phone'},
386 wboremail => $borr->{'email'},
387 wborstnum => $borr->{streetnumber},
388 wboraddress => $borr->{'address'},
389 wboraddress2 => $borr->{'address2'},
390 wborcity => $borr->{'city'},
391 wborzip => $borr->{'zipcode'},
392 wborrowernumber => $reserve->{'borrowernumber'},
393 wborcnum => $borr->{'cardnumber'},
394 wtransfertFrom => $userenv_branch,
399 # reserve found and item arrived at the expected branch
401 if ( $messages->{'ResFound'}) {
402 my $reserve = $messages->{'ResFound'};
403 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
404 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
406 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
407 if ( $reserve->{'ResFound'} eq "Waiting" ) {
408 $template->param(
409 waiting => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
411 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
412 $template->param(
413 intransit => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
414 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
415 resbarcode => $barcode,
416 reserved => 1,
420 # same params for Waiting or Reserved
421 $template->param(
422 found => 1,
423 currentbranch => $branches->{$userenv_branch}->{'branchname'},
424 destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
425 name => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
426 borfirstname => $borr->{'firstname'},
427 borsurname => $borr->{'surname'},
428 bortitle => $borr->{'title'},
429 borphone => $borr->{'phone'},
430 boremail => $borr->{'email'},
431 boraddress => $borr->{'address'},
432 boraddress2 => $borr->{'address2'},
433 borstnum => $borr->{streetnumber},
434 borcity => $borr->{'city'},
435 borzip => $borr->{'zipcode'},
436 borcnum => $borr->{'cardnumber'},
437 debarred => $borr->{'debarred'},
438 gonenoaddress => $borr->{'gonenoaddress'},
439 barcode => $barcode,
440 destbranch => $reserve->{'branchcode'},
441 borrowernumber => $reserve->{'borrowernumber'},
442 itemnumber => $reserve->{'itemnumber'},
443 reservenotes => $reserve->{'reservenotes'},
445 } # else { ; } # error?
448 # Error Messages
449 my @errmsgloop;
450 foreach my $code ( keys %$messages ) {
451 my %err;
452 my $exit_required_p = 0;
453 if ( $code eq 'BadBarcode' ) {
454 $err{badbarcode} = 1;
455 $err{msg} = $messages->{'BadBarcode'};
457 elsif ( $code eq 'NotIssued' ) {
458 $err{notissued} = 1;
459 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
461 elsif ( $code eq 'LocalUse' ) {
462 $err{localuse} = 1;
464 elsif ( $code eq 'WasLost' ) {
465 $err{waslost} = 1;
467 elsif ( $code eq 'LostItemFeeRefunded' ) {
468 $template->param( LostItemFeeRefunded => 1 );
470 elsif ( $code eq 'ResFound' ) {
471 ; # FIXME... anything to do here?
473 elsif ( $code eq 'WasReturned' ) {
474 ; # FIXME... anything to do here?
476 elsif ( $code eq 'WasTransfered' ) {
477 ; # FIXME... anything to do here?
479 elsif ( $code eq 'withdrawn' ) {
480 $err{withdrawn} = 1;
481 $exit_required_p = 1 if C4::Context->preference("BlockReturnOfWithdrawnItems");
483 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
484 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
485 $err{ispermanent} = 1;
486 $err{msg} =
487 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
490 elsif ( $code eq 'WrongTransfer' ) {
491 ; # FIXME... anything to do here?
493 elsif ( $code eq 'WrongTransferItem' ) {
494 ; # FIXME... anything to do here?
496 elsif ( $code eq 'NeedsTransfer' ) {
498 elsif ( $code eq 'Wrongbranch' ) {
500 elsif ( $code eq 'Debarred' ) {
501 $err{debarred} = $messages->{'Debarred'};
502 $err{debarcardnumber} = $borrower->{cardnumber};
503 $err{debarborrowernumber} = $borrower->{borrowernumber};
504 $err{debarname} = "$borrower->{firstname} $borrower->{surname}";
506 elsif ( $code eq 'PrevDebarred' ) {
507 $err{prevdebarred} = $messages->{'PrevDebarred'};
509 elsif ( $code eq 'NotForLoanStatusUpdated' ) {
510 $err{NotForLoanStatusUpdated} = $messages->{NotForLoanStatusUpdated};
512 else {
513 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
514 # This forces the issue of staying in sync w/ Circulation.pm
516 if (%err) {
517 push( @errmsgloop, \%err );
519 last if $exit_required_p;
521 $template->param( errmsgloop => \@errmsgloop );
523 #set up so only the last 8 returned items display (make for faster loading pages)
524 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
525 my $count = 0;
526 my @riloop;
527 my $shelflocations = GetKohaAuthorisedValues('items.location','');
528 foreach ( sort { $a <=> $b } keys %returneditems ) {
529 my %ri;
530 if ( $count++ < $returned_counter ) {
531 my $bar_code = $returneditems{$_};
532 if ($riduedate{$_}) {
533 my $duedate = dt_from_string( $riduedate{$_}, 'sql');
534 $ri{year} = $duedate->year();
535 $ri{month} = $duedate->month();
536 $ri{day} = $duedate->day();
537 $ri{hour} = $duedate->hour();
538 $ri{minute} = $duedate->minute();
539 $ri{duedate} = output_pref($duedate);
540 my ($b) = GetMemberDetails( $riborrowernumber{$_}, 0 );
541 $ri{return_overdue} = 1 if (DateTime->compare($duedate, DateTime->now()) == -1 );
542 $ri{borrowernumber} = $b->{'borrowernumber'};
543 $ri{borcnum} = $b->{'cardnumber'};
544 $ri{borfirstname} = $b->{'firstname'};
545 $ri{borsurname} = $b->{'surname'};
546 $ri{bortitle} = $b->{'title'};
547 $ri{bornote} = $b->{'borrowernotes'};
548 $ri{borcategorycode}= $b->{'categorycode'};
550 else {
551 $ri{borrowernumber} = $riborrowernumber{$_};
554 # my %ri;
555 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($bar_code));
556 my $item = GetItem( GetItemnumberFromBarcode($bar_code) );
557 # fix up item type for display
558 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
559 $ri{itembiblionumber} = $biblio->{'biblionumber'};
560 $ri{itemtitle} = $biblio->{'title'};
561 $ri{itemauthor} = $biblio->{'author'};
562 $ri{itemcallnumber} = $biblio->{'itemcallnumber'};
563 $ri{itemtype} = $biblio->{'itemtype'};
564 $ri{itemnote} = $biblio->{'itemnotes'};
565 $ri{ccode} = $biblio->{'ccode'};
566 $ri{itemnumber} = $biblio->{'itemnumber'};
567 $ri{barcode} = $bar_code;
568 $ri{homebranch} = $item->{'homebranch'};
569 $ri{holdingbranch} = $item->{'holdingbranch'};
571 $ri{location} = $biblio->{'location'};
572 my $shelfcode = $ri{'location'};
573 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
576 else {
577 last;
579 push @riloop, \%ri;
581 my ($genbrname, $genprname);
582 if (my $b = $branches->{$userenv_branch}) {
583 $genbrname = $b->{'branchname'};
585 if (my $p = $printers->{$printer}) {
586 $genprname = $p->{'printername'};
588 $template->param(
589 riloop => \@riloop,
590 genbrname => $genbrname,
591 genprname => $genprname,
592 branchname => $genbrname,
593 printer => $printer,
594 errmsgloop => \@errmsgloop,
595 exemptfine => $exemptfine,
596 dropboxmode => $dropboxmode,
597 dropboxdate => output_pref($dropboxdate),
598 overduecharges => $overduecharges,
599 soundon => C4::Context->preference("SoundOn"),
600 BlockReturnOfWithdrawnItems => C4::Context->preference("BlockReturnOfWithdrawnItems"),
603 ### Comment out rotating collections for now to allow it a little more time to bake
604 ### for 3.4; in particular, must ensure that it doesn't fight with transfers required
605 ### to fill hold requests
606 ### -- Galen Charlton 2010-10-06
607 #my $itemnumber = GetItemnumberFromBarcode( $query->param('barcode') );
608 #if ( $itemnumber ) {
609 # my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
610 # if ( ! ( $holdingBranch eq $collectionBranch ) ) {
611 # $template->param(
612 # collectionItemNeedsTransferred => 1,
613 # collectionBranch => GetBranchName($collectionBranch),
614 # );
618 # actually print the page!
619 output_html_with_http_headers $query, $cookie, $template->output;