3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
22 script to execute returns of books
24 written 11/3/2002 by Finlay
30 use C4::Circulation::Circ2;
36 use C4::Interface::CGI::Output;
37 use C4::Branch; # GetBranchName
38 use C4::Koha; # FIXME : is it still useful ?
44 my ( $template, $librarian, $cookie ) = get_template_and_user(
46 template_name => "circ/returns.tmpl",
50 flagsrequired => { circulate => 1 },
58 my $branches = GetBranches();
59 my $printers = GetPrinters( \%env );
61 #my $branch = C4::Context->userenv?C4::Context->userenv->{'branch'}:"";
62 my $printer = C4::Context->userenv?C4::Context->userenv->{'branchprinter'}:"";
65 # Some code to handle the error if there is no branch or printer setting.....
68 #$env{'branchcode'} = $branch;
69 $env{'printer'} = $printer;
70 $env{'queue'} = $printer;
73 # Set up the item stack ....
78 foreach ( $query->param ) {
79 (next) unless (/ri-(\d*)/);
82 (next) if ( $counter > 20 );
83 my $barcode = $query->param("ri-$counter");
84 my $duedate = $query->param("dd-$counter");
85 my $borrowernumber = $query->param("bn-$counter");
89 $barcode = cuecatbarcodedecode($barcode);
91 ######################
92 #Are these lines still useful ?
93 $returneditems{$counter} = $barcode;
94 $riduedate{$counter} = $duedate;
95 $riborrowernumber{$counter} = $borrowernumber;
97 #######################
98 $input{counter} = $counter;
99 $input{barcode} = $barcode;
100 $input{duedate} = $duedate;
101 $input{borrowernumber} = $borrowernumber;
102 push( @inputloop, \%input );
106 # Deal with the requests....
108 if ($query->param('WT-itemNumber')){
109 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
113 if ( $query->param('resbarcode') ) {
114 my $item = $query->param('itemnumber');
115 my $borrowernumber = $query->param('borrowernumber');
116 my $resbarcode = $query->param('resbarcode');
117 my $diffBranchReturned = $query->param('diffBranch');
119 my $iteminfo = getiteminformation($item);
122 # addin in ReserveWaiting the possibility to check if the document is expected in this library or not,
123 # if not we send a value in reserve waiting for not implementting waiting status
124 if ($diffBranchReturned) {
125 $diffBranchSend = $diffBranchReturned;
128 $diffBranchSend = undef;
131 my $tobranchcd = ReserveWaiting( $item, $borrowernumber,$diffBranchSend);
132 # check if we have other reservs for this document, if we have a return send the message of transfer
133 my ( $messages, $nextreservinfo ) = OtherReserves($item);
135 my $branchname = GetBranchName( $messages->{'transfert'} );
136 my ($borr) = getpatroninformation( \%env, $nextreservinfo, 0 );
137 my $borcnum = $borr->{'cardnumber'};
139 $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
140 my $slip = $query->param('resslip');
143 if ( $messages->{'transfert'} ) {
145 itemtitle => $iteminfo->{'title'},
146 iteminfo => $iteminfo->{'author'},
147 tobranchname => $branchname,
149 borrowernumber => $borrowernumber,
151 borfirstname => $borr->{'firstname'},
152 borsurname => $borr->{'surname'},
162 my $barcode = $query->param('barcode');
164 # actually return book and prepare item table.....
167 $barcode = cuecatbarcodedecode($barcode);
168 ( $returned, $messages, $iteminformation, $borrower ) =
169 returnbook( $barcode, C4::Context->userenv->{'branch'} );
171 $returneditems{0} = $barcode;
172 $riborrowernumber{0} = $borrower->{'borrowernumber'};
173 $riduedate{0} = $iteminformation->{'date_due'};
177 $input{barcode} = $barcode;
178 $input{duedate} = $riduedate{0};
179 $input{borrowernumber} = $riborrowernumber{0};
180 push( @inputloop, \%input );
182 # check if the branch is the same as homebranch
183 # if not, we want to put a message
184 if ( $iteminformation->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
185 $template->param( homebranch => $iteminformation->{'homebranch'} );
188 elsif ( !$messages->{'BadBarcode'} ) {
192 $input{barcode} = $barcode;
195 $returneditems{0} = $barcode;
197 if ( $messages->{'wthdrawn'} ) {
198 $input{withdrawn} = 1;
199 $input{borrowernumber} = "Item Cancelled";
200 $riborrowernumber{0} = 'Item Cancelled';
203 $input{borrowernumber} = " ";
204 $riborrowernumber{0} = ' ';
206 push( @inputloop, \%input );
209 returned => $returned,
210 itemtitle => $iteminformation->{'title'},
212 # itembc => $iteminformation->{'barcode'},
213 # itemdatedue => $iteminformation->{'datedue'},
214 itemauthor => $iteminformation->{'author'}
217 $template->param( inputloop => \@inputloop );
223 # new op dev : we check if the document must be returned to his homebranch directly,
224 # if the document is transfered, we have warning message .
226 if ( $messages->{'WasTransfered'} ) {
228 my ($iteminfo) = getiteminformation( 0, $barcode );
234 $branches->{ $iteminfo->{'homebranch'} }->{'branchname'}
239 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
241 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
244 TransferWaitingAt => $messages->{'WrongTransfer'},
245 WrongTransferItem => $messages->{'WrongTransferItem'},
248 my $res = $messages->{'ResFound'};
249 my $branchname = $branches->{ $res->{'branchcode'} }->{'branchname'};
250 my ($borr) = getpatroninformation( \%env, $res->{'borrowernumber'}, 0 );
252 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
253 my ($iteminfo) = getiteminformation( 0, $barcode );
257 wborfirstname => $borr->{'firstname'},
258 wborsurname => $borr->{'surname'},
259 wbortitle => $borr->{'title'},
260 wborphone => $borr->{'phone'},
261 wboremail => $borr->{'emailaddress'},
262 wborstraddress => $borr->{'streetaddress'},
263 wborcity => $borr->{'city'},
264 wborzip => $borr->{'zipcode'},
265 wborrowernumber => $res->{'borrowernumber'},
266 wborcnum => $borr->{'cardnumber'},
267 witemtitle => $iteminfo->{'title'},
268 witemauthor => $iteminfo->{'author'},
269 witembarcode => $iteminfo->{'barcode'},
270 witemtype => $iteminfo->{'itemtype'},
271 wccode => $iteminfo->{'ccode'},
272 witembiblionumber => $iteminfo->{'biblionumber'},
273 wtransfertFrom => C4::Context->userenv->{'branch'},
278 if ( $messages->{'ResFound'} and not $messages->{'WrongTransfer'}) {
279 my $res = $messages->{'ResFound'};
280 my $branchname = $branches->{ $res->{'branchcode'} }->{'branchname'};
281 my ($borr) = getpatroninformation( \%env, $res->{'borrowernumber'}, 0 );
283 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
284 my ($iteminfo) = getiteminformation( 0, $barcode );
286 if ( $res->{'ResFound'} eq "Waiting" ) {
287 if ( C4::Context->userenv->{'branch'} eq $res->{'branchcode'} ) {
288 $template->param( waiting => 1 );
291 $template->param( waiting => 0 );
297 borfirstname => $borr->{'firstname'},
298 borsurname => $borr->{'surname'},
299 bortitle => $borr->{'title'},
300 borphone => $borr->{'phone'},
301 boremail => $borr->{'emailaddress'},
302 borstraddress => $borr->{'streetaddress'},
303 borcity => $borr->{'city'},
304 borzip => $borr->{'zipcode'},
305 borrowernumber => $res->{'borrowernumber'},
306 borcnum => $borr->{'cardnumber'},
307 debarred => $borr->{'debarred'},
308 gonenoaddress => $borr->{'gonenoaddress'},
309 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
310 itemnumber => $res->{'itemnumber'},
311 itemtitle => $iteminfo->{'title'},
312 itemauthor => $iteminfo->{'author'},
313 itembarcode => $iteminfo->{'barcode'},
314 itemtype => $iteminfo->{'itemtype'},
315 ccode => $iteminfo->{'ccode'},
316 itembiblionumber => $iteminfo->{'biblionumber'}
320 if ( $res->{'ResFound'} eq "Reserved" ) {
321 my @da = localtime( time() );
323 sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
324 . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
327 if ( C4::Context->userenv->{'branch'} eq $res->{'branchcode'} ) {
328 $template->param( intransit => 0 );
331 $template->param( intransit => 1 );
336 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
339 $branches->{ $res->{'branchcode'} }->{'branchname'},
340 destbranch => $res->{'branchcode'},
341 transfertodo => ( C4::Context->userenv->{'branch'} eq $res->{'branchcode'} ? 0 : 1 ),
343 today => $todaysdate,
344 itemnumber => $res->{'itemnumber'},
345 itemtitle => $iteminfo->{'title'},
346 itemauthor => $iteminfo->{'author'},
347 itembarcode => $iteminfo->{'barcode'},
348 itemtype => $iteminfo->{'itemtype'},
349 ccode => $iteminfo->{'ccode'},
350 itembiblionumber => $iteminfo->{'biblionumber'},
351 borsurname => $borr->{'surname'},
352 bortitle => $borr->{'title'},
353 borfirstname => $borr->{'firstname'},
354 borrowernumber => $res->{'borrowernumber'},
355 borcnum => $borr->{'cardnumber'},
356 borphone => $borr->{'phone'},
357 borstraddress => $borr->{'streetaddress'},
358 borsub => $borr->{'suburb'},
359 borcity => $borr->{'city'},
360 borzip => $borr->{'zipcode'},
361 boremail => $borr->{'emailaddress'},
362 debarred => $borr->{'debarred'},
363 gonenoaddress => $borr->{'gonenoaddress'},
371 foreach my $code ( keys %$messages ) {
375 my $exit_required_p = 0;
376 if ( $code eq 'BadBarcode' ) {
377 $err{badbarcode} = 1;
378 $err{msg} = $messages->{'BadBarcode'};
380 elsif ( $code eq 'NotIssued' ) {
382 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
384 elsif ( $code eq 'WasLost' ) {
387 elsif ( $code eq 'ResFound' ) {
388 ; # FIXME... anything to do here?
390 elsif ( $code eq 'WasReturned' ) {
391 ; # FIXME... anything to do here?
393 elsif ( $code eq 'WasTransfered' ) {
394 ; # FIXME... anything to do here?
396 elsif ( $code eq 'wthdrawn' ) {
398 $exit_required_p = 1;
400 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
401 if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
402 $err{ispermanent} = 1;
404 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
407 elsif ( $code eq 'WrongTransfer' ) {
408 ; # FIXME... anything to do here?
410 elsif ( $code eq 'WrongTransferItem' ) {
411 ; # FIXME... anything to do here?
414 die "Unknown error code $code"; # XXX
417 push( @errmsgloop, \%err );
419 last if $exit_required_p;
421 $template->param( errmsgloop => \@errmsgloop );
425 my $flags = $borrower->{'flags'};
428 foreach my $flag ( sort keys %$flags ) {
430 unless ($flagset) { $flagset = 1; }
431 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
432 $flaginfo{flag} = $flag;
433 if ( $flag eq 'CHARGES' ) {
434 $flaginfo{msg} = $flag;
435 $flaginfo{charges} = 1;
436 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
438 elsif ( $flag eq 'WAITING' ) {
439 $flaginfo{msg} = $flag;
440 $flaginfo{waiting} = 1;
442 my $items = $flags->{$flag}->{'itemlist'};
443 foreach my $item (@$items) {
444 my ($iteminformation) =
445 getiteminformation( $item->{'itemnumber'}, 0 );
447 $waitingitem{biblionum} = $iteminformation->{'biblionumber'};
448 $waitingitem{barcode} = $iteminformation->{'barcode'};
449 $waitingitem{title} = $iteminformation->{'title'};
450 $waitingitem{brname} =
451 $branches->{ $iteminformation->{'holdingbranch'} }
453 push( @waitingitemloop, \%waitingitem );
455 $flaginfo{itemloop} = \@waitingitemloop;
457 elsif ( $flag eq 'ODUES' ) {
458 my $items = $flags->{$flag}->{'itemlist'};
460 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
463 my ($iteminformation) =
464 getiteminformation( $item->{'itemnumber'}, 0 );
466 $overdueitem{duedate} = format_date( $item->{'date_due'} );
467 $overdueitem{biblionum} = $iteminformation->{'biblionumber'};
468 $overdueitem{barcode} = $iteminformation->{'barcode'};
469 $overdueitem{title} = $iteminformation->{'title'};
470 $overdueitem{brname} =
471 $branches->{ $iteminformation->{'holdingbranch'} }
473 push( @itemloop, \%overdueitem );
475 $flaginfo{itemloop} = \@itemloop;
476 $flaginfo{overdue} = 1;
479 $flaginfo{other} = 1;
480 $flaginfo{msg} = $flags->{$flag}->{'message'};
482 push( @flagloop, \%flaginfo );
486 flagloop => \@flagloop,
487 riborrowernumber => $borrower->{'borrowernumber'},
488 riborcnum => $borrower->{'cardnumber'},
489 riborsurname => $borrower->{'surname'},
490 ribortitle => $borrower->{'title'},
491 riborfirstname => $borrower->{'firstname'}
495 #set up so only the last 8 returned items display (make for faster loading pages)
498 foreach ( sort { $a <=> $b } keys %returneditems ) {
501 my $barcode = $returneditems{$_};
502 my $duedate = $riduedate{$_};
506 my @tempdate = split( /-/, $duedate );
507 $ri{year} = $tempdate[0];
508 $ri{month} = $tempdate[1];
509 $ri{day} = $tempdate[2];
510 my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
511 my @datearr = localtime( time() );
514 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
515 . sprintf( "%0.2d", $datearr[3] );
516 $ri{duedate} = format_date($duedate);
518 getpatroninformation( \%env, $riborrowernumber{$_}, 0 );
519 $ri{borrowernumber} = $borrower->{'borrowernumber'};
520 $ri{borcnum} = $borrower->{'cardnumber'};
521 $ri{borfirstname} = $borrower->{'firstname'};
522 $ri{borsurname} = $borrower->{'surname'};
523 $ri{bortitle} = $borrower->{'title'};
526 $ri{borrowernumber} = $riborrowernumber{$_};
530 my ($iteminformation) = getiteminformation( 0, $barcode );
531 $ri{itembiblionumber} = $iteminformation->{'biblionumber'};
532 $ri{itemtitle} = $iteminformation->{'title'};
533 $ri{itemauthor} = $iteminformation->{'author'};
534 $ri{itemtype} = $iteminformation->{'itemtype'};
535 $ri{ccode} = $iteminformation->{'ccode'};
536 $ri{barcode} = $barcode;
542 push( @riloop, \%ri );
544 $template->param( riloop => \@riloop );
547 genbrname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
548 genprname => $printers->{$printer}->{'printername'},
549 branchname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
551 errmsgloop => \@errmsgloop,
552 intranetcolorstylesheet =>
553 C4::Context->preference("intranetcolorstylesheet"),
554 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
555 IntranetNav => C4::Context->preference("IntranetNav"),
558 # actually print the page!
559 output_html_with_http_headers $query, $cookie, $template->output;
561 sub cuecatbarcodedecode {
564 my @fields = split( /\./, $barcode );
565 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
566 if ( $#results == 2 ) {