bugfix: missing tmpl_if (not related to new_acq ?)
[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 warnings;
30
31 use C4::Context;
32 use C4::Auth;
33 use C4::Output;
34 use CGI;
35 use C4::Members;
36 use C4::Accounts;
37 use C4::Stats;
38 use C4::Koha;
39 use C4::Overdues;
40 use C4::Branch; # GetBranches
41
42 my $input = new CGI;
43
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45     {
46         template_name   => "members/pay.tmpl",
47         query           => $input,
48         type            => "intranet",
49         authnotrequired => 0,
50         flagsrequired   => { borrowers => 1, updatecharges => 1 },
51         debug           => 1,
52     }
53 );
54
55 my $borrowernumber = $input->param('borrowernumber');
56 if ( $borrowernumber eq '' ) {
57     $borrowernumber = $input->param('borrowernumber0');
58 }
59
60 # get borrower details
61 my $data = GetMember( borrowernumber => $borrowernumber );
62 my $user = $input->remote_user;
63
64 # get account details
65 my $branches = GetBranches();
66 my $branch   = GetBranch( $input, $branches );
67
68 my @names = $input->param;
69 my %inp;
70 my $check = 0;
71 for ( my $i = 0 ; $i < @names ; $i++ ) {
72     my $temp = $input->param( $names[$i] );
73     if ( $temp eq 'wo' ) {
74         $inp{ $names[$i] } = $temp;
75         $check = 1;
76     }
77     if ( $temp eq 'yes' ) {
78
79 # FIXME : using array +4, +5, +6 is dirty. Should use arrays for each accountline
80         my $amount         = $input->param( $names[ $i + 4 ] );
81         my $borrowernumber = $input->param( $names[ $i + 5 ] );
82         my $accountno      = $input->param( $names[ $i + 6 ] );
83         makepayment( $borrowernumber, $accountno, $amount, $user, $branch );
84         $check = 2;
85     }
86 }
87 my $total = $input->param('total') || '';
88 if ( $check == 0 ) {
89     if ( $total ne '' ) {
90         recordpayment( $borrowernumber, $total );
91     }
92
93     my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
94
95     my @allfile;
96     my @notify = NumberNotifyId($borrowernumber);
97
98     my $numberofnotify = scalar(@notify);
99     for ( my $j = 0 ; $j < scalar(@notify) ; $j++ ) {
100         my @loop_pay;
101         my ( $total , $accts, $numaccts) =
102           GetBorNotifyAcctRecord( $borrowernumber, $notify[$j] );
103         for ( my $i = 0 ; $i < $numaccts ; $i++ ) {
104             my %line;
105             if ( $accts->[$i]{'amountoutstanding'} != 0 ) {
106                 $accts->[$i]{'amount'}            += 0.00;
107                 $accts->[$i]{'amountoutstanding'} += 0.00;
108                 $line{i}           = $j . "" . $i;
109                 $line{itemnumber}  = $accts->[$i]{'itemnumber'};
110                 $line{accounttype} = $accts->[$i]{'accounttype'};
111                 $line{amount}      = sprintf( "%.2f", $accts->[$i]{'amount'} );
112                 $line{amountoutstanding} =
113                   sprintf( "%.2f", $accts->[$i]{'amountoutstanding'} );
114                 $line{borrowernumber} = $borrowernumber;
115                 $line{accountno}      = $accts->[$i]{'accountno'};
116                 $line{description}    = $accts->[$i]{'description'};
117                 $line{title}          = $accts->[$i]{'title'};
118                 $line{notify_id}      = $accts->[$i]{'notify_id'};
119                 $line{notify_level}   = $accts->[$i]{'notify_level'};
120                 $line{net_balance} = 1 if($accts->[$i]{'amountoutstanding'} > 0); # you can't pay a credit.
121                 push( @loop_pay, \%line );
122             }
123         }
124
125         my $totalnotify = AmountNotify( $notify[$j], $borrowernumber );
126         ( $totalnotify = '0' ) if ( $totalnotify =~ /^0.00/ );
127         push @allfile,
128           {
129             'loop_pay' => \@loop_pay,
130             'notify'   => $notify[$j],
131             'total'    =>  sprintf( "%.2f",$totalnotify),
132                         
133           };
134     }
135         
136 if ( $data->{'category_type'} eq 'C') {
137    my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
138    my $cnt = scalar(@$catcodes);
139    $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
140    $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
141 }
142         
143 $template->param( adultborrower => 1 ) if ( $data->{'category_type'} eq 'A' );
144 my ($picture, $dberror) = GetPatronImage($data->{'cardnumber'});
145 $template->param( picture => 1 ) if $picture;
146         
147     $template->param(
148         allfile        => \@allfile,
149         firstname      => $data->{'firstname'},
150         surname        => $data->{'surname'},
151         borrowernumber => $borrowernumber,
152         cardnumber => $data->{'cardnumber'},
153         categorycode => $data->{'categorycode'},
154         category_type => $data->{'category_type'},
155         categoryname  => $data->{'description'},
156         address => $data->{'address'},
157         address2 => $data->{'address2'},
158         city => $data->{'city'},
159         zipcode => $data->{'zipcode'},
160         country => $data->{'country'},
161         phone => $data->{'phone'},
162         email => $data->{'email'},
163         branchcode => $data->{'branchcode'},
164         branchname => GetBranchName($data->{'branchcode'}),
165         is_child        => ($data->{'category_type'} eq 'C'),
166         total          => sprintf( "%.2f", $total )
167     );
168     output_html_with_http_headers $input, $cookie, $template->output;
169
170 }
171 else {
172
173     my %inp;
174     my @name = $input->param;
175     for ( my $i = 0 ; $i < @name ; $i++ ) {
176         my $test = $input->param( $name[$i] );
177         if ( $test eq 'wo' ) {
178             my $temp = $name[$i];
179             $temp =~ s/payfine//;
180             $inp{ $name[$i] } = $temp;
181         }
182     }
183     my $borrowernumber;
184     while ( my ( $key, $value ) = each %inp ) {
185
186         my $accounttype = $input->param("accounttype$value");
187         $borrowernumber = $input->param("borrowernumber$value");
188         my $itemno    = $input->param("itemnumber$value");
189         my $amount    = $input->param("amount$value");
190         my $accountno = $input->param("accountno$value");
191         writeoff( $borrowernumber, $accountno, $itemno, $accounttype, $amount );
192     }
193     $borrowernumber = $input->param('borrowernumber');
194     print $input->redirect(
195         "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
196 }
197
198 sub writeoff {
199     my ( $borrowernumber, $accountnum, $itemnum, $accounttype, $amount ) = @_;
200     my $user = $input->remote_user;
201     my $dbh  = C4::Context->dbh;
202     undef $itemnum unless $itemnum; # if no item is attached to fine, make sure to store it as a NULL
203     my $sth =
204       $dbh->prepare(
205 "Update accountlines set amountoutstanding=0 where accountno=? and borrowernumber=?"
206       );
207     $sth->execute( $accountnum, $borrowernumber );
208     $sth->finish;
209     $sth = $dbh->prepare("select max(accountno) from accountlines");
210     $sth->execute;
211     my $account = $sth->fetchrow_hashref;
212     $sth->finish;
213     $account->{'max(accountno)'}++;
214     $sth = $dbh->prepare(
215 "insert into accountlines (borrowernumber,accountno,itemnumber,date,amount,description,accounttype)
216                                                 values (?,?,?,now(),?,'Writeoff','W')"
217     );
218     $sth->execute( $borrowernumber, $account->{'max(accountno)'},
219         $itemnum, $amount );
220     $sth->finish;
221     UpdateStats( $branch, 'writeoff', $amount, '', '', '',
222         $borrowernumber );
223 }