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 with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
24 script to execute returns of books
29 # use warnings; # FIXME
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 ?
49 if (!C4::Context->userenv){
50 my $sessionID = $query->cookie("CGISESSID");
51 my $session = get_session($sessionID);
52 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
53 # no branch set we can't return
54 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
60 my ( $template, $librarian, $cookie ) = get_template_and_user(
62 template_name => "circ/returns.tmpl",
66 flagsrequired => { circulate => "circulate_remaining_permissions" },
72 my $branches = GetBranches();
73 my $printers = GetPrinters();
75 #my $branch = C4::Context->userenv?C4::Context->userenv->{'branch'}:"";
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 ) {
90 (next) unless (/ri-(\d*)/);
93 (next) if ( $counter > 20 );
94 my $barcode = $query->param("ri-$counter");
95 my $duedate = $query->param("dd-$counter");
96 my $borrowernumber = $query->param("bn-$counter");
99 # decode barcode ## Didn't we already decode them before passing them back last time??
100 $barcode = barcodedecode($barcode) if(C4::Context->preference('itemBarcodeInputFilter'));
102 ######################
103 #Are these lines still useful ?
104 $returneditems{$counter} = $barcode;
105 $riduedate{$counter} = $duedate;
106 $riborrowernumber{$counter} = $borrowernumber;
108 #######################
109 $input{counter} = $counter;
110 $input{barcode} = $barcode;
111 $input{duedate} = $duedate;
112 $input{borrowernumber} = $borrowernumber;
113 push( @inputloop, \%input );
117 # Deal with the requests....
119 if ($query->param('WT-itemNumber')){
120 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
123 if ( $query->param('resbarcode') ) {
124 my $item = $query->param('itemnumber');
125 my $borrowernumber = $query->param('borrowernumber');
126 my $resbarcode = $query->param('resbarcode');
127 my $diffBranchReturned = $query->param('diffBranch');
128 my $iteminfo = GetBiblioFromItemNumber($item);
129 # fix up item type for display
130 $iteminfo->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $iteminfo->{'itype'} : $iteminfo->{'itemtype'};
131 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ? $diffBranchReturned : undef;
132 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
133 # i.e., whether to apply waiting status
134 ModReserveAffect( $item, $borrowernumber, $diffBranchSend);
135 # check if we have other reserves for this document, if we have a return send the message of transfer
136 my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
138 my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
139 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
140 if ( $messages->{'transfert'} ) {
142 itemtitle => $iteminfo->{'title'},
143 itembiblionumber => $iteminfo->{'biblionumber'},
144 iteminfo => $iteminfo->{'author'},
145 tobranchname => GetBranchName($messages->{'transfert'}),
147 borrowernumber => $borrowernumber,
148 borcnum => $borr->{'cardnumber'},
149 borfirstname => $borr->{'firstname'},
150 borsurname => $borr->{'surname'},
159 my $issueinformation;
161 my $barcode = $query->param('barcode');
162 my $exemptfine = $query->param('exemptfine');
163 my $dropboxmode = $query->param('dropboxmode');
164 my $dotransfer = $query->param('dotransfer');
165 my $calendar = C4::Calendar->new( branchcode => $userenv_branch );
166 #dropbox: get last open day (today - 1)
167 my $today = C4::Dates->new();
168 my $today_iso = $today->output('iso');
169 my $dropboxdate = $calendar->addDate($today, -1);
171 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
172 my $transferitem = $query->param('transferitem');
173 my $tobranch = $query->param('tobranch');
174 ModItemTransfer($transferitem, $userenv_branch, $tobranch);
177 # actually return book and prepare item table.....
179 $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
180 $itemnumber = GetItemnumberFromBarcode($barcode);
182 ( $returned, $messages, $issueinformation, $borrower ) =
183 AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode); # do the return
185 # get biblio description
186 my $biblio = GetBiblioFromItemNumber($itemnumber);
187 # fix up item type for display
188 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
191 title => $biblio->{'title'},
192 homebranch => $biblio->{'homebranch'},
193 author => $biblio->{'author'},
194 itembarcode => $biblio->{'barcode'},
195 itemtype => $biblio->{'itemtype'},
196 ccode => $biblio->{'ccode'},
197 itembiblionumber => $biblio->{'biblionumber'},
207 my $duedate = $issueinformation->{'date_due'};
208 $returneditems{0} = $barcode;
209 $riborrowernumber{0} = $borrower->{'borrowernumber'};
210 $riduedate{0} = $duedate;
211 $input{borrowernumber} = $borrower->{'borrowernumber'};
212 $input{duedate} = $duedate;
213 $input{return_overdue} = 1 if ($duedate and $duedate lt $today->output('iso'));
214 push( @inputloop, \%input );
216 elsif ( !$messages->{'BadBarcode'} ) {
218 $returneditems{0} = $barcode;
220 if ( $messages->{'wthdrawn'} ) {
221 $input{withdrawn} = 1;
222 $input{borrowernumber} = 'Item Cancelled'; # FIXME: should be in display layer ?
223 $riborrowernumber{0} = 'Item Cancelled';
226 $input{borrowernumber} = ' '; # This seems clearly bogus.
227 $riborrowernumber{0} = ' ';
229 push( @inputloop, \%input );
232 $template->param( inputloop => \@inputloop );
238 # new op dev : we check if the document must be returned to his homebranch directly,
239 # if the document is transfered, we have warning message .
241 if ( $messages->{'WasTransfered'} ) {
248 if ( $messages->{'NeedsTransfer'} ){
252 itemnumber => $itemnumber,
256 if ( $messages->{'Wrongbranch'} ){
262 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
264 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
267 TransferWaitingAt => $messages->{'WrongTransfer'},
268 WrongTransferItem => $messages->{'WrongTransferItem'},
271 my $reserve = $messages->{'ResFound'};
272 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
273 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
274 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
277 wborfirstname => $borr->{'firstname'},
278 wborsurname => $borr->{'surname'},
279 wbortitle => $borr->{'title'},
280 wborphone => $borr->{'phone'},
281 wboremail => $borr->{'email'},
282 wboraddress => $borr->{'address'},
283 wboraddress2 => $borr->{'address2'},
284 wborcity => $borr->{'city'},
285 wborzip => $borr->{'zipcode'},
286 wborrowernumber => $reserve->{'borrowernumber'},
287 wborcnum => $borr->{'cardnumber'},
288 wtransfertFrom => $userenv_branch,
293 # reserve found and item arrived at the expected branch
295 if ( $messages->{'ResFound'}) {
296 my $reserve = $messages->{'ResFound'};
297 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
298 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
300 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
301 if ( $reserve->{'ResFound'} eq "Waiting" ) {
303 waiting => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
305 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
307 intransit => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
308 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
309 resbarcode => $barcode,
314 # same params for Waiting or Reserved
317 currentbranch => $branches->{$userenv_branch}->{'branchname'},
318 destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
319 name => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
320 borfirstname => $borr->{'firstname'},
321 borsurname => $borr->{'surname'},
322 bortitle => $borr->{'title'},
323 borphone => $borr->{'phone'},
324 boremail => $borr->{'email'},
325 boraddress => $borr->{'address'},
326 boraddress2 => $borr->{'address2'},
327 borcity => $borr->{'city'},
328 borzip => $borr->{'zipcode'},
329 borcnum => $borr->{'cardnumber'},
330 debarred => $borr->{'debarred'},
331 gonenoaddress => $borr->{'gonenoaddress'},
333 destbranch => $reserve->{'branchcode'},
334 borrowernumber => $reserve->{'borrowernumber'},
335 itemnumber => $reserve->{'itemnumber'},
337 } # else { ; } # error?
342 foreach my $code ( keys %$messages ) {
344 my $exit_required_p = 0;
345 if ( $code eq 'BadBarcode' ) {
346 $err{badbarcode} = 1;
347 $err{msg} = $messages->{'BadBarcode'};
349 elsif ( $code eq 'NotIssued' ) {
351 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
353 elsif ( $code eq 'WasLost' ) {
356 elsif ( $code eq 'ResFound' ) {
357 ; # FIXME... anything to do here?
359 elsif ( $code eq 'WasReturned' ) {
360 ; # FIXME... anything to do here?
362 elsif ( $code eq 'WasTransfered' ) {
363 ; # FIXME... anything to do here?
365 elsif ( $code eq 'wthdrawn' ) {
367 $exit_required_p = 1;
369 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
370 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
371 $err{ispermanent} = 1;
373 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
376 elsif ( $code eq 'WrongTransfer' ) {
377 ; # FIXME... anything to do here?
379 elsif ( $code eq 'WrongTransferItem' ) {
380 ; # FIXME... anything to do here?
382 elsif ( $code eq 'NeedsTransfer' ) {
384 elsif ( $code eq 'Wrongbranch' ) {
388 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
389 # This forces the issue of staying in sync w/ Circulation.pm
392 push( @errmsgloop, \%err );
394 last if $exit_required_p;
396 $template->param( errmsgloop => \@errmsgloop );
400 my $flags = $borrower->{'flags'};
403 foreach my $flag ( sort keys %$flags ) {
405 unless ($flagset) { $flagset = 1; }
406 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
407 $flaginfo{flag} = $flag;
408 if ( $flag eq 'CHARGES' ) {
409 $flaginfo{msg} = $flag;
410 $flaginfo{charges} = 1;
411 $flaginfo{chargeamount} = $flags->{$flag}->{amount};
412 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
414 elsif ( $flag eq 'WAITING' ) {
415 $flaginfo{msg} = $flag;
416 $flaginfo{waiting} = 1;
418 my $items = $flags->{$flag}->{'itemlist'};
419 foreach my $item (@$items) {
420 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
421 push @waitingitemloop, {
422 biblionum => $biblio->{'biblionumber'},
423 barcode => $biblio->{'barcode'},
424 title => $biblio->{'title'},
425 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
428 $flaginfo{itemloop} = \@waitingitemloop;
430 elsif ( $flag eq 'ODUES' ) {
431 my $items = $flags->{$flag}->{'itemlist'};
433 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
436 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
438 duedate => format_date($item->{'date_due'}),
439 biblionum => $biblio->{'biblionumber'},
440 barcode => $biblio->{'barcode'},
441 title => $biblio->{'title'},
442 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
445 $flaginfo{itemloop} = \@itemloop;
446 $flaginfo{overdue} = 1;
449 $flaginfo{other} = 1;
450 $flaginfo{msg} = $flags->{$flag}->{'message'};
452 push( @flagloop, \%flaginfo );
456 flagloop => \@flagloop,
457 riborrowernumber => $borrower->{'borrowernumber'},
458 riborcnum => $borrower->{'cardnumber'},
459 riborsurname => $borrower->{'surname'},
460 ribortitle => $borrower->{'title'},
461 riborfirstname => $borrower->{'firstname'}
465 #set up so only the last 8 returned items display (make for faster loading pages)
466 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
469 foreach ( sort { $a <=> $b } keys %returneditems ) {
471 if ( $count++ < $returned_counter ) {
472 my $barcode = $returneditems{$_};
473 my $duedate = $riduedate{$_};
477 my @tempdate = split( /-/, $duedate );
478 $ri{year} = $tempdate[0];
479 $ri{month} = $tempdate[1];
480 $ri{day} = $tempdate[2];
481 $ri{duedate} = format_date($duedate);
482 my ($borrower) = GetMemberDetails( $riborrowernumber{$_}, 0 );
483 $ri{return_overdue} = 1 if ($duedate lt $today->output('iso'));
484 $ri{borrowernumber} = $borrower->{'borrowernumber'};
485 $ri{borcnum} = $borrower->{'cardnumber'};
486 $ri{borfirstname} = $borrower->{'firstname'};
487 $ri{borsurname} = $borrower->{'surname'};
488 $ri{bortitle} = $borrower->{'title'};
489 $ri{bornote} = $borrower->{'borrowernotes'};
490 $ri{borcategorycode}= $borrower->{'categorycode'};
493 $ri{borrowernumber} = $riborrowernumber{$_};
497 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
498 # fix up item type for display
499 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
500 $ri{itembiblionumber} = $biblio->{'biblionumber'};
501 $ri{itemtitle} = $biblio->{'title'};
502 $ri{itemauthor} = $biblio->{'author'};
503 $ri{itemtype} = $biblio->{'itemtype'};
504 $ri{itemnote} = $biblio->{'itemnotes'};
505 $ri{ccode} = $biblio->{'ccode'};
506 $ri{itemnumber} = $biblio->{'itemnumber'};
507 $ri{barcode} = $barcode;
512 push( @riloop, \%ri );
517 genbrname => $branches->{$userenv_branch}->{'branchname'},
518 genprname => $printers->{$printer}->{'printername'},
519 branchname => $branches->{$userenv_branch}->{'branchname'},
521 errmsgloop => \@errmsgloop,
522 exemptfine => $exemptfine,
523 dropboxmode => $dropboxmode,
524 dropboxdate => $dropboxdate->output(),
525 overduecharges => $overduecharges,
528 # actually print the page!
529 output_html_with_http_headers $query, $cookie, $template->output;