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');
156 my $exemptfine = $query->param('exemptfine');
158 my $dotransfer = $query->param('dotransfer');
160 # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
161 my $transferitem=$query->param('transferitem');
162 my $tobranch=$query->param('tobranch');
163 ModItemTransfer($transferitem, C4::Context->userenv->{'branch'}, $tobranch);
166 # actually return book and prepare item table.....
169 $barcode = cuecatbarcodedecode($barcode);
173 ( $returned, $messages, $issueinformation, $borrower ) =
174 AddReturn( $barcode, C4::Context->userenv->{'branch'}, $exemptfine );
175 # get biblio description
176 my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
178 title => $biblio->{'title'},
179 homebranch => $biblio->{'homebranch'},
180 author => $biblio->{'author'},
181 itembarcode => $biblio->{'barcode'},
182 itemtype => $biblio->{'itemtype'},
183 ccode => $biblio->{'ccode'},
184 itembiblionumber => $biblio->{'biblionumber'},
187 $returneditems{0} = $barcode;
188 $riborrowernumber{0} = $borrower->{'borrowernumber'};
189 $riduedate{0} = $issueinformation->{'date_due'};
193 $input{barcode} = $barcode;
194 $input{duedate} = $riduedate{0};
195 $input{borrowernumber} = $riborrowernumber{0};
196 push( @inputloop, \%input );
198 # check if the branch is the same as homebranch
199 # if not, we want to put a message
200 if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
201 $template->param( homebranch => $biblio->{'homebranch'} );
204 elsif ( !$messages->{'BadBarcode'} ) {
208 $input{barcode} = $barcode;
211 $returneditems{0} = $barcode;
213 if ( $messages->{'wthdrawn'} ) {
214 $input{withdrawn} = 1;
215 $input{borrowernumber} = "Item Cancelled";
216 $riborrowernumber{0} = 'Item Cancelled';
219 $input{borrowernumber} = " ";
220 $riborrowernumber{0} = ' ';
222 push( @inputloop, \%input );
225 $template->param( inputloop => \@inputloop );
231 # new op dev : we check if the document must be returned to his homebranch directly,
232 # if the document is transfered, we have warning message .
234 if ( $messages->{'WasTransfered'} ) {
241 if ( $messages->{'NeedsTransfer'} ){
245 itemnumber => $issueinformation->{'itemnumber'}
249 if ( $messages->{'Wrongbranch'} ){
255 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
257 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
260 TransferWaitingAt => $messages->{'WrongTransfer'},
261 WrongTransferItem => $messages->{'WrongTransferItem'},
264 my $reserve = $messages->{'ResFound'};
265 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
266 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
268 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
271 wborfirstname => $borr->{'firstname'},
272 wborsurname => $borr->{'surname'},
273 wbortitle => $borr->{'title'},
274 wborphone => $borr->{'phone'},
275 wboremail => $borr->{'email'},
276 wboraddress => $borr->{'address'},
277 wboraddress2 => $borr->{'address2'},
278 wborcity => $borr->{'city'},
279 wborzip => $borr->{'zipcode'},
280 wborrowernumber => $reserve->{'borrowernumber'},
281 wborcnum => $borr->{'cardnumber'},
282 wtransfertFrom => C4::Context->userenv->{'branch'},
288 # reserve found and item arrived at the expected branch
290 if ( $messages->{'ResFound'}) {
291 my $reserve = $messages->{'ResFound'};
292 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
293 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
294 if ( $reserve->{'ResFound'} eq "Waiting" ) {
295 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
296 $template->param( waiting => 1 );
299 $template->param( waiting => 0 );
304 name => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
305 borfirstname => $borr->{'firstname'},
306 borsurname => $borr->{'surname'},
307 bortitle => $borr->{'title'},
308 borphone => $borr->{'phone'},
309 boremail => $borr->{'email'},
310 boraddress => $borr->{'address'},
311 boraddress2 => $borr->{'address2'},
312 borcity => $borr->{'city'},
313 borzip => $borr->{'zipcode'},
314 borrowernumber => $reserve->{'borrowernumber'},
315 borcnum => $borr->{'cardnumber'},
316 debarred => $borr->{'debarred'},
317 gonenoaddress => $borr->{'gonenoaddress'},
318 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
319 itemnumber => $reserve->{'itemnumber'},
324 if ( $reserve->{'ResFound'} eq "Reserved" ) {
325 my @da = localtime( time() );
327 sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
328 . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
331 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
332 $template->param( intransit => 0 );
335 $template->param( intransit => 1 );
340 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
342 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
343 destbranch => $reserve->{'branchcode'},
344 transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
346 resbarcode => $barcode,
347 today => $todaysdate,
348 itemnumber => $reserve->{'itemnumber'},
349 borsurname => $borr->{'surname'},
350 bortitle => $borr->{'title'},
351 borfirstname => $borr->{'firstname'},
352 borrowernumber => $reserve->{'borrowernumber'},
353 borcnum => $borr->{'cardnumber'},
354 borphone => $borr->{'phone'},
355 boraddress => $borr->{'address'},
356 boraddress2 => $borr->{'address2'},
357 borsub => $borr->{'suburb'},
358 borcity => $borr->{'city'},
359 borzip => $borr->{'zipcode'},
360 boremail => $borr->{'email'},
361 debarred => $borr->{'debarred'},
362 gonenoaddress => $borr->{'gonenoaddress'},
370 foreach my $code ( keys %$messages ) {
374 my $exit_required_p = 0;
375 if ( $code eq 'BadBarcode' ) {
376 $err{badbarcode} = 1;
377 $err{msg} = $messages->{'BadBarcode'};
379 elsif ( $code eq 'NotIssued' ) {
381 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
383 elsif ( $code eq 'WasLost' ) {
386 elsif ( $code eq 'ResFound' ) {
387 ; # FIXME... anything to do here?
389 elsif ( $code eq 'WasReturned' ) {
390 ; # FIXME... anything to do here?
392 elsif ( $code eq 'WasTransfered' ) {
393 ; # FIXME... anything to do here?
395 elsif ( $code eq 'wthdrawn' ) {
397 $exit_required_p = 1;
399 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
400 if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
401 $err{ispermanent} = 1;
403 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
406 elsif ( $code eq 'WrongTransfer' ) {
407 ; # FIXME... anything to do here?
409 elsif ( $code eq 'WrongTransferItem' ) {
410 ; # FIXME... anything to do here?
412 elsif ( $code eq 'NeedsTransfer' ) {
414 elsif ( $code eq 'Wrongbranch' ) {
418 die "Unknown error code $code"; # XXX
421 push( @errmsgloop, \%err );
423 last if $exit_required_p;
425 $template->param( errmsgloop => \@errmsgloop );
429 my $flags = $borrower->{'flags'};
432 foreach my $flag ( sort keys %$flags ) {
434 unless ($flagset) { $flagset = 1; }
435 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
436 $flaginfo{flag} = $flag;
437 if ( $flag eq 'CHARGES' ) {
438 $flaginfo{msg} = $flag;
439 $flaginfo{charges} = 1;
440 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
442 elsif ( $flag eq 'WAITING' ) {
443 $flaginfo{msg} = $flag;
444 $flaginfo{waiting} = 1;
446 my $items = $flags->{$flag}->{'itemlist'};
447 foreach my $item (@$items) {
449 GetBiblioFromItemNumber( $item->{'itemnumber'});
451 $waitingitem{biblionum} = $biblio->{'biblionumber'};
452 $waitingitem{barcode} = $biblio->{'barcode'};
453 $waitingitem{title} = $biblio->{'title'};
454 $waitingitem{brname} =
455 $branches->{ $biblio->{'holdingbranch'} }
457 push( @waitingitemloop, \%waitingitem );
459 $flaginfo{itemloop} = \@waitingitemloop;
461 elsif ( $flag eq 'ODUES' ) {
462 my $items = $flags->{$flag}->{'itemlist'};
464 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
468 GetBiblioFromItemNumber( $item->{'itemnumber'});
470 $overdueitem{duedate} = format_date( $item->{'date_due'} );
471 $overdueitem{biblionum} = $biblio->{'biblionumber'};
472 $overdueitem{barcode} = $biblio->{'barcode'};
473 $overdueitem{title} = $biblio->{'title'};
474 $overdueitem{brname} =
475 $branches->{ $biblio->{'holdingbranch'} }
477 push( @itemloop, \%overdueitem );
479 $flaginfo{itemloop} = \@itemloop;
480 $flaginfo{overdue} = 1;
483 $flaginfo{other} = 1;
484 $flaginfo{msg} = $flags->{$flag}->{'message'};
486 push( @flagloop, \%flaginfo );
490 flagloop => \@flagloop,
491 riborrowernumber => $borrower->{'borrowernumber'},
492 riborcnum => $borrower->{'cardnumber'},
493 riborsurname => $borrower->{'surname'},
494 ribortitle => $borrower->{'title'},
495 riborfirstname => $borrower->{'firstname'}
499 #set up so only the last 8 returned items display (make for faster loading pages)
502 foreach ( sort { $a <=> $b } keys %returneditems ) {
505 my $barcode = $returneditems{$_};
506 my $duedate = $riduedate{$_};
510 my @tempdate = split( /-/, $duedate );
511 $ri{year} = $tempdate[0];
512 $ri{month} = $tempdate[1];
513 $ri{day} = $tempdate[2];
514 my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
515 my @datearr = localtime( time() );
518 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
519 . sprintf( "%0.2d", $datearr[3] );
520 $ri{duedate} = format_date($duedate);
522 GetMemberDetails( $riborrowernumber{$_}, 0 );
523 $ri{borrowernumber} = $borrower->{'borrowernumber'};
524 $ri{borcnum} = $borrower->{'cardnumber'};
525 $ri{borfirstname} = $borrower->{'firstname'};
526 $ri{borsurname} = $borrower->{'surname'};
527 $ri{bortitle} = $borrower->{'title'};
530 $ri{borrowernumber} = $riborrowernumber{$_};
534 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
535 $ri{itembiblionumber} = $biblio->{'biblionumber'};
536 $ri{itemtitle} = $biblio->{'title'};
537 $ri{itemauthor} = $biblio->{'author'};
538 $ri{itemtype} = $biblio->{'itemtype'};
539 $ri{ccode} = $biblio->{'ccode'};
540 $ri{barcode} = $barcode;
546 push( @riloop, \%ri );
548 $template->param( riloop => \@riloop );
551 genbrname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
552 genprname => $printers->{$printer}->{'printername'},
553 branchname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
555 errmsgloop => \@errmsgloop,
556 exemptfine => $exemptfine,
557 overduecharges => $overduecharges,
560 # actually print the page!
561 output_html_with_http_headers $query, $cookie, $template->output;