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; # 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');
129 my $iteminfo = GetBiblioFromItemNumber($item);
130 # fix up item type for display
131 $iteminfo->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $iteminfo->{'itype'} : $iteminfo->{'itemtype'};
134 # addin in ModReserveAffect the possibility to check if the document is expected in this library or not,
135 # if not we send a value in reserve waiting for not implementting waiting status
136 if ($userenv_branch ne $diffBranchReturned) {
137 $diffBranchSend = $diffBranchReturned;
140 $diffBranchSend = undef;
142 ModReserveAffect( $item, $borrowernumber,$diffBranchSend);
143 # check if we have other reservs for this document, if we have a return send the message of transfer
144 my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
146 my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
147 my $borcnum = $borr->{'cardnumber'};
148 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
150 my $slip = $query->param('resslip');
152 if ( $messages->{'transfert'} ) {
154 itemtitle => $iteminfo->{'title'},
155 itembiblionumber => $iteminfo->{'biblionumber'},
156 iteminfo => $iteminfo->{'author'},
157 tobranchname => GetBranchName($messages->{'transfert'}),
159 borrowernumber => $borrowernumber,
161 borfirstname => $borr->{'firstname'},
162 borsurname => $borr->{'surname'},
171 my $issueinformation;
172 my $barcode = $query->param('barcode');
173 my $exemptfine = $query->param('exemptfine');
174 my $dropboxmode = $query->param('dropboxmode');
175 my $dotransfer = $query->param('dotransfer');
176 my $calendar = C4::Calendar->new( branchcode => $userenv_branch );
177 #dropbox: get last open day (today - 1)
178 my $today = C4::Dates->new();
179 my $today_iso = $today->output('iso');
180 my $dropboxdate = $calendar->addDate($today, -1);
182 # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
183 my $transferitem = $query->param('transferitem');
184 my $tobranch = $query->param('tobranch');
185 ModItemTransfer($transferitem, $userenv_branch, $tobranch);
188 # actually return book and prepare item table.....
190 $barcode = barcodedecode($barcode) if(C4::Context->preference('itemBarcodeInputFilter'));
194 ( $returned, $messages, $issueinformation, $borrower ) =
195 AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode);
196 # get biblio description
197 my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
198 # fix up item type for display
199 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
202 title => $biblio->{'title'},
203 homebranch => $biblio->{'homebranch'},
204 author => $biblio->{'author'},
205 itembarcode => $biblio->{'barcode'},
206 itemtype => $biblio->{'itemtype'},
207 ccode => $biblio->{'ccode'},
208 itembiblionumber => $biblio->{'biblionumber'},
212 $returneditems{0} = $barcode;
213 $riborrowernumber{0} = $borrower->{'borrowernumber'};
214 $riduedate{0} = $issueinformation->{'date_due'};
218 $input{barcode} = $barcode;
219 $input{borrowernumber} = $borrower->{'borrowernumber'};
220 $input{duedate} = $issueinformation->{'date_due'};
221 $input{return_overdue} = 1 if ($issueinformation->{'date_due'} lt $today->output('iso'));
222 push( @inputloop, \%input );
224 # check if the branch is the same as homebranch
225 # if not, we want to put a message
226 if ( $biblio->{'homebranch'} ne $userenv_branch ) {
227 $template->param( homebranch => $biblio->{'homebranch'} );
230 elsif ( !$messages->{'BadBarcode'} ) {
234 $input{barcode} = $barcode;
237 $returneditems{0} = $barcode;
239 if ( $messages->{'wthdrawn'} ) {
240 $input{withdrawn} = 1;
241 $input{borrowernumber} = 'Item Cancelled';
242 $riborrowernumber{0} = 'Item Cancelled';
245 $input{borrowernumber} = ' ';
246 $riborrowernumber{0} = ' ';
248 push( @inputloop, \%input );
251 $template->param( inputloop => \@inputloop );
257 # new op dev : we check if the document must be returned to his homebranch directly,
258 # if the document is transfered, we have warning message .
260 if ( $messages->{'WasTransfered'} ) {
267 if ( $messages->{'NeedsTransfer'} ){
271 itemnumber => $issueinformation->{'itemnumber'}
275 if ( $messages->{'Wrongbranch'} ){
281 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
283 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
286 TransferWaitingAt => $messages->{'WrongTransfer'},
287 WrongTransferItem => $messages->{'WrongTransferItem'},
290 my $reserve = $messages->{'ResFound'};
291 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
292 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
293 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
296 wborfirstname => $borr->{'firstname'},
297 wborsurname => $borr->{'surname'},
298 wbortitle => $borr->{'title'},
299 wborphone => $borr->{'phone'},
300 wboremail => $borr->{'email'},
301 wboraddress => $borr->{'address'},
302 wboraddress2 => $borr->{'address2'},
303 wborcity => $borr->{'city'},
304 wborzip => $borr->{'zipcode'},
305 wborrowernumber => $reserve->{'borrowernumber'},
306 wborcnum => $borr->{'cardnumber'},
307 wtransfertFrom => $userenv_branch,
313 # reserve found and item arrived at the expected branch
315 if ( $messages->{'ResFound'}) {
316 my $reserve = $messages->{'ResFound'};
317 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
318 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
320 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
321 if ( $reserve->{'ResFound'} eq "Waiting" ) {
323 waiting => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
325 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
327 intransit => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
328 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
329 resbarcode => $barcode,
334 # same params for Waiting or Reserved
337 currentbranch => $branches->{$userenv_branch}->{'branchname'},
338 destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
339 name => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
340 borfirstname => $borr->{'firstname'},
341 borsurname => $borr->{'surname'},
342 bortitle => $borr->{'title'},
343 borphone => $borr->{'phone'},
344 boremail => $borr->{'email'},
345 boraddress => $borr->{'address'},
346 boraddress2 => $borr->{'address2'},
347 borcity => $borr->{'city'},
348 borzip => $borr->{'zipcode'},
349 borcnum => $borr->{'cardnumber'},
350 debarred => $borr->{'debarred'},
351 gonenoaddress => $borr->{'gonenoaddress'},
353 destbranch => $reserve->{'branchcode'},
354 borrowernumber => $reserve->{'borrowernumber'},
355 itemnumber => $reserve->{'itemnumber'},
357 } # else { ; } # error?
362 foreach my $code ( keys %$messages ) {
366 my $exit_required_p = 0;
367 if ( $code eq 'BadBarcode' ) {
368 $err{badbarcode} = 1;
369 $err{msg} = $messages->{'BadBarcode'};
371 elsif ( $code eq 'NotIssued' ) {
373 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
375 elsif ( $code eq 'WasLost' ) {
378 elsif ( $code eq 'ResFound' ) {
379 ; # FIXME... anything to do here?
381 elsif ( $code eq 'WasReturned' ) {
382 ; # FIXME... anything to do here?
384 elsif ( $code eq 'WasTransfered' ) {
385 ; # FIXME... anything to do here?
387 elsif ( $code eq 'wthdrawn' ) {
389 $exit_required_p = 1;
391 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
392 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
393 $err{ispermanent} = 1;
395 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
398 elsif ( $code eq 'WrongTransfer' ) {
399 ; # FIXME... anything to do here?
401 elsif ( $code eq 'WrongTransferItem' ) {
402 ; # FIXME... anything to do here?
404 elsif ( $code eq 'NeedsTransfer' ) {
406 elsif ( $code eq 'Wrongbranch' ) {
410 die "Unknown error code $code"; # XXX
413 push( @errmsgloop, \%err );
415 last if $exit_required_p;
417 $template->param( errmsgloop => \@errmsgloop );
421 my $flags = $borrower->{'flags'};
424 foreach my $flag ( sort keys %$flags ) {
426 unless ($flagset) { $flagset = 1; }
427 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
428 $flaginfo{flag} = $flag;
429 if ( $flag eq 'CHARGES' ) {
430 $flaginfo{msg} = $flag;
431 $flaginfo{charges} = 1;
432 $flaginfo{chargeamount} = $flags->{$flag}->{amount};
433 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
435 elsif ( $flag eq 'WAITING' ) {
436 $flaginfo{msg} = $flag;
437 $flaginfo{waiting} = 1;
439 my $items = $flags->{$flag}->{'itemlist'};
440 foreach my $item (@$items) {
441 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
443 $waitingitem{biblionum} = $biblio->{'biblionumber'};
444 $waitingitem{barcode} = $biblio->{'barcode'};
445 $waitingitem{title} = $biblio->{'title'};
446 $waitingitem{brname} = $branches->{ $biblio->{'holdingbranch'} }->{'branchname'};
447 push( @waitingitemloop, \%waitingitem );
449 $flaginfo{itemloop} = \@waitingitemloop;
451 elsif ( $flag eq 'ODUES' ) {
452 my $items = $flags->{$flag}->{'itemlist'};
454 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
457 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
459 $overdueitem{duedate} = format_date( $item->{'date_due'} );
460 $overdueitem{biblionum} = $biblio->{'biblionumber'};
461 $overdueitem{barcode} = $biblio->{'barcode'};
462 $overdueitem{title} = $biblio->{'title'};
463 $overdueitem{brname} = $branches->{ $biblio->{'holdingbranch'}} ->{'branchname'};
464 push( @itemloop, \%overdueitem );
466 $flaginfo{itemloop} = \@itemloop;
467 $flaginfo{overdue} = 1;
470 $flaginfo{other} = 1;
471 $flaginfo{msg} = $flags->{$flag}->{'message'};
473 push( @flagloop, \%flaginfo );
477 flagloop => \@flagloop,
478 riborrowernumber => $borrower->{'borrowernumber'},
479 riborcnum => $borrower->{'cardnumber'},
480 riborsurname => $borrower->{'surname'},
481 ribortitle => $borrower->{'title'},
482 riborfirstname => $borrower->{'firstname'}
486 #set up so only the last 8 returned items display (make for faster loading pages)
487 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
490 foreach ( sort { $a <=> $b } keys %returneditems ) {
492 if ( $count++ < $returned_counter ) {
493 my $barcode = $returneditems{$_};
494 my $duedate = $riduedate{$_};
498 my @tempdate = split( /-/, $duedate );
499 $ri{year} = $tempdate[0];
500 $ri{month} = $tempdate[1];
501 $ri{day} = $tempdate[2];
502 $ri{duedate} = format_date($duedate);
503 my ($borrower) = GetMemberDetails( $riborrowernumber{$_}, 0 );
504 $ri{return_overdue} = 1 if ($duedate lt $today->output('iso'));
505 $ri{borrowernumber} = $borrower->{'borrowernumber'};
506 $ri{borcnum} = $borrower->{'cardnumber'};
507 $ri{borfirstname} = $borrower->{'firstname'};
508 $ri{borsurname} = $borrower->{'surname'};
509 $ri{bortitle} = $borrower->{'title'};
510 $ri{bornote} = $borrower->{'borrowernotes'};
511 $ri{borcategorycode}= $borrower->{'categorycode'};
514 $ri{borrowernumber} = $riborrowernumber{$_};
518 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
519 # fix up item type for display
520 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
521 $ri{itembiblionumber} = $biblio->{'biblionumber'};
522 $ri{itemtitle} = $biblio->{'title'};
523 $ri{itemauthor} = $biblio->{'author'};
524 $ri{itemtype} = $biblio->{'itemtype'};
525 $ri{itemnote} = $biblio->{'itemnotes'};
526 $ri{ccode} = $biblio->{'ccode'};
527 $ri{itemnumber} = $biblio->{'itemnumber'};
528 $ri{barcode} = $barcode;
533 push( @riloop, \%ri );
535 $template->param( riloop => \@riloop );
538 genbrname => $branches->{$userenv_branch}->{'branchname'},
539 genprname => $printers->{$printer}->{'printername'},
540 branchname => $branches->{$userenv_branch}->{'branchname'},
542 errmsgloop => \@errmsgloop,
543 exemptfine => $exemptfine,
544 dropboxmode => $dropboxmode,
545 dropboxdate => $dropboxdate->output(),
546 overduecharges => $overduecharges,
549 # actually print the page!
550 output_html_with_http_headers $query, $cookie, $template->output;