adding 'exempt fine on return' option.
[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 CGI;
30 use C4::Context;
31 use C4::Auth;
32 use C4::Output;
33 use C4::Circulation;
34 use C4::Dates qw/format_date/;
35 use C4::Print;
36 use C4::Reserves;
37 use C4::Biblio;
38 use C4::Members;
39 use C4::Branch; # GetBranchName
40 use C4::Koha;   # FIXME : is it still useful ?
41
42 my $query = new CGI;
43
44 #getting the template
45 my ( $template, $librarian, $cookie ) = get_template_and_user(
46     {
47         template_name   => "circ/returns.tmpl",
48         query           => $query,
49         type            => "intranet",
50         authnotrequired => 0,
51         flagsrequired   => { circulate => 1 },
52     }
53 );
54
55 #####################
56 #Global vars
57 my $branches = GetBranches();
58 my $printers = GetPrinters();
59
60 #my $branch  = C4::Context->userenv?C4::Context->userenv->{'branch'}:"";
61 my $printer = C4::Context->userenv?C4::Context->userenv->{'branchprinter'}:"";
62 my $overduecharges = (C4::Context->preference('finesMode') && C4::Context->preference('finesMode') ne 'off');
63 #
64 # Some code to handle the error if there is no branch or printer setting.....
65 #
66
67 # Set up the item stack ....
68 my %returneditems;
69 my %riduedate;
70 my %riborrowernumber;
71 my @inputloop;
72 foreach ( $query->param ) {
73     (next) unless (/ri-(\d*)/);
74     my %input;
75     my $counter = $1;
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");
80     $counter++;
81
82     # decode cuecat
83     $barcode = cuecatbarcodedecode($barcode);
84
85     ######################
86     #Are these lines still useful ?
87     $returneditems{$counter}    = $barcode;
88     $riduedate{$counter}        = $duedate;
89     $riborrowernumber{$counter} = $borrowernumber;
90
91     #######################
92     $input{counter}        = $counter;
93     $input{barcode}        = $barcode;
94     $input{duedate}        = $duedate;
95     $input{borrowernumber} = $borrowernumber;
96     push( @inputloop, \%input );
97 }
98
99 ############
100 # Deal with the requests....
101
102 if ($query->param('WT-itemNumber')){
103         updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
104 }
105
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');
111     # set to waiting....
112     my $iteminfo   = GetBiblioFromItemNumber($item);
113     my $diffBranchSend;
114     
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;
119     }
120     else {
121         $diffBranchSend = undef;
122     }
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);
126
127     my $branchname = GetBranchName( $messages->{'transfert'} );
128     my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
129     my $borcnum = $borr->{'cardnumber'};
130     my $name    =
131       $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
132     my $slip = $query->param('resslip');
133
134
135     if ( $messages->{'transfert'} ) {
136         $template->param(
137             itemtitle      => $iteminfo->{'title'},
138                         itembiblionumber => $iteminfo->{'biblionumber'},
139             iteminfo       => $iteminfo->{'author'},
140             tobranchname   => $branchname,
141             name           => $name,
142             borrowernumber => $borrowernumber,
143             borcnum        => $borcnum,
144             borfirstname   => $borr->{'firstname'},
145             borsurname     => $borr->{'surname'},
146             diffbranch     => 1
147         );
148     }
149 }
150
151 my $borrower;
152 my $returned = 0;
153 my $messages;
154 my $issueinformation;
155 my $barcode = $query->param('barcode');
156 my $exemptfine = $query->param('exemptfine');
157
158 my $dotransfer = $query->param('dotransfer');
159 if ($dotransfer){
160         # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
161         my $transferitem=$query->param('transferitem');
162         my $tobranch=$query->param('tobranch');
163         ModItemTransfer($transferitem, C4::Context->userenv->{'branch'}, $tobranch); 
164 }
165
166 # actually return book and prepare item table.....
167 if ($barcode) {
168     # decode cuecat
169     $barcode = cuecatbarcodedecode($barcode);
170 #
171 # save the return
172 #
173     ( $returned, $messages, $issueinformation, $borrower ) =
174       AddReturn( $barcode, C4::Context->userenv->{'branch'}, $exemptfine );
175     # get biblio description
176     my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
177     $template->param(
178         title            => $biblio->{'title'},
179         homebranch       => $biblio->{'homebranch'},
180         author           => $biblio->{'author'},
181         itembarcode      => $biblio->{'barcode'},
182         itemtype         => $biblio->{'itemtype'},
183         ccode            => $biblio->{'ccode'},
184         itembiblionumber => $biblio->{'biblionumber'},    
185     );
186     if ($returned) {
187         $returneditems{0}    = $barcode;
188         $riborrowernumber{0} = $borrower->{'borrowernumber'};
189         $riduedate{0}        = $issueinformation->{'date_due'};
190         my %input;
191         $input{counter}        = 0;
192         $input{first}          = 1;
193         $input{barcode}        = $barcode;
194         $input{duedate}        = $riduedate{0};
195         $input{borrowernumber} = $riborrowernumber{0};
196         push( @inputloop, \%input );
197
198         # check if the branch is the same as homebranch
199         # if not, we want to put a message
200         if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
201             $template->param( homebranch => $biblio->{'homebranch'} );
202         }
203     }
204     elsif ( !$messages->{'BadBarcode'} ) {
205         my %input;
206         $input{counter} = 0;
207         $input{first}   = 1;
208         $input{barcode} = $barcode;
209         $input{duedate} = 0;
210
211         $returneditems{0} = $barcode;
212         $riduedate{0}     = 0;
213         if ( $messages->{'wthdrawn'} ) {
214             $input{withdrawn}      = 1;
215             $input{borrowernumber} = "Item Cancelled";
216             $riborrowernumber{0}   = 'Item Cancelled';
217         }
218         else {
219             $input{borrowernumber} = " ";
220             $riborrowernumber{0} = ' ';
221         }
222         push( @inputloop, \%input );
223     }
224 }
225 $template->param( inputloop => \@inputloop );
226
227 my $found    = 0;
228 my $waiting  = 0;
229 my $reserved = 0;
230
231 # new op dev : we check if the document must be returned to his homebranch directly,
232 #  if the document is transfered, we have warning message .
233
234 if ( $messages->{'WasTransfered'} ) {
235     $template->param(
236         found          => 1,
237         transfer       => 1,
238     );
239 }
240
241 if ( $messages->{'NeedsTransfer'} ){
242         $template->param(
243                 found          => 1,
244                 needstransfer  => 1,
245                 itemnumber => $issueinformation->{'itemnumber'}
246         );
247 }
248
249 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
250
251 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
252         $template->param(
253         WrongTransfer  => 1,
254         TransferWaitingAt => $messages->{'WrongTransfer'},
255         WrongTransferItem => $messages->{'WrongTransferItem'},
256     );
257
258     my $reserve        = $messages->{'ResFound'};
259     my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
260     my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
261     my $name =
262       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
263         $template->param(
264             wname           => $name,
265             wborfirstname   => $borr->{'firstname'},
266             wborsurname     => $borr->{'surname'},
267             wbortitle       => $borr->{'title'},
268             wborphone       => $borr->{'phone'},
269             wboremail       => $borr->{'email'},
270             wboraddress  => $borr->{'address'},
271             wboraddress2 => $borr->{'address2'},
272             wborcity        => $borr->{'city'},
273             wborzip         => $borr->{'zipcode'},
274             wborrowernumber => $reserve->{'borrowernumber'},
275             wborcnum        => $borr->{'cardnumber'},
276             wtransfertFrom    => C4::Context->userenv->{'branch'},
277         );
278 }
279
280
281 #
282 # reserve found and item arrived at the expected branch
283 #
284 if ( $messages->{'ResFound'}) {
285     my $reserve        = $messages->{'ResFound'};
286     my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
287     my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
288     if ( $reserve->{'ResFound'} eq "Waiting" ) {
289         if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
290             $template->param( waiting => 1 );
291         }
292         else {
293             $template->param( waiting => 0 );
294         }
295
296         $template->param(
297             found          => 1,
298             name           => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
299             borfirstname   => $borr->{'firstname'},
300             borsurname     => $borr->{'surname'},
301             bortitle       => $borr->{'title'},
302             borphone       => $borr->{'phone'},
303             boremail       => $borr->{'email'},
304             boraddress  => $borr->{'address'},
305             boraddress2  => $borr->{'address2'},
306             borcity        => $borr->{'city'},
307             borzip         => $borr->{'zipcode'},
308             borrowernumber => $reserve->{'borrowernumber'},
309             borcnum        => $borr->{'cardnumber'},
310             debarred       => $borr->{'debarred'},
311             gonenoaddress  => $borr->{'gonenoaddress'},
312             currentbranch  => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
313             itemnumber       => $reserve->{'itemnumber'},
314             barcode     => $barcode,
315         );
316
317     }
318     if ( $reserve->{'ResFound'} eq "Reserved" ) {
319         my @da         = localtime( time() );
320         my $todaysdate =
321             sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
322           . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
323           . ( $da[5] + 1900 );
324
325         if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
326             $template->param( intransit => 0 );
327         }
328         else {
329             $template->param( intransit => 1 );
330         }
331
332         $template->param(
333             found          => 1,
334             currentbranch  => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
335             destbranchname =>
336               $branches->{ $reserve->{'branchcode'} }->{'branchname'},
337             destbranch     => $reserve->{'branchcode'},
338             transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
339             reserved => 1,
340             resbarcode       => $barcode,
341             today            => $todaysdate,
342             itemnumber       => $reserve->{'itemnumber'},
343             borsurname       => $borr->{'surname'},
344             bortitle         => $borr->{'title'},
345             borfirstname     => $borr->{'firstname'},
346             borrowernumber   => $reserve->{'borrowernumber'},
347             borcnum          => $borr->{'cardnumber'},
348             borphone         => $borr->{'phone'},
349             boraddress    => $borr->{'address'},
350             boraddress2    => $borr->{'address2'},
351             borsub           => $borr->{'suburb'},
352             borcity          => $borr->{'city'},
353             borzip           => $borr->{'zipcode'},
354             boremail         => $borr->{'email'},
355             debarred         => $borr->{'debarred'},
356             gonenoaddress    => $borr->{'gonenoaddress'},
357             barcode          => $barcode
358         );
359     }
360 }
361
362 # Error Messages
363 my @errmsgloop;
364 foreach my $code ( keys %$messages ) {
365
366     #    warn $code;
367     my %err;
368     my $exit_required_p = 0;
369     if ( $code eq 'BadBarcode' ) {
370         $err{badbarcode} = 1;
371         $err{msg}        = $messages->{'BadBarcode'};
372     }
373     elsif ( $code eq 'NotIssued' ) {
374         $err{notissued} = 1;
375         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
376     }
377     elsif ( $code eq 'WasLost' ) {
378         $err{waslost} = 1;
379     }
380     elsif ( $code eq 'ResFound' ) {
381         ;    # FIXME... anything to do here?
382     }
383     elsif ( $code eq 'WasReturned' ) {
384         ;    # FIXME... anything to do here?
385     }
386     elsif ( $code eq 'WasTransfered' ) {
387         ;    # FIXME... anything to do here?
388     }
389     elsif ( $code eq 'wthdrawn' ) {
390         $err{withdrawn} = 1;
391         $exit_required_p = 1;
392     }
393     elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
394         if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
395             $err{ispermanent} = 1;
396             $err{msg}         =
397               $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
398         }
399     }
400     elsif ( $code eq 'WrongTransfer' ) {
401         ;    # FIXME... anything to do here?
402     }
403     elsif ( $code eq 'WrongTransferItem' ) {
404         ;    # FIXME... anything to do here?
405     }
406         elsif ( $code eq 'NeedsTransfer' ) {
407         }
408                 
409     else {
410         die "Unknown error code $code";    # XXX
411     }
412     if (%err) {
413         push( @errmsgloop, \%err );
414     }
415     last if $exit_required_p;
416 }
417 $template->param( errmsgloop => \@errmsgloop );
418
419 # patrontable ....
420 if ($borrower) {
421     my $flags = $borrower->{'flags'};
422     my @flagloop;
423     my $flagset;
424     foreach my $flag ( sort keys %$flags ) {
425         my %flaginfo;
426         unless ($flagset) { $flagset = 1; }
427         $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
428         $flaginfo{flag}    = $flag;
429         if ( $flag eq 'CHARGES' ) {
430             $flaginfo{msg}            = $flag;
431             $flaginfo{charges}        = 1;
432             $flaginfo{borrowernumber} = $borrower->{borrowernumber};
433         }
434         elsif ( $flag eq 'WAITING' ) {
435             $flaginfo{msg}     = $flag;
436             $flaginfo{waiting} = 1;
437             my @waitingitemloop;
438             my $items = $flags->{$flag}->{'itemlist'};
439             foreach my $item (@$items) {
440                 my $biblio =
441                   GetBiblioFromItemNumber( $item->{'itemnumber'});
442                 my %waitingitem;
443                 $waitingitem{biblionum} = $biblio->{'biblionumber'};
444                 $waitingitem{barcode}   = $biblio->{'barcode'};
445                 $waitingitem{title}     = $biblio->{'title'};
446                 $waitingitem{brname}    =
447                   $branches->{ $biblio->{'holdingbranch'} }
448                   ->{'branchname'};
449                 push( @waitingitemloop, \%waitingitem );
450             }
451             $flaginfo{itemloop} = \@waitingitemloop;
452         }
453         elsif ( $flag eq 'ODUES' ) {
454             my $items = $flags->{$flag}->{'itemlist'};
455             my @itemloop;
456             foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
457                 @$items )
458             {
459                 my $biblio =
460                   GetBiblioFromItemNumber( $item->{'itemnumber'});
461                 my %overdueitem;
462                 $overdueitem{duedate}   = format_date( $item->{'date_due'} );
463                 $overdueitem{biblionum} = $biblio->{'biblionumber'};
464                 $overdueitem{barcode}   = $biblio->{'barcode'};
465                 $overdueitem{title}     = $biblio->{'title'};
466                 $overdueitem{brname}    =
467                   $branches->{ $biblio->{'holdingbranch'} }
468                   ->{'branchname'};
469                 push( @itemloop, \%overdueitem );
470             }
471             $flaginfo{itemloop} = \@itemloop;
472             $flaginfo{overdue}  = 1;
473         }
474         else {
475             $flaginfo{other} = 1;
476             $flaginfo{msg}   = $flags->{$flag}->{'message'};
477         }
478         push( @flagloop, \%flaginfo );
479     }
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'}
488     );
489 }
490
491 #set up so only the last 8 returned items display (make for faster loading pages)
492 my $count = 0;
493 my @riloop;
494 foreach ( sort { $a <=> $b } keys %returneditems ) {
495     my %ri;
496     if ( $count < 8 ) {
497         my $barcode = $returneditems{$_};
498         my $duedate = $riduedate{$_};
499         my $overduetext;
500         my $borrowerinfo;
501         if ($duedate) {
502             my @tempdate = split( /-/, $duedate );
503             $ri{year}  = $tempdate[0];
504             $ri{month} = $tempdate[1];
505             $ri{day}   = $tempdate[2];
506             my $duedatenz  = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
507             my @datearr    = localtime( time() );
508             my $todaysdate =
509                 $datearr[5] . '-'
510               . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
511               . sprintf( "%0.2d", $datearr[3] );
512             $ri{duedate} = format_date($duedate);
513             my ($borrower) =
514               GetMemberDetails( $riborrowernumber{$_}, 0 );
515             $ri{borrowernumber} = $borrower->{'borrowernumber'};
516             $ri{borcnum}        = $borrower->{'cardnumber'};
517             $ri{borfirstname}   = $borrower->{'firstname'};
518             $ri{borsurname}     = $borrower->{'surname'};
519             $ri{bortitle}       = $borrower->{'title'};
520         }
521         else {
522             $ri{borrowernumber} = $riborrowernumber{$_};
523         }
524
525         #        my %ri;
526         my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
527         $ri{itembiblionumber} = $biblio->{'biblionumber'};
528         $ri{itemtitle}        = $biblio->{'title'};
529         $ri{itemauthor}       = $biblio->{'author'};
530         $ri{itemtype}         = $biblio->{'itemtype'};
531         $ri{ccode}            = $biblio->{'ccode'};
532         $ri{barcode}          = $barcode;
533     }
534     else {
535         last;
536     }
537     $count++;
538     push( @riloop, \%ri );
539 }
540 $template->param( riloop => \@riloop );
541
542 $template->param(
543     genbrname               => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
544     genprname               => $printers->{$printer}->{'printername'},
545     branchname              => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
546     printer                 => $printer,
547     errmsgloop              => \@errmsgloop,
548     exemptfine              => $exemptfine,
549         overduecharges          => $overduecharges,
550 );
551
552 # actually print the page!
553 output_html_with_http_headers $query, $cookie, $template->output;