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
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 iteminfo => $iteminfo->{'author'},
139 tobranchname => $branchname,
141 borrowernumber => $borrowernumber,
143 borfirstname => $borr->{'firstname'},
144 borsurname => $borr->{'surname'},
153 my $issueinformation;
154 my $barcode = $query->param('barcode');
156 # actually return book and prepare item table.....
159 $barcode = cuecatbarcodedecode($barcode);
163 ( $returned, $messages, $issueinformation, $borrower ) =
164 AddReturn( $barcode, C4::Context->userenv->{'branch'} );
165 # get biblio description
166 my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
168 title => $biblio->{'title'},
169 homebranch => $biblio->{'homebranch'},
170 author => $biblio->{'author'},
171 itembarcode => $biblio->{'barcode'},
172 itemtype => $biblio->{'itemtype'},
173 ccode => $biblio->{'ccode'},
174 itembiblionumber => $biblio->{'biblionumber'},
177 $returneditems{0} = $barcode;
178 $riborrowernumber{0} = $borrower->{'borrowernumber'};
179 $riduedate{0} = $issueinformation->{'date_due'};
183 $input{barcode} = $barcode;
184 $input{duedate} = $riduedate{0};
185 $input{borrowernumber} = $riborrowernumber{0};
186 push( @inputloop, \%input );
188 # check if the branch is the same as homebranch
189 # if not, we want to put a message
190 if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
191 $template->param( homebranch => $biblio->{'homebranch'} );
194 elsif ( !$messages->{'BadBarcode'} ) {
198 $input{barcode} = $barcode;
201 $returneditems{0} = $barcode;
203 if ( $messages->{'wthdrawn'} ) {
204 $input{withdrawn} = 1;
205 $input{borrowernumber} = "Item Cancelled";
206 $riborrowernumber{0} = 'Item Cancelled';
209 $input{borrowernumber} = " ";
210 $riborrowernumber{0} = ' ';
212 push( @inputloop, \%input );
215 $template->param( inputloop => \@inputloop );
221 # new op dev : we check if the document must be returned to his homebranch directly,
222 # if the document is transfered, we have warning message .
224 if ( $messages->{'WasTransfered'} ) {
232 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
234 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
237 TransferWaitingAt => $messages->{'WrongTransfer'},
238 WrongTransferItem => $messages->{'WrongTransferItem'},
241 my $reserve = $messages->{'ResFound'};
242 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
243 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
245 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
248 wborfirstname => $borr->{'firstname'},
249 wborsurname => $borr->{'surname'},
250 wbortitle => $borr->{'title'},
251 wborphone => $borr->{'phone'},
252 wboremail => $borr->{'emailaddress'},
253 wborstraddress => $borr->{'streetaddress'},
254 wborcity => $borr->{'city'},
255 wborzip => $borr->{'zipcode'},
256 wborrowernumber => $reserve->{'borrowernumber'},
257 wborcnum => $borr->{'cardnumber'},
258 wtransfertFrom => C4::Context->userenv->{'branch'},
264 # reserve found and item arrived at the expected branch
266 if ( $messages->{'ResFound'} and not $messages->{'WrongTransfer'}) {
267 my $reserve = $messages->{'ResFound'};
268 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
269 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
270 if ( $reserve->{'ResFound'} eq "Waiting" ) {
271 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
272 $template->param( waiting => 1 );
275 $template->param( waiting => 0 );
280 name => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
281 borfirstname => $borr->{'firstname'},
282 borsurname => $borr->{'surname'},
283 bortitle => $borr->{'title'},
284 borphone => $borr->{'phone'},
285 boremail => $borr->{'emailaddress'},
286 borstraddress => $borr->{'streetaddress'},
287 borcity => $borr->{'city'},
288 borzip => $borr->{'zipcode'},
289 borrowernumber => $reserve->{'borrowernumber'},
290 borcnum => $borr->{'cardnumber'},
291 debarred => $borr->{'debarred'},
292 gonenoaddress => $borr->{'gonenoaddress'},
293 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
294 itemnumber => $reserve->{'itemnumber'},
299 if ( $reserve->{'ResFound'} eq "Reserved" ) {
300 my @da = localtime( time() );
302 sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
303 . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
306 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
307 $template->param( intransit => 0 );
310 $template->param( intransit => 1 );
315 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
317 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
318 destbranch => $reserve->{'branchcode'},
319 transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
321 resbarcode => $barcode,
322 today => $todaysdate,
323 itemnumber => $reserve->{'itemnumber'},
324 borsurname => $borr->{'surname'},
325 bortitle => $borr->{'title'},
326 borfirstname => $borr->{'firstname'},
327 borrowernumber => $reserve->{'borrowernumber'},
328 borcnum => $borr->{'cardnumber'},
329 borphone => $borr->{'phone'},
330 borstraddress => $borr->{'streetaddress'},
331 borsub => $borr->{'suburb'},
332 borcity => $borr->{'city'},
333 borzip => $borr->{'zipcode'},
334 boremail => $borr->{'emailaddress'},
335 debarred => $borr->{'debarred'},
336 gonenoaddress => $borr->{'gonenoaddress'},
344 foreach my $code ( keys %$messages ) {
348 my $exit_required_p = 0;
349 if ( $code eq 'BadBarcode' ) {
350 $err{badbarcode} = 1;
351 $err{msg} = $messages->{'BadBarcode'};
353 elsif ( $code eq 'NotIssued' ) {
355 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
357 elsif ( $code eq 'WasLost' ) {
360 elsif ( $code eq 'ResFound' ) {
361 ; # FIXME... anything to do here?
363 elsif ( $code eq 'WasReturned' ) {
364 ; # FIXME... anything to do here?
366 elsif ( $code eq 'WasTransfered' ) {
367 ; # FIXME... anything to do here?
369 elsif ( $code eq 'wthdrawn' ) {
371 $exit_required_p = 1;
373 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
374 if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
375 $err{ispermanent} = 1;
377 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
380 elsif ( $code eq 'WrongTransfer' ) {
381 ; # FIXME... anything to do here?
383 elsif ( $code eq 'WrongTransferItem' ) {
384 ; # FIXME... anything to do here?
387 die "Unknown error code $code"; # XXX
390 push( @errmsgloop, \%err );
392 last if $exit_required_p;
394 $template->param( errmsgloop => \@errmsgloop );
398 my $flags = $borrower->{'flags'};
401 foreach my $flag ( sort keys %$flags ) {
403 unless ($flagset) { $flagset = 1; }
404 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
405 $flaginfo{flag} = $flag;
406 if ( $flag eq 'CHARGES' ) {
407 $flaginfo{msg} = $flag;
408 $flaginfo{charges} = 1;
409 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
411 elsif ( $flag eq 'WAITING' ) {
412 $flaginfo{msg} = $flag;
413 $flaginfo{waiting} = 1;
415 my $items = $flags->{$flag}->{'itemlist'};
416 foreach my $item (@$items) {
418 GetBiblioFromItemNumber( $item->{'itemnumber'});
420 $waitingitem{biblionum} = $biblio->{'biblionumber'};
421 $waitingitem{barcode} = $biblio->{'barcode'};
422 $waitingitem{title} = $biblio->{'title'};
423 $waitingitem{brname} =
424 $branches->{ $biblio->{'holdingbranch'} }
426 push( @waitingitemloop, \%waitingitem );
428 $flaginfo{itemloop} = \@waitingitemloop;
430 elsif ( $flag eq 'ODUES' ) {
431 my $items = $flags->{$flag}->{'itemlist'};
433 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
437 GetBiblioFromItemNumber( $item->{'itemnumber'});
439 $overdueitem{duedate} = format_date( $item->{'date_due'} );
440 $overdueitem{biblionum} = $biblio->{'biblionumber'};
441 $overdueitem{barcode} = $biblio->{'barcode'};
442 $overdueitem{title} = $biblio->{'title'};
443 $overdueitem{brname} =
444 $branches->{ $biblio->{'holdingbranch'} }
446 push( @itemloop, \%overdueitem );
448 $flaginfo{itemloop} = \@itemloop;
449 $flaginfo{overdue} = 1;
452 $flaginfo{other} = 1;
453 $flaginfo{msg} = $flags->{$flag}->{'message'};
455 push( @flagloop, \%flaginfo );
459 flagloop => \@flagloop,
460 riborrowernumber => $borrower->{'borrowernumber'},
461 riborcnum => $borrower->{'cardnumber'},
462 riborsurname => $borrower->{'surname'},
463 ribortitle => $borrower->{'title'},
464 riborfirstname => $borrower->{'firstname'}
468 #set up so only the last 8 returned items display (make for faster loading pages)
471 foreach ( sort { $a <=> $b } keys %returneditems ) {
474 my $barcode = $returneditems{$_};
475 my $duedate = $riduedate{$_};
479 my @tempdate = split( /-/, $duedate );
480 $ri{year} = $tempdate[0];
481 $ri{month} = $tempdate[1];
482 $ri{day} = $tempdate[2];
483 my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
484 my @datearr = localtime( time() );
487 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
488 . sprintf( "%0.2d", $datearr[3] );
489 $ri{duedate} = format_date($duedate);
491 GetMemberDetails( $riborrowernumber{$_}, 0 );
492 $ri{borrowernumber} = $borrower->{'borrowernumber'};
493 $ri{borcnum} = $borrower->{'cardnumber'};
494 $ri{borfirstname} = $borrower->{'firstname'};
495 $ri{borsurname} = $borrower->{'surname'};
496 $ri{bortitle} = $borrower->{'title'};
499 $ri{borrowernumber} = $riborrowernumber{$_};
503 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
504 $ri{itembiblionumber} = $biblio->{'biblionumber'};
505 $ri{itemtitle} = $biblio->{'title'};
506 $ri{itemauthor} = $biblio->{'author'};
507 $ri{itemtype} = $biblio->{'itemtype'};
508 $ri{ccode} = $biblio->{'ccode'};
509 $ri{barcode} = $barcode;
515 push( @riloop, \%ri );
517 $template->param( riloop => \@riloop );
520 genbrname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
521 genprname => $printers->{$printer}->{'printername'},
522 branchname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
524 errmsgloop => \@errmsgloop,
525 intranetcolorstylesheet =>
526 C4::Context->preference("intranetcolorstylesheet"),
527 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
528 IntranetNav => C4::Context->preference("IntranetNav"),
531 # actually print the page!
532 output_html_with_http_headers $query, $cookie, $template->output;