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