MT 2040, Follow-up : Broken language search
[koha.git] / circ / returns.pl
blob40eeed153f5bc5a8660e513ea9cb7acb07577922
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # 2006 SAN-OP
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
12 # version.
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
22 =head1 returns.pl
24 script to execute returns of books
26 =cut
28 use strict;
29 # use warnings; # FIXME
31 use CGI;
32 use C4::Context;
33 use C4::Auth qw/:DEFAULT get_session/;
34 use C4::Output;
35 use C4::Circulation;
36 use C4::Dates qw/format_date/;
37 use Date::Calc qw/Add_Delta_Days/;
38 use C4::Calendar;
39 use C4::Print;
40 use C4::Reserves;
41 use C4::Biblio;
42 use C4::Items;
43 use C4::Members;
44 use C4::Branch; # GetBranches GetBranchName
45 use C4::Koha; # FIXME : is it still useful ?
47 my $query = new CGI;
49 if (!C4::Context->userenv){
50 my $sessionID = $query->cookie("CGISESSID");
51 my $session = get_session($sessionID);
52 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
53 # no branch set we can't return
54 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
55 exit;
59 #getting the template
60 my ( $template, $librarian, $cookie ) = get_template_and_user(
62 template_name => "circ/returns.tmpl",
63 query => $query,
64 type => "intranet",
65 authnotrequired => 0,
66 flagsrequired => { circulate => "circulate_remaining_permissions" },
70 #####################
71 #Global vars
72 my $branches = GetBranches();
73 my $printers = GetPrinters();
75 #my $branch = C4::Context->userenv?C4::Context->userenv->{'branch'}:"";
76 my $printer = C4::Context->userenv ? C4::Context->userenv->{'branchprinter'} : "";
77 my $overduecharges = (C4::Context->preference('finesMode') && C4::Context->preference('finesMode') ne 'off');
79 my $userenv_branch = C4::Context->userenv->{'branch'} || '';
81 # Some code to handle the error if there is no branch or printer setting.....
84 # Set up the item stack ....
85 my %returneditems;
86 my %riduedate;
87 my %riborrowernumber;
88 my @inputloop;
89 foreach ( $query->param ) {
90 (next) unless (/ri-(\d*)/);
91 my %input;
92 my $counter = $1;
93 (next) if ( $counter > 20 );
94 my $barcode = $query->param("ri-$counter");
95 my $duedate = $query->param("dd-$counter");
96 my $borrowernumber = $query->param("bn-$counter");
97 $counter++;
99 # decode barcode ## Didn't we already decode them before passing them back last time??
100 $barcode = barcodedecode($barcode) if(C4::Context->preference('itemBarcodeInputFilter'));
102 ######################
103 #Are these lines still useful ?
104 $returneditems{$counter} = $barcode;
105 $riduedate{$counter} = $duedate;
106 $riborrowernumber{$counter} = $borrowernumber;
108 #######################
109 $input{counter} = $counter;
110 $input{barcode} = $barcode;
111 $input{duedate} = $duedate;
112 $input{borrowernumber} = $borrowernumber;
113 push( @inputloop, \%input );
116 ############
117 # Deal with the requests....
119 if ($query->param('WT-itemNumber')){
120 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
123 if ( $query->param('resbarcode') ) {
124 my $item = $query->param('itemnumber');
125 my $borrowernumber = $query->param('borrowernumber');
126 my $resbarcode = $query->param('resbarcode');
127 my $diffBranchReturned = $query->param('diffBranch');
128 my $iteminfo = GetBiblioFromItemNumber($item);
129 # fix up item type for display
130 $iteminfo->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $iteminfo->{'itype'} : $iteminfo->{'itemtype'};
131 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ? $diffBranchReturned : undef;
132 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
133 # i.e., whether to apply waiting status
134 ModReserveAffect( $item, $borrowernumber, $diffBranchSend);
135 # check if we have other reserves for this document, if we have a return send the message of transfer
136 my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
138 my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
139 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
140 if ( $messages->{'transfert'} ) {
141 $template->param(
142 itemtitle => $iteminfo->{'title'},
143 itembiblionumber => $iteminfo->{'biblionumber'},
144 iteminfo => $iteminfo->{'author'},
145 tobranchname => GetBranchName($messages->{'transfert'}),
146 name => $name,
147 borrowernumber => $borrowernumber,
148 borcnum => $borr->{'cardnumber'},
149 borfirstname => $borr->{'firstname'},
150 borsurname => $borr->{'surname'},
151 diffbranch => 1,
156 my $borrower;
157 my $returned = 0;
158 my $messages;
159 my $issueinformation;
160 my $itemnumber;
161 my $barcode = $query->param('barcode');
162 my $exemptfine = $query->param('exemptfine');
163 my $dropboxmode = $query->param('dropboxmode');
164 my $dotransfer = $query->param('dotransfer');
165 my $calendar = C4::Calendar->new( branchcode => $userenv_branch );
166 #dropbox: get last open day (today - 1)
167 my $today = C4::Dates->new();
168 my $today_iso = $today->output('iso');
169 my $dropboxdate = $calendar->addDate($today, -1);
170 if ($dotransfer){
171 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
172 my $transferitem = $query->param('transferitem');
173 my $tobranch = $query->param('tobranch');
174 ModItemTransfer($transferitem, $userenv_branch, $tobranch);
177 # actually return book and prepare item table.....
178 if ($barcode) {
179 $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
180 $itemnumber = GetItemnumberFromBarcode($barcode);
182 if ( C4::Context->preference("InProcessingToShelvingCart") ) {
183 my $item = GetItem( $itemnumber );
184 if ( $item->{'location'} eq 'PROC' ) {
185 $item->{'location'} = 'CART';
186 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
190 if ( C4::Context->preference("ReturnToShelvingCart") ) {
191 my $item = GetItem( $itemnumber );
192 $item->{'location'} = 'CART';
193 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
197 # save the return
199 ( $returned, $messages, $issueinformation, $borrower ) =
200 AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode); # do the return
202 # get biblio description
203 my $biblio = GetBiblioFromItemNumber($itemnumber);
204 # fix up item type for display
205 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
207 $template->param(
208 title => $biblio->{'title'},
209 homebranch => $biblio->{'homebranch'},
210 author => $biblio->{'author'},
211 itembarcode => $biblio->{'barcode'},
212 itemtype => $biblio->{'itemtype'},
213 ccode => $biblio->{'ccode'},
214 itembiblionumber => $biblio->{'biblionumber'},
217 my %input = (
218 counter => 0,
219 first => 1,
220 barcode => $barcode,
223 if ($returned) {
224 my $duedate = $issueinformation->{'date_due'};
225 $returneditems{0} = $barcode;
226 $riborrowernumber{0} = $borrower->{'borrowernumber'};
227 $riduedate{0} = $duedate;
228 $input{borrowernumber} = $borrower->{'borrowernumber'};
229 $input{duedate} = $duedate;
230 $input{return_overdue} = 1 if ($duedate and $duedate lt $today->output('iso'));
231 push( @inputloop, \%input );
233 elsif ( !$messages->{'BadBarcode'} ) {
234 $input{duedate} = 0;
235 $returneditems{0} = $barcode;
236 $riduedate{0} = 0;
237 if ( $messages->{'wthdrawn'} ) {
238 $input{withdrawn} = 1;
239 $input{borrowernumber} = 'Item Cancelled'; # FIXME: should be in display layer ?
240 $riborrowernumber{0} = 'Item Cancelled';
242 else {
243 $input{borrowernumber} = ' '; # This seems clearly bogus.
244 $riborrowernumber{0} = ' ';
246 push( @inputloop, \%input );
249 $template->param( inputloop => \@inputloop );
251 my $found = 0;
252 my $waiting = 0;
253 my $reserved = 0;
255 # new op dev : we check if the document must be returned to his homebranch directly,
256 # if the document is transfered, we have warning message .
258 if ( $messages->{'WasTransfered'} ) {
259 $template->param(
260 found => 1,
261 transfer => 1,
265 if ( $messages->{'NeedsTransfer'} ){
266 $template->param(
267 found => 1,
268 needstransfer => 1,
269 itemnumber => $itemnumber,
273 if ( $messages->{'Wrongbranch'} ){
274 $template->param(
275 wrongbranch => 1,
279 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
281 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
282 $template->param(
283 WrongTransfer => 1,
284 TransferWaitingAt => $messages->{'WrongTransfer'},
285 WrongTransferItem => $messages->{'WrongTransferItem'},
288 my $reserve = $messages->{'ResFound'};
289 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
290 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
291 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
292 $template->param(
293 wname => $name,
294 wborfirstname => $borr->{'firstname'},
295 wborsurname => $borr->{'surname'},
296 wbortitle => $borr->{'title'},
297 wborphone => $borr->{'phone'},
298 wboremail => $borr->{'email'},
299 wboraddress => $borr->{'address'},
300 wboraddress2 => $borr->{'address2'},
301 wborcity => $borr->{'city'},
302 wborzip => $borr->{'zipcode'},
303 wborrowernumber => $reserve->{'borrowernumber'},
304 wborcnum => $borr->{'cardnumber'},
305 wtransfertFrom => $userenv_branch,
310 # reserve found and item arrived at the expected branch
312 if ( $messages->{'ResFound'}) {
313 my $reserve = $messages->{'ResFound'};
314 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
315 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
317 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
318 if ( $reserve->{'ResFound'} eq "Waiting" ) {
319 $template->param(
320 waiting => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
322 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
323 $template->param(
324 intransit => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
325 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
326 resbarcode => $barcode,
327 reserved => 1,
331 # same params for Waiting or Reserved
332 $template->param(
333 found => 1,
334 currentbranch => $branches->{$userenv_branch}->{'branchname'},
335 destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
336 name => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
337 borfirstname => $borr->{'firstname'},
338 borsurname => $borr->{'surname'},
339 bortitle => $borr->{'title'},
340 borphone => $borr->{'phone'},
341 boremail => $borr->{'email'},
342 boraddress => $borr->{'address'},
343 boraddress2 => $borr->{'address2'},
344 borcity => $borr->{'city'},
345 borzip => $borr->{'zipcode'},
346 borcnum => $borr->{'cardnumber'},
347 debarred => $borr->{'debarred'},
348 gonenoaddress => $borr->{'gonenoaddress'},
349 barcode => $barcode,
350 destbranch => $reserve->{'branchcode'},
351 borrowernumber => $reserve->{'borrowernumber'},
352 itemnumber => $reserve->{'itemnumber'},
353 reservenotes => $reserve->{'reservenotes'},
355 } # else { ; } # error?
358 # Error Messages
359 my @errmsgloop;
360 foreach my $code ( keys %$messages ) {
361 my %err;
362 my $exit_required_p = 0;
363 if ( $code eq 'BadBarcode' ) {
364 $err{badbarcode} = 1;
365 $err{msg} = $messages->{'BadBarcode'};
367 elsif ( $code eq 'NotIssued' ) {
368 $err{notissued} = 1;
369 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
371 elsif ( $code eq 'WasLost' ) {
372 $err{waslost} = 1;
374 elsif ( $code eq 'ResFound' ) {
375 ; # FIXME... anything to do here?
377 elsif ( $code eq 'WasReturned' ) {
378 ; # FIXME... anything to do here?
380 elsif ( $code eq 'WasTransfered' ) {
381 ; # FIXME... anything to do here?
383 elsif ( $code eq 'wthdrawn' ) {
384 $err{withdrawn} = 1;
385 $exit_required_p = 1;
387 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
388 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
389 $err{ispermanent} = 1;
390 $err{msg} =
391 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
394 elsif ( $code eq 'WrongTransfer' ) {
395 ; # FIXME... anything to do here?
397 elsif ( $code eq 'WrongTransferItem' ) {
398 ; # FIXME... anything to do here?
400 elsif ( $code eq 'NeedsTransfer' ) {
402 elsif ( $code eq 'Wrongbranch' ) {
405 else {
406 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
407 # This forces the issue of staying in sync w/ Circulation.pm
409 if (%err) {
410 push( @errmsgloop, \%err );
412 last if $exit_required_p;
414 $template->param( errmsgloop => \@errmsgloop );
416 # patrontable ....
417 if ($borrower) {
418 my $flags = $borrower->{'flags'};
419 my @flagloop;
420 my $flagset;
421 foreach my $flag ( sort keys %$flags ) {
422 my %flaginfo;
423 unless ($flagset) { $flagset = 1; }
424 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
425 $flaginfo{flag} = $flag;
426 if ( $flag eq 'CHARGES' ) {
427 $flaginfo{msg} = $flag;
428 $flaginfo{charges} = 1;
429 $flaginfo{chargeamount} = $flags->{$flag}->{amount};
430 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
432 elsif ( $flag eq 'WAITING' ) {
433 $flaginfo{msg} = $flag;
434 $flaginfo{waiting} = 1;
435 my @waitingitemloop;
436 my $items = $flags->{$flag}->{'itemlist'};
437 foreach my $item (@$items) {
438 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
439 push @waitingitemloop, {
440 biblionum => $biblio->{'biblionumber'},
441 barcode => $biblio->{'barcode'},
442 title => $biblio->{'title'},
443 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
446 $flaginfo{itemloop} = \@waitingitemloop;
448 elsif ( $flag eq 'ODUES' ) {
449 my $items = $flags->{$flag}->{'itemlist'};
450 my @itemloop;
451 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
452 @$items )
454 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
455 push @itemloop, {
456 duedate => format_date($item->{'date_due'}),
457 biblionum => $biblio->{'biblionumber'},
458 barcode => $biblio->{'barcode'},
459 title => $biblio->{'title'},
460 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
463 $flaginfo{itemloop} = \@itemloop;
464 $flaginfo{overdue} = 1;
466 else {
467 $flaginfo{other} = 1;
468 $flaginfo{msg} = $flags->{$flag}->{'message'};
470 push( @flagloop, \%flaginfo );
472 $template->param(
473 flagset => $flagset,
474 flagloop => \@flagloop,
475 riborrowernumber => $borrower->{'borrowernumber'},
476 riborcnum => $borrower->{'cardnumber'},
477 riborsurname => $borrower->{'surname'},
478 ribortitle => $borrower->{'title'},
479 riborfirstname => $borrower->{'firstname'}
483 #set up so only the last 8 returned items display (make for faster loading pages)
484 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
485 my $count = 0;
486 my @riloop;
487 foreach ( sort { $a <=> $b } keys %returneditems ) {
488 my %ri;
489 if ( $count++ < $returned_counter ) {
490 my $barcode = $returneditems{$_};
491 my $duedate = $riduedate{$_};
492 my $overduetext;
493 my $borrowerinfo;
494 if ($duedate) {
495 my @tempdate = split( /-/, $duedate );
496 $ri{year} = $tempdate[0];
497 $ri{month} = $tempdate[1];
498 $ri{day} = $tempdate[2];
499 $ri{duedate} = format_date($duedate);
500 my ($borrower) = GetMemberDetails( $riborrowernumber{$_}, 0 );
501 $ri{return_overdue} = 1 if ($duedate lt $today->output('iso'));
502 $ri{borrowernumber} = $borrower->{'borrowernumber'};
503 $ri{borcnum} = $borrower->{'cardnumber'};
504 $ri{borfirstname} = $borrower->{'firstname'};
505 $ri{borsurname} = $borrower->{'surname'};
506 $ri{bortitle} = $borrower->{'title'};
507 $ri{bornote} = $borrower->{'borrowernotes'};
508 $ri{borcategorycode}= $borrower->{'categorycode'};
510 else {
511 $ri{borrowernumber} = $riborrowernumber{$_};
514 # my %ri;
515 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
516 # fix up item type for display
517 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
518 $ri{itembiblionumber} = $biblio->{'biblionumber'};
519 $ri{itemtitle} = $biblio->{'title'};
520 $ri{itemauthor} = $biblio->{'author'};
521 $ri{itemtype} = $biblio->{'itemtype'};
522 $ri{itemnote} = $biblio->{'itemnotes'};
523 $ri{ccode} = $biblio->{'ccode'};
524 $ri{itemnumber} = $biblio->{'itemnumber'};
525 $ri{barcode} = $barcode;
527 else {
528 last;
530 push( @riloop, \%ri );
533 $template->param(
534 riloop => \@riloop,
535 genbrname => $branches->{$userenv_branch}->{'branchname'},
536 genprname => $printers->{$printer}->{'printername'},
537 branchname => $branches->{$userenv_branch}->{'branchname'},
538 printer => $printer,
539 errmsgloop => \@errmsgloop,
540 exemptfine => $exemptfine,
541 dropboxmode => $dropboxmode,
542 dropboxdate => $dropboxdate->output(),
543 overduecharges => $overduecharges,
546 # actually print the page!
547 output_html_with_http_headers $query, $cookie, $template->output;