Cleanup stopwords.pl and .tmpl
[koha.git] / circ / returns.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #           2006 SAN-OP
5 #           2007 BibLibre, Paul POULAIN
6 #
7 # This file is part of Koha.
8 #
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.
13 #
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.
17 #
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
21
22 =head1 returns.pl
23
24 script to execute returns of books
25
26 =cut
27
28 use strict;
29 # use warnings; # FIXME
30
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
47 my $query = new CGI;
48
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;
56         }
57
58
59 #getting the template
60 my ( $template, $librarian, $cookie ) = get_template_and_user(
61     {
62         template_name   => "circ/returns.tmpl",
63         query           => $query,
64         type            => "intranet",
65         authnotrequired => 0,
66         flagsrequired   => { circulate => "circulate_remaining_permissions" },
67     }
68 );
69
70 #####################
71 #Global vars
72 my $branches = GetBranches();
73 my $printers = GetPrinters();
74
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');
78
79 my $userenv_branch = C4::Context->userenv->{'branch'} || '';
80 #
81 # Some code to handle the error if there is no branch or printer setting.....
82 #
83
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++;
98
99     # decode barcode
100     $barcode = barcodedecode($barcode) if(C4::Context->preference('itemBarcodeInputFilter'));
101
102     ######################
103     #Are these lines still useful ?
104     $returneditems{$counter}    = $barcode;
105     $riduedate{$counter}        = $duedate;
106     $riborrowernumber{$counter} = $borrowernumber;
107
108     #######################
109     $input{counter}        = $counter;
110     $input{barcode}        = $barcode;
111     $input{duedate}        = $duedate;
112     $input{borrowernumber} = $borrowernumber;
113     push( @inputloop, \%input );
114 }
115
116 ############
117 # Deal with the requests....
118
119 if ($query->param('WT-itemNumber')){
120         updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
121 }
122
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);
137
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,
152         );
153     }
154 }
155
156 my $borrower;
157 my $returned = 0;
158 my $messages;
159 my $issueinformation;
160 my $barcode     = $query->param('barcode');
161 my $exemptfine  = $query->param('exemptfine');
162 my $dropboxmode = $query->param('dropboxmode');
163 my $dotransfer  = $query->param('dotransfer');
164 my $calendar    = C4::Calendar->new( branchcode => $userenv_branch );
165         #dropbox: get last open day (today - 1)
166 my $today       = C4::Dates->new();
167 my $today_iso   = $today->output('iso');
168 my $dropboxdate = $calendar->addDate($today, -1);
169 if ($dotransfer){
170         # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
171         my $transferitem = $query->param('transferitem');
172         my $tobranch     = $query->param('tobranch');
173         ModItemTransfer($transferitem, $userenv_branch, $tobranch); 
174 }
175
176 # actually return book and prepare item table.....
177 if ($barcode) {
178     $barcode = barcodedecode($barcode) if C4::Context->preference('itemBarcodeInputFilter');
179 #
180 # save the return
181 #
182     ( $returned, $messages, $issueinformation, $borrower ) =
183       AddReturn( $barcode, $userenv_branch, $exemptfine, $dropboxmode);
184     # get biblio description
185     my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
186     # fix up item type for display
187     $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
188
189     $template->param(
190         title            => $biblio->{'title'},
191         homebranch       => $biblio->{'homebranch'},
192         author           => $biblio->{'author'},
193         itembarcode      => $biblio->{'barcode'},
194         itemtype         => $biblio->{'itemtype'},
195         ccode            => $biblio->{'ccode'},
196         itembiblionumber => $biblio->{'biblionumber'},    
197     );
198
199     my %input = (
200         counter => 0,
201         first   => 1,
202         barcode => $barcode,
203     );
204
205     if ($returned) {
206         $returneditems{0}    = $barcode;
207         $riborrowernumber{0} = $borrower->{'borrowernumber'};
208         $riduedate{0}        = $issueinformation->{'date_due'};
209         $input{borrowernumber} = $borrower->{'borrowernumber'};
210         $input{duedate}        = $issueinformation->{'date_due'};
211         $input{return_overdue} = 1 if ($issueinformation->{'date_due'} lt $today->output('iso'));
212         push( @inputloop, \%input );
213
214         # check if the branch is the same as homebranch
215         # if not, we want to put a message
216         if ( $biblio->{'homebranch'} ne $userenv_branch ) {
217             $template->param( homebranch => $biblio->{'homebranch'} );
218         }
219     }
220     elsif ( !$messages->{'BadBarcode'} ) {
221         $input{duedate}   = 0;
222         $returneditems{0} = $barcode;
223         $riduedate{0}     = 0;
224         if ( $messages->{'wthdrawn'} ) {
225             $input{withdrawn}      = 1;
226             $input{borrowernumber} = 'Item Cancelled';  # FIXME: should be in display layer ?
227             $riborrowernumber{0}   = 'Item Cancelled';
228         }
229         else {
230             $input{borrowernumber} = ' ';
231             $riborrowernumber{0}   = ' ';
232         }
233         push( @inputloop, \%input );
234     }
235 }
236 $template->param( inputloop => \@inputloop );
237
238 my $found    = 0;
239 my $waiting  = 0;
240 my $reserved = 0;
241
242 # new op dev : we check if the document must be returned to his homebranch directly,
243 #  if the document is transfered, we have warning message .
244
245 if ( $messages->{'WasTransfered'} ) {
246     $template->param(
247         found          => 1,
248         transfer       => 1,
249     );
250 }
251
252 if ( $messages->{'NeedsTransfer'} ){
253         $template->param(
254                 found          => 1,
255                 needstransfer  => 1,
256                 itemnumber => $issueinformation->{'itemnumber'}
257         );
258 }
259
260 if ( $messages->{'Wrongbranch'} ){
261         $template->param(
262                 wrongbranch => 1,
263         );
264 }
265
266 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
267
268 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
269         $template->param(
270         WrongTransfer  => 1,
271         TransferWaitingAt => $messages->{'WrongTransfer'},
272         WrongTransferItem => $messages->{'WrongTransferItem'},
273     );
274
275     my $reserve    = $messages->{'ResFound'};
276     my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
277     my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
278     my $name = $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
279     $template->param(
280             wname           => $name,
281             wborfirstname   => $borr->{'firstname'},
282             wborsurname     => $borr->{'surname'},
283             wbortitle       => $borr->{'title'},
284             wborphone       => $borr->{'phone'},
285             wboremail       => $borr->{'email'},
286             wboraddress     => $borr->{'address'},
287             wboraddress2    => $borr->{'address2'},
288             wborcity        => $borr->{'city'},
289             wborzip         => $borr->{'zipcode'},
290             wborrowernumber => $reserve->{'borrowernumber'},
291             wborcnum        => $borr->{'cardnumber'},
292             wtransfertFrom  => $userenv_branch,
293     );
294 }
295
296
297 #
298 # reserve found and item arrived at the expected branch
299 #
300 if ( $messages->{'ResFound'}) {
301     my $reserve    = $messages->{'ResFound'};
302     my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
303     my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
304
305     if ( $reserve->{'ResFound'} eq "Waiting" or $reserve->{'ResFound'} eq "Reserved" ) {
306         if ( $reserve->{'ResFound'} eq "Waiting" ) {
307             $template->param(
308                 waiting      => ($userenv_branch eq $reserve->{'branchcode'} ? 1 : 0 ),
309             );
310         } elsif ( $reserve->{'ResFound'} eq "Reserved" ) {
311             $template->param(
312                 intransit    => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
313                 transfertodo => ($userenv_branch eq $reserve->{'branchcode'} ? 0 : 1 ),
314                 resbarcode   => $barcode,
315                 reserved     => 1,
316             );
317         }
318
319         # same params for Waiting or Reserved
320         $template->param(
321             found          => 1,
322             currentbranch  => $branches->{$userenv_branch}->{'branchname'},
323             destbranchname => $branches->{ $reserve->{'branchcode'} }->{'branchname'},
324             name           => $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'},
325             borfirstname   => $borr->{'firstname'},
326             borsurname     => $borr->{'surname'},
327             bortitle       => $borr->{'title'},
328             borphone       => $borr->{'phone'},
329             boremail       => $borr->{'email'},
330             boraddress     => $borr->{'address'},
331             boraddress2    => $borr->{'address2'},
332             borcity        => $borr->{'city'},
333             borzip         => $borr->{'zipcode'},
334             borcnum        => $borr->{'cardnumber'},
335             debarred       => $borr->{'debarred'},
336             gonenoaddress  => $borr->{'gonenoaddress'},
337             barcode        => $barcode,
338             destbranch     => $reserve->{'branchcode'},
339             borrowernumber => $reserve->{'borrowernumber'},
340             itemnumber     => $reserve->{'itemnumber'},
341         );
342     } # else { ; }  # error?
343 }
344
345 # Error Messages
346 my @errmsgloop;
347 foreach my $code ( keys %$messages ) {
348
349     #    warn $code;
350     my %err;
351     my $exit_required_p = 0;
352     if ( $code eq 'BadBarcode' ) {
353         $err{badbarcode} = 1;
354         $err{msg}        = $messages->{'BadBarcode'};
355     }
356     elsif ( $code eq 'NotIssued' ) {
357         $err{notissued} = 1;
358         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
359     }
360     elsif ( $code eq 'WasLost' ) {
361         $err{waslost} = 1;
362     }
363     elsif ( $code eq 'ResFound' ) {
364         ;    # FIXME... anything to do here?
365     }
366     elsif ( $code eq 'WasReturned' ) {
367         ;    # FIXME... anything to do here?
368     }
369     elsif ( $code eq 'WasTransfered' ) {
370         ;    # FIXME... anything to do here?
371     }
372     elsif ( $code eq 'wthdrawn' ) {
373         $err{withdrawn} = 1;
374         $exit_required_p = 1;
375     }
376     elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
377         if ( $messages->{'IsPermanent'} ne $userenv_branch ) {
378             $err{ispermanent} = 1;
379             $err{msg}         =
380               $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
381         }
382     }
383     elsif ( $code eq 'WrongTransfer' ) {
384         ;    # FIXME... anything to do here?
385     }
386     elsif ( $code eq 'WrongTransferItem' ) {
387         ;    # FIXME... anything to do here?
388     }
389     elsif ( $code eq 'NeedsTransfer' ) {
390     }
391     elsif ( $code eq 'Wrongbranch' ) {
392     }
393                 
394     else {
395         die "Unknown error code $code";    # XXX
396     }
397     if (%err) {
398         push( @errmsgloop, \%err );
399     }
400     last if $exit_required_p;
401 }
402 $template->param( errmsgloop => \@errmsgloop );
403
404 # patrontable ....
405 if ($borrower) {
406     my $flags = $borrower->{'flags'};
407     my @flagloop;
408     my $flagset;
409     foreach my $flag ( sort keys %$flags ) {
410         my %flaginfo;
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{chargeamount}   = $flags->{$flag}->{amount};
418             $flaginfo{borrowernumber} = $borrower->{borrowernumber};
419         }
420         elsif ( $flag eq 'WAITING' ) {
421             $flaginfo{msg}     = $flag;
422             $flaginfo{waiting} = 1;
423             my @waitingitemloop;
424             my $items = $flags->{$flag}->{'itemlist'};
425             foreach my $item (@$items) {
426                 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
427                 push @waitingitemloop, {
428                     biblionum => $biblio->{'biblionumber'},
429                     barcode   => $biblio->{'barcode'},
430                     title     => $biblio->{'title'},
431                     brname    => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
432                 };
433             }
434             $flaginfo{itemloop} = \@waitingitemloop;
435         }
436         elsif ( $flag eq 'ODUES' ) {
437             my $items = $flags->{$flag}->{'itemlist'};
438             my @itemloop;
439             foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
440                 @$items )
441             {
442                 my $biblio = GetBiblioFromItemNumber( $item->{'itemnumber'});
443                 push @itemloop, {
444                     duedate   => format_date($item->{'date_due'}),
445                     biblionum => $biblio->{'biblionumber'},
446                     barcode   => $biblio->{'barcode'},
447                     title     => $biblio->{'title'},
448                     brname    => $branches->{ $biblio->{'holdingbranch'} }->{'branchname'},
449                 };
450             }
451             $flaginfo{itemloop} = \@itemloop;
452             $flaginfo{overdue}  = 1;
453         }
454         else {
455             $flaginfo{other} = 1;
456             $flaginfo{msg}   = $flags->{$flag}->{'message'};
457         }
458         push( @flagloop, \%flaginfo );
459     }
460     $template->param(
461         flagset          => $flagset,
462         flagloop         => \@flagloop,
463         riborrowernumber => $borrower->{'borrowernumber'},
464         riborcnum        => $borrower->{'cardnumber'},
465         riborsurname     => $borrower->{'surname'},
466         ribortitle       => $borrower->{'title'},
467         riborfirstname   => $borrower->{'firstname'}
468     );
469 }
470
471 #set up so only the last 8 returned items display (make for faster loading pages)
472 my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C4::Context->preference('numReturnedItemsToShow') : 8;
473 my $count = 0;
474 my @riloop;
475 foreach ( sort { $a <=> $b } keys %returneditems ) {
476     my %ri;
477     if ( $count++ < $returned_counter ) {
478         my $barcode = $returneditems{$_};
479         my $duedate = $riduedate{$_};
480         my $overduetext;
481         my $borrowerinfo;
482         if ($duedate) {
483             my @tempdate = split( /-/, $duedate );
484             $ri{year}  = $tempdate[0];
485             $ri{month} = $tempdate[1];
486             $ri{day}   = $tempdate[2];
487             $ri{duedate} = format_date($duedate);
488             my ($borrower) = GetMemberDetails( $riborrowernumber{$_}, 0 );
489             $ri{return_overdue} = 1 if ($duedate lt $today->output('iso'));
490             $ri{borrowernumber} = $borrower->{'borrowernumber'};
491             $ri{borcnum}        = $borrower->{'cardnumber'};
492             $ri{borfirstname}   = $borrower->{'firstname'};
493             $ri{borsurname}     = $borrower->{'surname'};
494             $ri{bortitle}       = $borrower->{'title'};
495             $ri{bornote}        = $borrower->{'borrowernotes'};
496             $ri{borcategorycode}= $borrower->{'categorycode'};
497         }
498         else {
499             $ri{borrowernumber} = $riborrowernumber{$_};
500         }
501
502         #        my %ri;
503         my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
504         # fix up item type for display
505         $biblio->{'itemtype'} = C4::Context->preference('item-level_itypes') ? $biblio->{'itype'} : $biblio->{'itemtype'};
506         $ri{itembiblionumber} = $biblio->{'biblionumber'};
507         $ri{itemtitle}        = $biblio->{'title'};
508         $ri{itemauthor}       = $biblio->{'author'};
509         $ri{itemtype}         = $biblio->{'itemtype'};
510         $ri{itemnote}         = $biblio->{'itemnotes'};
511         $ri{ccode}            = $biblio->{'ccode'};
512         $ri{itemnumber}       = $biblio->{'itemnumber'};
513         $ri{barcode}          = $barcode;
514     }
515     else {
516         last;
517     }
518     push( @riloop, \%ri );
519 }
520
521 $template->param(
522     riloop         => \@riloop,
523     genbrname      => $branches->{$userenv_branch}->{'branchname'},
524     genprname      => $printers->{$printer}->{'printername'},
525     branchname     => $branches->{$userenv_branch}->{'branchname'},
526     printer        => $printer,
527     errmsgloop     => \@errmsgloop,
528     exemptfine     => $exemptfine,
529     dropboxmode    => $dropboxmode,
530     dropboxdate    => $dropboxdate->output(),
531     overduecharges => $overduecharges,
532 );
533
534 # actually print the page!
535 output_html_with_http_headers $query, $cookie, $template->output;