bug 2540: allow download of report
[koha.git] / members / pay.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20
21 =head1 pay.pl
22
23  written 11/1/2000 by chris@katipo.oc.nz
24  part of the koha library system, script to facilitate paying off fines
25
26 =cut
27
28 use strict;
29 use C4::Context;
30 use C4::Auth;
31 use C4::Output;
32 use CGI;
33 use C4::Members;
34 use C4::Accounts;
35 use C4::Stats;
36 use C4::Koha;
37 use C4::Overdues;
38 use C4::Branch; # GetBranches
39
40 my $input = new CGI;
41
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43     {
44         template_name   => "members/pay.tmpl",
45         query           => $input,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => { borrowers => 1 },
49         debug           => 1,
50     }
51 );
52
53 my $borrowernumber = $input->param('borrowernumber');
54 if ( $borrowernumber eq '' ) {
55     $borrowernumber = $input->param('borrowernumber0');
56 }
57
58 # get borrower details
59 my $data = GetMember( $borrowernumber,'borrowernumber' );
60 my $user = $input->remote_user;
61
62 # get account details
63 my $branches = GetBranches();
64 my $branch   = GetBranch( $input, $branches );
65
66 my @names = $input->param;
67 my %inp;
68 my $check = 0;
69 for ( my $i = 0 ; $i < @names ; $i++ ) {
70     my $temp = $input->param( $names[$i] );
71     if ( $temp eq 'wo' ) {
72         $inp{ $names[$i] } = $temp;
73         $check = 1;
74     }
75     if ( $temp eq 'yes' ) {
76
77 # FIXME : using array +4, +5, +6 is dirty. Should use arrays for each accountline
78         my $amount         = $input->param( $names[ $i + 4 ] );
79         my $borrowernumber = $input->param( $names[ $i + 5 ] );
80         my $accountno      = $input->param( $names[ $i + 6 ] );
81         makepayment( $borrowernumber, $accountno, $amount, $user, $branch );
82         $check = 2;
83     }
84 }
85 my $total = $input->param('total');
86 if ( $check == 0 ) {
87     if ( $total ne '' ) {
88         recordpayment( $borrowernumber, $total );
89     }
90
91     my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
92
93     my @allfile;
94     my @notify = NumberNotifyId($borrowernumber);
95
96     my $numberofnotify = scalar(@notify);
97     for ( my $j = 0 ; $j < scalar(@notify) ; $j++ ) {
98         my @loop_pay;
99         my ( $total , $accts, $numaccts) =
100           GetBorNotifyAcctRecord( $borrowernumber, $notify[$j] );
101         for ( my $i = 0 ; $i < $numaccts ; $i++ ) {
102             my %line;
103             if ( $accts->[$i]{'amountoutstanding'} != 0 ) {
104                 $accts->[$i]{'amount'}            += 0.00;
105                 $accts->[$i]{'amountoutstanding'} += 0.00;
106                 $line{i}           = $j . "" . $i;
107                 $line{itemnumber}  = $accts->[$i]{'itemnumber'};
108                 $line{accounttype} = $accts->[$i]{'accounttype'};
109                 $line{amount}      = sprintf( "%.2f", $accts->[$i]{'amount'} );
110                 $line{amountoutstanding} =
111                   sprintf( "%.2f", $accts->[$i]{'amountoutstanding'} );
112                 $line{borrowernumber} = $borrowernumber;
113                 $line{accountno}      = $accts->[$i]{'accountno'};
114                 $line{description}    = $accts->[$i]{'description'};
115                 $line{title}          = $accts->[$i]{'title'};
116                 $line{notify_id}      = $accts->[$i]{'notify_id'};
117                 $line{notify_level}   = $accts->[$i]{'notify_level'};
118                 $line{net_balance} = 1 if($accts->[$i]{'amountoutstanding'} > 0); # you can't pay a credit.
119                 push( @loop_pay, \%line );
120             }
121         }
122
123         my $totalnotify = AmountNotify( $notify[$j], $borrowernumber );
124         ( $totalnotify = '0' ) if ( $totalnotify =~ /^0.00/ );
125         push @allfile,
126           {
127             'loop_pay' => \@loop_pay,
128             'notify'   => $notify[$j],
129             'total'    =>  sprintf( "%.2f",$totalnotify),
130                         
131           };
132     }
133         
134 if ( $data->{'category_type'} eq 'C') {
135    my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
136    my $cnt = scalar(@$catcodes);
137    $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
138    $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
139 }
140         
141 $template->param( adultborrower => 1 ) if ( $data->{'category_type'} eq 'A' );
142 my ($picture, $dberror) = GetPatronImage($data->{'cardnumber'});
143 $template->param( picture => 1 ) if $picture;
144         
145     $template->param(
146         allfile        => \@allfile,
147         firstname      => $data->{'firstname'},
148         surname        => $data->{'surname'},
149         borrowernumber => $borrowernumber,
150                 cardnumber => $data->{'cardnumber'},
151             categorycode => $data->{'categorycode'},
152             category_type => $data->{'category_type'},
153             category_description => $data->{'description'},
154             address => $data->{'address'},
155                 address2 => $data->{'address2'},
156             city => $data->{'city'},
157                 zipcode => $data->{'zipcode'},
158                 phone => $data->{'phone'},
159                 email => $data->{'email'},
160             branchcode => $data->{'branchcode'},
161                 is_child        => ($data->{'category_type'} eq 'C'),
162         total          => sprintf( "%.2f", $total )
163     );
164     output_html_with_http_headers $input, $cookie, $template->output;
165
166 }
167 else {
168
169     my %inp;
170     my @name = $input->param;
171     for ( my $i = 0 ; $i < @name ; $i++ ) {
172         my $test = $input->param( $name[$i] );
173         if ( $test eq 'wo' ) {
174             my $temp = $name[$i];
175             $temp =~ s/payfine//;
176             $inp{ $name[$i] } = $temp;
177         }
178     }
179     my $borrowernumber;
180     while ( my ( $key, $value ) = each %inp ) {
181
182         my $accounttype = $input->param("accounttype$value");
183         $borrowernumber = $input->param("borrowernumber$value");
184         my $itemno    = $input->param("itemnumber$value");
185         my $amount    = $input->param("amount$value");
186         my $accountno = $input->param("accountno$value");
187         writeoff( $borrowernumber, $accountno, $itemno, $accounttype, $amount );
188     }
189     $borrowernumber = $input->param('borrowernumber');
190     print $input->redirect(
191         "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
192 }
193
194 sub writeoff {
195     my ( $borrowernumber, $accountnum, $itemnum, $accounttype, $amount ) = @_;
196     my $user = $input->remote_user;
197     my $dbh  = C4::Context->dbh;
198     undef $itemnum unless $itemnum; # if no item is attached to fine, make sure to store it as a NULL
199     my $sth =
200       $dbh->prepare(
201 "Update accountlines set amountoutstanding=0 where (accounttype='Res' OR accounttype='FU' OR accounttype ='IP' OR accounttype='CH' OR accounttype='N' OR accounttype='F' OR accounttype='A' OR accounttype='M' OR accounttype='L' OR accounttype='RE' OR accounttype='RL') and accountno=? and borrowernumber=?"
202       );
203     $sth->execute( $accountnum, $borrowernumber );
204     $sth->finish;
205     $sth = $dbh->prepare("select max(accountno) from accountlines");
206     $sth->execute;
207     my $account = $sth->fetchrow_hashref;
208     $sth->finish;
209     $account->{'max(accountno)'}++;
210     $sth = $dbh->prepare(
211 "insert into accountlines (borrowernumber,accountno,itemnumber,date,amount,description,accounttype)
212                                                 values (?,?,?,now(),?,'Writeoff','W')"
213     );
214     $sth->execute( $borrowernumber, $account->{'max(accountno)'},
215         $itemnum, $amount );
216     $sth->finish;
217     UpdateStats( $branch, 'writeoff', $amount, '', '', '',
218         $borrowernumber );
219 }