Merge remote branch 'kc/new/bug_5234' 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 }
88 my $total = $input->param('total') || '';
89 if ( $check == 0 ) {
90     if ( $total ne '' ) {
91         recordpayment( $borrowernumber, $total );
92     }
93
94     my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
95
96     my @allfile;
97     my @notify = NumberNotifyId($borrowernumber);
98
99     my $numberofnotify = scalar(@notify);
100     for ( my $j = 0 ; $j < scalar(@notify) ; $j++ ) {
101         my @loop_pay;
102         my ( $total , $accts, $numaccts) =
103           GetBorNotifyAcctRecord( $borrowernumber, $notify[$j] );
104         for ( my $i = 0 ; $i < $numaccts ; $i++ ) {
105             my %line;
106             if ( $accts->[$i]{'amountoutstanding'} != 0 ) {
107                 $accts->[$i]{'amount'}            += 0.00;
108                 $accts->[$i]{'amountoutstanding'} += 0.00;
109                 $line{i}           = $j . "" . $i;
110                 $line{itemnumber}  = $accts->[$i]{'itemnumber'};
111                 $line{accounttype} = $accts->[$i]{'accounttype'};
112                 $line{amount}      = sprintf( "%.2f", $accts->[$i]{'amount'} );
113                 $line{amountoutstanding} =
114                   sprintf( "%.2f", $accts->[$i]{'amountoutstanding'} );
115                 $line{borrowernumber} = $borrowernumber;
116                 $line{accountno}      = $accts->[$i]{'accountno'};
117                 $line{description}    = $accts->[$i]{'description'};
118                 $line{title}          = $accts->[$i]{'title'};
119                 $line{notify_id}      = $accts->[$i]{'notify_id'};
120                 $line{notify_level}   = $accts->[$i]{'notify_level'};
121                 $line{net_balance} = 1 if($accts->[$i]{'amountoutstanding'} > 0); # you can't pay a credit.
122                 push( @loop_pay, \%line );
123             }
124         }
125
126         my $totalnotify = AmountNotify( $notify[$j], $borrowernumber );
127         ( $totalnotify = '0' ) if ( $totalnotify =~ /^0.00/ );
128         push @allfile,
129           {
130             'loop_pay' => \@loop_pay,
131             'notify'   => $notify[$j],
132             'total'    =>  sprintf( "%.2f",$totalnotify),
133                         
134           };
135     }
136         
137 if ( $data->{'category_type'} eq 'C') {
138    my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
139    my $cnt = scalar(@$catcodes);
140    $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
141    $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
142 }
143         
144 $template->param( adultborrower => 1 ) if ( $data->{'category_type'} eq 'A' );
145 my ($picture, $dberror) = GetPatronImage($data->{'cardnumber'});
146 $template->param( picture => 1 ) if $picture;
147         
148     $template->param(
149         allfile        => \@allfile,
150         firstname      => $data->{'firstname'},
151         surname        => $data->{'surname'},
152         borrowernumber => $borrowernumber,
153         cardnumber => $data->{'cardnumber'},
154         categorycode => $data->{'categorycode'},
155         category_type => $data->{'category_type'},
156         categoryname  => $data->{'description'},
157         address => $data->{'address'},
158         address2 => $data->{'address2'},
159         city => $data->{'city'},
160         zipcode => $data->{'zipcode'},
161         country => $data->{'country'},
162         phone => $data->{'phone'},
163         email => $data->{'email'},
164         branchcode => $data->{'branchcode'},
165         branchname => GetBranchName($data->{'branchcode'}),
166         is_child        => ($data->{'category_type'} eq 'C'),
167         total          => sprintf( "%.2f", $total )
168     );
169     output_html_with_http_headers $input, $cookie, $template->output;
170
171 }
172 else {
173
174     my %inp;
175     my @name = $input->param;
176     for ( my $i = 0 ; $i < @name ; $i++ ) {
177         my $test = $input->param( $name[$i] );
178         if ( $test eq 'wo' ) {
179             my $temp = $name[$i];
180             $temp =~ s/payfine//;
181             $inp{ $name[$i] } = $temp;
182         }
183     }
184     my $borrowernumber;
185     while ( my ( $key, $value ) = each %inp ) {
186
187         my $accounttype = $input->param("accounttype$value");
188         $borrowernumber = $input->param("borrowernumber$value");
189         my $itemno    = $input->param("itemnumber$value");
190         my $amount    = $input->param("amount$value");
191         my $accountno = $input->param("accountno$value");
192         writeoff( $borrowernumber, $accountno, $itemno, $accounttype, $amount );
193     }
194     $borrowernumber = $input->param('borrowernumber');
195     print $input->redirect(
196         "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
197 }
198
199 sub writeoff {
200     my ( $borrowernumber, $accountnum, $itemnum, $accounttype, $amount ) = @_;
201     my $user = $input->remote_user;
202     my $dbh  = C4::Context->dbh;
203     undef $itemnum unless $itemnum; # if no item is attached to fine, make sure to store it as a NULL
204     my $sth =
205       $dbh->prepare(
206 "Update accountlines set amountoutstanding=0 where accountno=? and borrowernumber=?"
207       );
208     $sth->execute( $accountnum, $borrowernumber );
209     $sth->finish;
210     $sth = $dbh->prepare("select max(accountno) from accountlines");
211     $sth->execute;
212     my $account = $sth->fetchrow_hashref;
213     $sth->finish;
214     $account->{'max(accountno)'}++;
215     $sth = $dbh->prepare(
216 "insert into accountlines (borrowernumber,accountno,itemnumber,date,amount,description,accounttype)
217                                                 values (?,?,?,now(),?,'Writeoff','W')"
218     );
219     $sth->execute( $borrowernumber, $account->{'max(accountno)'},
220         $itemnum, $amount );
221     $sth->finish;
222     UpdateStats( $branch, 'writeoff', $amount, '', '', '',
223         $borrowernumber );
224 }