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 },
60 my $branches = GetBranches();
61 my $printers = GetPrinters( \%env );
63 #my $branch = C4::Context->userenv?C4::Context->userenv->{'branch'}:"";
64 my $printer = C4::Context->userenv?C4::Context->userenv->{'branchprinter'}:"";
67 # Some code to handle the error if there is no branch or printer setting.....
70 #$env{'branchcode'} = $branch;
71 $env{'printer'} = $printer;
72 $env{'queue'} = $printer;
75 # Set up the item stack ....
80 foreach ( $query->param ) {
81 (next) unless (/ri-(\d*)/);
84 (next) if ( $counter > 20 );
85 my $barcode = $query->param("ri-$counter");
86 my $duedate = $query->param("dd-$counter");
87 my $borrowernumber = $query->param("bn-$counter");
91 $barcode = cuecatbarcodedecode($barcode);
93 ######################
94 #Are these lines still useful ?
95 $returneditems{$counter} = $barcode;
96 $riduedate{$counter} = $duedate;
97 $riborrowernumber{$counter} = $borrowernumber;
99 #######################
100 $input{counter} = $counter;
101 $input{barcode} = $barcode;
102 $input{duedate} = $duedate;
103 $input{borrowernumber} = $borrowernumber;
104 push( @inputloop, \%input );
108 # Deal with the requests....
110 if ($query->param('WT-itemNumber')){
111 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
115 if ( $query->param('resbarcode') ) {
116 my $item = $query->param('itemnumber');
117 my $borrowernumber = $query->param('borrowernumber');
118 my $resbarcode = $query->param('resbarcode');
119 my $diffBranchReturned = $query->param('diffBranch');
121 my $iteminfo = GetBiblioFromItemNumber($item);
124 # addin in ReserveWaiting the possibility to check if the document is expected in this library or not,
125 # if not we send a value in reserve waiting for not implementting waiting status
126 if ($diffBranchReturned) {
127 $diffBranchSend = $diffBranchReturned;
130 $diffBranchSend = undef;
133 my $tobranchcd = ReserveWaiting( $item, $borrowernumber,$diffBranchSend);
134 # check if we have other reservs for this document, if we have a return send the message of transfer
135 my ( $messages, $nextreservinfo ) = OtherReserves($item);
137 my $branchname = GetBranchName( $messages->{'transfert'} );
138 my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
139 my $borcnum = $borr->{'cardnumber'};
141 $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
142 my $slip = $query->param('resslip');
145 if ( $messages->{'transfert'} ) {
147 itemtitle => $iteminfo->{'title'},
148 iteminfo => $iteminfo->{'author'},
149 tobranchname => $branchname,
151 borrowernumber => $borrowernumber,
153 borfirstname => $borr->{'firstname'},
154 borsurname => $borr->{'surname'},
163 my $issueinformation;
164 my $barcode = $query->param('barcode');
166 # actually return book and prepare item table.....
169 $barcode = cuecatbarcodedecode($barcode);
173 ( $returned, $messages, $issueinformation, $borrower ) =
174 AddReturn( $barcode, C4::Context->userenv->{'branch'} );
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'} ) {
242 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
244 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
247 TransferWaitingAt => $messages->{'WrongTransfer'},
248 WrongTransferItem => $messages->{'WrongTransferItem'},
251 my $reserve = $messages->{'ResFound'};
252 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
253 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
255 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
258 wborfirstname => $borr->{'firstname'},
259 wborsurname => $borr->{'surname'},
260 wbortitle => $borr->{'title'},
261 wborphone => $borr->{'phone'},
262 wboremail => $borr->{'emailaddress'},
263 wborstraddress => $borr->{'streetaddress'},
264 wborcity => $borr->{'city'},
265 wborzip => $borr->{'zipcode'},
266 wborrowernumber => $reserve->{'borrowernumber'},
267 wborcnum => $borr->{'cardnumber'},
268 wtransfertFrom => C4::Context->userenv->{'branch'},
274 # reserve found and item arrived at the expected branch
276 if ( $messages->{'ResFound'} and not $messages->{'WrongTransfer'}) {
277 my $reserve = $messages->{'ResFound'};
278 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
279 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
280 if ( $reserve->{'ResFound'} eq "Waiting" ) {
281 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
282 $template->param( waiting => 1 );
285 $template->param( waiting => 0 );
290 name => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
291 borfirstname => $borr->{'firstname'},
292 borsurname => $borr->{'surname'},
293 bortitle => $borr->{'title'},
294 borphone => $borr->{'phone'},
295 boremail => $borr->{'emailaddress'},
296 borstraddress => $borr->{'streetaddress'},
297 borcity => $borr->{'city'},
298 borzip => $borr->{'zipcode'},
299 borrowernumber => $reserve->{'borrowernumber'},
300 borcnum => $borr->{'cardnumber'},
301 debarred => $borr->{'debarred'},
302 gonenoaddress => $borr->{'gonenoaddress'},
303 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
304 itemnumber => $reserve->{'itemnumber'},
308 if ( $reserve->{'ResFound'} eq "Reserved" ) {
309 my @da = localtime( time() );
311 sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
312 . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
315 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
316 $template->param( intransit => 0 );
319 $template->param( intransit => 1 );
324 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
326 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
327 destbranch => $reserve->{'branchcode'},
328 transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
330 today => $todaysdate,
331 itemnumber => $reserve->{'itemnumber'},
332 borsurname => $borr->{'surname'},
333 bortitle => $borr->{'title'},
334 borfirstname => $borr->{'firstname'},
335 borrowernumber => $reserve->{'borrowernumber'},
336 borcnum => $borr->{'cardnumber'},
337 borphone => $borr->{'phone'},
338 borstraddress => $borr->{'streetaddress'},
339 borsub => $borr->{'suburb'},
340 borcity => $borr->{'city'},
341 borzip => $borr->{'zipcode'},
342 boremail => $borr->{'emailaddress'},
343 debarred => $borr->{'debarred'},
344 gonenoaddress => $borr->{'gonenoaddress'},
352 foreach my $code ( keys %$messages ) {
356 my $exit_required_p = 0;
357 if ( $code eq 'BadBarcode' ) {
358 $err{badbarcode} = 1;
359 $err{msg} = $messages->{'BadBarcode'};
361 elsif ( $code eq 'NotIssued' ) {
363 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
365 elsif ( $code eq 'WasLost' ) {
368 elsif ( $code eq 'ResFound' ) {
369 ; # FIXME... anything to do here?
371 elsif ( $code eq 'WasReturned' ) {
372 ; # FIXME... anything to do here?
374 elsif ( $code eq 'WasTransfered' ) {
375 ; # FIXME... anything to do here?
377 elsif ( $code eq 'wthdrawn' ) {
379 $exit_required_p = 1;
381 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
382 if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
383 $err{ispermanent} = 1;
385 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
388 elsif ( $code eq 'WrongTransfer' ) {
389 ; # FIXME... anything to do here?
391 elsif ( $code eq 'WrongTransferItem' ) {
392 ; # FIXME... anything to do here?
395 die "Unknown error code $code"; # XXX
398 push( @errmsgloop, \%err );
400 last if $exit_required_p;
402 $template->param( errmsgloop => \@errmsgloop );
406 my $flags = $borrower->{'flags'};
409 foreach my $flag ( sort keys %$flags ) {
411 unless ($flagset) { $flagset = 1; }
412 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
413 $flaginfo{flag} = $flag;
414 if ( $flag eq 'CHARGES' ) {
415 $flaginfo{msg} = $flag;
416 $flaginfo{charges} = 1;
417 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
419 elsif ( $flag eq 'WAITING' ) {
420 $flaginfo{msg} = $flag;
421 $flaginfo{waiting} = 1;
423 my $items = $flags->{$flag}->{'itemlist'};
424 foreach my $item (@$items) {
426 GetBiblioFromItemNumber( $item->{'itemnumber'});
428 $waitingitem{biblionum} = $biblio->{'biblionumber'};
429 $waitingitem{barcode} = $biblio->{'barcode'};
430 $waitingitem{title} = $biblio->{'title'};
431 $waitingitem{brname} =
432 $branches->{ $biblio->{'holdingbranch'} }
434 push( @waitingitemloop, \%waitingitem );
436 $flaginfo{itemloop} = \@waitingitemloop;
438 elsif ( $flag eq 'ODUES' ) {
439 my $items = $flags->{$flag}->{'itemlist'};
441 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
445 GetBiblioFromItemNumber( $item->{'itemnumber'});
447 $overdueitem{duedate} = format_date( $item->{'date_due'} );
448 $overdueitem{biblionum} = $biblio->{'biblionumber'};
449 $overdueitem{barcode} = $biblio->{'barcode'};
450 $overdueitem{title} = $biblio->{'title'};
451 $overdueitem{brname} =
452 $branches->{ $biblio->{'holdingbranch'} }
454 push( @itemloop, \%overdueitem );
456 $flaginfo{itemloop} = \@itemloop;
457 $flaginfo{overdue} = 1;
460 $flaginfo{other} = 1;
461 $flaginfo{msg} = $flags->{$flag}->{'message'};
463 push( @flagloop, \%flaginfo );
467 flagloop => \@flagloop,
468 riborrowernumber => $borrower->{'borrowernumber'},
469 riborcnum => $borrower->{'cardnumber'},
470 riborsurname => $borrower->{'surname'},
471 ribortitle => $borrower->{'title'},
472 riborfirstname => $borrower->{'firstname'}
476 #set up so only the last 8 returned items display (make for faster loading pages)
479 foreach ( sort { $a <=> $b } keys %returneditems ) {
482 my $barcode = $returneditems{$_};
483 my $duedate = $riduedate{$_};
487 my @tempdate = split( /-/, $duedate );
488 $ri{year} = $tempdate[0];
489 $ri{month} = $tempdate[1];
490 $ri{day} = $tempdate[2];
491 my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
492 my @datearr = localtime( time() );
495 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
496 . sprintf( "%0.2d", $datearr[3] );
497 $ri{duedate} = format_date($duedate);
499 GetMemberDetails( $riborrowernumber{$_}, 0 );
500 $ri{borrowernumber} = $borrower->{'borrowernumber'};
501 $ri{borcnum} = $borrower->{'cardnumber'};
502 $ri{borfirstname} = $borrower->{'firstname'};
503 $ri{borsurname} = $borrower->{'surname'};
504 $ri{bortitle} = $borrower->{'title'};
507 $ri{borrowernumber} = $riborrowernumber{$_};
511 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
512 $ri{itembiblionumber} = $biblio->{'biblionumber'};
513 $ri{itemtitle} = $biblio->{'title'};
514 $ri{itemauthor} = $biblio->{'author'};
515 $ri{itemtype} = $biblio->{'itemtype'};
516 $ri{ccode} = $biblio->{'ccode'};
517 $ri{barcode} = $barcode;
523 push( @riloop, \%ri );
525 $template->param( riloop => \@riloop );
528 genbrname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
529 genprname => $printers->{$printer}->{'printername'},
530 branchname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
532 errmsgloop => \@errmsgloop,
533 intranetcolorstylesheet =>
534 C4::Context->preference("intranetcolorstylesheet"),
535 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
536 IntranetNav => C4::Context->preference("IntranetNav"),
539 # actually print the page!
540 output_html_with_http_headers $query, $cookie, $template->output;
542 sub cuecatbarcodedecode {
545 my @fields = split( /\./, $barcode );
546 my @results = map( decode($_), @fields[ 1 .. $#fields ] );
547 if ( $#results == 2 ) {