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 = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
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);
124 # fix up item type for display
125 $iteminfo->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$iteminfo->{'itype'} : $iteminfo->{'itemtype'};
128 # addin in ModReserveAffect the possibility to check if the document is expected in this library or not,
129 # if not we send a value in reserve waiting for not implementting waiting status
130 if ($diffBranchReturned) {
131 $diffBranchSend = $diffBranchReturned;
134 $diffBranchSend = undef;
136 ModReserveAffect
( $item, $borrowernumber,$diffBranchSend);
137 # check if we have other reservs for this document, if we have a return send the message of transfer
138 my ( $messages, $nextreservinfo ) = GetOtherReserves
($item);
140 my $branchname = GetBranchName
( $messages->{'transfert'} );
141 my ($borr) = GetMemberDetails
( $nextreservinfo, 0 );
142 my $borcnum = $borr->{'cardnumber'};
144 $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
145 my $slip = $query->param('resslip');
148 if ( $messages->{'transfert'} ) {
150 itemtitle
=> $iteminfo->{'title'},
151 itembiblionumber
=> $iteminfo->{'biblionumber'},
152 iteminfo
=> $iteminfo->{'author'},
153 tobranchname
=> $branchname,
155 borrowernumber
=> $borrowernumber,
157 borfirstname
=> $borr->{'firstname'},
158 borsurname
=> $borr->{'surname'},
167 my $issueinformation;
168 my $barcode = $query->param('barcode');
170 # $barcode =~ s/\s*//g; - use barcodedecode for this; whitespace is not invalid.
171 my $exemptfine = $query->param('exemptfine');
173 my $dotransfer = $query->param('dotransfer');
175 # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
176 my $transferitem=$query->param('transferitem');
177 my $tobranch=$query->param('tobranch');
178 ModItemTransfer
($transferitem, C4
::Context
->userenv->{'branch'}, $tobranch);
181 # actually return book and prepare item table.....
183 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
187 ( $returned, $messages, $issueinformation, $borrower ) =
188 AddReturn
( $barcode, C4
::Context
->userenv->{'branch'}, $exemptfine );
189 # get biblio description
190 my $biblio = GetBiblioFromItemNumber
($issueinformation->{'itemnumber'});
191 # fix up item type for display
192 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
195 title
=> $biblio->{'title'},
196 homebranch
=> $biblio->{'homebranch'},
197 author
=> $biblio->{'author'},
198 itembarcode
=> $biblio->{'barcode'},
199 itemtype
=> $biblio->{'itemtype'},
200 ccode
=> $biblio->{'ccode'},
201 itembiblionumber
=> $biblio->{'biblionumber'},
204 $returneditems{0} = $barcode;
205 $riborrowernumber{0} = $borrower->{'borrowernumber'};
206 $riduedate{0} = $issueinformation->{'date_due'};
210 $input{barcode
} = $barcode;
211 $input{duedate
} = $riduedate{0};
212 $input{borrowernumber
} = $riborrowernumber{0};
213 push( @inputloop, \
%input );
215 # check if the branch is the same as homebranch
216 # if not, we want to put a message
217 if ( $biblio->{'homebranch'} ne C4
::Context
->userenv->{'branch'} ) {
218 $template->param( homebranch
=> $biblio->{'homebranch'} );
221 elsif ( !$messages->{'BadBarcode'} ) {
225 $input{barcode
} = $barcode;
228 $returneditems{0} = $barcode;
230 if ( $messages->{'wthdrawn'} ) {
231 $input{withdrawn
} = 1;
232 $input{borrowernumber
} = "Item Cancelled";
233 $riborrowernumber{0} = 'Item Cancelled';
236 $input{borrowernumber
} = " ";
237 $riborrowernumber{0} = ' ';
239 push( @inputloop, \
%input );
242 $template->param( inputloop
=> \
@inputloop );
248 # new op dev : we check if the document must be returned to his homebranch directly,
249 # if the document is transfered, we have warning message .
251 if ( $messages->{'WasTransfered'} ) {
258 if ( $messages->{'NeedsTransfer'} ){
262 itemnumber
=> $issueinformation->{'itemnumber'}
266 if ( $messages->{'Wrongbranch'} ){
272 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
274 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
277 TransferWaitingAt
=> $messages->{'WrongTransfer'},
278 WrongTransferItem
=> $messages->{'WrongTransferItem'},
281 my $reserve = $messages->{'ResFound'};
282 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
283 my ($borr) = GetMemberDetails
( $reserve->{'borrowernumber'}, 0 );
285 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
288 wborfirstname
=> $borr->{'firstname'},
289 wborsurname
=> $borr->{'surname'},
290 wbortitle
=> $borr->{'title'},
291 wborphone
=> $borr->{'phone'},
292 wboremail
=> $borr->{'email'},
293 wboraddress
=> $borr->{'address'},
294 wboraddress2
=> $borr->{'address2'},
295 wborcity
=> $borr->{'city'},
296 wborzip
=> $borr->{'zipcode'},
297 wborrowernumber
=> $reserve->{'borrowernumber'},
298 wborcnum
=> $borr->{'cardnumber'},
299 wtransfertFrom
=> C4
::Context
->userenv->{'branch'},
305 # reserve found and item arrived at the expected branch
307 if ( $messages->{'ResFound'}) {
308 my $reserve = $messages->{'ResFound'};
309 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
310 my ($borr) = GetMemberDetails
( $reserve->{'borrowernumber'}, 0 );
311 if ( $reserve->{'ResFound'} eq "Waiting" ) {
312 if ( C4
::Context
->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
313 $template->param( waiting
=> 1 );
316 $template->param( waiting
=> 0 );
321 name
=> $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
322 borfirstname
=> $borr->{'firstname'},
323 borsurname
=> $borr->{'surname'},
324 bortitle
=> $borr->{'title'},
325 borphone
=> $borr->{'phone'},
326 boremail
=> $borr->{'email'},
327 boraddress
=> $borr->{'address'},
328 boraddress2
=> $borr->{'address2'},
329 borcity
=> $borr->{'city'},
330 borzip
=> $borr->{'zipcode'},
331 borrowernumber
=> $reserve->{'borrowernumber'},
332 borcnum
=> $borr->{'cardnumber'},
333 debarred
=> $borr->{'debarred'},
334 gonenoaddress
=> $borr->{'gonenoaddress'},
335 currentbranch
=> $branches->{C4
::Context
->userenv->{'branch'}}->{'branchname'},
336 itemnumber
=> $reserve->{'itemnumber'},
341 if ( $reserve->{'ResFound'} eq "Reserved" ) {
342 my @da = localtime( time() );
344 sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
345 . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
348 if ( C4
::Context
->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
349 $template->param( intransit
=> 0 );
352 $template->param( intransit
=> 1 );
357 currentbranch
=> $branches->{C4
::Context
->userenv->{'branch'}}->{'branchname'},
359 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
360 destbranch
=> $reserve->{'branchcode'},
361 transfertodo
=> ( C4
::Context
->userenv->{'branch'} eq $reserve->{'branchcode'} ?
0 : 1 ),
363 resbarcode
=> $barcode,
364 today
=> $todaysdate,
365 itemnumber
=> $reserve->{'itemnumber'},
366 borsurname
=> $borr->{'surname'},
367 bortitle
=> $borr->{'title'},
368 borfirstname
=> $borr->{'firstname'},
369 borrowernumber
=> $reserve->{'borrowernumber'},
370 borcnum
=> $borr->{'cardnumber'},
371 borphone
=> $borr->{'phone'},
372 boraddress
=> $borr->{'address'},
373 boraddress2
=> $borr->{'address2'},
374 borsub
=> $borr->{'suburb'},
375 borcity
=> $borr->{'city'},
376 borzip
=> $borr->{'zipcode'},
377 boremail
=> $borr->{'email'},
378 debarred
=> $borr->{'debarred'},
379 gonenoaddress
=> $borr->{'gonenoaddress'},
387 foreach my $code ( keys %$messages ) {
391 my $exit_required_p = 0;
392 if ( $code eq 'BadBarcode' ) {
393 $err{badbarcode
} = 1;
394 $err{msg
} = $messages->{'BadBarcode'};
396 elsif ( $code eq 'NotIssued' ) {
398 $err{msg
} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
400 elsif ( $code eq 'WasLost' ) {
403 elsif ( $code eq 'ResFound' ) {
404 ; # FIXME... anything to do here?
406 elsif ( $code eq 'WasReturned' ) {
407 ; # FIXME... anything to do here?
409 elsif ( $code eq 'WasTransfered' ) {
410 ; # FIXME... anything to do here?
412 elsif ( $code eq 'wthdrawn' ) {
414 $exit_required_p = 1;
416 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
417 if ( $messages->{'IsPermanent'} ne C4
::Context
->userenv->{'branch'} ) {
418 $err{ispermanent
} = 1;
420 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
423 elsif ( $code eq 'WrongTransfer' ) {
424 ; # FIXME... anything to do here?
426 elsif ( $code eq 'WrongTransferItem' ) {
427 ; # FIXME... anything to do here?
429 elsif ( $code eq 'NeedsTransfer' ) {
431 elsif ( $code eq 'Wrongbranch' ) {
435 die "Unknown error code $code"; # XXX
438 push( @errmsgloop, \
%err );
440 last if $exit_required_p;
442 $template->param( errmsgloop
=> \
@errmsgloop );
446 my $flags = $borrower->{'flags'};
449 foreach my $flag ( sort keys %$flags ) {
451 unless ($flagset) { $flagset = 1; }
452 $flaginfo{redfont
} = ( $flags->{$flag}->{'noissues'} );
453 $flaginfo{flag
} = $flag;
454 if ( $flag eq 'CHARGES' ) {
455 $flaginfo{msg
} = $flag;
456 $flaginfo{charges
} = 1;
457 $flaginfo{borrowernumber
} = $borrower->{borrowernumber
};
459 elsif ( $flag eq 'WAITING' ) {
460 $flaginfo{msg
} = $flag;
461 $flaginfo{waiting
} = 1;
463 my $items = $flags->{$flag}->{'itemlist'};
464 foreach my $item (@
$items) {
466 GetBiblioFromItemNumber
( $item->{'itemnumber'});
468 $waitingitem{biblionum
} = $biblio->{'biblionumber'};
469 $waitingitem{barcode
} = $biblio->{'barcode'};
470 $waitingitem{title
} = $biblio->{'title'};
471 $waitingitem{brname
} =
472 $branches->{ $biblio->{'holdingbranch'} }
474 push( @waitingitemloop, \
%waitingitem );
476 $flaginfo{itemloop
} = \
@waitingitemloop;
478 elsif ( $flag eq 'ODUES' ) {
479 my $items = $flags->{$flag}->{'itemlist'};
481 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
485 GetBiblioFromItemNumber
( $item->{'itemnumber'});
487 $overdueitem{duedate
} = format_date
( $item->{'date_due'} );
488 $overdueitem{biblionum
} = $biblio->{'biblionumber'};
489 $overdueitem{barcode
} = $biblio->{'barcode'};
490 $overdueitem{title
} = $biblio->{'title'};
491 $overdueitem{brname
} =
492 $branches->{ $biblio->{'holdingbranch'} }
494 push( @itemloop, \
%overdueitem );
496 $flaginfo{itemloop
} = \
@itemloop;
497 $flaginfo{overdue
} = 1;
500 $flaginfo{other
} = 1;
501 $flaginfo{msg
} = $flags->{$flag}->{'message'};
503 push( @flagloop, \
%flaginfo );
507 flagloop
=> \
@flagloop,
508 riborrowernumber
=> $borrower->{'borrowernumber'},
509 riborcnum
=> $borrower->{'cardnumber'},
510 riborsurname
=> $borrower->{'surname'},
511 ribortitle
=> $borrower->{'title'},
512 riborfirstname
=> $borrower->{'firstname'}
516 #set up so only the last 8 returned items display (make for faster loading pages)
519 foreach ( sort { $a <=> $b } keys %returneditems ) {
522 my $barcode = $returneditems{$_};
523 my $duedate = $riduedate{$_};
527 my @tempdate = split( /-/, $duedate );
528 $ri{year
} = $tempdate[0];
529 $ri{month
} = $tempdate[1];
530 $ri{day
} = $tempdate[2];
531 my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
532 my @datearr = localtime( time() );
535 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
536 . sprintf( "%0.2d", $datearr[3] );
537 $ri{duedate
} = format_date
($duedate);
539 GetMemberDetails
( $riborrowernumber{$_}, 0 );
540 $ri{borrowernumber
} = $borrower->{'borrowernumber'};
541 $ri{borcnum
} = $borrower->{'cardnumber'};
542 $ri{borfirstname
} = $borrower->{'firstname'};
543 $ri{borsurname
} = $borrower->{'surname'};
544 $ri{bortitle
} = $borrower->{'title'};
547 $ri{borrowernumber
} = $riborrowernumber{$_};
551 my $biblio = GetBiblioFromItemNumber
(GetItemnumberFromBarcode
($barcode));
552 # fix up item type for display
553 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
554 $ri{itembiblionumber
} = $biblio->{'biblionumber'};
555 $ri{itemtitle
} = $biblio->{'title'};
556 $ri{itemauthor
} = $biblio->{'author'};
557 $ri{itemtype
} = $biblio->{'itemtype'};
558 $ri{ccode
} = $biblio->{'ccode'};
559 $ri{barcode
} = $barcode;
565 push( @riloop, \
%ri );
567 $template->param( riloop
=> \
@riloop );
570 genbrname
=> $branches->{C4
::Context
->userenv->{'branch'}}->{'branchname'},
571 genprname
=> $printers->{$printer}->{'printername'},
572 branchname
=> $branches->{C4
::Context
->userenv->{'branch'}}->{'branchname'},
574 errmsgloop
=> \
@errmsgloop,
575 exemptfine
=> $exemptfine,
576 overduecharges
=> $overduecharges,
579 # actually print the page!
580 output_html_with_http_headers
$query, $cookie, $template->output;