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/;
40 use C4::Branch; # GetBranchName
41 use C4::Koha; # FIXME : is it still useful ?
45 if (!C4::Context->userenv){
46 my $sessionID = $query->cookie("CGISESSID");
47 my $session = get_session($sessionID);
48 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
49 # no branch set we can't return
50 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
56 my ( $template, $librarian, $cookie ) = get_template_and_user(
58 template_name => "circ/returns.tmpl",
62 flagsrequired => { circulate => 1 },
68 my $branches = GetBranches();
69 my $printers = GetPrinters();
71 #my $branch = C4::Context->userenv?C4::Context->userenv->{'branch'}:"";
72 my $printer = C4::Context->userenv?C4::Context->userenv->{'branchprinter'}:"";
73 my $overduecharges = (C4::Context->preference('finesMode') && C4::Context->preference('finesMode') ne 'off');
75 # Some code to handle the error if there is no branch or printer setting.....
78 # Set up the item stack ....
83 foreach ( $query->param ) {
84 (next) unless (/ri-(\d*)/);
87 (next) if ( $counter > 20 );
88 my $barcode = $query->param("ri-$counter");
89 my $duedate = $query->param("dd-$counter");
90 my $borrowernumber = $query->param("bn-$counter");
94 $barcode = cuecatbarcodedecode($barcode);
96 ######################
97 #Are these lines still useful ?
98 $returneditems{$counter} = $barcode;
99 $riduedate{$counter} = $duedate;
100 $riborrowernumber{$counter} = $borrowernumber;
102 #######################
103 $input{counter} = $counter;
104 $input{barcode} = $barcode;
105 $input{duedate} = $duedate;
106 $input{borrowernumber} = $borrowernumber;
107 push( @inputloop, \%input );
111 # Deal with the requests....
113 if ($query->param('WT-itemNumber')){
114 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
117 if ( $query->param('resbarcode') ) {
118 my $item = $query->param('itemnumber');
119 my $borrowernumber = $query->param('borrowernumber');
120 my $resbarcode = $query->param('resbarcode');
121 my $diffBranchReturned = $query->param('diffBranch');
123 my $iteminfo = GetBiblioFromItemNumber($item);
126 # addin in ModReserveAffect the possibility to check if the document is expected in this library or not,
127 # if not we send a value in reserve waiting for not implementting waiting status
128 if ($diffBranchReturned) {
129 $diffBranchSend = $diffBranchReturned;
132 $diffBranchSend = undef;
134 ModReserveAffect( $item, $borrowernumber,$diffBranchSend);
135 # check if we have other reservs for this document, if we have a return send the message of transfer
136 my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
138 my $branchname = GetBranchName( $messages->{'transfert'} );
139 my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
140 my $borcnum = $borr->{'cardnumber'};
142 $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
143 my $slip = $query->param('resslip');
146 if ( $messages->{'transfert'} ) {
148 itemtitle => $iteminfo->{'title'},
149 itembiblionumber => $iteminfo->{'biblionumber'},
150 iteminfo => $iteminfo->{'author'},
151 tobranchname => $branchname,
153 borrowernumber => $borrowernumber,
155 borfirstname => $borr->{'firstname'},
156 borsurname => $borr->{'surname'},
165 my $issueinformation;
166 my $barcode = $query->param('barcode');
168 $barcode =~ s/\s*//g;
169 my $exemptfine = $query->param('exemptfine');
171 my $dotransfer = $query->param('dotransfer');
173 # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
174 my $transferitem=$query->param('transferitem');
175 my $tobranch=$query->param('tobranch');
176 ModItemTransfer($transferitem, C4::Context->userenv->{'branch'}, $tobranch);
179 # actually return book and prepare item table.....
182 $barcode = cuecatbarcodedecode($barcode);
186 ( $returned, $messages, $issueinformation, $borrower ) =
187 AddReturn( $barcode, C4::Context->userenv->{'branch'}, $exemptfine );
188 # get biblio description
189 my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
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'},
200 $returneditems{0} = $barcode;
201 $riborrowernumber{0} = $borrower->{'borrowernumber'};
202 $riduedate{0} = $issueinformation->{'date_due'};
206 $input{barcode} = $barcode;
207 $input{duedate} = $riduedate{0};
208 $input{borrowernumber} = $riborrowernumber{0};
209 push( @inputloop, \%input );
211 # check if the branch is the same as homebranch
212 # if not, we want to put a message
213 if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
214 $template->param( homebranch => $biblio->{'homebranch'} );
217 elsif ( !$messages->{'BadBarcode'} ) {
221 $input{barcode} = $barcode;
224 $returneditems{0} = $barcode;
226 if ( $messages->{'wthdrawn'} ) {
227 $input{withdrawn} = 1;
228 $input{borrowernumber} = "Item Cancelled";
229 $riborrowernumber{0} = 'Item Cancelled';
232 $input{borrowernumber} = " ";
233 $riborrowernumber{0} = ' ';
235 push( @inputloop, \%input );
238 $template->param( inputloop => \@inputloop );
244 # new op dev : we check if the document must be returned to his homebranch directly,
245 # if the document is transfered, we have warning message .
247 if ( $messages->{'WasTransfered'} ) {
254 if ( $messages->{'NeedsTransfer'} ){
258 itemnumber => $issueinformation->{'itemnumber'}
262 if ( $messages->{'Wrongbranch'} ){
268 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
270 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
273 TransferWaitingAt => $messages->{'WrongTransfer'},
274 WrongTransferItem => $messages->{'WrongTransferItem'},
277 my $reserve = $messages->{'ResFound'};
278 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
279 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
281 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
284 wborfirstname => $borr->{'firstname'},
285 wborsurname => $borr->{'surname'},
286 wbortitle => $borr->{'title'},
287 wborphone => $borr->{'phone'},
288 wboremail => $borr->{'email'},
289 wboraddress => $borr->{'address'},
290 wboraddress2 => $borr->{'address2'},
291 wborcity => $borr->{'city'},
292 wborzip => $borr->{'zipcode'},
293 wborrowernumber => $reserve->{'borrowernumber'},
294 wborcnum => $borr->{'cardnumber'},
295 wtransfertFrom => C4::Context->userenv->{'branch'},
301 # reserve found and item arrived at the expected branch
303 if ( $messages->{'ResFound'}) {
304 my $reserve = $messages->{'ResFound'};
305 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
306 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
307 if ( $reserve->{'ResFound'} eq "Waiting" ) {
308 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
309 $template->param( waiting => 1 );
312 $template->param( waiting => 0 );
317 name => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
318 borfirstname => $borr->{'firstname'},
319 borsurname => $borr->{'surname'},
320 bortitle => $borr->{'title'},
321 borphone => $borr->{'phone'},
322 boremail => $borr->{'email'},
323 boraddress => $borr->{'address'},
324 boraddress2 => $borr->{'address2'},
325 borcity => $borr->{'city'},
326 borzip => $borr->{'zipcode'},
327 borrowernumber => $reserve->{'borrowernumber'},
328 borcnum => $borr->{'cardnumber'},
329 debarred => $borr->{'debarred'},
330 gonenoaddress => $borr->{'gonenoaddress'},
331 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
332 itemnumber => $reserve->{'itemnumber'},
337 if ( $reserve->{'ResFound'} eq "Reserved" ) {
338 my @da = localtime( time() );
340 sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
341 . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
344 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
345 $template->param( intransit => 0 );
348 $template->param( intransit => 1 );
353 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
355 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
356 destbranch => $reserve->{'branchcode'},
357 transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
359 resbarcode => $barcode,
360 today => $todaysdate,
361 itemnumber => $reserve->{'itemnumber'},
362 borsurname => $borr->{'surname'},
363 bortitle => $borr->{'title'},
364 borfirstname => $borr->{'firstname'},
365 borrowernumber => $reserve->{'borrowernumber'},
366 borcnum => $borr->{'cardnumber'},
367 borphone => $borr->{'phone'},
368 boraddress => $borr->{'address'},
369 boraddress2 => $borr->{'address2'},
370 borsub => $borr->{'suburb'},
371 borcity => $borr->{'city'},
372 borzip => $borr->{'zipcode'},
373 boremail => $borr->{'email'},
374 debarred => $borr->{'debarred'},
375 gonenoaddress => $borr->{'gonenoaddress'},
383 foreach my $code ( keys %$messages ) {
387 my $exit_required_p = 0;
388 if ( $code eq 'BadBarcode' ) {
389 $err{badbarcode} = 1;
390 $err{msg} = $messages->{'BadBarcode'};
392 elsif ( $code eq 'NotIssued' ) {
394 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
396 elsif ( $code eq 'WasLost' ) {
399 elsif ( $code eq 'ResFound' ) {
400 ; # FIXME... anything to do here?
402 elsif ( $code eq 'WasReturned' ) {
403 ; # FIXME... anything to do here?
405 elsif ( $code eq 'WasTransfered' ) {
406 ; # FIXME... anything to do here?
408 elsif ( $code eq 'wthdrawn' ) {
410 $exit_required_p = 1;
412 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
413 if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
414 $err{ispermanent} = 1;
416 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
419 elsif ( $code eq 'WrongTransfer' ) {
420 ; # FIXME... anything to do here?
422 elsif ( $code eq 'WrongTransferItem' ) {
423 ; # FIXME... anything to do here?
425 elsif ( $code eq 'NeedsTransfer' ) {
427 elsif ( $code eq 'Wrongbranch' ) {
431 die "Unknown error code $code"; # XXX
434 push( @errmsgloop, \%err );
436 last if $exit_required_p;
438 $template->param( errmsgloop => \@errmsgloop );
442 my $flags = $borrower->{'flags'};
445 foreach my $flag ( sort keys %$flags ) {
447 unless ($flagset) { $flagset = 1; }
448 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
449 $flaginfo{flag} = $flag;
450 if ( $flag eq 'CHARGES' ) {
451 $flaginfo{msg} = $flag;
452 $flaginfo{charges} = 1;
453 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
455 elsif ( $flag eq 'WAITING' ) {
456 $flaginfo{msg} = $flag;
457 $flaginfo{waiting} = 1;
459 my $items = $flags->{$flag}->{'itemlist'};
460 foreach my $item (@$items) {
462 GetBiblioFromItemNumber( $item->{'itemnumber'});
464 $waitingitem{biblionum} = $biblio->{'biblionumber'};
465 $waitingitem{barcode} = $biblio->{'barcode'};
466 $waitingitem{title} = $biblio->{'title'};
467 $waitingitem{brname} =
468 $branches->{ $biblio->{'holdingbranch'} }
470 push( @waitingitemloop, \%waitingitem );
472 $flaginfo{itemloop} = \@waitingitemloop;
474 elsif ( $flag eq 'ODUES' ) {
475 my $items = $flags->{$flag}->{'itemlist'};
477 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
481 GetBiblioFromItemNumber( $item->{'itemnumber'});
483 $overdueitem{duedate} = format_date( $item->{'date_due'} );
484 $overdueitem{biblionum} = $biblio->{'biblionumber'};
485 $overdueitem{barcode} = $biblio->{'barcode'};
486 $overdueitem{title} = $biblio->{'title'};
487 $overdueitem{brname} =
488 $branches->{ $biblio->{'holdingbranch'} }
490 push( @itemloop, \%overdueitem );
492 $flaginfo{itemloop} = \@itemloop;
493 $flaginfo{overdue} = 1;
496 $flaginfo{other} = 1;
497 $flaginfo{msg} = $flags->{$flag}->{'message'};
499 push( @flagloop, \%flaginfo );
503 flagloop => \@flagloop,
504 riborrowernumber => $borrower->{'borrowernumber'},
505 riborcnum => $borrower->{'cardnumber'},
506 riborsurname => $borrower->{'surname'},
507 ribortitle => $borrower->{'title'},
508 riborfirstname => $borrower->{'firstname'}
512 #set up so only the last 8 returned items display (make for faster loading pages)
515 foreach ( sort { $a <=> $b } keys %returneditems ) {
518 my $barcode = $returneditems{$_};
519 my $duedate = $riduedate{$_};
523 my @tempdate = split( /-/, $duedate );
524 $ri{year} = $tempdate[0];
525 $ri{month} = $tempdate[1];
526 $ri{day} = $tempdate[2];
527 my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
528 my @datearr = localtime( time() );
531 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
532 . sprintf( "%0.2d", $datearr[3] );
533 $ri{duedate} = format_date($duedate);
535 GetMemberDetails( $riborrowernumber{$_}, 0 );
536 $ri{borrowernumber} = $borrower->{'borrowernumber'};
537 $ri{borcnum} = $borrower->{'cardnumber'};
538 $ri{borfirstname} = $borrower->{'firstname'};
539 $ri{borsurname} = $borrower->{'surname'};
540 $ri{bortitle} = $borrower->{'title'};
543 $ri{borrowernumber} = $riborrowernumber{$_};
547 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
548 $ri{itembiblionumber} = $biblio->{'biblionumber'};
549 $ri{itemtitle} = $biblio->{'title'};
550 $ri{itemauthor} = $biblio->{'author'};
551 $ri{itemtype} = $biblio->{'itemtype'};
552 $ri{ccode} = $biblio->{'ccode'};
553 $ri{barcode} = $barcode;
559 push( @riloop, \%ri );
561 $template->param( riloop => \@riloop );
564 genbrname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
565 genprname => $printers->{$printer}->{'printername'},
566 branchname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
568 errmsgloop => \@errmsgloop,
569 exemptfine => $exemptfine,
570 overduecharges => $overduecharges,
573 # actually print the page!
574 output_html_with_http_headers $query, $cookie, $template->output;