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
38 use C4::Interface::CGI::Output;
39 use C4::Branch; # GetBranchName
40 use C4::Koha; # FIXME : is it still useful ?
46 my ( $template, $librarian, $cookie ) = get_template_and_user(
48 template_name => "circ/returns.tmpl",
52 flagsrequired => { circulate => 1 },
58 my $branches = GetBranches();
59 my $printers = GetPrinters();
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 # Set up the item stack ....
73 foreach ( $query->param ) {
74 (next) unless (/ri-(\d*)/);
77 (next) if ( $counter > 20 );
78 my $barcode = $query->param("ri-$counter");
79 my $duedate = $query->param("dd-$counter");
80 my $borrowernumber = $query->param("bn-$counter");
84 $barcode = cuecatbarcodedecode($barcode);
86 ######################
87 #Are these lines still useful ?
88 $returneditems{$counter} = $barcode;
89 $riduedate{$counter} = $duedate;
90 $riborrowernumber{$counter} = $borrowernumber;
92 #######################
93 $input{counter} = $counter;
94 $input{barcode} = $barcode;
95 $input{duedate} = $duedate;
96 $input{borrowernumber} = $borrowernumber;
97 push( @inputloop, \%input );
101 # Deal with the requests....
103 if ($query->param('WT-itemNumber')){
104 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
108 if ( $query->param('resbarcode') ) {
109 my $item = $query->param('itemnumber');
110 my $borrowernumber = $query->param('borrowernumber');
111 my $resbarcode = $query->param('resbarcode');
112 my $diffBranchReturned = $query->param('diffBranch');
114 my $iteminfo = GetBiblioFromItemNumber($item);
117 # addin in ReserveWaiting the possibility to check if the document is expected in this library or not,
118 # if not we send a value in reserve waiting for not implementting waiting status
119 if ($diffBranchReturned) {
120 $diffBranchSend = $diffBranchReturned;
123 $diffBranchSend = undef;
126 my $tobranchcd = ReserveWaiting( $item, $borrowernumber,$diffBranchSend);
127 # check if we have other reservs for this document, if we have a return send the message of transfer
128 my ( $messages, $nextreservinfo ) = OtherReserves($item);
130 my $branchname = GetBranchName( $messages->{'transfert'} );
131 my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
132 my $borcnum = $borr->{'cardnumber'};
134 $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
135 my $slip = $query->param('resslip');
138 if ( $messages->{'transfert'} ) {
140 itemtitle => $iteminfo->{'title'},
141 iteminfo => $iteminfo->{'author'},
142 tobranchname => $branchname,
144 borrowernumber => $borrowernumber,
146 borfirstname => $borr->{'firstname'},
147 borsurname => $borr->{'surname'},
156 my $issueinformation;
157 my $barcode = $query->param('barcode');
159 # actually return book and prepare item table.....
162 $barcode = cuecatbarcodedecode($barcode);
166 ( $returned, $messages, $issueinformation, $borrower ) =
167 AddReturn( $barcode, C4::Context->userenv->{'branch'} );
168 # get biblio description
169 my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
171 title => $biblio->{'title'},
172 homebranch => $biblio->{'homebranch'},
173 author => $biblio->{'author'},
174 itembarcode => $biblio->{'barcode'},
175 itemtype => $biblio->{'itemtype'},
176 ccode => $biblio->{'ccode'},
177 itembiblionumber => $biblio->{'biblionumber'},
180 $returneditems{0} = $barcode;
181 $riborrowernumber{0} = $borrower->{'borrowernumber'};
182 $riduedate{0} = $issueinformation->{'date_due'};
186 $input{barcode} = $barcode;
187 $input{duedate} = $riduedate{0};
188 $input{borrowernumber} = $riborrowernumber{0};
189 push( @inputloop, \%input );
191 # check if the branch is the same as homebranch
192 # if not, we want to put a message
193 if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
194 $template->param( homebranch => $biblio->{'homebranch'} );
197 elsif ( !$messages->{'BadBarcode'} ) {
201 $input{barcode} = $barcode;
204 $returneditems{0} = $barcode;
206 if ( $messages->{'wthdrawn'} ) {
207 $input{withdrawn} = 1;
208 $input{borrowernumber} = "Item Cancelled";
209 $riborrowernumber{0} = 'Item Cancelled';
212 $input{borrowernumber} = " ";
213 $riborrowernumber{0} = ' ';
215 push( @inputloop, \%input );
218 $template->param( inputloop => \@inputloop );
224 # new op dev : we check if the document must be returned to his homebranch directly,
225 # if the document is transfered, we have warning message .
227 if ( $messages->{'WasTransfered'} ) {
235 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
237 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
240 TransferWaitingAt => $messages->{'WrongTransfer'},
241 WrongTransferItem => $messages->{'WrongTransferItem'},
244 my $reserve = $messages->{'ResFound'};
245 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
246 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
248 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
251 wborfirstname => $borr->{'firstname'},
252 wborsurname => $borr->{'surname'},
253 wbortitle => $borr->{'title'},
254 wborphone => $borr->{'phone'},
255 wboremail => $borr->{'emailaddress'},
256 wborstraddress => $borr->{'streetaddress'},
257 wborcity => $borr->{'city'},
258 wborzip => $borr->{'zipcode'},
259 wborrowernumber => $reserve->{'borrowernumber'},
260 wborcnum => $borr->{'cardnumber'},
261 wtransfertFrom => C4::Context->userenv->{'branch'},
267 # reserve found and item arrived at the expected branch
269 if ( $messages->{'ResFound'} and not $messages->{'WrongTransfer'}) {
270 my $reserve = $messages->{'ResFound'};
271 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
272 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
273 if ( $reserve->{'ResFound'} eq "Waiting" ) {
274 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
275 $template->param( waiting => 1 );
278 $template->param( waiting => 0 );
283 name => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
284 borfirstname => $borr->{'firstname'},
285 borsurname => $borr->{'surname'},
286 bortitle => $borr->{'title'},
287 borphone => $borr->{'phone'},
288 boremail => $borr->{'emailaddress'},
289 borstraddress => $borr->{'streetaddress'},
290 borcity => $borr->{'city'},
291 borzip => $borr->{'zipcode'},
292 borrowernumber => $reserve->{'borrowernumber'},
293 borcnum => $borr->{'cardnumber'},
294 debarred => $borr->{'debarred'},
295 gonenoaddress => $borr->{'gonenoaddress'},
296 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
297 itemnumber => $reserve->{'itemnumber'},
301 if ( $reserve->{'ResFound'} eq "Reserved" ) {
302 my @da = localtime( time() );
304 sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
305 . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
308 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
309 $template->param( intransit => 0 );
312 $template->param( intransit => 1 );
317 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
319 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
320 destbranch => $reserve->{'branchcode'},
321 transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
323 today => $todaysdate,
324 itemnumber => $reserve->{'itemnumber'},
325 borsurname => $borr->{'surname'},
326 bortitle => $borr->{'title'},
327 borfirstname => $borr->{'firstname'},
328 borrowernumber => $reserve->{'borrowernumber'},
329 borcnum => $borr->{'cardnumber'},
330 borphone => $borr->{'phone'},
331 borstraddress => $borr->{'streetaddress'},
332 borsub => $borr->{'suburb'},
333 borcity => $borr->{'city'},
334 borzip => $borr->{'zipcode'},
335 boremail => $borr->{'emailaddress'},
336 debarred => $borr->{'debarred'},
337 gonenoaddress => $borr->{'gonenoaddress'},
345 foreach my $code ( keys %$messages ) {
349 my $exit_required_p = 0;
350 if ( $code eq 'BadBarcode' ) {
351 $err{badbarcode} = 1;
352 $err{msg} = $messages->{'BadBarcode'};
354 elsif ( $code eq 'NotIssued' ) {
356 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
358 elsif ( $code eq 'WasLost' ) {
361 elsif ( $code eq 'ResFound' ) {
362 ; # FIXME... anything to do here?
364 elsif ( $code eq 'WasReturned' ) {
365 ; # FIXME... anything to do here?
367 elsif ( $code eq 'WasTransfered' ) {
368 ; # FIXME... anything to do here?
370 elsif ( $code eq 'wthdrawn' ) {
372 $exit_required_p = 1;
374 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
375 if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
376 $err{ispermanent} = 1;
378 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
381 elsif ( $code eq 'WrongTransfer' ) {
382 ; # FIXME... anything to do here?
384 elsif ( $code eq 'WrongTransferItem' ) {
385 ; # FIXME... anything to do here?
388 die "Unknown error code $code"; # XXX
391 push( @errmsgloop, \%err );
393 last if $exit_required_p;
395 $template->param( errmsgloop => \@errmsgloop );
399 my $flags = $borrower->{'flags'};
402 foreach my $flag ( sort keys %$flags ) {
404 unless ($flagset) { $flagset = 1; }
405 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
406 $flaginfo{flag} = $flag;
407 if ( $flag eq 'CHARGES' ) {
408 $flaginfo{msg} = $flag;
409 $flaginfo{charges} = 1;
410 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
412 elsif ( $flag eq 'WAITING' ) {
413 $flaginfo{msg} = $flag;
414 $flaginfo{waiting} = 1;
416 my $items = $flags->{$flag}->{'itemlist'};
417 foreach my $item (@$items) {
419 GetBiblioFromItemNumber( $item->{'itemnumber'});
421 $waitingitem{biblionum} = $biblio->{'biblionumber'};
422 $waitingitem{barcode} = $biblio->{'barcode'};
423 $waitingitem{title} = $biblio->{'title'};
424 $waitingitem{brname} =
425 $branches->{ $biblio->{'holdingbranch'} }
427 push( @waitingitemloop, \%waitingitem );
429 $flaginfo{itemloop} = \@waitingitemloop;
431 elsif ( $flag eq 'ODUES' ) {
432 my $items = $flags->{$flag}->{'itemlist'};
434 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
438 GetBiblioFromItemNumber( $item->{'itemnumber'});
440 $overdueitem{duedate} = format_date( $item->{'date_due'} );
441 $overdueitem{biblionum} = $biblio->{'biblionumber'};
442 $overdueitem{barcode} = $biblio->{'barcode'};
443 $overdueitem{title} = $biblio->{'title'};
444 $overdueitem{brname} =
445 $branches->{ $biblio->{'holdingbranch'} }
447 push( @itemloop, \%overdueitem );
449 $flaginfo{itemloop} = \@itemloop;
450 $flaginfo{overdue} = 1;
453 $flaginfo{other} = 1;
454 $flaginfo{msg} = $flags->{$flag}->{'message'};
456 push( @flagloop, \%flaginfo );
460 flagloop => \@flagloop,
461 riborrowernumber => $borrower->{'borrowernumber'},
462 riborcnum => $borrower->{'cardnumber'},
463 riborsurname => $borrower->{'surname'},
464 ribortitle => $borrower->{'title'},
465 riborfirstname => $borrower->{'firstname'}
469 #set up so only the last 8 returned items display (make for faster loading pages)
472 foreach ( sort { $a <=> $b } keys %returneditems ) {
475 my $barcode = $returneditems{$_};
476 my $duedate = $riduedate{$_};
480 my @tempdate = split( /-/, $duedate );
481 $ri{year} = $tempdate[0];
482 $ri{month} = $tempdate[1];
483 $ri{day} = $tempdate[2];
484 my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
485 my @datearr = localtime( time() );
488 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
489 . sprintf( "%0.2d", $datearr[3] );
490 $ri{duedate} = format_date($duedate);
492 GetMemberDetails( $riborrowernumber{$_}, 0 );
493 $ri{borrowernumber} = $borrower->{'borrowernumber'};
494 $ri{borcnum} = $borrower->{'cardnumber'};
495 $ri{borfirstname} = $borrower->{'firstname'};
496 $ri{borsurname} = $borrower->{'surname'};
497 $ri{bortitle} = $borrower->{'title'};
500 $ri{borrowernumber} = $riborrowernumber{$_};
504 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
505 $ri{itembiblionumber} = $biblio->{'biblionumber'};
506 $ri{itemtitle} = $biblio->{'title'};
507 $ri{itemauthor} = $biblio->{'author'};
508 $ri{itemtype} = $biblio->{'itemtype'};
509 $ri{ccode} = $biblio->{'ccode'};
510 $ri{barcode} = $barcode;
516 push( @riloop, \%ri );
518 $template->param( riloop => \@riloop );
521 genbrname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
522 genprname => $printers->{$printer}->{'printername'},
523 branchname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
525 errmsgloop => \@errmsgloop,
526 intranetcolorstylesheet =>
527 C4::Context->preference("intranetcolorstylesheet"),
528 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
529 IntranetNav => C4::Context->preference("IntranetNav"),
532 # actually print the page!
533 output_html_with_http_headers $query, $cookie, $template->output;
535 sub cuecatbarcodedecode {
538 my @fields = split( /\./, $barcode );
539 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
540 if ( $#results == 2 ) {