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
34 use C4::Dates qw/format_date/;
39 use C4::Branch; # GetBranchName
40 use C4::Koha; # FIXME : is it still useful ?
45 my ( $template, $librarian, $cookie ) = get_template_and_user(
47 template_name => "circ/returns.tmpl",
51 flagsrequired => { circulate => 1 },
57 my $branches = GetBranches();
58 my $printers = GetPrinters();
60 #my $branch = C4::Context->userenv?C4::Context->userenv->{'branch'}:"";
61 my $printer = C4::Context->userenv?C4::Context->userenv->{'branchprinter'}:"";
62 my $overduecharges = (C4::Context->preference('finesMode') && C4::Context->preference('finesMode') ne 'off');
64 # Some code to handle the error if there is no branch or printer setting.....
67 # Set up the item stack ....
72 foreach ( $query->param ) {
73 (next) unless (/ri-(\d*)/);
76 (next) if ( $counter > 20 );
77 my $barcode = $query->param("ri-$counter");
78 my $duedate = $query->param("dd-$counter");
79 my $borrowernumber = $query->param("bn-$counter");
83 $barcode = cuecatbarcodedecode($barcode);
85 ######################
86 #Are these lines still useful ?
87 $returneditems{$counter} = $barcode;
88 $riduedate{$counter} = $duedate;
89 $riborrowernumber{$counter} = $borrowernumber;
91 #######################
92 $input{counter} = $counter;
93 $input{barcode} = $barcode;
94 $input{duedate} = $duedate;
95 $input{borrowernumber} = $borrowernumber;
96 push( @inputloop, \%input );
100 # Deal with the requests....
102 if ($query->param('WT-itemNumber')){
103 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
106 if ( $query->param('resbarcode') ) {
107 my $item = $query->param('itemnumber');
108 my $borrowernumber = $query->param('borrowernumber');
109 my $resbarcode = $query->param('resbarcode');
110 my $diffBranchReturned = $query->param('diffBranch');
112 my $iteminfo = GetBiblioFromItemNumber($item);
115 # addin in ModReserveAffect the possibility to check if the document is expected in this library or not,
116 # if not we send a value in reserve waiting for not implementting waiting status
117 if ($diffBranchReturned) {
118 $diffBranchSend = $diffBranchReturned;
121 $diffBranchSend = undef;
123 ModReserveAffect( $item, $borrowernumber,$diffBranchSend);
124 # check if we have other reservs for this document, if we have a return send the message of transfer
125 my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
127 my $branchname = GetBranchName( $messages->{'transfert'} );
128 my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
129 my $borcnum = $borr->{'cardnumber'};
131 $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
132 my $slip = $query->param('resslip');
135 if ( $messages->{'transfert'} ) {
137 itemtitle => $iteminfo->{'title'},
138 itembiblionumber => $iteminfo->{'biblionumber'},
139 iteminfo => $iteminfo->{'author'},
140 tobranchname => $branchname,
142 borrowernumber => $borrowernumber,
144 borfirstname => $borr->{'firstname'},
145 borsurname => $borr->{'surname'},
154 my $issueinformation;
155 my $barcode = $query->param('barcode');
157 $barcode =~ s/\s*//g;
158 my $exemptfine = $query->param('exemptfine');
160 my $dotransfer = $query->param('dotransfer');
162 # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
163 my $transferitem=$query->param('transferitem');
164 my $tobranch=$query->param('tobranch');
165 ModItemTransfer($transferitem, C4::Context->userenv->{'branch'}, $tobranch);
168 # actually return book and prepare item table.....
171 $barcode = cuecatbarcodedecode($barcode);
175 ( $returned, $messages, $issueinformation, $borrower ) =
176 AddReturn( $barcode, C4::Context->userenv->{'branch'}, $exemptfine );
177 # get biblio description
178 my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
180 title => $biblio->{'title'},
181 homebranch => $biblio->{'homebranch'},
182 author => $biblio->{'author'},
183 itembarcode => $biblio->{'barcode'},
184 itemtype => $biblio->{'itemtype'},
185 ccode => $biblio->{'ccode'},
186 itembiblionumber => $biblio->{'biblionumber'},
189 $returneditems{0} = $barcode;
190 $riborrowernumber{0} = $borrower->{'borrowernumber'};
191 $riduedate{0} = $issueinformation->{'date_due'};
195 $input{barcode} = $barcode;
196 $input{duedate} = $riduedate{0};
197 $input{borrowernumber} = $riborrowernumber{0};
198 push( @inputloop, \%input );
200 # check if the branch is the same as homebranch
201 # if not, we want to put a message
202 if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
203 $template->param( homebranch => $biblio->{'homebranch'} );
206 elsif ( !$messages->{'BadBarcode'} ) {
210 $input{barcode} = $barcode;
213 $returneditems{0} = $barcode;
215 if ( $messages->{'wthdrawn'} ) {
216 $input{withdrawn} = 1;
217 $input{borrowernumber} = "Item Cancelled";
218 $riborrowernumber{0} = 'Item Cancelled';
221 $input{borrowernumber} = " ";
222 $riborrowernumber{0} = ' ';
224 push( @inputloop, \%input );
227 $template->param( inputloop => \@inputloop );
233 # new op dev : we check if the document must be returned to his homebranch directly,
234 # if the document is transfered, we have warning message .
236 if ( $messages->{'WasTransfered'} ) {
243 if ( $messages->{'NeedsTransfer'} ){
247 itemnumber => $issueinformation->{'itemnumber'}
251 if ( $messages->{'Wrongbranch'} ){
257 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
259 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
262 TransferWaitingAt => $messages->{'WrongTransfer'},
263 WrongTransferItem => $messages->{'WrongTransferItem'},
266 my $reserve = $messages->{'ResFound'};
267 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
268 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
270 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
273 wborfirstname => $borr->{'firstname'},
274 wborsurname => $borr->{'surname'},
275 wbortitle => $borr->{'title'},
276 wborphone => $borr->{'phone'},
277 wboremail => $borr->{'email'},
278 wboraddress => $borr->{'address'},
279 wboraddress2 => $borr->{'address2'},
280 wborcity => $borr->{'city'},
281 wborzip => $borr->{'zipcode'},
282 wborrowernumber => $reserve->{'borrowernumber'},
283 wborcnum => $borr->{'cardnumber'},
284 wtransfertFrom => C4::Context->userenv->{'branch'},
290 # reserve found and item arrived at the expected branch
292 if ( $messages->{'ResFound'}) {
293 my $reserve = $messages->{'ResFound'};
294 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
295 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
296 if ( $reserve->{'ResFound'} eq "Waiting" ) {
297 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
298 $template->param( waiting => 1 );
301 $template->param( waiting => 0 );
306 name => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
307 borfirstname => $borr->{'firstname'},
308 borsurname => $borr->{'surname'},
309 bortitle => $borr->{'title'},
310 borphone => $borr->{'phone'},
311 boremail => $borr->{'email'},
312 boraddress => $borr->{'address'},
313 boraddress2 => $borr->{'address2'},
314 borcity => $borr->{'city'},
315 borzip => $borr->{'zipcode'},
316 borrowernumber => $reserve->{'borrowernumber'},
317 borcnum => $borr->{'cardnumber'},
318 debarred => $borr->{'debarred'},
319 gonenoaddress => $borr->{'gonenoaddress'},
320 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
321 itemnumber => $reserve->{'itemnumber'},
326 if ( $reserve->{'ResFound'} eq "Reserved" ) {
327 my @da = localtime( time() );
329 sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
330 . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
333 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
334 $template->param( intransit => 0 );
337 $template->param( intransit => 1 );
342 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
344 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
345 destbranch => $reserve->{'branchcode'},
346 transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
348 resbarcode => $barcode,
349 today => $todaysdate,
350 itemnumber => $reserve->{'itemnumber'},
351 borsurname => $borr->{'surname'},
352 bortitle => $borr->{'title'},
353 borfirstname => $borr->{'firstname'},
354 borrowernumber => $reserve->{'borrowernumber'},
355 borcnum => $borr->{'cardnumber'},
356 borphone => $borr->{'phone'},
357 boraddress => $borr->{'address'},
358 boraddress2 => $borr->{'address2'},
359 borsub => $borr->{'suburb'},
360 borcity => $borr->{'city'},
361 borzip => $borr->{'zipcode'},
362 boremail => $borr->{'email'},
363 debarred => $borr->{'debarred'},
364 gonenoaddress => $borr->{'gonenoaddress'},
372 foreach my $code ( keys %$messages ) {
376 my $exit_required_p = 0;
377 if ( $code eq 'BadBarcode' ) {
378 $err{badbarcode} = 1;
379 $err{msg} = $messages->{'BadBarcode'};
381 elsif ( $code eq 'NotIssued' ) {
383 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
385 elsif ( $code eq 'WasLost' ) {
388 elsif ( $code eq 'ResFound' ) {
389 ; # FIXME... anything to do here?
391 elsif ( $code eq 'WasReturned' ) {
392 ; # FIXME... anything to do here?
394 elsif ( $code eq 'WasTransfered' ) {
395 ; # FIXME... anything to do here?
397 elsif ( $code eq 'wthdrawn' ) {
399 $exit_required_p = 1;
401 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
402 if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
403 $err{ispermanent} = 1;
405 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
408 elsif ( $code eq 'WrongTransfer' ) {
409 ; # FIXME... anything to do here?
411 elsif ( $code eq 'WrongTransferItem' ) {
412 ; # FIXME... anything to do here?
414 elsif ( $code eq 'NeedsTransfer' ) {
416 elsif ( $code eq 'Wrongbranch' ) {
420 die "Unknown error code $code"; # XXX
423 push( @errmsgloop, \%err );
425 last if $exit_required_p;
427 $template->param( errmsgloop => \@errmsgloop );
431 my $flags = $borrower->{'flags'};
434 foreach my $flag ( sort keys %$flags ) {
436 unless ($flagset) { $flagset = 1; }
437 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
438 $flaginfo{flag} = $flag;
439 if ( $flag eq 'CHARGES' ) {
440 $flaginfo{msg} = $flag;
441 $flaginfo{charges} = 1;
442 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
444 elsif ( $flag eq 'WAITING' ) {
445 $flaginfo{msg} = $flag;
446 $flaginfo{waiting} = 1;
448 my $items = $flags->{$flag}->{'itemlist'};
449 foreach my $item (@$items) {
451 GetBiblioFromItemNumber( $item->{'itemnumber'});
453 $waitingitem{biblionum} = $biblio->{'biblionumber'};
454 $waitingitem{barcode} = $biblio->{'barcode'};
455 $waitingitem{title} = $biblio->{'title'};
456 $waitingitem{brname} =
457 $branches->{ $biblio->{'holdingbranch'} }
459 push( @waitingitemloop, \%waitingitem );
461 $flaginfo{itemloop} = \@waitingitemloop;
463 elsif ( $flag eq 'ODUES' ) {
464 my $items = $flags->{$flag}->{'itemlist'};
466 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
470 GetBiblioFromItemNumber( $item->{'itemnumber'});
472 $overdueitem{duedate} = format_date( $item->{'date_due'} );
473 $overdueitem{biblionum} = $biblio->{'biblionumber'};
474 $overdueitem{barcode} = $biblio->{'barcode'};
475 $overdueitem{title} = $biblio->{'title'};
476 $overdueitem{brname} =
477 $branches->{ $biblio->{'holdingbranch'} }
479 push( @itemloop, \%overdueitem );
481 $flaginfo{itemloop} = \@itemloop;
482 $flaginfo{overdue} = 1;
485 $flaginfo{other} = 1;
486 $flaginfo{msg} = $flags->{$flag}->{'message'};
488 push( @flagloop, \%flaginfo );
492 flagloop => \@flagloop,
493 riborrowernumber => $borrower->{'borrowernumber'},
494 riborcnum => $borrower->{'cardnumber'},
495 riborsurname => $borrower->{'surname'},
496 ribortitle => $borrower->{'title'},
497 riborfirstname => $borrower->{'firstname'}
501 #set up so only the last 8 returned items display (make for faster loading pages)
504 foreach ( sort { $a <=> $b } keys %returneditems ) {
507 my $barcode = $returneditems{$_};
508 my $duedate = $riduedate{$_};
512 my @tempdate = split( /-/, $duedate );
513 $ri{year} = $tempdate[0];
514 $ri{month} = $tempdate[1];
515 $ri{day} = $tempdate[2];
516 my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
517 my @datearr = localtime( time() );
520 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
521 . sprintf( "%0.2d", $datearr[3] );
522 $ri{duedate} = format_date($duedate);
524 GetMemberDetails( $riborrowernumber{$_}, 0 );
525 $ri{borrowernumber} = $borrower->{'borrowernumber'};
526 $ri{borcnum} = $borrower->{'cardnumber'};
527 $ri{borfirstname} = $borrower->{'firstname'};
528 $ri{borsurname} = $borrower->{'surname'};
529 $ri{bortitle} = $borrower->{'title'};
532 $ri{borrowernumber} = $riborrowernumber{$_};
536 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
537 $ri{itembiblionumber} = $biblio->{'biblionumber'};
538 $ri{itemtitle} = $biblio->{'title'};
539 $ri{itemauthor} = $biblio->{'author'};
540 $ri{itemtype} = $biblio->{'itemtype'};
541 $ri{ccode} = $biblio->{'ccode'};
542 $ri{barcode} = $barcode;
548 push( @riloop, \%ri );
550 $template->param( riloop => \@riloop );
553 genbrname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
554 genprname => $printers->{$printer}->{'printername'},
555 branchname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
557 errmsgloop => \@errmsgloop,
558 exemptfine => $exemptfine,
559 overduecharges => $overduecharges,
562 # actually print the page!
563 output_html_with_http_headers $query, $cookie, $template->output;