rel_3_0 moved to HEAD
[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::Accounts2;
36 use C4::Stats;
37 use C4::Koha;
38 use C4::Circulation::Fines;
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 = borrdata( '', $borrowernumber );
50 my $user = $input->remote_user;
51
52 # get account details
53 my %bor;
54 $bor{'borrowernumber'} = $borrowernumber;
55 my $branches = GetBranches();
56 my $printers = GetPrinters();
57 my $branch   = GetBranch( $input, $branches );
58
59 my @names = $input->param;
60 my %inp;
61 my $check = 0;
62 for ( my $i = 0 ; $i < @names ; $i++ ) {
63     my $temp = $input->param( $names[$i] );
64     if ( $temp eq 'wo' ) {
65         $inp{ $names[$i] } = $temp;
66         $check = 1;
67     }
68     if ( $temp eq 'yes' ) {
69
70 # FIXME : using array +4, +5, +6 is dirty. Should use arrays for each accountline
71         my $amount         = $input->param( $names[ $i + 4 ] );
72         my $borrowernumber = $input->param( $names[ $i + 5 ] );
73         my $accountno      = $input->param( $names[ $i + 6 ] );
74         makepayment( $borrowernumber, $accountno, $amount, $user, $branch );
75         $check = 2;
76     }
77 }
78 my %env;
79
80 $env{'branchcode'} = $branch;
81 my $total = $input->param('total');
82 if ( $check == 0 ) {
83     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
84         {
85             template_name   => "members/pay.tmpl",
86             query           => $input,
87             type            => "intranet",
88             authnotrequired => 0,
89             flagsrequired   => { borrowers => 1 },
90             debug           => 1,
91         }
92     );
93     if ( $total ne '' ) {
94         recordpayment( \%env, $borrowernumber, $total );
95     }
96
97     my ( $numaccts, $accts, $total ) = getboracctrecord( '', \%bor );
98
99 #       creation d'une fonction qui va nous retourner le notify_id dans un tableau
100
101     my @allfile;
102     my @notify = NumberNotifyId($borrowernumber);
103
104     my $numberofnotify = scalar(@notify);
105     for ( my $j = 0 ; $j < scalar(@notify) ; $j++ ) {
106         my @loop_pay;
107         my ( $numaccts, $accts, $total ) =
108           GetBorNotifyAcctRecord( '', \%bor, $notify[$j] );
109         for ( my $i = 0 ; $i < $numaccts ; $i++ ) {
110             my %line;
111             if ( $accts->[$i]{'amountoutstanding'} > 0 ) {
112                 $accts->[$i]{'amount'}            += 0.00;
113                 $accts->[$i]{'amountoutstanding'} += 0.00;
114                 $line{i}           = $j . "" . $i;
115                 $line{itemnumber}  = $accts->[$i]{'itemnumber'};
116                 $line{accounttype} = $accts->[$i]{'accounttype'};
117                 $line{amount}      = sprintf( "%.2f", $accts->[$i]{'amount'} );
118                 $line{amountoutstanding} =
119                   sprintf( "%.2f", $accts->[$i]{'amountoutstanding'} );
120                 $line{borrowernumber} = $borrowernumber;
121                 $line{accountno}      = $accts->[$i]{'accountno'};
122                 $line{description}    = $accts->[$i]{'description'};
123                 $line{title}          = $accts->[$i]{'title'};
124                 $line{notify_id}      = $accts->[$i]{'notify_id'};
125                 $line{notify_level}   = $accts->[$i]{'notify_level'};
126
127             }
128             push( @loop_pay, \%line );
129         }
130
131         my $totalnotify = AmountNotify( $notify[$j] );
132         ( $totalnotify = '0' ) if ( $totalnotify =~ /^0.00/ );
133         push @allfile,
134           {
135             'loop_pay' => \@loop_pay,
136             'notify'   => $notify[$j],
137             'total'    => $totalnotify
138           };
139     }
140
141     $template->param(
142         allfile        => \@allfile,
143         firstname      => $data->{'firstname'},
144         surname        => $data->{'surname'},
145         borrowernumber => $borrowernumber,
146         total          => sprintf( "%.2f", $total )
147     );
148     print "Content-Type: text/html\n\n", $template->output;
149
150 }
151 else {
152
153     my %inp;
154     my @name = $input->param;
155     for ( my $i = 0 ; $i < @name ; $i++ ) {
156         my $test = $input->param( $name[$i] );
157         if ( $test eq 'wo' ) {
158             my $temp = $name[$i];
159             $temp =~ s/payfine//;
160             $inp{ $name[$i] } = $temp;
161         }
162     }
163     my $borrowernumber;
164     while ( my ( $key, $value ) = each %inp ) {
165
166         my $accounttype = $input->param("accounttype$value");
167         $borrowernumber = $input->param("borrowernumber$value");
168         my $itemno    = $input->param("itemnumber$value");
169         my $amount    = $input->param("amount$value");
170         my $accountno = $input->param("accountno$value");
171         writeoff( $borrowernumber, $accountno, $itemno, $accounttype, $amount );
172     }
173     $borrowernumber = $input->param('borrowernumber');
174     print $input->redirect(
175         "/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
176 }
177
178 sub writeoff {
179     my ( $borrowernumber, $accountnum, $itemnum, $accounttype, $amount ) = @_;
180     my $user = $input->remote_user;
181     my $dbh  = C4::Context->dbh;
182     my $env;
183     my $sth =
184       $dbh->prepare(
185 "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=?"
186       );
187     $sth->execute( $accountnum, $borrowernumber );
188     $sth->finish;
189     $sth = $dbh->prepare("select max(accountno) from accountlines");
190     $sth->execute;
191     my $account = $sth->fetchrow_hashref;
192     $sth->finish;
193     $account->{'max(accountno)'}++;
194     $sth = $dbh->prepare(
195 "insert into accountlines (borrowernumber,accountno,itemnumber,date,amount,description,accounttype)
196                                                 values (?,?,?,now(),?,'Writeoff','W')"
197     );
198     $sth->execute( $borrowernumber, $account->{'max(accountno)'},
199         $itemnum, $amount );
200     $sth->finish;
201     UpdateStats( $env, $branch, 'writeoff', $amount, '', '', '',
202         $borrowernumber );
203 }