fines management (bugfixes & minor improvement)
[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 # $Id$
21
22 =head1 pay.pl
23
24  written 11/1/2000 by chris@katipo.oc.nz
25  part of the koha library system, script to facilitate paying off fines
26
27 =cut
28
29 use strict;
30 use C4::Context;
31 use C4::Auth;
32 use C4::Output;
33 use CGI;
34 use C4::Members;
35 use C4::Accounts;
36 use C4::Stats;
37 use C4::Koha;
38 use C4::Overdues;
39 use C4::Branch; # GetBranches
40
41 my $input = new CGI;
42
43 my $borrowernumber = $input->param('borrowernumber');
44 if ( $borrowernumber eq '' ) {
45     $borrowernumber = $input->param('borrowernumber0');
46 }
47
48 # get borrower details
49 my $data = GetMember( $borrowernumber,'borrowernumber' );
50 my $user = $input->remote_user;
51
52 # get account details
53 my $branches = GetBranches();
54 my $branch   = GetBranch( $input, $branches );
55
56 my @names = $input->param;
57 my %inp;
58 my $check = 0;
59 for ( my $i = 0 ; $i < @names ; $i++ ) {
60     my $temp = $input->param( $names[$i] );
61     if ( $temp eq 'wo' ) {
62         $inp{ $names[$i] } = $temp;
63         $check = 1;
64     }
65     if ( $temp eq 'yes' ) {
66
67 # FIXME : using array +4, +5, +6 is dirty. Should use arrays for each accountline
68         my $amount         = $input->param( $names[ $i + 4 ] );
69         my $borrowernumber = $input->param( $names[ $i + 5 ] );
70         my $accountno      = $input->param( $names[ $i + 6 ] );
71         makepayment( $borrowernumber, $accountno, $amount, $user, $branch );
72         $check = 2;
73     }
74 }
75 my $total = $input->param('total');
76 if ( $check == 0 ) {
77     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
78         {
79             template_name   => "members/pay.tmpl",
80             query           => $input,
81             type            => "intranet",
82             authnotrequired => 0,
83             flagsrequired   => { borrowers => 1 },
84             debug           => 1,
85         }
86     );
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
119             }
120             push( @loop_pay, \%line );
121         }
122
123         my $totalnotify = AmountNotify( $notify[$j] );
124         ( $totalnotify = '0' ) if ( $totalnotify =~ /^0.00/ );
125         push @allfile,
126           {
127             'loop_pay' => \@loop_pay,
128             'notify'   => $notify[$j],
129             'total'    => $totalnotify
130           };
131     }
132     $template->param(
133         allfile        => \@allfile,
134         firstname      => $data->{'firstname'},
135         surname        => $data->{'surname'},
136         borrowernumber => $borrowernumber,
137         total          => sprintf( "%.2f", $total )
138     );
139     output_html_with_http_headers $input, $cookie, $template->output;
140
141 }
142 else {
143
144     my %inp;
145     my @name = $input->param;
146     for ( my $i = 0 ; $i < @name ; $i++ ) {
147         my $test = $input->param( $name[$i] );
148         if ( $test eq 'wo' ) {
149             my $temp = $name[$i];
150             $temp =~ s/payfine//;
151             $inp{ $name[$i] } = $temp;
152         }
153     }
154     my $borrowernumber;
155     while ( my ( $key, $value ) = each %inp ) {
156
157         my $accounttype = $input->param("accounttype$value");
158         $borrowernumber = $input->param("borrowernumber$value");
159         my $itemno    = $input->param("itemnumber$value");
160         my $amount    = $input->param("amount$value");
161         my $accountno = $input->param("accountno$value");
162         writeoff( $borrowernumber, $accountno, $itemno, $accounttype, $amount );
163     }
164     $borrowernumber = $input->param('borrowernumber');
165     print $input->redirect(
166         "/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
167 }
168
169 sub writeoff {
170     my ( $borrowernumber, $accountnum, $itemnum, $accounttype, $amount ) = @_;
171     my $user = $input->remote_user;
172     my $dbh  = C4::Context->dbh;
173     my $sth =
174       $dbh->prepare(
175 "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=?"
176       );
177     $sth->execute( $accountnum, $borrowernumber );
178     $sth->finish;
179     $sth = $dbh->prepare("select max(accountno) from accountlines");
180     $sth->execute;
181     my $account = $sth->fetchrow_hashref;
182     $sth->finish;
183     $account->{'max(accountno)'}++;
184     $sth = $dbh->prepare(
185 "insert into accountlines (borrowernumber,accountno,itemnumber,date,amount,description,accounttype)
186                                                 values (?,?,?,now(),?,'Writeoff','W')"
187     );
188     $sth->execute( $borrowernumber, $account->{'max(accountno)'},
189         $itemnum, $amount );
190     $sth->finish;
191     UpdateStats( $branch, 'writeoff', $amount, '', '', '',
192         $borrowernumber );
193 }