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");
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;
160 my $barcode = $query->param('barcode');
161 my $exemptfine = $query->param('exemptfine');
162 my $dropboxmode = $query->param('dropboxmode');
163 my $dotransfer = $query->param('dotransfer');
164 my $calendar = C4::Calendar->new( branchcode => $userenv_branch );
165 #dropbox: get last open day (today - 1)
166 my $today = C4::Dates->new();
167 my $today_iso = $today->output('iso');
168 my $dropboxdate = $calendar->addDate($today, -1);
170 # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
171 my $transferitem = $query->param('transferitem');
172 my $tobranch = $query->param('tobranch');
173 ModItemTransfer($transferitem, $userenv_branch, $tobranch);
176 # actually return book and prepare item table.....
178 $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
182 ( $returned, $messages, $issueinformation, $borrower ) =
183 AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode);
184 # get biblio description
185 my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
186 # fix up item type for display
187 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
190 title => $biblio->{'title'},
191 homebranch => $biblio->{'homebranch'},
192 author => $biblio->{'author'},
193 itembarcode => $biblio->{'barcode'},
194 itemtype => $biblio->{'itemtype'},
195 ccode => $biblio->{'ccode'},
196 itembiblionumber => $biblio->{'biblionumber'},
206 $returneditems{0} = $barcode;
207 $riborrowernumber{0} = $borrower->{'borrowernumber'};
208 $riduedate{0} = $issueinformation->{'date_due'};
209 $input{borrowernumber} = $borrower->{'borrowernumber'};
210 $input{duedate} = $issueinformation->{'date_due'};
211 $input{return_overdue} = 1 if ($issueinformation->{'date_due'} lt $today->output('iso'));
212 push( @inputloop, \%input );
214 # check if the branch is the same as homebranch
215 # if not, we want to put a message
216 if ( $biblio->{'homebranch'} ne $userenv_branch ) {
217 $template->param( homebranch => $biblio->{'homebranch'} );
220 elsif ( !$messages->{'BadBarcode'} ) {
222 $returneditems{0} = $barcode;
224 if ( $messages->{'wthdrawn'} ) {
225 $input{withdrawn} = 1;
226 $input{borrowernumber} = 'Item Cancelled'; # FIXME: should be in display layer ?
227 $riborrowernumber{0} = 'Item Cancelled';
230 $input{borrowernumber} = ' ';
231 $riborrowernumber{0} = ' ';
233 push( @inputloop, \%input );
236 $template->param( inputloop => \@inputloop );
242 # new op dev : we check if the document must be returned to his homebranch directly,
243 # if the document is transfered, we have warning message .
245 if ( $messages->{'WasTransfered'} ) {
252 if ( $messages->{'NeedsTransfer'} ){
256 itemnumber => $issueinformation->{'itemnumber'}
260 if ( $messages->{'Wrongbranch'} ){
266 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
268 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
271 TransferWaitingAt => $messages->{'WrongTransfer'},
272 WrongTransferItem => $messages->{'WrongTransferItem'},
275 my $reserve = $messages->{'ResFound'};
276 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
277 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
278 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
281 wborfirstname => $borr->{'firstname'},
282 wborsurname => $borr->{'surname'},
283 wbortitle => $borr->{'title'},
284 wborphone => $borr->{'phone'},
285 wboremail => $borr->{'email'},
286 wboraddress => $borr->{'address'},
287 wboraddress2 => $borr->{'address2'},
288 wborcity => $borr->{'city'},
289 wborzip => $borr->{'zipcode'},
290 wborrowernumber => $reserve->{'borrowernumber'},
291 wborcnum => $borr->{'cardnumber'},
292 wtransfertFrom => $userenv_branch,
298 # reserve found and item arrived at the expected branch
300 if ( $messages->{'ResFound'}) {
301 my $reserve = $messages->{'ResFound'};
302 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
303 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
305 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
306 if ( $reserve->{'ResFound'} eq "Waiting" ) {
308 waiting => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
310 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
312 intransit => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
313 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
314 resbarcode => $barcode,
319 # same params for Waiting or Reserved
322 currentbranch => $branches->{$userenv_branch}->{'branchname'},
323 destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
324 name => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
325 borfirstname => $borr->{'firstname'},
326 borsurname => $borr->{'surname'},
327 bortitle => $borr->{'title'},
328 borphone => $borr->{'phone'},
329 boremail => $borr->{'email'},
330 boraddress => $borr->{'address'},
331 boraddress2 => $borr->{'address2'},
332 borcity => $borr->{'city'},
333 borzip => $borr->{'zipcode'},
334 borcnum => $borr->{'cardnumber'},
335 debarred => $borr->{'debarred'},
336 gonenoaddress => $borr->{'gonenoaddress'},
338 destbranch => $reserve->{'branchcode'},
339 borrowernumber => $reserve->{'borrowernumber'},
340 itemnumber => $reserve->{'itemnumber'},
342 } # else { ; } # error?
347 foreach my $code ( keys %$messages ) {
351 my $exit_required_p = 0;
352 if ( $code eq 'BadBarcode' ) {
353 $err{badbarcode} = 1;
354 $err{msg} = $messages->{'BadBarcode'};
356 elsif ( $code eq 'NotIssued' ) {
358 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
360 elsif ( $code eq 'WasLost' ) {
363 elsif ( $code eq 'ResFound' ) {
364 ; # FIXME... anything to do here?
366 elsif ( $code eq 'WasReturned' ) {
367 ; # FIXME... anything to do here?
369 elsif ( $code eq 'WasTransfered' ) {
370 ; # FIXME... anything to do here?
372 elsif ( $code eq 'wthdrawn' ) {
374 $exit_required_p = 1;
376 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
377 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
378 $err{ispermanent} = 1;
380 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
383 elsif ( $code eq 'WrongTransfer' ) {
384 ; # FIXME... anything to do here?
386 elsif ( $code eq 'WrongTransferItem' ) {
387 ; # FIXME... anything to do here?
389 elsif ( $code eq 'NeedsTransfer' ) {
391 elsif ( $code eq 'Wrongbranch' ) {
395 die "Unknown error code $code"; # XXX
398 push( @errmsgloop, \%err );
400 last if $exit_required_p;
402 $template->param( errmsgloop => \@errmsgloop );
406 my $flags = $borrower->{'flags'};
409 foreach my $flag ( sort keys %$flags ) {
411 unless ($flagset) { $flagset = 1; }
412 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
413 $flaginfo{flag} = $flag;
414 if ( $flag eq 'CHARGES' ) {
415 $flaginfo{msg} = $flag;
416 $flaginfo{charges} = 1;
417 $flaginfo{chargeamount} = $flags->{$flag}->{amount};
418 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
420 elsif ( $flag eq 'WAITING' ) {
421 $flaginfo{msg} = $flag;
422 $flaginfo{waiting} = 1;
424 my $items = $flags->{$flag}->{'itemlist'};
425 foreach my $item (@$items) {
426 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
427 push @waitingitemloop, {
428 biblionum => $biblio->{'biblionumber'},
429 barcode => $biblio->{'barcode'},
430 title => $biblio->{'title'},
431 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
434 $flaginfo{itemloop} = \@waitingitemloop;
436 elsif ( $flag eq 'ODUES' ) {
437 my $items = $flags->{$flag}->{'itemlist'};
439 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
442 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
444 duedate => format_date($item->{'date_due'}),
445 biblionum => $biblio->{'biblionumber'},
446 barcode => $biblio->{'barcode'},
447 title => $biblio->{'title'},
448 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
451 $flaginfo{itemloop} = \@itemloop;
452 $flaginfo{overdue} = 1;
455 $flaginfo{other} = 1;
456 $flaginfo{msg} = $flags->{$flag}->{'message'};
458 push( @flagloop, \%flaginfo );
462 flagloop => \@flagloop,
463 riborrowernumber => $borrower->{'borrowernumber'},
464 riborcnum => $borrower->{'cardnumber'},
465 riborsurname => $borrower->{'surname'},
466 ribortitle => $borrower->{'title'},
467 riborfirstname => $borrower->{'firstname'}
471 #set up so only the last 8 returned items display (make for faster loading pages)
472 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
475 foreach ( sort { $a <=> $b } keys %returneditems ) {
477 if ( $count++ < $returned_counter ) {
478 my $barcode = $returneditems{$_};
479 my $duedate = $riduedate{$_};
483 my @tempdate = split( /-/, $duedate );
484 $ri{year} = $tempdate[0];
485 $ri{month} = $tempdate[1];
486 $ri{day} = $tempdate[2];
487 $ri{duedate} = format_date($duedate);
488 my ($borrower) = GetMemberDetails( $riborrowernumber{$_}, 0 );
489 $ri{return_overdue} = 1 if ($duedate lt $today->output('iso'));
490 $ri{borrowernumber} = $borrower->{'borrowernumber'};
491 $ri{borcnum} = $borrower->{'cardnumber'};
492 $ri{borfirstname} = $borrower->{'firstname'};
493 $ri{borsurname} = $borrower->{'surname'};
494 $ri{bortitle} = $borrower->{'title'};
495 $ri{bornote} = $borrower->{'borrowernotes'};
496 $ri{borcategorycode}= $borrower->{'categorycode'};
499 $ri{borrowernumber} = $riborrowernumber{$_};
503 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
504 # fix up item type for display
505 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
506 $ri{itembiblionumber} = $biblio->{'biblionumber'};
507 $ri{itemtitle} = $biblio->{'title'};
508 $ri{itemauthor} = $biblio->{'author'};
509 $ri{itemtype} = $biblio->{'itemtype'};
510 $ri{itemnote} = $biblio->{'itemnotes'};
511 $ri{ccode} = $biblio->{'ccode'};
512 $ri{itemnumber} = $biblio->{'itemnumber'};
513 $ri{barcode} = $barcode;
518 push( @riloop, \%ri );
523 genbrname => $branches->{$userenv_branch}->{'branchname'},
524 genprname => $printers->{$printer}->{'printername'},
525 branchname => $branches->{$userenv_branch}->{'branchname'},
527 errmsgloop => \@errmsgloop,
528 exemptfine => $exemptfine,
529 dropboxmode => $dropboxmode,
530 dropboxdate => $dropboxdate->output(),
531 overduecharges => $overduecharges,
534 # actually print the page!
535 output_html_with_http_headers $query, $cookie, $template->output;