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
31 use C4::Auth qw/:DEFAULT get_session/;
34 use C4::Dates qw/format_date/;
35 use Date::Calc qw/Add_Delta_Days/;
41 use C4::Branch; # GetBranchName
42 use C4::Koha; # FIXME : is it still useful ?
46 if (!C4::Context->userenv){
47 my $sessionID = $query->cookie("CGISESSID");
48 my $session = get_session($sessionID);
49 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
50 # no branch set we can't return
51 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
57 my ( $template, $librarian, $cookie ) = get_template_and_user(
59 template_name => "circ/returns.tmpl",
63 flagsrequired => { circulate => 1 },
69 my $branches = GetBranches();
70 my $printers = GetPrinters();
72 #my $branch = C4::Context->userenv?C4::Context->userenv->{'branch'}:"";
73 my $printer = C4::Context->userenv?C4::Context->userenv->{'branchprinter'}:"";
74 my $overduecharges = (C4::Context->preference('finesMode') && C4::Context->preference('finesMode') ne 'off');
76 # Some code to handle the error if there is no branch or printer setting.....
79 # Set up the item stack ....
84 foreach ( $query->param ) {
85 (next) unless (/ri-(\d*)/);
88 (next) if ( $counter > 20 );
89 my $barcode = $query->param("ri-$counter");
90 my $duedate = $query->param("dd-$counter");
91 my $borrowernumber = $query->param("bn-$counter");
95 $barcode = barcodedecode($barcode) if(C4::Context->preference('itemBarcodeInputFilter'));
97 ######################
98 #Are these lines still useful ?
99 $returneditems{$counter} = $barcode;
100 $riduedate{$counter} = $duedate;
101 $riborrowernumber{$counter} = $borrowernumber;
103 #######################
104 $input{counter} = $counter;
105 $input{barcode} = $barcode;
106 $input{duedate} = $duedate;
107 $input{borrowernumber} = $borrowernumber;
108 push( @inputloop, \%input );
112 # Deal with the requests....
114 if ($query->param('WT-itemNumber')){
115 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
118 if ( $query->param('resbarcode') ) {
119 my $item = $query->param('itemnumber');
120 my $borrowernumber = $query->param('borrowernumber');
121 my $resbarcode = $query->param('resbarcode');
122 my $diffBranchReturned = $query->param('diffBranch');
124 my $iteminfo = GetBiblioFromItemNumber($item);
125 # fix up item type for display
126 $iteminfo->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $iteminfo->{'itype'} : $iteminfo->{'itemtype'};
129 # addin in ModReserveAffect the possibility to check if the document is expected in this library or not,
130 # if not we send a value in reserve waiting for not implementting waiting status
131 if ($diffBranchReturned) {
132 $diffBranchSend = $diffBranchReturned;
135 $diffBranchSend = undef;
137 ModReserveAffect( $item, $borrowernumber,$diffBranchSend);
138 # check if we have other reservs for this document, if we have a return send the message of transfer
139 my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
141 my $branchname = GetBranchName( $messages->{'transfert'} );
142 my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
143 my $borcnum = $borr->{'cardnumber'};
145 $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
146 my $slip = $query->param('resslip');
149 if ( $messages->{'transfert'} ) {
151 itemtitle => $iteminfo->{'title'},
152 itembiblionumber => $iteminfo->{'biblionumber'},
153 iteminfo => $iteminfo->{'author'},
154 tobranchname => $branchname,
156 borrowernumber => $borrowernumber,
158 borfirstname => $borr->{'firstname'},
159 borsurname => $borr->{'surname'},
168 my $issueinformation;
169 my $barcode = $query->param('barcode');
171 # $barcode =~ s/\s*//g; - use barcodedecode for this; whitespace is not invalid.
172 my $exemptfine = $query->param('exemptfine');
173 my $dropboxmode= $query->param('dropboxmode');
174 my @datearr = localtime( time() );
175 my @yesterdayarr = Add_Delta_Days( $datearr[5] + 1900 , $datearr[4] + 1, $datearr[3] , -1 );
176 my $yesterday= C4::Dates->new( sprintf("%0.4d-%0.2d-%0.2d",@yesterdayarr),'iso');
177 my $dotransfer = $query->param('dotransfer');
179 # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
180 my $transferitem=$query->param('transferitem');
181 my $tobranch=$query->param('tobranch');
182 ModItemTransfer($transferitem, C4::Context->userenv->{'branch'}, $tobranch);
185 # actually return book and prepare item table.....
187 $barcode = barcodedecode($barcode) if(C4::Context->preference('itemBarcodeInputFilter'));
191 ( $returned, $messages, $issueinformation, $borrower ) =
192 AddReturn( $barcode, C4::Context->userenv->{'branch'}, $exemptfine, $dropboxmode);
193 # get biblio description
194 my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
195 # fix up item type for display
196 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
199 title => $biblio->{'title'},
200 homebranch => $biblio->{'homebranch'},
201 author => $biblio->{'author'},
202 itembarcode => $biblio->{'barcode'},
203 itemtype => $biblio->{'itemtype'},
204 ccode => $biblio->{'ccode'},
205 itembiblionumber => $biblio->{'biblionumber'},
208 $returneditems{0} = $barcode;
209 $riborrowernumber{0} = $borrower->{'borrowernumber'};
210 $riduedate{0} = $issueinformation->{'date_due'};
214 $input{barcode} = $barcode;
215 $input{duedate} = $riduedate{0};
216 $input{borrowernumber} = $riborrowernumber{0};
217 push( @inputloop, \%input );
219 # check if the branch is the same as homebranch
220 # if not, we want to put a message
221 if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
222 $template->param( homebranch => $biblio->{'homebranch'} );
225 elsif ( !$messages->{'BadBarcode'} ) {
229 $input{barcode} = $barcode;
232 $returneditems{0} = $barcode;
234 if ( $messages->{'wthdrawn'} ) {
235 $input{withdrawn} = 1;
236 $input{borrowernumber} = "Item Cancelled";
237 $riborrowernumber{0} = 'Item Cancelled';
240 $input{borrowernumber} = " ";
241 $riborrowernumber{0} = ' ';
243 push( @inputloop, \%input );
246 $template->param( inputloop => \@inputloop );
252 # new op dev : we check if the document must be returned to his homebranch directly,
253 # if the document is transfered, we have warning message .
255 if ( $messages->{'WasTransfered'} ) {
262 if ( $messages->{'NeedsTransfer'} ){
266 itemnumber => $issueinformation->{'itemnumber'}
270 if ( $messages->{'Wrongbranch'} ){
276 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
278 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
281 TransferWaitingAt => $messages->{'WrongTransfer'},
282 WrongTransferItem => $messages->{'WrongTransferItem'},
285 my $reserve = $messages->{'ResFound'};
286 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
287 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
289 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
292 wborfirstname => $borr->{'firstname'},
293 wborsurname => $borr->{'surname'},
294 wbortitle => $borr->{'title'},
295 wborphone => $borr->{'phone'},
296 wboremail => $borr->{'email'},
297 wboraddress => $borr->{'address'},
298 wboraddress2 => $borr->{'address2'},
299 wborcity => $borr->{'city'},
300 wborzip => $borr->{'zipcode'},
301 wborrowernumber => $reserve->{'borrowernumber'},
302 wborcnum => $borr->{'cardnumber'},
303 wtransfertFrom => C4::Context->userenv->{'branch'},
309 # reserve found and item arrived at the expected branch
311 if ( $messages->{'ResFound'}) {
312 my $reserve = $messages->{'ResFound'};
313 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
314 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
315 if ( $reserve->{'ResFound'} eq "Waiting" ) {
316 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
317 $template->param( waiting => 1 );
320 $template->param( waiting => 0 );
325 name => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
326 borfirstname => $borr->{'firstname'},
327 borsurname => $borr->{'surname'},
328 bortitle => $borr->{'title'},
329 borphone => $borr->{'phone'},
330 boremail => $borr->{'email'},
331 boraddress => $borr->{'address'},
332 boraddress2 => $borr->{'address2'},
333 borcity => $borr->{'city'},
334 borzip => $borr->{'zipcode'},
335 borrowernumber => $reserve->{'borrowernumber'},
336 borcnum => $borr->{'cardnumber'},
337 debarred => $borr->{'debarred'},
338 gonenoaddress => $borr->{'gonenoaddress'},
339 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
340 itemnumber => $reserve->{'itemnumber'},
345 if ( $reserve->{'ResFound'} eq "Reserved" ) {
346 # my @da = localtime( time() );
347 # my $todaysdate = sprintf( "%0.2d/%0.2d/%0.4d", ( $datearr[3] + 1 ),( $datearr[4] + 1 ),( $datearr[5] + 1900 ) );
348 # FIXME - use Dates obj , locale. AND, why [4]+1 ??
349 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
350 $template->param( intransit => 0 );
353 $template->param( intransit => 1 );
358 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
360 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
361 destbranch => $reserve->{'branchcode'},
362 transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
364 resbarcode => $barcode,
365 # today => $todaysdate,
366 itemnumber => $reserve->{'itemnumber'},
367 borsurname => $borr->{'surname'},
368 bortitle => $borr->{'title'},
369 borfirstname => $borr->{'firstname'},
370 borrowernumber => $reserve->{'borrowernumber'},
371 borcnum => $borr->{'cardnumber'},
372 borphone => $borr->{'phone'},
373 boraddress => $borr->{'address'},
374 boraddress2 => $borr->{'address2'},
375 borsub => $borr->{'suburb'},
376 borcity => $borr->{'city'},
377 borzip => $borr->{'zipcode'},
378 boremail => $borr->{'email'},
379 debarred => $borr->{'debarred'},
380 gonenoaddress => $borr->{'gonenoaddress'},
388 foreach my $code ( keys %$messages ) {
392 my $exit_required_p = 0;
393 if ( $code eq 'BadBarcode' ) {
394 $err{badbarcode} = 1;
395 $err{msg} = $messages->{'BadBarcode'};
397 elsif ( $code eq 'NotIssued' ) {
399 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
401 elsif ( $code eq 'WasLost' ) {
404 elsif ( $code eq 'ResFound' ) {
405 ; # FIXME... anything to do here?
407 elsif ( $code eq 'WasReturned' ) {
408 ; # FIXME... anything to do here?
410 elsif ( $code eq 'WasTransfered' ) {
411 ; # FIXME... anything to do here?
413 elsif ( $code eq 'wthdrawn' ) {
415 $exit_required_p = 1;
417 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
418 if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
419 $err{ispermanent} = 1;
421 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
424 elsif ( $code eq 'WrongTransfer' ) {
425 ; # FIXME... anything to do here?
427 elsif ( $code eq 'WrongTransferItem' ) {
428 ; # FIXME... anything to do here?
430 elsif ( $code eq 'NeedsTransfer' ) {
432 elsif ( $code eq 'Wrongbranch' ) {
436 die "Unknown error code $code"; # XXX
439 push( @errmsgloop, \%err );
441 last if $exit_required_p;
443 $template->param( errmsgloop => \@errmsgloop );
447 my $flags = $borrower->{'flags'};
450 foreach my $flag ( sort keys %$flags ) {
452 unless ($flagset) { $flagset = 1; }
453 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
454 $flaginfo{flag} = $flag;
455 if ( $flag eq 'CHARGES' ) {
456 $flaginfo{msg} = $flag;
457 $flaginfo{charges} = 1;
458 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
460 elsif ( $flag eq 'WAITING' ) {
461 $flaginfo{msg} = $flag;
462 $flaginfo{waiting} = 1;
464 my $items = $flags->{$flag}->{'itemlist'};
465 foreach my $item (@$items) {
467 GetBiblioFromItemNumber( $item->{'itemnumber'});
469 $waitingitem{biblionum} = $biblio->{'biblionumber'};
470 $waitingitem{barcode} = $biblio->{'barcode'};
471 $waitingitem{title} = $biblio->{'title'};
472 $waitingitem{brname} =
473 $branches->{ $biblio->{'holdingbranch'} }
475 push( @waitingitemloop, \%waitingitem );
477 $flaginfo{itemloop} = \@waitingitemloop;
479 elsif ( $flag eq 'ODUES' ) {
480 my $items = $flags->{$flag}->{'itemlist'};
482 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
486 GetBiblioFromItemNumber( $item->{'itemnumber'});
488 $overdueitem{duedate} = format_date( $item->{'date_due'} );
489 $overdueitem{biblionum} = $biblio->{'biblionumber'};
490 $overdueitem{barcode} = $biblio->{'barcode'};
491 $overdueitem{title} = $biblio->{'title'};
492 $overdueitem{brname} =
493 $branches->{ $biblio->{'holdingbranch'} }
495 push( @itemloop, \%overdueitem );
497 $flaginfo{itemloop} = \@itemloop;
498 $flaginfo{overdue} = 1;
501 $flaginfo{other} = 1;
502 $flaginfo{msg} = $flags->{$flag}->{'message'};
504 push( @flagloop, \%flaginfo );
508 flagloop => \@flagloop,
509 riborrowernumber => $borrower->{'borrowernumber'},
510 riborcnum => $borrower->{'cardnumber'},
511 riborsurname => $borrower->{'surname'},
512 ribortitle => $borrower->{'title'},
513 riborfirstname => $borrower->{'firstname'}
517 #set up so only the last 8 returned items display (make for faster loading pages)
520 foreach ( sort { $a <=> $b } keys %returneditems ) {
523 my $barcode = $returneditems{$_};
524 my $duedate = $riduedate{$_};
528 my @tempdate = split( /-/, $duedate );
529 $ri{year} = $tempdate[0];
530 $ri{month} = $tempdate[1];
531 $ri{day} = $tempdate[2];
532 my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
533 # my @datearr = localtime( time() );
536 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
537 . sprintf( "%0.2d", $datearr[3] );
538 # FIXME - todaysdate isn't used, and what date _is_ it ?
539 $ri{duedate} = format_date($duedate);
541 GetMemberDetails( $riborrowernumber{$_}, 0 );
542 $ri{borrowernumber} = $borrower->{'borrowernumber'};
543 $ri{borcnum} = $borrower->{'cardnumber'};
544 $ri{borfirstname} = $borrower->{'firstname'};
545 $ri{borsurname} = $borrower->{'surname'};
546 $ri{bortitle} = $borrower->{'title'};
549 $ri{borrowernumber} = $riborrowernumber{$_};
553 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
554 # fix up item type for display
555 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
556 $ri{itembiblionumber} = $biblio->{'biblionumber'};
557 $ri{itemtitle} = $biblio->{'title'};
558 $ri{itemauthor} = $biblio->{'author'};
559 $ri{itemtype} = $biblio->{'itemtype'};
560 $ri{ccode} = $biblio->{'ccode'};
561 $ri{barcode} = $barcode;
567 push( @riloop, \%ri );
569 $template->param( riloop => \@riloop );
572 genbrname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
573 genprname => $printers->{$printer}->{'printername'},
574 branchname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
576 errmsgloop => \@errmsgloop,
577 exemptfine => $exemptfine,
578 dropboxmode => $dropboxmode,
579 yesterdaysdate => $yesterday->output(),
580 overduecharges => $overduecharges,
583 # actually print the page!
584 output_html_with_http_headers $query, $cookie, $template->output;