3 # Copyright 2000-2002 Katipo Communications
5 # 2007 BibLibre, Paul POULAIN
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 script to execute returns of books
29 #use warnings; FIXME - Bug 2505
33 use C4::Auth qw/:DEFAULT get_session/;
36 use C4::Dates qw/format_date/;
37 use Date::Calc qw/Add_Delta_Days/;
44 use C4::Branch; # GetBranches GetBranchName
45 use C4::Koha; # FIXME : is it still useful ?
46 use C4::RotatingCollections;
50 if (!C4::Context->userenv){
51 my $sessionID = $query->cookie("CGISESSID");
52 my $session = get_session($sessionID);
53 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
54 # no branch set we can't return
55 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
61 my ( $template, $librarian, $cookie ) = get_template_and_user(
63 template_name => "circ/returns.tmpl",
67 flagsrequired => { circulate => "circulate_remaining_permissions" },
73 my $branches = GetBranches();
74 my $printers = GetPrinters();
76 my $printer = C4::Context->userenv ? C4::Context->userenv->{'branchprinter'} : "";
77 my $overduecharges = (C4::Context->preference('finesMode') && C4::Context->preference('finesMode') ne 'off');
79 my $userenv_branch = C4::Context->userenv->{'branch'} || '';
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 itembiblionumber => $iteminfo->{'biblionumber'},
153 iteminfo => $iteminfo->{'author'},
154 tobranchname => GetBranchName($messages->{'transfert'}),
156 borrowernumber => $borrowernumber,
157 borcnum => $borr->{'cardnumber'},
158 borfirstname => $borr->{'firstname'},
159 borsurname => $borr->{'surname'},
168 my $issueinformation;
170 my $barcode = $query->param('barcode');
171 my $exemptfine = $query->param('exemptfine');
172 my $dropboxmode = $query->param('dropboxmode');
173 my $dotransfer = $query->param('dotransfer');
174 my $calendar = C4::Calendar->new( branchcode => $userenv_branch );
175 #dropbox: get last open day (today - 1)
176 my $today = C4::Dates->new();
177 my $today_iso = $today->output('iso');
178 my $dropboxdate = $calendar->addDate($today, -1);
180 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
181 my $transferitem = $query->param('transferitem');
182 my $tobranch = $query->param('tobranch');
183 ModItemTransfer($transferitem, $userenv_branch, $tobranch);
186 # actually return book and prepare item table.....
188 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
189 $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
190 $itemnumber = GetItemnumberFromBarcode($barcode);
192 if ( C4::Context->preference("InProcessingToShelvingCart") ) {
193 my $item = GetItem( $itemnumber );
194 if ( $item->{'location'} eq 'PROC' ) {
195 $item->{'location'} = 'CART';
196 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
200 if ( C4::Context->preference("ReturnToShelvingCart") ) {
201 my $item = GetItem( $itemnumber );
202 $item->{'location'} = 'CART';
203 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
209 ( $returned, $messages, $issueinformation, $borrower ) =
210 AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode); # do the return
211 my $homeorholdingbranchreturn = C4::Context->preference('HomeOrHoldingBranchReturn') or 'homebranch';
213 # get biblio description
214 my $biblio = GetBiblioFromItemNumber($itemnumber);
215 # fix up item type for display
216 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
219 title => $biblio->{'title'},
220 homebranch => $biblio->{'homebranch'},
221 homebranchname => GetBranchName( $biblio->{$homeorholdingbranchreturn} ),
222 author => $biblio->{'author'},
223 itembarcode => $biblio->{'barcode'},
224 itemtype => $biblio->{'itemtype'},
225 ccode => $biblio->{'ccode'},
226 itembiblionumber => $biblio->{'biblionumber'},
236 my $duedate = $issueinformation->{'date_due'};
237 $returneditems{0} = $barcode;
238 $riborrowernumber{0} = $borrower->{'borrowernumber'};
239 $riduedate{0} = $duedate;
240 $input{borrowernumber} = $borrower->{'borrowernumber'};
241 $input{duedate} = $duedate;
242 $input{return_overdue} = 1 if ($duedate and $duedate lt $today->output('iso'));
243 push( @inputloop, \%input );
245 elsif ( !$messages->{'BadBarcode'} ) {
247 $returneditems{0} = $barcode;
249 if ( $messages->{'wthdrawn'} ) {
250 $input{withdrawn} = 1;
251 $input{borrowernumber} = 'Item Cancelled'; # FIXME: should be in display layer ?
252 $riborrowernumber{0} = 'Item Cancelled';
255 $input{borrowernumber} = ' '; # This seems clearly bogus.
256 $riborrowernumber{0} = ' ';
258 push( @inputloop, \%input );
261 $template->param( inputloop => \@inputloop );
267 # new op dev : we check if the document must be returned to his homebranch directly,
268 # if the document is transfered, we have warning message .
270 if ( $messages->{'WasTransfered'} ) {
277 if ( $messages->{'NeedsTransfer'} ){
281 itemnumber => $itemnumber,
285 if ( $messages->{'Wrongbranch'} ){
291 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
293 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
294 $messages->{'WrongTransfer'} = GetBranchName( $messages->{'WrongTransfer'} );
297 TransferWaitingAt => $messages->{'WrongTransfer'},
298 WrongTransferItem => $messages->{'WrongTransferItem'},
301 my $reserve = $messages->{'ResFound'};
302 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
303 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
304 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
307 wborfirstname => $borr->{'firstname'},
308 wborsurname => $borr->{'surname'},
309 wbortitle => $borr->{'title'},
310 wborphone => $borr->{'phone'},
311 wboremail => $borr->{'email'},
312 wboraddress => $borr->{'address'},
313 wboraddress2 => $borr->{'address2'},
314 wborcity => $borr->{'city'},
315 wborzip => $borr->{'zipcode'},
316 wborrowernumber => $reserve->{'borrowernumber'},
317 wborcnum => $borr->{'cardnumber'},
318 wtransfertFrom => $userenv_branch,
323 # reserve found and item arrived at the expected branch
325 if ( $messages->{'ResFound'}) {
326 my $reserve = $messages->{'ResFound'};
327 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
328 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
330 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
331 if ( $reserve->{'ResFound'} eq "Waiting" ) {
333 waiting => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
335 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
337 intransit => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
338 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
339 resbarcode => $barcode,
344 # same params for Waiting or Reserved
347 currentbranch => $branches->{$userenv_branch}->{'branchname'},
348 destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
349 name => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
350 borfirstname => $borr->{'firstname'},
351 borsurname => $borr->{'surname'},
352 bortitle => $borr->{'title'},
353 borphone => $borr->{'phone'},
354 boremail => $borr->{'email'},
355 boraddress => $borr->{'address'},
356 boraddress2 => $borr->{'address2'},
357 borcity => $borr->{'city'},
358 borzip => $borr->{'zipcode'},
359 borcnum => $borr->{'cardnumber'},
360 debarred => $borr->{'debarred'},
361 gonenoaddress => $borr->{'gonenoaddress'},
363 destbranch => $reserve->{'branchcode'},
364 borrowernumber => $reserve->{'borrowernumber'},
365 itemnumber => $reserve->{'itemnumber'},
366 reservenotes => $reserve->{'reservenotes'},
368 } # else { ; } # error?
373 foreach my $code ( keys %$messages ) {
375 my $exit_required_p = 0;
376 if ( $code eq 'BadBarcode' ) {
377 $err{badbarcode} = 1;
378 $err{msg} = $messages->{'BadBarcode'};
380 elsif ( $code eq 'NotIssued' ) {
382 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
384 elsif ( $code eq 'WasLost' ) {
387 elsif ( $code eq 'ResFound' ) {
388 ; # FIXME... anything to do here?
390 elsif ( $code eq 'WasReturned' ) {
391 ; # FIXME... anything to do here?
393 elsif ( $code eq 'WasTransfered' ) {
394 ; # FIXME... anything to do here?
396 elsif ( $code eq 'wthdrawn' ) {
398 $exit_required_p = 1;
400 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
401 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
402 $err{ispermanent} = 1;
404 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
407 elsif ( $code eq 'WrongTransfer' ) {
408 ; # FIXME... anything to do here?
410 elsif ( $code eq 'WrongTransferItem' ) {
411 ; # FIXME... anything to do here?
413 elsif ( $code eq 'NeedsTransfer' ) {
415 elsif ( $code eq 'Wrongbranch' ) {
419 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
420 # This forces the issue of staying in sync w/ Circulation.pm
423 push( @errmsgloop, \%err );
425 last if $exit_required_p;
427 $template->param( errmsgloop => \@errmsgloop );
431 my $flags = $borrower->{'flags'};
434 foreach my $flag ( sort keys %$flags ) {
436 unless ($flagset) { $flagset = 1; }
437 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
438 $flaginfo{flag} = $flag;
439 if ( $flag eq 'CHARGES' ) {
440 $flaginfo{msg} = $flag;
441 $flaginfo{charges} = 1;
442 $flaginfo{chargeamount} = $flags->{$flag}->{amount};
443 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
445 elsif ( $flag eq 'WAITING' ) {
446 $flaginfo{msg} = $flag;
447 $flaginfo{waiting} = 1;
449 my $items = $flags->{$flag}->{'itemlist'};
450 foreach my $item (@$items) {
451 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
452 push @waitingitemloop, {
453 biblionum => $biblio->{'biblionumber'},
454 barcode => $biblio->{'barcode'},
455 title => $biblio->{'title'},
456 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
459 $flaginfo{itemloop} = \@waitingitemloop;
461 elsif ( $flag eq 'ODUES' ) {
462 my $items = $flags->{$flag}->{'itemlist'};
464 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
467 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
469 duedate => format_date($item->{'date_due'}),
470 biblionum => $biblio->{'biblionumber'},
471 barcode => $biblio->{'barcode'},
472 title => $biblio->{'title'},
473 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
476 $flaginfo{itemloop} = \@itemloop;
477 $flaginfo{overdue} = 1;
480 $flaginfo{other} = 1;
481 $flaginfo{msg} = $flags->{$flag}->{'message'};
483 push( @flagloop, \%flaginfo );
487 flagloop => \@flagloop,
488 riborrowernumber => $borrower->{'borrowernumber'},
489 riborcnum => $borrower->{'cardnumber'},
490 riborsurname => $borrower->{'surname'},
491 ribortitle => $borrower->{'title'},
492 riborfirstname => $borrower->{'firstname'}
496 #set up so only the last 8 returned items display (make for faster loading pages)
497 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
500 foreach ( sort { $a <=> $b } keys %returneditems ) {
502 if ( $count++ < $returned_counter ) {
503 my $bar_code = $returneditems{$_};
504 my $duedate = $riduedate{$_};
506 my @tempdate = split( /-/, $duedate );
507 $ri{year} = $tempdate[0];
508 $ri{month} = $tempdate[1];
509 $ri{day} = $tempdate[2];
510 $ri{duedate} = format_date($duedate);
511 my ($b) = GetMemberDetails( $riborrowernumber{$_}, 0 );
512 $ri{return_overdue} = 1 if ($duedate lt $today->output('iso'));
513 $ri{borrowernumber} = $b->{'borrowernumber'};
514 $ri{borcnum} = $b->{'cardnumber'};
515 $ri{borfirstname} = $b->{'firstname'};
516 $ri{borsurname} = $b->{'surname'};
517 $ri{bortitle} = $b->{'title'};
518 $ri{bornote} = $b->{'borrowernotes'};
519 $ri{borcategorycode}= $b->{'categorycode'};
522 $ri{borrowernumber} = $riborrowernumber{$_};
526 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($bar_code));
527 # fix up item type for display
528 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
529 $ri{itembiblionumber} = $biblio->{'biblionumber'};
530 $ri{itemtitle} = $biblio->{'title'};
531 $ri{itemauthor} = $biblio->{'author'};
532 $ri{itemtype} = $biblio->{'itemtype'};
533 $ri{itemnote} = $biblio->{'itemnotes'};
534 $ri{ccode} = $biblio->{'ccode'};
535 $ri{itemnumber} = $biblio->{'itemnumber'};
536 $ri{barcode} = $bar_code;
546 genbrname => $branches->{$userenv_branch}->{'branchname'},
547 genprname => $printers->{$printer}->{'printername'},
548 branchname => $branches->{$userenv_branch}->{'branchname'},
550 errmsgloop => \@errmsgloop,
551 exemptfine => $exemptfine,
552 dropboxmode => $dropboxmode,
553 dropboxdate => $dropboxdate->output(),
554 overduecharges => $overduecharges,
555 soundon => C4::Context->preference("SoundOn"),
558 ### Comment out rotating collections for now to allow it a little more time to bake
559 ### for 3.4; in particular, must ensure that it doesn't fight with transfers required
560 ### to fill hold requests
561 ### -- Galen Charlton 2010-10-06
562 #my $itemnumber = GetItemnumberFromBarcode( $query->param('barcode') );
563 #if ( $itemnumber ) {
564 # my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
565 # if ( ! ( $holdingBranch eq $collectionBranch ) ) {
567 # collectionItemNeedsTransferred => 1,
568 # collectionBranch => GetBranchName($collectionBranch),
573 # actually print the page!
574 output_html_with_http_headers $query, $cookie, $template->output;