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'}:"";
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 my $dotransfer = $query->param('dotransfer');
159 # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
160 my $transferitem=$query->param('transferitem');
161 my $tobranch=$query->param('tobranch');
162 ModItemTransfer($transferitem, C4::Context->userenv->{'branch'}, $tobranch);
165 # actually return book and prepare item table.....
168 $barcode = cuecatbarcodedecode($barcode);
172 ( $returned, $messages, $issueinformation, $borrower ) =
173 AddReturn( $barcode, C4::Context->userenv->{'branch'} );
174 # get biblio description
175 my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
177 title => $biblio->{'title'},
178 homebranch => $biblio->{'homebranch'},
179 author => $biblio->{'author'},
180 itembarcode => $biblio->{'barcode'},
181 itemtype => $biblio->{'itemtype'},
182 ccode => $biblio->{'ccode'},
183 itembiblionumber => $biblio->{'biblionumber'},
186 $returneditems{0} = $barcode;
187 $riborrowernumber{0} = $borrower->{'borrowernumber'};
188 $riduedate{0} = $issueinformation->{'date_due'};
192 $input{barcode} = $barcode;
193 $input{duedate} = $riduedate{0};
194 $input{borrowernumber} = $riborrowernumber{0};
195 push( @inputloop, \%input );
197 # check if the branch is the same as homebranch
198 # if not, we want to put a message
199 if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
200 $template->param( homebranch => $biblio->{'homebranch'} );
203 elsif ( !$messages->{'BadBarcode'} ) {
207 $input{barcode} = $barcode;
210 $returneditems{0} = $barcode;
212 if ( $messages->{'wthdrawn'} ) {
213 $input{withdrawn} = 1;
214 $input{borrowernumber} = "Item Cancelled";
215 $riborrowernumber{0} = 'Item Cancelled';
218 $input{borrowernumber} = " ";
219 $riborrowernumber{0} = ' ';
221 push( @inputloop, \%input );
224 $template->param( inputloop => \@inputloop );
230 # new op dev : we check if the document must be returned to his homebranch directly,
231 # if the document is transfered, we have warning message .
233 if ( $messages->{'WasTransfered'} ) {
240 if ( $messages->{'NeedsTransfer'} ){
244 itemnumber => $issueinformation->{'itemnumber'}
248 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
250 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
253 TransferWaitingAt => $messages->{'WrongTransfer'},
254 WrongTransferItem => $messages->{'WrongTransferItem'},
257 my $reserve = $messages->{'ResFound'};
258 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
259 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
261 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
264 wborfirstname => $borr->{'firstname'},
265 wborsurname => $borr->{'surname'},
266 wbortitle => $borr->{'title'},
267 wborphone => $borr->{'phone'},
268 wboremail => $borr->{'email'},
269 wboraddress => $borr->{'address'},
270 wboraddress2 => $borr->{'address2'},
271 wborcity => $borr->{'city'},
272 wborzip => $borr->{'zipcode'},
273 wborrowernumber => $reserve->{'borrowernumber'},
274 wborcnum => $borr->{'cardnumber'},
275 wtransfertFrom => C4::Context->userenv->{'branch'},
281 # reserve found and item arrived at the expected branch
283 if ( $messages->{'ResFound'}) {
284 my $reserve = $messages->{'ResFound'};
285 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
286 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
287 if ( $reserve->{'ResFound'} eq "Waiting" ) {
288 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
289 $template->param( waiting => 1 );
292 $template->param( waiting => 0 );
297 name => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
298 borfirstname => $borr->{'firstname'},
299 borsurname => $borr->{'surname'},
300 bortitle => $borr->{'title'},
301 borphone => $borr->{'phone'},
302 boremail => $borr->{'email'},
303 boraddress => $borr->{'address'},
304 boraddress2 => $borr->{'address2'},
305 borcity => $borr->{'city'},
306 borzip => $borr->{'zipcode'},
307 borrowernumber => $reserve->{'borrowernumber'},
308 borcnum => $borr->{'cardnumber'},
309 debarred => $borr->{'debarred'},
310 gonenoaddress => $borr->{'gonenoaddress'},
311 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
312 itemnumber => $reserve->{'itemnumber'},
317 if ( $reserve->{'ResFound'} eq "Reserved" ) {
318 my @da = localtime( time() );
320 sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
321 . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
324 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
325 $template->param( intransit => 0 );
328 $template->param( intransit => 1 );
333 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
335 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
336 destbranch => $reserve->{'branchcode'},
337 transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
339 resbarcode => $barcode,
340 today => $todaysdate,
341 itemnumber => $reserve->{'itemnumber'},
342 borsurname => $borr->{'surname'},
343 bortitle => $borr->{'title'},
344 borfirstname => $borr->{'firstname'},
345 borrowernumber => $reserve->{'borrowernumber'},
346 borcnum => $borr->{'cardnumber'},
347 borphone => $borr->{'phone'},
348 boraddress => $borr->{'address'},
349 boraddress2 => $borr->{'address2'},
350 borsub => $borr->{'suburb'},
351 borcity => $borr->{'city'},
352 borzip => $borr->{'zipcode'},
353 boremail => $borr->{'email'},
354 debarred => $borr->{'debarred'},
355 gonenoaddress => $borr->{'gonenoaddress'},
363 foreach my $code ( keys %$messages ) {
367 my $exit_required_p = 0;
368 if ( $code eq 'BadBarcode' ) {
369 $err{badbarcode} = 1;
370 $err{msg} = $messages->{'BadBarcode'};
372 elsif ( $code eq 'NotIssued' ) {
374 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
376 elsif ( $code eq 'WasLost' ) {
379 elsif ( $code eq 'ResFound' ) {
380 ; # FIXME... anything to do here?
382 elsif ( $code eq 'WasReturned' ) {
383 ; # FIXME... anything to do here?
385 elsif ( $code eq 'WasTransfered' ) {
386 ; # FIXME... anything to do here?
388 elsif ( $code eq 'wthdrawn' ) {
390 $exit_required_p = 1;
392 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
393 if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
394 $err{ispermanent} = 1;
396 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
399 elsif ( $code eq 'WrongTransfer' ) {
400 ; # FIXME... anything to do here?
402 elsif ( $code eq 'WrongTransferItem' ) {
403 ; # FIXME... anything to do here?
405 elsif ( $code eq 'NeedsTransfer' ) {
409 die "Unknown error code $code"; # XXX
412 push( @errmsgloop, \%err );
414 last if $exit_required_p;
416 $template->param( errmsgloop => \@errmsgloop );
420 my $flags = $borrower->{'flags'};
423 foreach my $flag ( sort keys %$flags ) {
425 unless ($flagset) { $flagset = 1; }
426 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
427 $flaginfo{flag} = $flag;
428 if ( $flag eq 'CHARGES' ) {
429 $flaginfo{msg} = $flag;
430 $flaginfo{charges} = 1;
431 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
433 elsif ( $flag eq 'WAITING' ) {
434 $flaginfo{msg} = $flag;
435 $flaginfo{waiting} = 1;
437 my $items = $flags->{$flag}->{'itemlist'};
438 foreach my $item (@$items) {
440 GetBiblioFromItemNumber( $item->{'itemnumber'});
442 $waitingitem{biblionum} = $biblio->{'biblionumber'};
443 $waitingitem{barcode} = $biblio->{'barcode'};
444 $waitingitem{title} = $biblio->{'title'};
445 $waitingitem{brname} =
446 $branches->{ $biblio->{'holdingbranch'} }
448 push( @waitingitemloop, \%waitingitem );
450 $flaginfo{itemloop} = \@waitingitemloop;
452 elsif ( $flag eq 'ODUES' ) {
453 my $items = $flags->{$flag}->{'itemlist'};
455 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
459 GetBiblioFromItemNumber( $item->{'itemnumber'});
461 $overdueitem{duedate} = format_date( $item->{'date_due'} );
462 $overdueitem{biblionum} = $biblio->{'biblionumber'};
463 $overdueitem{barcode} = $biblio->{'barcode'};
464 $overdueitem{title} = $biblio->{'title'};
465 $overdueitem{brname} =
466 $branches->{ $biblio->{'holdingbranch'} }
468 push( @itemloop, \%overdueitem );
470 $flaginfo{itemloop} = \@itemloop;
471 $flaginfo{overdue} = 1;
474 $flaginfo{other} = 1;
475 $flaginfo{msg} = $flags->{$flag}->{'message'};
477 push( @flagloop, \%flaginfo );
481 flagloop => \@flagloop,
482 riborrowernumber => $borrower->{'borrowernumber'},
483 riborcnum => $borrower->{'cardnumber'},
484 riborsurname => $borrower->{'surname'},
485 ribortitle => $borrower->{'title'},
486 riborfirstname => $borrower->{'firstname'}
490 #set up so only the last 8 returned items display (make for faster loading pages)
493 foreach ( sort { $a <=> $b } keys %returneditems ) {
496 my $barcode = $returneditems{$_};
497 my $duedate = $riduedate{$_};
501 my @tempdate = split( /-/, $duedate );
502 $ri{year} = $tempdate[0];
503 $ri{month} = $tempdate[1];
504 $ri{day} = $tempdate[2];
505 my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
506 my @datearr = localtime( time() );
509 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
510 . sprintf( "%0.2d", $datearr[3] );
511 $ri{duedate} = format_date($duedate);
513 GetMemberDetails( $riborrowernumber{$_}, 0 );
514 $ri{borrowernumber} = $borrower->{'borrowernumber'};
515 $ri{borcnum} = $borrower->{'cardnumber'};
516 $ri{borfirstname} = $borrower->{'firstname'};
517 $ri{borsurname} = $borrower->{'surname'};
518 $ri{bortitle} = $borrower->{'title'};
521 $ri{borrowernumber} = $riborrowernumber{$_};
525 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
526 $ri{itembiblionumber} = $biblio->{'biblionumber'};
527 $ri{itemtitle} = $biblio->{'title'};
528 $ri{itemauthor} = $biblio->{'author'};
529 $ri{itemtype} = $biblio->{'itemtype'};
530 $ri{ccode} = $biblio->{'ccode'};
531 $ri{barcode} = $barcode;
537 push( @riloop, \%ri );
539 $template->param( riloop => \@riloop );
542 genbrname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
543 genprname => $printers->{$printer}->{'printername'},
544 branchname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
546 errmsgloop => \@errmsgloop,
549 # actually print the page!
550 output_html_with_http_headers $query, $cookie, $template->output;