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 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
251 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
254 TransferWaitingAt => $messages->{'WrongTransfer'},
255 WrongTransferItem => $messages->{'WrongTransferItem'},
258 my $reserve = $messages->{'ResFound'};
259 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
260 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
262 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
265 wborfirstname => $borr->{'firstname'},
266 wborsurname => $borr->{'surname'},
267 wbortitle => $borr->{'title'},
268 wborphone => $borr->{'phone'},
269 wboremail => $borr->{'email'},
270 wboraddress => $borr->{'address'},
271 wboraddress2 => $borr->{'address2'},
272 wborcity => $borr->{'city'},
273 wborzip => $borr->{'zipcode'},
274 wborrowernumber => $reserve->{'borrowernumber'},
275 wborcnum => $borr->{'cardnumber'},
276 wtransfertFrom => C4::Context->userenv->{'branch'},
282 # reserve found and item arrived at the expected branch
284 if ( $messages->{'ResFound'}) {
285 my $reserve = $messages->{'ResFound'};
286 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
287 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
288 if ( $reserve->{'ResFound'} eq "Waiting" ) {
289 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
290 $template->param( waiting => 1 );
293 $template->param( waiting => 0 );
298 name => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
299 borfirstname => $borr->{'firstname'},
300 borsurname => $borr->{'surname'},
301 bortitle => $borr->{'title'},
302 borphone => $borr->{'phone'},
303 boremail => $borr->{'email'},
304 boraddress => $borr->{'address'},
305 boraddress2 => $borr->{'address2'},
306 borcity => $borr->{'city'},
307 borzip => $borr->{'zipcode'},
308 borrowernumber => $reserve->{'borrowernumber'},
309 borcnum => $borr->{'cardnumber'},
310 debarred => $borr->{'debarred'},
311 gonenoaddress => $borr->{'gonenoaddress'},
312 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
313 itemnumber => $reserve->{'itemnumber'},
318 if ( $reserve->{'ResFound'} eq "Reserved" ) {
319 my @da = localtime( time() );
321 sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
322 . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
325 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
326 $template->param( intransit => 0 );
329 $template->param( intransit => 1 );
334 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
336 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
337 destbranch => $reserve->{'branchcode'},
338 transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
340 resbarcode => $barcode,
341 today => $todaysdate,
342 itemnumber => $reserve->{'itemnumber'},
343 borsurname => $borr->{'surname'},
344 bortitle => $borr->{'title'},
345 borfirstname => $borr->{'firstname'},
346 borrowernumber => $reserve->{'borrowernumber'},
347 borcnum => $borr->{'cardnumber'},
348 borphone => $borr->{'phone'},
349 boraddress => $borr->{'address'},
350 boraddress2 => $borr->{'address2'},
351 borsub => $borr->{'suburb'},
352 borcity => $borr->{'city'},
353 borzip => $borr->{'zipcode'},
354 boremail => $borr->{'email'},
355 debarred => $borr->{'debarred'},
356 gonenoaddress => $borr->{'gonenoaddress'},
364 foreach my $code ( keys %$messages ) {
368 my $exit_required_p = 0;
369 if ( $code eq 'BadBarcode' ) {
370 $err{badbarcode} = 1;
371 $err{msg} = $messages->{'BadBarcode'};
373 elsif ( $code eq 'NotIssued' ) {
375 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
377 elsif ( $code eq 'WasLost' ) {
380 elsif ( $code eq 'ResFound' ) {
381 ; # FIXME... anything to do here?
383 elsif ( $code eq 'WasReturned' ) {
384 ; # FIXME... anything to do here?
386 elsif ( $code eq 'WasTransfered' ) {
387 ; # FIXME... anything to do here?
389 elsif ( $code eq 'wthdrawn' ) {
391 $exit_required_p = 1;
393 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
394 if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
395 $err{ispermanent} = 1;
397 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
400 elsif ( $code eq 'WrongTransfer' ) {
401 ; # FIXME... anything to do here?
403 elsif ( $code eq 'WrongTransferItem' ) {
404 ; # FIXME... anything to do here?
406 elsif ( $code eq 'NeedsTransfer' ) {
410 die "Unknown error code $code"; # XXX
413 push( @errmsgloop, \%err );
415 last if $exit_required_p;
417 $template->param( errmsgloop => \@errmsgloop );
421 my $flags = $borrower->{'flags'};
424 foreach my $flag ( sort keys %$flags ) {
426 unless ($flagset) { $flagset = 1; }
427 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
428 $flaginfo{flag} = $flag;
429 if ( $flag eq 'CHARGES' ) {
430 $flaginfo{msg} = $flag;
431 $flaginfo{charges} = 1;
432 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
434 elsif ( $flag eq 'WAITING' ) {
435 $flaginfo{msg} = $flag;
436 $flaginfo{waiting} = 1;
438 my $items = $flags->{$flag}->{'itemlist'};
439 foreach my $item (@$items) {
441 GetBiblioFromItemNumber( $item->{'itemnumber'});
443 $waitingitem{biblionum} = $biblio->{'biblionumber'};
444 $waitingitem{barcode} = $biblio->{'barcode'};
445 $waitingitem{title} = $biblio->{'title'};
446 $waitingitem{brname} =
447 $branches->{ $biblio->{'holdingbranch'} }
449 push( @waitingitemloop, \%waitingitem );
451 $flaginfo{itemloop} = \@waitingitemloop;
453 elsif ( $flag eq 'ODUES' ) {
454 my $items = $flags->{$flag}->{'itemlist'};
456 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
460 GetBiblioFromItemNumber( $item->{'itemnumber'});
462 $overdueitem{duedate} = format_date( $item->{'date_due'} );
463 $overdueitem{biblionum} = $biblio->{'biblionumber'};
464 $overdueitem{barcode} = $biblio->{'barcode'};
465 $overdueitem{title} = $biblio->{'title'};
466 $overdueitem{brname} =
467 $branches->{ $biblio->{'holdingbranch'} }
469 push( @itemloop, \%overdueitem );
471 $flaginfo{itemloop} = \@itemloop;
472 $flaginfo{overdue} = 1;
475 $flaginfo{other} = 1;
476 $flaginfo{msg} = $flags->{$flag}->{'message'};
478 push( @flagloop, \%flaginfo );
482 flagloop => \@flagloop,
483 riborrowernumber => $borrower->{'borrowernumber'},
484 riborcnum => $borrower->{'cardnumber'},
485 riborsurname => $borrower->{'surname'},
486 ribortitle => $borrower->{'title'},
487 riborfirstname => $borrower->{'firstname'}
491 #set up so only the last 8 returned items display (make for faster loading pages)
494 foreach ( sort { $a <=> $b } keys %returneditems ) {
497 my $barcode = $returneditems{$_};
498 my $duedate = $riduedate{$_};
502 my @tempdate = split( /-/, $duedate );
503 $ri{year} = $tempdate[0];
504 $ri{month} = $tempdate[1];
505 $ri{day} = $tempdate[2];
506 my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
507 my @datearr = localtime( time() );
510 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
511 . sprintf( "%0.2d", $datearr[3] );
512 $ri{duedate} = format_date($duedate);
514 GetMemberDetails( $riborrowernumber{$_}, 0 );
515 $ri{borrowernumber} = $borrower->{'borrowernumber'};
516 $ri{borcnum} = $borrower->{'cardnumber'};
517 $ri{borfirstname} = $borrower->{'firstname'};
518 $ri{borsurname} = $borrower->{'surname'};
519 $ri{bortitle} = $borrower->{'title'};
522 $ri{borrowernumber} = $riborrowernumber{$_};
526 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
527 $ri{itembiblionumber} = $biblio->{'biblionumber'};
528 $ri{itemtitle} = $biblio->{'title'};
529 $ri{itemauthor} = $biblio->{'author'};
530 $ri{itemtype} = $biblio->{'itemtype'};
531 $ri{ccode} = $biblio->{'ccode'};
532 $ri{barcode} = $barcode;
538 push( @riloop, \%ri );
540 $template->param( riloop => \@riloop );
543 genbrname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
544 genprname => $printers->{$printer}->{'printername'},
545 branchname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
547 errmsgloop => \@errmsgloop,
548 exemptfine => $exemptfine,
549 overduecharges => $overduecharges,
552 # actually print the page!
553 output_html_with_http_headers $query, $cookie, $template->output;