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
29 # use warnings; # FIXME
33 use C4
::Auth qw
/:DEFAULT get_session/;
36 use C4
::Dates qw
/format_date/;
37 use Date
::Calc qw
/Add_Delta_Days/;
44 use C4
::Branch
; # GetBranchName
45 use C4
::Koha
; # FIXME : is it still useful ?
49 if (!C4
::Context
->userenv){
50 my $sessionID = $query->cookie("CGISESSID");
51 my $session = get_session
($sessionID);
52 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
53 # no branch set we can't return
54 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
60 my ( $template, $librarian, $cookie ) = get_template_and_user
(
62 template_name
=> "circ/returns.tmpl",
66 flagsrequired
=> { circulate
=> "circulate_remaining_permissions" },
72 my $branches = GetBranches
();
73 my $printers = GetPrinters
();
75 #my $branch = C4::Context->userenv?C4::Context->userenv->{'branch'}:"";
76 my $printer = C4
::Context
->userenv?C4
::Context
->userenv->{'branchprinter'}:"";
77 my $overduecharges = (C4
::Context
->preference('finesMode') && C4
::Context
->preference('finesMode') ne 'off');
79 # Some code to handle the error if there is no branch or printer setting.....
82 # Set up the item stack ....
87 foreach ( $query->param ) {
88 (next) unless (/ri-(\d*)/);
91 (next) if ( $counter > 20 );
92 my $barcode = $query->param("ri-$counter");
93 my $duedate = $query->param("dd-$counter");
94 my $borrowernumber = $query->param("bn-$counter");
98 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
100 ######################
101 #Are these lines still useful ?
102 $returneditems{$counter} = $barcode;
103 $riduedate{$counter} = $duedate;
104 $riborrowernumber{$counter} = $borrowernumber;
106 #######################
107 $input{counter
} = $counter;
108 $input{barcode
} = $barcode;
109 $input{duedate
} = $duedate;
110 $input{borrowernumber
} = $borrowernumber;
111 push( @inputloop, \
%input );
115 # Deal with the requests....
117 if ($query->param('WT-itemNumber')){
118 updateWrongTransfer
($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
121 if ( $query->param('resbarcode') ) {
122 my $item = $query->param('itemnumber');
123 my $borrowernumber = $query->param('borrowernumber');
124 my $resbarcode = $query->param('resbarcode');
125 my $diffBranchReturned = $query->param('diffBranch');
127 my $iteminfo = GetBiblioFromItemNumber
($item);
128 # fix up item type for display
129 $iteminfo->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$iteminfo->{'itype'} : $iteminfo->{'itemtype'};
132 # addin in ModReserveAffect the possibility to check if the document is expected in this library or not,
133 # if not we send a value in reserve waiting for not implementting waiting status
134 if (C4
::Context
->userenv->{'branch'} ne $diffBranchReturned) {
135 $diffBranchSend = $diffBranchReturned;
138 $diffBranchSend = undef;
140 ModReserveAffect
( $item, $borrowernumber,$diffBranchSend);
141 # check if we have other reservs for this document, if we have a return send the message of transfer
142 my ( $messages, $nextreservinfo ) = GetOtherReserves
($item);
144 my $branchname = GetBranchName
( $messages->{'transfert'} );
145 my ($borr) = GetMemberDetails
( $nextreservinfo, 0 );
146 my $borcnum = $borr->{'cardnumber'};
148 $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
149 my $slip = $query->param('resslip');
152 if ( $messages->{'transfert'} ) {
154 itemtitle
=> $iteminfo->{'title'},
155 itembiblionumber
=> $iteminfo->{'biblionumber'},
156 iteminfo
=> $iteminfo->{'author'},
157 tobranchname
=> $branchname,
159 borrowernumber
=> $borrowernumber,
161 borfirstname
=> $borr->{'firstname'},
162 borsurname
=> $borr->{'surname'},
171 my $issueinformation;
172 my $barcode = $query->param('barcode');
174 # $barcode =~ s/\s*//g; - use barcodedecode for this; whitespace is not invalid.
175 my $exemptfine = $query->param('exemptfine');
176 my $dropboxmode= $query->param('dropboxmode');
177 my $calendar = C4
::Calendar
->new( branchcode
=> C4
::Context
->userenv->{'branch'} );
178 #dropbox: get last open day (today - 1)
179 my $today = C4
::Dates
->new();
180 my $today_iso = $today->output('iso');
181 my $dropboxdate = $calendar->addDate($today, -1 );
182 my $dotransfer = $query->param('dotransfer');
184 # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
185 my $transferitem=$query->param('transferitem');
186 my $tobranch=$query->param('tobranch');
187 ModItemTransfer
($transferitem, C4
::Context
->userenv->{'branch'}, $tobranch);
190 # actually return book and prepare item table.....
192 $barcode = barcodedecode
($barcode) if(C4
::Context
->preference('itemBarcodeInputFilter'));
196 ( $returned, $messages, $issueinformation, $borrower ) =
197 AddReturn
( $barcode, C4
::Context
->userenv->{'branch'}, $exemptfine, $dropboxmode);
198 # get biblio description
199 my $biblio = GetBiblioFromItemNumber
($issueinformation->{'itemnumber'});
200 # fix up item type for display
201 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
204 title
=> $biblio->{'title'},
205 homebranch
=> $biblio->{'homebranch'},
206 author
=> $biblio->{'author'},
207 itembarcode
=> $biblio->{'barcode'},
208 itemtype
=> $biblio->{'itemtype'},
209 ccode
=> $biblio->{'ccode'},
210 itembiblionumber
=> $biblio->{'biblionumber'},
214 $returneditems{0} = $barcode;
215 $riborrowernumber{0} = $borrower->{'borrowernumber'};
216 $riduedate{0} = $issueinformation->{'date_due'};
220 $input{barcode
} = $barcode;
221 $input{borrowernumber
} = $borrower->{'borrowernumber'};
222 $input{duedate
} = $issueinformation->{'date_due'};
223 $input{return_overdue
} = 1 if ($issueinformation->{'date_due'} lt $today->output('iso'));
224 push( @inputloop, \
%input );
226 # check if the branch is the same as homebranch
227 # if not, we want to put a message
228 if ( $biblio->{'homebranch'} ne C4
::Context
->userenv->{'branch'} ) {
229 $template->param( homebranch
=> $biblio->{'homebranch'} );
232 elsif ( !$messages->{'BadBarcode'} ) {
236 $input{barcode
} = $barcode;
239 $returneditems{0} = $barcode;
241 if ( $messages->{'wthdrawn'} ) {
242 $input{withdrawn
} = 1;
243 $input{borrowernumber
} = 'Item Cancelled';
244 $riborrowernumber{0} = 'Item Cancelled';
247 $input{borrowernumber
} = ' ';
248 $riborrowernumber{0} = ' ';
250 push( @inputloop, \
%input );
253 $template->param( inputloop
=> \
@inputloop );
259 # new op dev : we check if the document must be returned to his homebranch directly,
260 # if the document is transfered, we have warning message .
262 if ( $messages->{'WasTransfered'} ) {
269 if ( $messages->{'NeedsTransfer'} ){
273 itemnumber
=> $issueinformation->{'itemnumber'}
277 if ( $messages->{'Wrongbranch'} ){
283 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
285 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
288 TransferWaitingAt
=> $messages->{'WrongTransfer'},
289 WrongTransferItem
=> $messages->{'WrongTransferItem'},
292 my $reserve = $messages->{'ResFound'};
293 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
294 my ($borr) = GetMemberDetails
( $reserve->{'borrowernumber'}, 0 );
296 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
299 wborfirstname
=> $borr->{'firstname'},
300 wborsurname
=> $borr->{'surname'},
301 wbortitle
=> $borr->{'title'},
302 wborphone
=> $borr->{'phone'},
303 wboremail
=> $borr->{'email'},
304 wboraddress
=> $borr->{'address'},
305 wboraddress2
=> $borr->{'address2'},
306 wborcity
=> $borr->{'city'},
307 wborzip
=> $borr->{'zipcode'},
308 wborrowernumber
=> $reserve->{'borrowernumber'},
309 wborcnum
=> $borr->{'cardnumber'},
310 wtransfertFrom
=> C4
::Context
->userenv->{'branch'},
316 # reserve found and item arrived at the expected branch
318 if ( $messages->{'ResFound'}) {
319 my $reserve = $messages->{'ResFound'};
320 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
321 my ($borr) = GetMemberDetails
( $reserve->{'borrowernumber'}, 0 );
322 if ( $reserve->{'ResFound'} eq "Waiting" ) {
323 if ( C4
::Context
->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
324 $template->param( waiting
=> 1 );
327 $template->param( waiting
=> 0 );
332 name
=> $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
333 borfirstname
=> $borr->{'firstname'},
334 borsurname
=> $borr->{'surname'},
335 bortitle
=> $borr->{'title'},
336 borphone
=> $borr->{'phone'},
337 boremail
=> $borr->{'email'},
338 boraddress
=> $borr->{'address'},
339 boraddress2
=> $borr->{'address2'},
340 borcity
=> $borr->{'city'},
341 borzip
=> $borr->{'zipcode'},
342 borrowernumber
=> $reserve->{'borrowernumber'},
343 borcnum
=> $borr->{'cardnumber'},
344 debarred
=> $borr->{'debarred'},
345 gonenoaddress
=> $borr->{'gonenoaddress'},
346 currentbranch
=> $branches->{C4
::Context
->userenv->{'branch'}}->{'branchname'},
347 itemnumber
=> $reserve->{'itemnumber'},
350 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
351 destbranch
=> $reserve->{'branchcode'},
355 if ( $reserve->{'ResFound'} eq "Reserved" ) {
356 if ( C4
::Context
->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
357 $template->param( intransit
=> 0 );
360 $template->param( intransit
=> 1 );
365 currentbranch
=> $branches->{C4
::Context
->userenv->{'branch'}}->{'branchname'},
367 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
368 destbranch
=> $reserve->{'branchcode'},
369 transfertodo
=> ( C4
::Context
->userenv->{'branch'} eq $reserve->{'branchcode'} ?
0 : 1 ),
371 resbarcode
=> $barcode,
372 # today => $todaysdate,
373 itemnumber
=> $reserve->{'itemnumber'},
374 borsurname
=> $borr->{'surname'},
375 bortitle
=> $borr->{'title'},
376 borfirstname
=> $borr->{'firstname'},
377 borrowernumber
=> $reserve->{'borrowernumber'},
378 borcnum
=> $borr->{'cardnumber'},
379 borphone
=> $borr->{'phone'},
380 boraddress
=> $borr->{'address'},
381 boraddress2
=> $borr->{'address2'},
382 borsub
=> $borr->{'suburb'},
383 borcity
=> $borr->{'city'},
384 borzip
=> $borr->{'zipcode'},
385 boremail
=> $borr->{'email'},
386 debarred
=> $borr->{'debarred'},
387 gonenoaddress
=> $borr->{'gonenoaddress'},
395 foreach my $code ( keys %$messages ) {
399 my $exit_required_p = 0;
400 if ( $code eq 'BadBarcode' ) {
401 $err{badbarcode
} = 1;
402 $err{msg
} = $messages->{'BadBarcode'};
404 elsif ( $code eq 'NotIssued' ) {
406 $err{msg
} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
408 elsif ( $code eq 'WasLost' ) {
411 elsif ( $code eq 'ResFound' ) {
412 ; # FIXME... anything to do here?
414 elsif ( $code eq 'WasReturned' ) {
415 ; # FIXME... anything to do here?
417 elsif ( $code eq 'WasTransfered' ) {
418 ; # FIXME... anything to do here?
420 elsif ( $code eq 'wthdrawn' ) {
422 $exit_required_p = 1;
424 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
425 if ( $messages->{'IsPermanent'} ne C4
::Context
->userenv->{'branch'} ) {
426 $err{ispermanent
} = 1;
428 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
431 elsif ( $code eq 'WrongTransfer' ) {
432 ; # FIXME... anything to do here?
434 elsif ( $code eq 'WrongTransferItem' ) {
435 ; # FIXME... anything to do here?
437 elsif ( $code eq 'NeedsTransfer' ) {
439 elsif ( $code eq 'Wrongbranch' ) {
443 die "Unknown error code $code"; # XXX
446 push( @errmsgloop, \
%err );
448 last if $exit_required_p;
450 $template->param( errmsgloop
=> \
@errmsgloop );
454 my $flags = $borrower->{'flags'};
457 foreach my $flag ( sort keys %$flags ) {
459 unless ($flagset) { $flagset = 1; }
460 $flaginfo{redfont
} = ( $flags->{$flag}->{'noissues'} );
461 $flaginfo{flag
} = $flag;
462 if ( $flag eq 'CHARGES' ) {
463 $flaginfo{msg
} = $flag;
464 $flaginfo{charges
} = 1;
465 $flaginfo{chargeamount
} = $flags->{$flag}->{amount
};
466 $flaginfo{borrowernumber
} = $borrower->{borrowernumber
};
468 elsif ( $flag eq 'WAITING' ) {
469 $flaginfo{msg
} = $flag;
470 $flaginfo{waiting
} = 1;
472 my $items = $flags->{$flag}->{'itemlist'};
473 foreach my $item (@
$items) {
475 GetBiblioFromItemNumber
( $item->{'itemnumber'});
477 $waitingitem{biblionum
} = $biblio->{'biblionumber'};
478 $waitingitem{barcode
} = $biblio->{'barcode'};
479 $waitingitem{title
} = $biblio->{'title'};
480 $waitingitem{brname
} =
481 $branches->{ $biblio->{'holdingbranch'} }
483 push( @waitingitemloop, \
%waitingitem );
485 $flaginfo{itemloop
} = \
@waitingitemloop;
487 elsif ( $flag eq 'ODUES' ) {
488 my $items = $flags->{$flag}->{'itemlist'};
490 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
493 my $biblio = GetBiblioFromItemNumber
( $item->{'itemnumber'});
495 $overdueitem{duedate
} = format_date
( $item->{'date_due'} );
496 $overdueitem{biblionum
} = $biblio->{'biblionumber'};
497 $overdueitem{barcode
} = $biblio->{'barcode'};
498 $overdueitem{title
} = $biblio->{'title'};
499 $overdueitem{brname
} =
500 $branches->{ $biblio->{'holdingbranch'}} ->{'branchname'};
501 push( @itemloop, \
%overdueitem );
503 $flaginfo{itemloop
} = \
@itemloop;
504 $flaginfo{overdue
} = 1;
507 $flaginfo{other
} = 1;
508 $flaginfo{msg
} = $flags->{$flag}->{'message'};
510 push( @flagloop, \
%flaginfo );
514 flagloop
=> \
@flagloop,
515 riborrowernumber
=> $borrower->{'borrowernumber'},
516 riborcnum
=> $borrower->{'cardnumber'},
517 riborsurname
=> $borrower->{'surname'},
518 ribortitle
=> $borrower->{'title'},
519 riborfirstname
=> $borrower->{'firstname'}
523 #set up so only the last 8 returned items display (make for faster loading pages)
526 foreach ( sort { $a <=> $b } keys %returneditems ) {
528 if ( $count++ < 8 ) {
529 my $barcode = $returneditems{$_};
530 my $duedate = $riduedate{$_};
534 my @tempdate = split( /-/, $duedate );
535 $ri{year
} = $tempdate[0];
536 $ri{month
} = $tempdate[1];
537 $ri{day
} = $tempdate[2];
538 $ri{duedate
} = format_date
($duedate);
540 GetMemberDetails
( $riborrowernumber{$_}, 0 );
541 $ri{return_overdue
} = 1 if ($duedate lt $today->output('iso'));
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'};
547 $ri{bornote
} = $borrower->{'borrowernotes'};
548 $ri{borcategorycode
} = $borrower->{'categorycode'};
551 $ri{borrowernumber
} = $riborrowernumber{$_};
555 my $biblio = GetBiblioFromItemNumber
(GetItemnumberFromBarcode
($barcode));
556 # fix up item type for display
557 $biblio->{'itemtype'} = C4
::Context
->preference('item-level_itypes') ?
$biblio->{'itype'} : $biblio->{'itemtype'};
558 $ri{itembiblionumber
} = $biblio->{'biblionumber'};
559 $ri{itemtitle
} = $biblio->{'title'};
560 $ri{itemauthor
} = $biblio->{'author'};
561 $ri{itemtype
} = $biblio->{'itemtype'};
562 $ri{itemnote
} = $biblio->{'itemnotes'};
563 $ri{ccode
} = $biblio->{'ccode'};
564 $ri{itemnumber
} = $biblio->{'itemnumber'};
565 $ri{barcode
} = $barcode;
570 push( @riloop, \
%ri );
572 $template->param( riloop
=> \
@riloop );
575 genbrname
=> $branches->{C4
::Context
->userenv->{'branch'}}->{'branchname'},
576 genprname
=> $printers->{$printer}->{'printername'},
577 branchname
=> $branches->{C4
::Context
->userenv->{'branch'}}->{'branchname'},
579 errmsgloop
=> \
@errmsgloop,
580 exemptfine
=> $exemptfine,
581 dropboxmode
=> $dropboxmode,
582 dropboxdate
=> $dropboxdate->output(),
583 overduecharges
=> $overduecharges,
586 # actually print the page!
587 output_html_with_http_headers
$query, $cookie, $template->output;