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 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 # actually return book and prepare item table.....
160 $barcode = cuecatbarcodedecode($barcode);
164 ( $returned, $messages, $issueinformation, $borrower ) =
165 AddReturn( $barcode, C4::Context->userenv->{'branch'} );
166 # get biblio description
167 my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
169 title => $biblio->{'title'},
170 homebranch => $biblio->{'homebranch'},
171 author => $biblio->{'author'},
172 itembarcode => $biblio->{'barcode'},
173 itemtype => $biblio->{'itemtype'},
174 ccode => $biblio->{'ccode'},
175 itembiblionumber => $biblio->{'biblionumber'},
178 $returneditems{0} = $barcode;
179 $riborrowernumber{0} = $borrower->{'borrowernumber'};
180 $riduedate{0} = $issueinformation->{'date_due'};
184 $input{barcode} = $barcode;
185 $input{duedate} = $riduedate{0};
186 $input{borrowernumber} = $riborrowernumber{0};
187 push( @inputloop, \%input );
189 # check if the branch is the same as homebranch
190 # if not, we want to put a message
191 if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
192 $template->param( homebranch => $biblio->{'homebranch'} );
195 elsif ( !$messages->{'BadBarcode'} ) {
199 $input{barcode} = $barcode;
202 $returneditems{0} = $barcode;
204 if ( $messages->{'wthdrawn'} ) {
205 $input{withdrawn} = 1;
206 $input{borrowernumber} = "Item Cancelled";
207 $riborrowernumber{0} = 'Item Cancelled';
210 $input{borrowernumber} = " ";
211 $riborrowernumber{0} = ' ';
213 push( @inputloop, \%input );
216 $template->param( inputloop => \@inputloop );
222 # new op dev : we check if the document must be returned to his homebranch directly,
223 # if the document is transfered, we have warning message .
225 if ( $messages->{'WasTransfered'} ) {
233 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
235 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
238 TransferWaitingAt => $messages->{'WrongTransfer'},
239 WrongTransferItem => $messages->{'WrongTransferItem'},
242 my $reserve = $messages->{'ResFound'};
243 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
244 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
246 $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
249 wborfirstname => $borr->{'firstname'},
250 wborsurname => $borr->{'surname'},
251 wbortitle => $borr->{'title'},
252 wborphone => $borr->{'phone'},
253 wboremail => $borr->{'email'},
254 wboraddress => $borr->{'address'},
255 wboraddress2 => $borr->{'address2'},
256 wborcity => $borr->{'city'},
257 wborzip => $borr->{'zipcode'},
258 wborrowernumber => $reserve->{'borrowernumber'},
259 wborcnum => $borr->{'cardnumber'},
260 wtransfertFrom => C4::Context->userenv->{'branch'},
266 # reserve found and item arrived at the expected branch
268 if ( $messages->{'ResFound'} and not $messages->{'WrongTransfer'}) {
269 my $reserve = $messages->{'ResFound'};
270 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
271 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
272 if ( $reserve->{'ResFound'} eq "Waiting" ) {
273 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
274 $template->param( waiting => 1 );
277 $template->param( waiting => 0 );
282 name => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
283 borfirstname => $borr->{'firstname'},
284 borsurname => $borr->{'surname'},
285 bortitle => $borr->{'title'},
286 borphone => $borr->{'phone'},
287 boremail => $borr->{'email'},
288 boraddress => $borr->{'address'},
289 boraddress2 => $borr->{'address2'},
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'},
302 if ( $reserve->{'ResFound'} eq "Reserved" ) {
303 my @da = localtime( time() );
305 sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
306 . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
309 if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
310 $template->param( intransit => 0 );
313 $template->param( intransit => 1 );
318 currentbranch => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
320 $branches->{ $reserve->{'branchcode'} }->{'branchname'},
321 destbranch => $reserve->{'branchcode'},
322 transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
324 resbarcode => $barcode,
325 today => $todaysdate,
326 itemnumber => $reserve->{'itemnumber'},
327 borsurname => $borr->{'surname'},
328 bortitle => $borr->{'title'},
329 borfirstname => $borr->{'firstname'},
330 borrowernumber => $reserve->{'borrowernumber'},
331 borcnum => $borr->{'cardnumber'},
332 borphone => $borr->{'phone'},
333 boraddress => $borr->{'address'},
334 boraddress2 => $borr->{'address2'},
335 borsub => $borr->{'suburb'},
336 borcity => $borr->{'city'},
337 borzip => $borr->{'zipcode'},
338 boremail => $borr->{'email'},
339 debarred => $borr->{'debarred'},
340 gonenoaddress => $borr->{'gonenoaddress'},
348 foreach my $code ( keys %$messages ) {
352 my $exit_required_p = 0;
353 if ( $code eq 'BadBarcode' ) {
354 $err{badbarcode} = 1;
355 $err{msg} = $messages->{'BadBarcode'};
357 elsif ( $code eq 'NotIssued' ) {
359 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
361 elsif ( $code eq 'WasLost' ) {
364 elsif ( $code eq 'ResFound' ) {
365 ; # FIXME... anything to do here?
367 elsif ( $code eq 'WasReturned' ) {
368 ; # FIXME... anything to do here?
370 elsif ( $code eq 'WasTransfered' ) {
371 ; # FIXME... anything to do here?
373 elsif ( $code eq 'wthdrawn' ) {
375 $exit_required_p = 1;
377 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
378 if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
379 $err{ispermanent} = 1;
381 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
384 elsif ( $code eq 'WrongTransfer' ) {
385 ; # FIXME... anything to do here?
387 elsif ( $code eq 'WrongTransferItem' ) {
388 ; # FIXME... anything to do here?
391 die "Unknown error code $code"; # XXX
394 push( @errmsgloop, \%err );
396 last if $exit_required_p;
398 $template->param( errmsgloop => \@errmsgloop );
402 my $flags = $borrower->{'flags'};
405 foreach my $flag ( sort keys %$flags ) {
407 unless ($flagset) { $flagset = 1; }
408 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
409 $flaginfo{flag} = $flag;
410 if ( $flag eq 'CHARGES' ) {
411 $flaginfo{msg} = $flag;
412 $flaginfo{charges} = 1;
413 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
415 elsif ( $flag eq 'WAITING' ) {
416 $flaginfo{msg} = $flag;
417 $flaginfo{waiting} = 1;
419 my $items = $flags->{$flag}->{'itemlist'};
420 foreach my $item (@$items) {
422 GetBiblioFromItemNumber( $item->{'itemnumber'});
424 $waitingitem{biblionum} = $biblio->{'biblionumber'};
425 $waitingitem{barcode} = $biblio->{'barcode'};
426 $waitingitem{title} = $biblio->{'title'};
427 $waitingitem{brname} =
428 $branches->{ $biblio->{'holdingbranch'} }
430 push( @waitingitemloop, \%waitingitem );
432 $flaginfo{itemloop} = \@waitingitemloop;
434 elsif ( $flag eq 'ODUES' ) {
435 my $items = $flags->{$flag}->{'itemlist'};
437 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
441 GetBiblioFromItemNumber( $item->{'itemnumber'});
443 $overdueitem{duedate} = format_date( $item->{'date_due'} );
444 $overdueitem{biblionum} = $biblio->{'biblionumber'};
445 $overdueitem{barcode} = $biblio->{'barcode'};
446 $overdueitem{title} = $biblio->{'title'};
447 $overdueitem{brname} =
448 $branches->{ $biblio->{'holdingbranch'} }
450 push( @itemloop, \%overdueitem );
452 $flaginfo{itemloop} = \@itemloop;
453 $flaginfo{overdue} = 1;
456 $flaginfo{other} = 1;
457 $flaginfo{msg} = $flags->{$flag}->{'message'};
459 push( @flagloop, \%flaginfo );
463 flagloop => \@flagloop,
464 riborrowernumber => $borrower->{'borrowernumber'},
465 riborcnum => $borrower->{'cardnumber'},
466 riborsurname => $borrower->{'surname'},
467 ribortitle => $borrower->{'title'},
468 riborfirstname => $borrower->{'firstname'}
472 #set up so only the last 8 returned items display (make for faster loading pages)
475 foreach ( sort { $a <=> $b } keys %returneditems ) {
478 my $barcode = $returneditems{$_};
479 my $duedate = $riduedate{$_};
483 my @tempdate = split( /-/, $duedate );
484 $ri{year} = $tempdate[0];
485 $ri{month} = $tempdate[1];
486 $ri{day} = $tempdate[2];
487 my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
488 my @datearr = localtime( time() );
491 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
492 . sprintf( "%0.2d", $datearr[3] );
493 $ri{duedate} = format_date($duedate);
495 GetMemberDetails( $riborrowernumber{$_}, 0 );
496 $ri{borrowernumber} = $borrower->{'borrowernumber'};
497 $ri{borcnum} = $borrower->{'cardnumber'};
498 $ri{borfirstname} = $borrower->{'firstname'};
499 $ri{borsurname} = $borrower->{'surname'};
500 $ri{bortitle} = $borrower->{'title'};
503 $ri{borrowernumber} = $riborrowernumber{$_};
507 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
508 $ri{itembiblionumber} = $biblio->{'biblionumber'};
509 $ri{itemtitle} = $biblio->{'title'};
510 $ri{itemauthor} = $biblio->{'author'};
511 $ri{itemtype} = $biblio->{'itemtype'};
512 $ri{ccode} = $biblio->{'ccode'};
513 $ri{barcode} = $barcode;
519 push( @riloop, \%ri );
521 $template->param( riloop => \@riloop );
524 genbrname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
525 genprname => $printers->{$printer}->{'printername'},
526 branchname => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
528 errmsgloop => \@errmsgloop,
531 # actually print the page!
532 output_html_with_http_headers $query, $cookie, $template->output;