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 if ( C4
::Context
->preference("InProcessingToShelvingCart") ) {
183 my $item = GetItem
( $itemnumber );
184 if ( $item->{'location'} eq 'PROC' ) {
185 $item->{'location'} = 'CART';
186 ModItem
( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
190 if ( C4
::Context
->preference("ReturnToShelvingCart") ) {
191 my $item = GetItem
( $itemnumber );
192 $item->{'location'} = 'CART';
193 ModItem
( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
199 ( $returned, $messages, $issueinformation, $borrower ) =
200 AddReturn
( $barcode, $userenv_branch, $exemptfine, $dropboxmode); # do the return
202 # get biblio description
203 my $biblio = GetBiblioFromItemNumber
($itemnumber);
204 # fix up item type for display
205 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
208 title
=> $biblio->{'title'},
209 homebranch
=> $biblio->{'homebranch'},
210 author
=> $biblio->{'author'},
211 itembarcode
=> $biblio->{'barcode'},
212 itemtype
=> $biblio->{'itemtype'},
213 ccode
=> $biblio->{'ccode'},
214 itembiblionumber
=> $biblio->{'biblionumber'},
224 my $duedate = $issueinformation->{'date_due'};
225 $returneditems{0} = $barcode;
226 $riborrowernumber{0} = $borrower->{'borrowernumber'};
227 $riduedate{0} = $duedate;
228 $input{borrowernumber
} = $borrower->{'borrowernumber'};
229 $input{duedate
} = $duedate;
230 $input{return_overdue
} = 1 if ($duedate and $duedate lt $today->output('iso'));
231 push( @inputloop, \
%input );
233 elsif ( !$messages->{'BadBarcode'} ) {
235 $returneditems{0} = $barcode;
237 if ( $messages->{'wthdrawn'} ) {
238 $input{withdrawn
} = 1;
239 $input{borrowernumber
} = 'Item Cancelled'; # FIXME: should be in display layer ?
240 $riborrowernumber{0} = 'Item Cancelled';
243 $input{borrowernumber
} = ' '; # This seems clearly bogus.
244 $riborrowernumber{0} = ' ';
246 push( @inputloop, \
%input );
249 $template->param( inputloop
=> \
@inputloop );
255 # new op dev : we check if the document must be returned to his homebranch directly,
256 # if the document is transfered, we have warning message .
258 if ( $messages->{'WasTransfered'} ) {
265 if ( $messages->{'NeedsTransfer'} ){
269 itemnumber
=> $itemnumber,
273 if ( $messages->{'Wrongbranch'} ){
279 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
281 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
284 TransferWaitingAt
=> $messages->{'WrongTransfer'},
285 WrongTransferItem
=> $messages->{'WrongTransferItem'},
288 my $reserve = $messages->{'ResFound'};
289 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
290 my ($borr) = GetMemberDetails
( $reserve->{'borrowernumber'}, 0 );
291 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
294 wborfirstname
=> $borr->{'firstname'},
295 wborsurname
=> $borr->{'surname'},
296 wbortitle
=> $borr->{'title'},
297 wborphone
=> $borr->{'phone'},
298 wboremail
=> $borr->{'email'},
299 wboraddress
=> $borr->{'address'},
300 wboraddress2
=> $borr->{'address2'},
301 wborcity
=> $borr->{'city'},
302 wborzip
=> $borr->{'zipcode'},
303 wborrowernumber
=> $reserve->{'borrowernumber'},
304 wborcnum
=> $borr->{'cardnumber'},
305 wtransfertFrom
=> $userenv_branch,
310 # reserve found and item arrived at the expected branch
312 if ( $messages->{'ResFound'}) {
313 my $reserve = $messages->{'ResFound'};
314 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
315 my ($borr) = GetMemberDetails
( $reserve->{'borrowernumber'}, 0 );
317 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
318 if ( $reserve->{'ResFound'} eq "Waiting" ) {
320 waiting
=> ($userenv_branch eq $reserve->{'branchcode'} ?
1 : 0 ),
322 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
324 intransit
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
325 transfertodo
=> ($userenv_branch eq $reserve->{'branchcode'} ?
0 : 1 ),
326 resbarcode
=> $barcode,
331 # same params for Waiting or Reserved
334 currentbranch
=> $branches->{$userenv_branch}->{'branchname'},
335 destbranchname
=> $branches->{ $reserve->{'branchcode'} }->{'branchname'},
336 name
=> $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
337 borfirstname
=> $borr->{'firstname'},
338 borsurname
=> $borr->{'surname'},
339 bortitle
=> $borr->{'title'},
340 borphone
=> $borr->{'phone'},
341 boremail
=> $borr->{'email'},
342 boraddress
=> $borr->{'address'},
343 boraddress2
=> $borr->{'address2'},
344 borcity
=> $borr->{'city'},
345 borzip
=> $borr->{'zipcode'},
346 borcnum
=> $borr->{'cardnumber'},
347 debarred
=> $borr->{'debarred'},
348 gonenoaddress
=> $borr->{'gonenoaddress'},
350 destbranch
=> $reserve->{'branchcode'},
351 borrowernumber
=> $reserve->{'borrowernumber'},
352 itemnumber
=> $reserve->{'itemnumber'},
353 reservenotes
=> $reserve->{'reservenotes'},
355 } # else { ; } # error?
360 foreach my $code ( keys %$messages ) {
362 my $exit_required_p = 0;
363 if ( $code eq 'BadBarcode' ) {
364 $err{badbarcode
} = 1;
365 $err{msg
} = $messages->{'BadBarcode'};
367 elsif ( $code eq 'NotIssued' ) {
369 $err{msg
} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
371 elsif ( $code eq 'WasLost' ) {
374 elsif ( $code eq 'ResFound' ) {
375 ; # FIXME... anything to do here?
377 elsif ( $code eq 'WasReturned' ) {
378 ; # FIXME... anything to do here?
380 elsif ( $code eq 'WasTransfered' ) {
381 ; # FIXME... anything to do here?
383 elsif ( $code eq 'wthdrawn' ) {
385 $exit_required_p = 1;
387 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
388 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
389 $err{ispermanent
} = 1;
391 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
394 elsif ( $code eq 'WrongTransfer' ) {
395 ; # FIXME... anything to do here?
397 elsif ( $code eq 'WrongTransferItem' ) {
398 ; # FIXME... anything to do here?
400 elsif ( $code eq 'NeedsTransfer' ) {
402 elsif ( $code eq 'Wrongbranch' ) {
406 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
407 # This forces the issue of staying in sync w/ Circulation.pm
410 push( @errmsgloop, \
%err );
412 last if $exit_required_p;
414 $template->param( errmsgloop
=> \
@errmsgloop );
418 my $flags = $borrower->{'flags'};
421 foreach my $flag ( sort keys %$flags ) {
423 unless ($flagset) { $flagset = 1; }
424 $flaginfo{redfont
} = ( $flags->{$flag}->{'noissues'} );
425 $flaginfo{flag
} = $flag;
426 if ( $flag eq 'CHARGES' ) {
427 $flaginfo{msg
} = $flag;
428 $flaginfo{charges
} = 1;
429 $flaginfo{chargeamount
} = $flags->{$flag}->{amount
};
430 $flaginfo{borrowernumber
} = $borrower->{borrowernumber
};
432 elsif ( $flag eq 'WAITING' ) {
433 $flaginfo{msg
} = $flag;
434 $flaginfo{waiting
} = 1;
436 my $items = $flags->{$flag}->{'itemlist'};
437 foreach my $item (@
$items) {
438 my $biblio = GetBiblioFromItemNumber
( $item->{'itemnumber'});
439 push @waitingitemloop, {
440 biblionum
=> $biblio->{'biblionumber'},
441 barcode
=> $biblio->{'barcode'},
442 title
=> $biblio->{'title'},
443 brname
=> $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
446 $flaginfo{itemloop
} = \
@waitingitemloop;
448 elsif ( $flag eq 'ODUES' ) {
449 my $items = $flags->{$flag}->{'itemlist'};
451 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
454 my $biblio = GetBiblioFromItemNumber
( $item->{'itemnumber'});
456 duedate
=> format_date
($item->{'date_due'}),
457 biblionum
=> $biblio->{'biblionumber'},
458 barcode
=> $biblio->{'barcode'},
459 title
=> $biblio->{'title'},
460 brname
=> $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
463 $flaginfo{itemloop
} = \
@itemloop;
464 $flaginfo{overdue
} = 1;
467 $flaginfo{other
} = 1;
468 $flaginfo{msg
} = $flags->{$flag}->{'message'};
470 push( @flagloop, \
%flaginfo );
474 flagloop
=> \
@flagloop,
475 riborrowernumber
=> $borrower->{'borrowernumber'},
476 riborcnum
=> $borrower->{'cardnumber'},
477 riborsurname
=> $borrower->{'surname'},
478 ribortitle
=> $borrower->{'title'},
479 riborfirstname
=> $borrower->{'firstname'}
483 #set up so only the last 8 returned items display (make for faster loading pages)
484 my $returned_counter = ( C4
::Context
->preference('numReturnedItemsToShow') ) ? C4
::Context
->preference('numReturnedItemsToShow') : 8;
487 foreach ( sort { $a <=> $b } keys %returneditems ) {
489 if ( $count++ < $returned_counter ) {
490 my $barcode = $returneditems{$_};
491 my $duedate = $riduedate{$_};
495 my @tempdate = split( /-/, $duedate );
496 $ri{year
} = $tempdate[0];
497 $ri{month
} = $tempdate[1];
498 $ri{day
} = $tempdate[2];
499 $ri{duedate
} = format_date
($duedate);
500 my ($borrower) = GetMemberDetails
( $riborrowernumber{$_}, 0 );
501 $ri{return_overdue
} = 1 if ($duedate lt $today->output('iso'));
502 $ri{borrowernumber
} = $borrower->{'borrowernumber'};
503 $ri{borcnum
} = $borrower->{'cardnumber'};
504 $ri{borfirstname
} = $borrower->{'firstname'};
505 $ri{borsurname
} = $borrower->{'surname'};
506 $ri{bortitle
} = $borrower->{'title'};
507 $ri{bornote
} = $borrower->{'borrowernotes'};
508 $ri{borcategorycode
}= $borrower->{'categorycode'};
511 $ri{borrowernumber
} = $riborrowernumber{$_};
515 my $biblio = GetBiblioFromItemNumber
(GetItemnumberFromBarcode
($barcode));
516 # fix up item type for display
517 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
518 $ri{itembiblionumber
} = $biblio->{'biblionumber'};
519 $ri{itemtitle
} = $biblio->{'title'};
520 $ri{itemauthor
} = $biblio->{'author'};
521 $ri{itemtype
} = $biblio->{'itemtype'};
522 $ri{itemnote
} = $biblio->{'itemnotes'};
523 $ri{ccode
} = $biblio->{'ccode'};
524 $ri{itemnumber
} = $biblio->{'itemnumber'};
525 $ri{barcode
} = $barcode;
530 push( @riloop, \
%ri );
535 genbrname
=> $branches->{$userenv_branch}->{'branchname'},
536 genprname
=> $printers->{$printer}->{'printername'},
537 branchname
=> $branches->{$userenv_branch}->{'branchname'},
539 errmsgloop
=> \
@errmsgloop,
540 exemptfine
=> $exemptfine,
541 dropboxmode
=> $dropboxmode,
542 dropboxdate
=> $dropboxdate->output(),
543 overduecharges
=> $overduecharges,
546 # actually print the page!
547 output_html_with_http_headers
$query, $cookie, $template->output;