bug_2830: Remove reserve when checking out if the borrower is not the first one in...
[koha.git] / circ / returns.pl
blob98d310e12429c37791b8700840894793d76a99b0
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # 2006 SAN-OP
5 # 2007-2010 BibLibre, Paul POULAIN
6 # 2010 Catalyst IT
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 =head1 returns.pl
25 script to execute returns of books
27 =cut
29 use strict;
30 #use warnings; FIXME - Bug 2505
32 use CGI;
33 use C4::Context;
34 use C4::Auth qw/:DEFAULT get_session/;
35 use C4::Output;
36 use C4::Circulation;
37 use C4::Dates qw/format_date/;
38 use Date::Calc qw/Add_Delta_Days/;
39 use C4::Calendar;
40 use C4::Print;
41 use C4::Reserves;
42 use C4::Biblio;
43 use C4::Items;
44 use C4::Members;
45 use C4::Branch; # GetBranches GetBranchName
46 use C4::Koha; # FIXME : is it still useful ?
47 use C4::RotatingCollections;
49 my $query = new CGI;
51 if (!C4::Context->userenv){
52 my $sessionID = $query->cookie("CGISESSID");
53 my $session = get_session($sessionID);
54 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
55 # no branch set we can't return
56 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
57 exit;
61 #getting the template
62 my ( $template, $librarian, $cookie ) = get_template_and_user(
64 template_name => "circ/returns.tmpl",
65 query => $query,
66 type => "intranet",
67 authnotrequired => 0,
68 flagsrequired => { circulate => "circulate_remaining_permissions" },
72 #####################
73 #Global vars
74 my $branches = GetBranches();
75 my $printers = GetPrinters();
77 my $printer = C4::Context->userenv ? C4::Context->userenv->{'branchprinter'} : "";
78 my $overduecharges = (C4::Context->preference('finesMode') && C4::Context->preference('finesMode') ne 'off');
80 my $userenv_branch = C4::Context->userenv->{'branch'} || '';
82 # Some code to handle the error if there is no branch or printer setting.....
85 # Set up the item stack ....
86 my %returneditems;
87 my %riduedate;
88 my %riborrowernumber;
89 my @inputloop;
90 foreach ( $query->param ) {
91 my $counter;
92 if (/ri-(\d*)/) {
93 $counter = $1;
94 if ($counter > 20) {
95 next;
98 else {
99 next;
102 my %input;
103 my $barcode = $query->param("ri-$counter");
104 my $duedate = $query->param("dd-$counter");
105 my $borrowernumber = $query->param("bn-$counter");
106 $counter++;
108 # decode barcode ## Didn't we already decode them before passing them back last time??
109 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
110 $barcode = barcodedecode($barcode) if(C4::Context->preference('itemBarcodeInputFilter'));
112 ######################
113 #Are these lines still useful ?
114 $returneditems{$counter} = $barcode;
115 $riduedate{$counter} = $duedate;
116 $riborrowernumber{$counter} = $borrowernumber;
118 #######################
119 $input{counter} = $counter;
120 $input{barcode} = $barcode;
121 $input{duedate} = $duedate;
122 $input{borrowernumber} = $borrowernumber;
123 push( @inputloop, \%input );
126 ############
127 # Deal with the requests....
129 if ($query->param('WT-itemNumber')){
130 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
133 if ( $query->param('resbarcode') ) {
134 my $item = $query->param('itemnumber');
135 my $borrowernumber = $query->param('borrowernumber');
136 my $resbarcode = $query->param('resbarcode');
137 my $diffBranchReturned = $query->param('diffBranch');
138 my $iteminfo = GetBiblioFromItemNumber($item);
139 # fix up item type for display
140 $iteminfo->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $iteminfo->{'itype'} : $iteminfo->{'itemtype'};
141 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ? $diffBranchReturned : undef;
142 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
143 # i.e., whether to apply waiting status
144 ModReserveAffect( $item, $borrowernumber, $diffBranchSend);
145 # check if we have other reserves for this document, if we have a return send the message of transfer
146 my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
148 my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
149 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
150 if ( $messages->{'transfert'} ) {
151 $template->param(
152 itemtitle => $iteminfo->{'title'},
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 my $dropboxmode = $query->param('dropboxmode');
174 my $dotransfer = $query->param('dotransfer');
175 my $canceltransfer = $query->param('canceltransfer');
176 my $dest = $query->param('dest');
177 my $calendar = C4::Calendar->new( branchcode => $userenv_branch );
178 #dropbox: get last open day (today - 1)
179 my $today = C4::Dates->new();
180 my $today_iso = $today->output('iso');
181 my $dropboxdate = $calendar->addDate($today, -1);
182 if ($dotransfer){
183 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
184 my $transferitem = $query->param('transferitem');
185 my $tobranch = $query->param('tobranch');
186 ModItemTransfer($transferitem, $userenv_branch, $tobranch);
189 if ($canceltransfer){
190 $itemnumber=$query->param('itemnumber');
191 DeleteTransfer($itemnumber);
192 if($dest eq "ttr"){
193 print $query->redirect("/cgi-bin/koha/circ/transferstoreceive.pl");
194 exit;
195 } else {
196 $template->param( transfercancelled => 1);
200 # actually return book and prepare item table.....
201 if ($barcode) {
202 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
203 $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
204 $itemnumber = GetItemnumberFromBarcode($barcode);
206 if ( C4::Context->preference("InProcessingToShelvingCart") ) {
207 my $item = GetItem( $itemnumber );
208 if ( $item->{'location'} eq 'PROC' ) {
209 $item->{'location'} = 'CART';
210 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
214 if ( C4::Context->preference("ReturnToShelvingCart") ) {
215 my $item = GetItem( $itemnumber );
216 $item->{'location'} = 'CART';
217 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
221 # save the return
223 ( $returned, $messages, $issueinformation, $borrower ) =
224 AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode); # do the return
225 my $homeorholdingbranchreturn = C4::Context->preference('HomeOrHoldingBranchReturn') or 'homebranch';
227 # get biblio description
228 my $biblio = GetBiblioFromItemNumber($itemnumber);
229 # fix up item type for display
230 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
232 $template->param(
233 title => $biblio->{'title'},
234 homebranch => $biblio->{'homebranch'},
235 homebranchname => GetBranchName( $biblio->{$homeorholdingbranchreturn} ),
236 author => $biblio->{'author'},
237 itembarcode => $biblio->{'barcode'},
238 itemtype => $biblio->{'itemtype'},
239 ccode => $biblio->{'ccode'},
240 itembiblionumber => $biblio->{'biblionumber'},
243 my %input = (
244 counter => 0,
245 first => 1,
246 barcode => $barcode,
249 if ($returned) {
250 my $duedate = $issueinformation->{'date_due'};
251 $returneditems{0} = $barcode;
252 $riborrowernumber{0} = $borrower->{'borrowernumber'};
253 $riduedate{0} = $duedate;
254 $input{borrowernumber} = $borrower->{'borrowernumber'};
255 $input{duedate} = $duedate;
256 $input{return_overdue} = 1 if ($duedate and $duedate lt $today->output('iso'));
257 push( @inputloop, \%input );
259 if ( C4::Context->preference("FineNotifyAtCheckin") ) {
260 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $borrower->{'borrowernumber'} );
261 if ($fines > 0) {
262 $template->param( fines => sprintf("%.2f",$fines) );
263 $template->param( fineborrowernumber => $borrower->{'borrowernumber'} );
267 if (C4::Context->preference("WaitingNotifyAtCheckin") ) {
268 #Check for waiting holds
269 my @reserves = GetReservesFromBorrowernumber($borrower->{'borrowernumber'});
270 my $waiting_holds;
271 foreach my $num_res (@reserves) {
272 if ( $num_res->{'found'} eq 'W' && $num_res->{'branchcode'} eq $userenv_branch) {
273 $waiting_holds++;
276 if ($waiting_holds > 0) {
277 $template->param(
278 waiting_holds => $waiting_holds,
279 holdsborrowernumber => $borrower->{'borrowernumber'},
280 holdsfirstname => $borrower->{'firstname'},
281 holdssurname => $borrower->{'surname'},
286 elsif ( !$messages->{'BadBarcode'} ) {
287 $input{duedate} = 0;
288 $returneditems{0} = $barcode;
289 $riduedate{0} = 0;
290 if ( $messages->{'wthdrawn'} ) {
291 $input{withdrawn} = 1;
292 $input{borrowernumber} = 'Item Cancelled'; # FIXME: should be in display layer ?
293 $riborrowernumber{0} = 'Item Cancelled';
295 else {
296 $input{borrowernumber} = ' '; # This seems clearly bogus.
297 $riborrowernumber{0} = ' ';
299 push( @inputloop, \%input );
302 $template->param( inputloop => \@inputloop );
304 my $found = 0;
305 my $waiting = 0;
306 my $reserved = 0;
308 # new op dev : we check if the document must be returned to his homebranch directly,
309 # if the document is transfered, we have warning message .
311 if ( $messages->{'WasTransfered'} ) {
312 $template->param(
313 found => 1,
314 transfer => 1,
318 if ( $messages->{'NeedsTransfer'} ){
319 $template->param(
320 found => 1,
321 needstransfer => 1,
322 itemnumber => $itemnumber,
326 if ( $messages->{'Wrongbranch'} ){
327 $template->param(
328 wrongbranch => 1,
332 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
334 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
335 $messages->{'WrongTransfer'} = GetBranchName( $messages->{'WrongTransfer'} );
336 $template->param(
337 WrongTransfer => 1,
338 TransferWaitingAt => $messages->{'WrongTransfer'},
339 WrongTransferItem => $messages->{'WrongTransferItem'},
340 itemnumber => $itemnumber,
343 my $reserve = $messages->{'ResFound'};
344 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
345 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
346 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
347 $template->param(
348 wname => $name,
349 wborfirstname => $borr->{'firstname'},
350 wborsurname => $borr->{'surname'},
351 wbortitle => $borr->{'title'},
352 wborphone => $borr->{'phone'},
353 wboremail => $borr->{'email'},
354 wboraddress => $borr->{'address'},
355 wboraddress2 => $borr->{'address2'},
356 wborcity => $borr->{'city'},
357 wborzip => $borr->{'zipcode'},
358 wborrowernumber => $reserve->{'borrowernumber'},
359 wborcnum => $borr->{'cardnumber'},
360 wtransfertFrom => $userenv_branch,
365 # reserve found and item arrived at the expected branch
367 if ( $messages->{'ResFound'}) {
368 my $reserve = $messages->{'ResFound'};
369 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
370 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
372 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
373 if ( $reserve->{'ResFound'} eq "Waiting" ) {
374 $template->param(
375 waiting => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
377 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
378 $template->param(
379 intransit => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
380 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
381 resbarcode => $barcode,
382 reserved => 1,
386 # same params for Waiting or Reserved
387 $template->param(
388 found => 1,
389 currentbranch => $branches->{$userenv_branch}->{'branchname'},
390 destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
391 name => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
392 borfirstname => $borr->{'firstname'},
393 borsurname => $borr->{'surname'},
394 bortitle => $borr->{'title'},
395 borphone => $borr->{'phone'},
396 boremail => $borr->{'email'},
397 boraddress => $borr->{'address'},
398 boraddress2 => $borr->{'address2'},
399 borcity => $borr->{'city'},
400 borzip => $borr->{'zipcode'},
401 borcnum => $borr->{'cardnumber'},
402 debarred => $borr->{'debarred'},
403 gonenoaddress => $borr->{'gonenoaddress'},
404 barcode => $barcode,
405 destbranch => $reserve->{'branchcode'},
406 borrowernumber => $reserve->{'borrowernumber'},
407 itemnumber => $reserve->{'itemnumber'},
408 reservenotes => $reserve->{'reservenotes'},
410 } # else { ; } # error?
413 # Error Messages
414 my @errmsgloop;
415 foreach my $code ( keys %$messages ) {
416 my %err;
417 my $exit_required_p = 0;
418 if ( $code eq 'BadBarcode' ) {
419 $err{badbarcode} = 1;
420 $err{msg} = $messages->{'BadBarcode'};
422 elsif ( $code eq 'NotIssued' ) {
423 $err{notissued} = 1;
424 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
426 elsif ( $code eq 'LocalUse' ) {
427 $err{localuse} = 1;
429 elsif ( $code eq 'WasLost' ) {
430 $err{waslost} = 1;
432 elsif ( $code eq 'ResFound' ) {
433 ; # FIXME... anything to do here?
435 elsif ( $code eq 'WasReturned' ) {
436 ; # FIXME... anything to do here?
438 elsif ( $code eq 'WasTransfered' ) {
439 ; # FIXME... anything to do here?
441 elsif ( $code eq 'wthdrawn' ) {
442 $err{withdrawn} = 1;
443 $exit_required_p = 1;
445 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
446 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
447 $err{ispermanent} = 1;
448 $err{msg} =
449 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
452 elsif ( $code eq 'WrongTransfer' ) {
453 ; # FIXME... anything to do here?
455 elsif ( $code eq 'WrongTransferItem' ) {
456 ; # FIXME... anything to do here?
458 elsif ( $code eq 'NeedsTransfer' ) {
460 elsif ( $code eq 'Wrongbranch' ) {
462 elsif ( $code eq 'Debarred' ) {
463 $err{debarred} = format_date( $messages->{'Debarred'} );
464 $err{debarcardnumber} = $borrower->{cardnumber};
465 $err{debarborrowernumber} = $borrower->{borrowernumber};
466 $err{debarname} = "$borrower->{firstname} $borrower->{surname}";
468 else {
469 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
470 # This forces the issue of staying in sync w/ Circulation.pm
472 if (%err) {
473 push( @errmsgloop, \%err );
475 last if $exit_required_p;
477 $template->param( errmsgloop => \@errmsgloop );
479 # patrontable ....
480 if ($borrower) {
481 my $flags = $borrower->{'flags'};
482 my @flagloop;
483 my $flagset;
484 foreach my $flag ( sort keys %$flags ) {
485 my %flaginfo;
486 unless ($flagset) { $flagset = 1; }
487 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
488 $flaginfo{flag} = $flag;
489 if ( $flag eq 'CHARGES' ) {
490 $flaginfo{msg} = $flag;
491 $flaginfo{charges} = 1;
492 $flaginfo{chargeamount} = $flags->{$flag}->{amount};
493 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
495 elsif ( $flag eq 'WAITING' ) {
496 $flaginfo{msg} = $flag;
497 $flaginfo{waiting} = 1;
498 my @waitingitemloop;
499 my $items = $flags->{$flag}->{'itemlist'};
500 foreach my $item (@$items) {
501 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
502 push @waitingitemloop, {
503 biblionum => $biblio->{'biblionumber'},
504 barcode => $biblio->{'barcode'},
505 title => $biblio->{'title'},
506 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
509 $flaginfo{itemloop} = \@waitingitemloop;
511 elsif ( $flag eq 'ODUES' ) {
512 my $items = $flags->{$flag}->{'itemlist'};
513 my @itemloop;
514 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
515 @$items )
517 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
518 push @itemloop, {
519 duedate => format_date($item->{'date_due'}),
520 biblionum => $biblio->{'biblionumber'},
521 barcode => $biblio->{'barcode'},
522 title => $biblio->{'title'},
523 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
526 $flaginfo{itemloop} = \@itemloop;
527 $flaginfo{overdue} = 1;
529 else {
530 $flaginfo{other} = 1;
531 $flaginfo{msg} = $flags->{$flag}->{'message'};
533 push( @flagloop, \%flaginfo );
535 $template->param(
536 flagset => $flagset,
537 flagloop => \@flagloop,
538 riborrowernumber => $borrower->{'borrowernumber'},
539 riborcnum => $borrower->{'cardnumber'},
540 riborsurname => $borrower->{'surname'},
541 ribortitle => $borrower->{'title'},
542 riborfirstname => $borrower->{'firstname'}
546 #set up so only the last 8 returned items display (make for faster loading pages)
547 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
548 my $count = 0;
549 my @riloop;
550 my $shelflocations = GetKohaAuthorisedValues('items.location','');
551 foreach ( sort { $a <=> $b } keys %returneditems ) {
552 my %ri;
553 if ( $count++ < $returned_counter ) {
554 my $bar_code = $returneditems{$_};
555 my $duedate = $riduedate{$_};
556 if ($duedate) {
557 my @tempdate = split( /-/, $duedate );
558 $ri{year} = $tempdate[0];
559 $ri{month} = $tempdate[1];
560 $ri{day} = $tempdate[2];
561 $ri{duedate} = format_date($duedate);
562 my ($b) = GetMemberDetails( $riborrowernumber{$_}, 0 );
563 $ri{return_overdue} = 1 if ($duedate lt $today->output('iso'));
564 $ri{borrowernumber} = $b->{'borrowernumber'};
565 $ri{borcnum} = $b->{'cardnumber'};
566 $ri{borfirstname} = $b->{'firstname'};
567 $ri{borsurname} = $b->{'surname'};
568 $ri{bortitle} = $b->{'title'};
569 $ri{bornote} = $b->{'borrowernotes'};
570 $ri{borcategorycode}= $b->{'categorycode'};
572 else {
573 $ri{borrowernumber} = $riborrowernumber{$_};
576 # my %ri;
577 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($bar_code));
578 # fix up item type for display
579 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
580 $ri{itembiblionumber} = $biblio->{'biblionumber'};
581 $ri{itemtitle} = $biblio->{'title'};
582 $ri{itemauthor} = $biblio->{'author'};
583 $ri{itemcallnumber} = $biblio->{'itemcallnumber'};
584 $ri{itemtype} = $biblio->{'itemtype'};
585 $ri{itemnote} = $biblio->{'itemnotes'};
586 $ri{ccode} = $biblio->{'ccode'};
587 $ri{itemnumber} = $biblio->{'itemnumber'};
588 $ri{barcode} = $bar_code;
590 $ri{location} = $biblio->{'location'};
591 my $shelfcode = $ri{'location'};
592 $ri{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
595 else {
596 last;
598 push @riloop, \%ri;
601 $template->param(
602 riloop => \@riloop,
603 genbrname => $branches->{$userenv_branch}->{'branchname'},
604 genprname => $printers->{$printer}->{'printername'},
605 branchname => $branches->{$userenv_branch}->{'branchname'},
606 printer => $printer,
607 errmsgloop => \@errmsgloop,
608 exemptfine => $exemptfine,
609 dropboxmode => $dropboxmode,
610 dropboxdate => $dropboxdate->output(),
611 overduecharges => $overduecharges,
612 soundon => C4::Context->preference("SoundOn"),
615 ### Comment out rotating collections for now to allow it a little more time to bake
616 ### for 3.4; in particular, must ensure that it doesn't fight with transfers required
617 ### to fill hold requests
618 ### -- Galen Charlton 2010-10-06
619 #my $itemnumber = GetItemnumberFromBarcode( $query->param('barcode') );
620 #if ( $itemnumber ) {
621 # my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
622 # if ( ! ( $holdingBranch eq $collectionBranch ) ) {
623 # $template->param(
624 # collectionItemNeedsTransferred => 1,
625 # collectionBranch => GetBranchName($collectionBranch),
626 # );
630 # actually print the page!
631 output_html_with_http_headers $query, $cookie, $template->output;