More fixes to the shelves test
[koha.git] / circ / returns.pl
blob1ed378b27327d8e56ddebb1cdd3592dd08bb299c
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 ?
46 use C4::RotatingCollections;
48 my $query = new CGI;
50 if (!C4::Context->userenv){
51 my $sessionID = $query->cookie("CGISESSID");
52 my $session = get_session($sessionID);
53 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
54 # no branch set we can't return
55 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
56 exit;
60 #getting the template
61 my ( $template, $librarian, $cookie ) = get_template_and_user(
63 template_name => "circ/returns.tmpl",
64 query => $query,
65 type => "intranet",
66 authnotrequired => 0,
67 flagsrequired => { circulate => "circulate_remaining_permissions" },
71 #####################
72 #Global vars
73 my $branches = GetBranches();
74 my $printers = GetPrinters();
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 my $counter;
91 if (/ri-(\d*)/) {
92 $counter = $1;
93 if ($counter > 20) {
94 next;
97 else {
98 next;
101 my %input;
102 my $barcode = $query->param("ri-$counter");
103 my $duedate = $query->param("dd-$counter");
104 my $borrowernumber = $query->param("bn-$counter");
105 $counter++;
107 # decode barcode ## Didn't we already decode them before passing them back last time??
108 $barcode = barcodedecode($barcode) if(C4::Context->preference('itemBarcodeInputFilter'));
110 ######################
111 #Are these lines still useful ?
112 $returneditems{$counter} = $barcode;
113 $riduedate{$counter} = $duedate;
114 $riborrowernumber{$counter} = $borrowernumber;
116 #######################
117 $input{counter} = $counter;
118 $input{barcode} = $barcode;
119 $input{duedate} = $duedate;
120 $input{borrowernumber} = $borrowernumber;
121 push( @inputloop, \%input );
124 ############
125 # Deal with the requests....
127 if ($query->param('WT-itemNumber')){
128 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
131 if ( $query->param('resbarcode') ) {
132 my $item = $query->param('itemnumber');
133 my $borrowernumber = $query->param('borrowernumber');
134 my $resbarcode = $query->param('resbarcode');
135 my $diffBranchReturned = $query->param('diffBranch');
136 my $iteminfo = GetBiblioFromItemNumber($item);
137 # fix up item type for display
138 $iteminfo->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $iteminfo->{'itype'} : $iteminfo->{'itemtype'};
139 my $diffBranchSend = ($userenv_branch ne $diffBranchReturned) ? $diffBranchReturned : undef;
140 # diffBranchSend tells ModReserveAffect whether document is expected in this library or not,
141 # i.e., whether to apply waiting status
142 ModReserveAffect( $item, $borrowernumber, $diffBranchSend);
143 # check if we have other reserves for this document, if we have a return send the message of transfer
144 my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
146 my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
147 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
148 if ( $messages->{'transfert'} ) {
149 $template->param(
150 itemtitle => $iteminfo->{'title'},
151 itembiblionumber => $iteminfo->{'biblionumber'},
152 iteminfo => $iteminfo->{'author'},
153 tobranchname => GetBranchName($messages->{'transfert'}),
154 name => $name,
155 borrowernumber => $borrowernumber,
156 borcnum => $borr->{'cardnumber'},
157 borfirstname => $borr->{'firstname'},
158 borsurname => $borr->{'surname'},
159 diffbranch => 1,
164 my $borrower;
165 my $returned = 0;
166 my $messages;
167 my $issueinformation;
168 my $itemnumber;
169 my $barcode = $query->param('barcode');
170 my $exemptfine = $query->param('exemptfine');
171 my $dropboxmode = $query->param('dropboxmode');
172 my $dotransfer = $query->param('dotransfer');
173 my $calendar = C4::Calendar->new( branchcode => $userenv_branch );
174 #dropbox: get last open day (today - 1)
175 my $today = C4::Dates->new();
176 my $today_iso = $today->output('iso');
177 my $dropboxdate = $calendar->addDate($today, -1);
178 if ($dotransfer){
179 # An item has been returned to a branch other than the homebranch, and the librarian has chosen to initiate a transfer
180 my $transferitem = $query->param('transferitem');
181 my $tobranch = $query->param('tobranch');
182 ModItemTransfer($transferitem, $userenv_branch, $tobranch);
185 # actually return book and prepare item table.....
186 if ($barcode) {
187 $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
188 $itemnumber = GetItemnumberFromBarcode($barcode);
190 if ( C4::Context->preference("InProcessingToShelvingCart") ) {
191 my $item = GetItem( $itemnumber );
192 if ( $item->{'location'} eq 'PROC' ) {
193 $item->{'location'} = 'CART';
194 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
198 if ( C4::Context->preference("ReturnToShelvingCart") ) {
199 my $item = GetItem( $itemnumber );
200 $item->{'location'} = 'CART';
201 ModItem( $item, $item->{'biblionumber'}, $item->{'itemnumber'} );
205 # save the return
207 ( $returned, $messages, $issueinformation, $borrower ) =
208 AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode); # do the return
210 # get biblio description
211 my $biblio = GetBiblioFromItemNumber($itemnumber);
212 # fix up item type for display
213 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
215 $template->param(
216 title => $biblio->{'title'},
217 homebranch => $biblio->{'homebranch'},
218 author => $biblio->{'author'},
219 itembarcode => $biblio->{'barcode'},
220 itemtype => $biblio->{'itemtype'},
221 ccode => $biblio->{'ccode'},
222 itembiblionumber => $biblio->{'biblionumber'},
225 my %input = (
226 counter => 0,
227 first => 1,
228 barcode => $barcode,
231 if ($returned) {
232 my $duedate = $issueinformation->{'date_due'};
233 $returneditems{0} = $barcode;
234 $riborrowernumber{0} = $borrower->{'borrowernumber'};
235 $riduedate{0} = $duedate;
236 $input{borrowernumber} = $borrower->{'borrowernumber'};
237 $input{duedate} = $duedate;
238 $input{return_overdue} = 1 if ($duedate and $duedate lt $today->output('iso'));
239 push( @inputloop, \%input );
241 elsif ( !$messages->{'BadBarcode'} ) {
242 $input{duedate} = 0;
243 $returneditems{0} = $barcode;
244 $riduedate{0} = 0;
245 if ( $messages->{'wthdrawn'} ) {
246 $input{withdrawn} = 1;
247 $input{borrowernumber} = 'Item Cancelled'; # FIXME: should be in display layer ?
248 $riborrowernumber{0} = 'Item Cancelled';
250 else {
251 $input{borrowernumber} = ' '; # This seems clearly bogus.
252 $riborrowernumber{0} = ' ';
254 push( @inputloop, \%input );
257 $template->param( inputloop => \@inputloop );
259 my $found = 0;
260 my $waiting = 0;
261 my $reserved = 0;
263 # new op dev : we check if the document must be returned to his homebranch directly,
264 # if the document is transfered, we have warning message .
266 if ( $messages->{'WasTransfered'} ) {
267 $template->param(
268 found => 1,
269 transfer => 1,
273 if ( $messages->{'NeedsTransfer'} ){
274 $template->param(
275 found => 1,
276 needstransfer => 1,
277 itemnumber => $itemnumber,
281 if ( $messages->{'Wrongbranch'} ){
282 $template->param(
283 wrongbranch => 1,
287 # case of wrong transfert, if the document wasn't transfered to the right library (according to branchtransfer (tobranch) BDD)
289 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
290 $template->param(
291 WrongTransfer => 1,
292 TransferWaitingAt => $messages->{'WrongTransfer'},
293 WrongTransferItem => $messages->{'WrongTransferItem'},
296 my $reserve = $messages->{'ResFound'};
297 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
298 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
299 my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
300 $template->param(
301 wname => $name,
302 wborfirstname => $borr->{'firstname'},
303 wborsurname => $borr->{'surname'},
304 wbortitle => $borr->{'title'},
305 wborphone => $borr->{'phone'},
306 wboremail => $borr->{'email'},
307 wboraddress => $borr->{'address'},
308 wboraddress2 => $borr->{'address2'},
309 wborcity => $borr->{'city'},
310 wborzip => $borr->{'zipcode'},
311 wborrowernumber => $reserve->{'borrowernumber'},
312 wborcnum => $borr->{'cardnumber'},
313 wtransfertFrom => $userenv_branch,
318 # reserve found and item arrived at the expected branch
320 if ( $messages->{'ResFound'}) {
321 my $reserve = $messages->{'ResFound'};
322 my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
323 my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
325 if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
326 if ( $reserve->{'ResFound'} eq "Waiting" ) {
327 $template->param(
328 waiting => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
330 } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
331 $template->param(
332 intransit => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
333 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
334 resbarcode => $barcode,
335 reserved => 1,
339 # same params for Waiting or Reserved
340 $template->param(
341 found => 1,
342 currentbranch => $branches->{$userenv_branch}->{'branchname'},
343 destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
344 name => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
345 borfirstname => $borr->{'firstname'},
346 borsurname => $borr->{'surname'},
347 bortitle => $borr->{'title'},
348 borphone => $borr->{'phone'},
349 boremail => $borr->{'email'},
350 boraddress => $borr->{'address'},
351 boraddress2 => $borr->{'address2'},
352 borcity => $borr->{'city'},
353 borzip => $borr->{'zipcode'},
354 borcnum => $borr->{'cardnumber'},
355 debarred => $borr->{'debarred'},
356 gonenoaddress => $borr->{'gonenoaddress'},
357 barcode => $barcode,
358 destbranch => $reserve->{'branchcode'},
359 borrowernumber => $reserve->{'borrowernumber'},
360 itemnumber => $reserve->{'itemnumber'},
361 reservenotes => $reserve->{'reservenotes'},
363 } # else { ; } # error?
366 # Error Messages
367 my @errmsgloop;
368 foreach my $code ( keys %$messages ) {
369 my %err;
370 my $exit_required_p = 0;
371 if ( $code eq 'BadBarcode' ) {
372 $err{badbarcode} = 1;
373 $err{msg} = $messages->{'BadBarcode'};
375 elsif ( $code eq 'NotIssued' ) {
376 $err{notissued} = 1;
377 $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
379 elsif ( $code eq 'WasLost' ) {
380 $err{waslost} = 1;
382 elsif ( $code eq 'ResFound' ) {
383 ; # FIXME... anything to do here?
385 elsif ( $code eq 'WasReturned' ) {
386 ; # FIXME... anything to do here?
388 elsif ( $code eq 'WasTransfered' ) {
389 ; # FIXME... anything to do here?
391 elsif ( $code eq 'wthdrawn' ) {
392 $err{withdrawn} = 1;
393 $exit_required_p = 1;
395 elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
396 if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
397 $err{ispermanent} = 1;
398 $err{msg} =
399 $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
402 elsif ( $code eq 'WrongTransfer' ) {
403 ; # FIXME... anything to do here?
405 elsif ( $code eq 'WrongTransferItem' ) {
406 ; # FIXME... anything to do here?
408 elsif ( $code eq 'NeedsTransfer' ) {
410 elsif ( $code eq 'Wrongbranch' ) {
413 else {
414 die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die.
415 # This forces the issue of staying in sync w/ Circulation.pm
417 if (%err) {
418 push( @errmsgloop, \%err );
420 last if $exit_required_p;
422 $template->param( errmsgloop => \@errmsgloop );
424 # patrontable ....
425 if ($borrower) {
426 my $flags = $borrower->{'flags'};
427 my @flagloop;
428 my $flagset;
429 foreach my $flag ( sort keys %$flags ) {
430 my %flaginfo;
431 unless ($flagset) { $flagset = 1; }
432 $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
433 $flaginfo{flag} = $flag;
434 if ( $flag eq 'CHARGES' ) {
435 $flaginfo{msg} = $flag;
436 $flaginfo{charges} = 1;
437 $flaginfo{chargeamount} = $flags->{$flag}->{amount};
438 $flaginfo{borrowernumber} = $borrower->{borrowernumber};
440 elsif ( $flag eq 'WAITING' ) {
441 $flaginfo{msg} = $flag;
442 $flaginfo{waiting} = 1;
443 my @waitingitemloop;
444 my $items = $flags->{$flag}->{'itemlist'};
445 foreach my $item (@$items) {
446 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
447 push @waitingitemloop, {
448 biblionum => $biblio->{'biblionumber'},
449 barcode => $biblio->{'barcode'},
450 title => $biblio->{'title'},
451 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
454 $flaginfo{itemloop} = \@waitingitemloop;
456 elsif ( $flag eq 'ODUES' ) {
457 my $items = $flags->{$flag}->{'itemlist'};
458 my @itemloop;
459 foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
460 @$items )
462 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
463 push @itemloop, {
464 duedate => format_date($item->{'date_due'}),
465 biblionum => $biblio->{'biblionumber'},
466 barcode => $biblio->{'barcode'},
467 title => $biblio->{'title'},
468 brname => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
471 $flaginfo{itemloop} = \@itemloop;
472 $flaginfo{overdue} = 1;
474 else {
475 $flaginfo{other} = 1;
476 $flaginfo{msg} = $flags->{$flag}->{'message'};
478 push( @flagloop, \%flaginfo );
480 $template->param(
481 flagset => $flagset,
482 flagloop => \@flagloop,
483 riborrowernumber => $borrower->{'borrowernumber'},
484 riborcnum => $borrower->{'cardnumber'},
485 riborsurname => $borrower->{'surname'},
486 ribortitle => $borrower->{'title'},
487 riborfirstname => $borrower->{'firstname'}
491 #set up so only the last 8 returned items display (make for faster loading pages)
492 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
493 my $count = 0;
494 my @riloop;
495 foreach ( sort { $a <=> $b } keys %returneditems ) {
496 my %ri;
497 if ( $count++ < $returned_counter ) {
498 my $bar_code = $returneditems{$_};
499 my $duedate = $riduedate{$_};
500 if ($duedate) {
501 my @tempdate = split( /-/, $duedate );
502 $ri{year} = $tempdate[0];
503 $ri{month} = $tempdate[1];
504 $ri{day} = $tempdate[2];
505 $ri{duedate} = format_date($duedate);
506 my ($b) = GetMemberDetails( $riborrowernumber{$_}, 0 );
507 $ri{return_overdue} = 1 if ($duedate lt $today->output('iso'));
508 $ri{borrowernumber} = $b->{'borrowernumber'};
509 $ri{borcnum} = $b->{'cardnumber'};
510 $ri{borfirstname} = $b->{'firstname'};
511 $ri{borsurname} = $b->{'surname'};
512 $ri{bortitle} = $b->{'title'};
513 $ri{bornote} = $b->{'borrowernotes'};
514 $ri{borcategorycode}= $b->{'categorycode'};
516 else {
517 $ri{borrowernumber} = $riborrowernumber{$_};
520 # my %ri;
521 my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($bar_code));
522 # fix up item type for display
523 $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
524 $ri{itembiblionumber} = $biblio->{'biblionumber'};
525 $ri{itemtitle} = $biblio->{'title'};
526 $ri{itemauthor} = $biblio->{'author'};
527 $ri{itemtype} = $biblio->{'itemtype'};
528 $ri{itemnote} = $biblio->{'itemnotes'};
529 $ri{ccode} = $biblio->{'ccode'};
530 $ri{itemnumber} = $biblio->{'itemnumber'};
531 $ri{barcode} = $bar_code;
533 else {
534 last;
536 push @riloop, \%ri;
539 $template->param(
540 riloop => \@riloop,
541 genbrname => $branches->{$userenv_branch}->{'branchname'},
542 genprname => $printers->{$printer}->{'printername'},
543 branchname => $branches->{$userenv_branch}->{'branchname'},
544 printer => $printer,
545 errmsgloop => \@errmsgloop,
546 exemptfine => $exemptfine,
547 dropboxmode => $dropboxmode,
548 dropboxdate => $dropboxdate->output(),
549 overduecharges => $overduecharges,
552 # actually print the page!
553 output_html_with_http_headers $query, $cookie, $template->output;