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/;
39 use C4::Branch; # GetBranchName
40 use C4::Koha; # FIXME : is it still useful ?
44 if (!C4::Context->userenv){
45 my $sessionID = $query->cookie("CGISESSID");
46 my $session = get_session($sessionID);
47 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
48 # no branch set we can't return
49 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
55 my ( $template, $librarian, $cookie ) = get_template_and_user(
57 template_name => "circ/returns.tmpl",
61 flagsrequired => { circulate => 1 },
67 my $branches = GetBranches();
68 my $printers = GetPrinters();
70 #my $branch = C4::Context->userenv?C4::Context->userenv->{'branch'}:"";
71 my $printer = C4::Context->userenv?C4::Context->userenv->{'branchprinter'}:"";
72 my $overduecharges = (C4::Context->preference('finesMode') && C4::Context->preference('finesMode') ne 'off');
74 # Some code to handle the error if there is no branch or printer setting.....
77 # Set up the item stack ....
82 foreach ( $query->param ) {
83 (next) unless (/ri-(\d*)/);
86 (next) if ( $counter > 20 );
87 my $barcode = $query->param("ri-$counter");
88 my $duedate = $query->param("dd-$counter");
89 my $borrowernumber = $query->param("bn-$counter");
93 $barcode = cuecatbarcodedecode($barcode);
95 ######################
96 #Are these lines still useful ?
97 $returneditems{$counter} = $barcode;
98 $riduedate{$counter} = $duedate;
99 $riborrowernumber{$counter} = $borrowernumber;
101 #######################
102 $input{counter} = $counter;
103 $input{barcode} = $barcode;
104 $input{duedate} = $duedate;
105 $input{borrowernumber} = $borrowernumber;
106 push( @inputloop, \%input );
110 # Deal with the requests....
112 if ($query->param('WT-itemNumber')){
113 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
116 if ( $query->param('resbarcode') ) {
117 my $item = $query->param('itemnumber');
118 my $borrowernumber = $query->param('borrowernumber');
119 my $resbarcode = $query->param('resbarcode');
120 my $diffBranchReturned = $query->param('diffBranch');
122 my $iteminfo = GetBiblioFromItemNumber($item);
125 # addin in ModReserveAffect the possibility to check if the document is expected in this library or not,
126 # if not we send a value in reserve waiting for not implementting waiting status
127 if ($diffBranchReturned) {
128 $diffBranchSend = $diffBranchReturned;
131 $diffBranchSend = undef;
133 ModReserveAffect( $item, $borrowernumber,$diffBranchSend);
134 # check if we have other reservs for this document, if we have a return send the message of transfer
135 my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
137 my $branchname = GetBranchName( $messages->{'transfert'} );
138 my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
139 my $borcnum = $borr->{'cardnumber'};
141 $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
142 my $slip = $query->param('resslip');
145 if ( $messages->{'transfert'} ) {
147 itemtitle => $iteminfo->{'title'},
148 itembiblionumber => $iteminfo->{'biblionumber'},
149 iteminfo => $iteminfo->{'author'},
150 tobranchname => $branchname,
152 borrowernumber => $borrowernumber,
154 borfirstname => $borr->{'firstname'},
155 borsurname => $borr->{'surname'},
164 my $issueinformation;
165 my $barcode = $query->param('barcode');
167 $barcode =~ s/\s*//g;
168 my $exemptfine = $query->param('exemptfine');
170 my $dotransfer = $query->param('dotransfer');
172 # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
173 my $transferitem=$query->param('transferitem');
174 my $tobranch=$query->param('tobranch');
175 ModItemTransfer($transferitem, C4::Context->userenv->{'branch'}, $tobranch);
178 # actually return book and prepare item table.....
181 $barcode = cuecatbarcodedecode($barcode);
185 ( $returned, $messages, $issueinformation, $borrower ) =
186 AddReturn( $barcode, C4::Context->userenv->{'branch'}, $exemptfine );
187 # get biblio description
188 my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
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'},
199 $returneditems{0} = $barcode;
200 $riborrowernumber{0} = $borrower->{'borrowernumber'};
201 $riduedate{0} = $issueinformation->{'date_due'};
205 $input{barcode} = $barcode;
206 $input{duedate} = $riduedate{0};
207 $input{borrowernumber} = $riborrowernumber{0};
208 push( @inputloop, \%input );
210 # check if the branch is the same as homebranch
211 # if not, we want to put a message
212 if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
213 $template->param( homebranch => $biblio->{'homebranch'} );
216 elsif ( !$messages->{'BadBarcode'} ) {
220 $input{barcode} = $barcode;
223 $returneditems{0} = $barcode;
225 if ( $messages->{'wthdrawn'} ) {
226 $input{withdrawn} = 1;
227 $input{borrowernumber} = "Item Cancelled";
228 $riborrowernumber{0} = 'Item Cancelled';
231 $input{borrowernumber} = " ";
232 $riborrowernumber{0} = ' ';
234 push( @inputloop, \%input );
237 $template->param( inputloop => \@inputloop );
243 # new op dev : we check if the document must be returned to his homebranch directly,
244 # if the document is transfered, we have warning message .
246 if ( $messages->{'WasTransfered'} ) {
253 if ( $messages->{'NeedsTransfer'} ){
257 itemnumber => $issueinformation->{'itemnumber'}
261 if ( $messages->{'Wrongbranch'} ){
267 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
269 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
272 TransferWaitingAt => $messages->{'WrongTransfer'},
273 WrongTransferItem => $messages->{'WrongTransferItem'},
276 my $reserve = $messages->{'ResFound'};
277 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
278 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
280 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
283 wborfirstname => $borr->{'firstname'},
284 wborsurname => $borr->{'surname'},
285 wbortitle => $borr->{'title'},
286 wborphone => $borr->{'phone'},
287 wboremail => $borr->{'email'},
288 wboraddress => $borr->{'address'},
289 wboraddress2 => $borr->{'address2'},
290 wborcity => $borr->{'city'},
291 wborzip => $borr->{'zipcode'},
292 wborrowernumber => $reserve->{'borrowernumber'},
293 wborcnum => $borr->{'cardnumber'},
294 wtransfertFrom => C4::Context->userenv->{'branch'},
300 # reserve found and item arrived at the expected branch
302 if ( $messages->{'ResFound'}) {
303 my $reserve = $messages->{'ResFound'};
304 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
305 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
306 if ( $reserve->{'ResFound'} eq "Waiting" ) {
307 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
308 $template->param( waiting => 1 );
311 $template->param( waiting => 0 );
316 name => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
317 borfirstname => $borr->{'firstname'},
318 borsurname => $borr->{'surname'},
319 bortitle => $borr->{'title'},
320 borphone => $borr->{'phone'},
321 boremail => $borr->{'email'},
322 boraddress => $borr->{'address'},
323 boraddress2 => $borr->{'address2'},
324 borcity => $borr->{'city'},
325 borzip => $borr->{'zipcode'},
326 borrowernumber => $reserve->{'borrowernumber'},
327 borcnum => $borr->{'cardnumber'},
328 debarred => $borr->{'debarred'},
329 gonenoaddress => $borr->{'gonenoaddress'},
330 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
331 itemnumber => $reserve->{'itemnumber'},
336 if ( $reserve->{'ResFound'} eq "Reserved" ) {
337 my @da = localtime( time() );
339 sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
340 . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
343 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
344 $template->param( intransit => 0 );
347 $template->param( intransit => 1 );
352 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
354 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
355 destbranch => $reserve->{'branchcode'},
356 transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
358 resbarcode => $barcode,
359 today => $todaysdate,
360 itemnumber => $reserve->{'itemnumber'},
361 borsurname => $borr->{'surname'},
362 bortitle => $borr->{'title'},
363 borfirstname => $borr->{'firstname'},
364 borrowernumber => $reserve->{'borrowernumber'},
365 borcnum => $borr->{'cardnumber'},
366 borphone => $borr->{'phone'},
367 boraddress => $borr->{'address'},
368 boraddress2 => $borr->{'address2'},
369 borsub => $borr->{'suburb'},
370 borcity => $borr->{'city'},
371 borzip => $borr->{'zipcode'},
372 boremail => $borr->{'email'},
373 debarred => $borr->{'debarred'},
374 gonenoaddress => $borr->{'gonenoaddress'},
382 foreach my $code ( keys %$messages ) {
386 my $exit_required_p = 0;
387 if ( $code eq 'BadBarcode' ) {
388 $err{badbarcode} = 1;
389 $err{msg} = $messages->{'BadBarcode'};
391 elsif ( $code eq 'NotIssued' ) {
393 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
395 elsif ( $code eq 'WasLost' ) {
398 elsif ( $code eq 'ResFound' ) {
399 ; # FIXME... anything to do here?
401 elsif ( $code eq 'WasReturned' ) {
402 ; # FIXME... anything to do here?
404 elsif ( $code eq 'WasTransfered' ) {
405 ; # FIXME... anything to do here?
407 elsif ( $code eq 'wthdrawn' ) {
409 $exit_required_p = 1;
411 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
412 if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
413 $err{ispermanent} = 1;
415 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
418 elsif ( $code eq 'WrongTransfer' ) {
419 ; # FIXME... anything to do here?
421 elsif ( $code eq 'WrongTransferItem' ) {
422 ; # FIXME... anything to do here?
424 elsif ( $code eq 'NeedsTransfer' ) {
426 elsif ( $code eq 'Wrongbranch' ) {
430 die "Unknown error code $code"; # XXX
433 push( @errmsgloop, \%err );
435 last if $exit_required_p;
437 $template->param( errmsgloop => \@errmsgloop );
441 my $flags = $borrower->{'flags'};
444 foreach my $flag ( sort keys %$flags ) {
446 unless ($flagset) { $flagset = 1; }
447 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
448 $flaginfo{flag} = $flag;
449 if ( $flag eq 'CHARGES' ) {
450 $flaginfo{msg} = $flag;
451 $flaginfo{charges} = 1;
452 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
454 elsif ( $flag eq 'WAITING' ) {
455 $flaginfo{msg} = $flag;
456 $flaginfo{waiting} = 1;
458 my $items = $flags->{$flag}->{'itemlist'};
459 foreach my $item (@$items) {
461 GetBiblioFromItemNumber( $item->{'itemnumber'});
463 $waitingitem{biblionum} = $biblio->{'biblionumber'};
464 $waitingitem{barcode} = $biblio->{'barcode'};
465 $waitingitem{title} = $biblio->{'title'};
466 $waitingitem{brname} =
467 $branches->{ $biblio->{'holdingbranch'} }
469 push( @waitingitemloop, \%waitingitem );
471 $flaginfo{itemloop} = \@waitingitemloop;
473 elsif ( $flag eq 'ODUES' ) {
474 my $items = $flags->{$flag}->{'itemlist'};
476 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
480 GetBiblioFromItemNumber( $item->{'itemnumber'});
482 $overdueitem{duedate} = format_date( $item->{'date_due'} );
483 $overdueitem{biblionum} = $biblio->{'biblionumber'};
484 $overdueitem{barcode} = $biblio->{'barcode'};
485 $overdueitem{title} = $biblio->{'title'};
486 $overdueitem{brname} =
487 $branches->{ $biblio->{'holdingbranch'} }
489 push( @itemloop, \%overdueitem );
491 $flaginfo{itemloop} = \@itemloop;
492 $flaginfo{overdue} = 1;
495 $flaginfo{other} = 1;
496 $flaginfo{msg} = $flags->{$flag}->{'message'};
498 push( @flagloop, \%flaginfo );
502 flagloop => \@flagloop,
503 riborrowernumber => $borrower->{'borrowernumber'},
504 riborcnum => $borrower->{'cardnumber'},
505 riborsurname => $borrower->{'surname'},
506 ribortitle => $borrower->{'title'},
507 riborfirstname => $borrower->{'firstname'}
511 #set up so only the last 8 returned items display (make for faster loading pages)
514 foreach ( sort { $a <=> $b } keys %returneditems ) {
517 my $barcode = $returneditems{$_};
518 my $duedate = $riduedate{$_};
522 my @tempdate = split( /-/, $duedate );
523 $ri{year} = $tempdate[0];
524 $ri{month} = $tempdate[1];
525 $ri{day} = $tempdate[2];
526 my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
527 my @datearr = localtime( time() );
530 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
531 . sprintf( "%0.2d", $datearr[3] );
532 $ri{duedate} = format_date($duedate);
534 GetMemberDetails( $riborrowernumber{$_}, 0 );
535 $ri{borrowernumber} = $borrower->{'borrowernumber'};
536 $ri{borcnum} = $borrower->{'cardnumber'};
537 $ri{borfirstname} = $borrower->{'firstname'};
538 $ri{borsurname} = $borrower->{'surname'};
539 $ri{bortitle} = $borrower->{'title'};
542 $ri{borrowernumber} = $riborrowernumber{$_};
546 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
547 $ri{itembiblionumber} = $biblio->{'biblionumber'};
548 $ri{itemtitle} = $biblio->{'title'};
549 $ri{itemauthor} = $biblio->{'author'};
550 $ri{itemtype} = $biblio->{'itemtype'};
551 $ri{ccode} = $biblio->{'ccode'};
552 $ri{barcode} = $barcode;
558 push( @riloop, \%ri );
560 $template->param( riloop => \@riloop );
563 genbrname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
564 genprname => $printers->{$printer}->{'printername'},
565 branchname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
567 errmsgloop => \@errmsgloop,
568 exemptfine => $exemptfine,
569 overduecharges => $overduecharges,
572 # actually print the page!
573 output_html_with_http_headers $query, $cookie, $template->output;