don t display the 1 in location column if there is only 1 item : it's useless & confu...
[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 $printers = GetPrinters();
55 my $branch   = GetBranch( $input, $branches );
56
57 my @names = $input->param;
58 my %inp;
59 my $check = 0;
60 for ( my $i = 0 ; $i < @names ; $i++ ) {
61     my $temp = $input->param( $names[$i] );
62     if ( $temp eq 'wo' ) {
63         $inp{ $names[$i] } = $temp;
64         $check = 1;
65     }
66     if ( $temp eq 'yes' ) {
67
68 # FIXME : using array +4, +5, +6 is dirty. Should use arrays for each accountline
69         my $amount         = $input->param( $names[ $i + 4 ] );
70         my $borrowernumber = $input->param( $names[ $i + 5 ] );
71         my $accountno      = $input->param( $names[ $i + 6 ] );
72         makepayment( $borrowernumber, $accountno, $amount, $user, $branch );
73         $check = 2;
74     }
75 }
76 my $total = $input->param('total');
77 if ( $check == 0 ) {
78     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
79         {
80             template_name   => "members/pay.tmpl",
81             query           => $input,
82             type            => "intranet",
83             authnotrequired => 0,
84             flagsrequired   => { borrowers => 1 },
85             debug           => 1,
86         }
87     );
88     if ( $total ne '' ) {
89         recordpayment( $borrowernumber, $total );
90     }
91
92     my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
93
94 #       creation d'une fonction qui va nous retourner le notify_id dans un tableau
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
122             }
123             push( @loop_pay, \%line );
124         }
125
126         my $totalnotify = AmountNotify( $notify[$j] );
127         ( $totalnotify = '0' ) if ( $totalnotify =~ /^0.00/ );
128         push @allfile,
129           {
130             'loop_pay' => \@loop_pay,
131             'notify'   => $notify[$j],
132             'total'    => $totalnotify
133           };
134     }
135
136     $template->param(
137         allfile        => \@allfile,
138         firstname      => $data->{'firstname'},
139         surname        => $data->{'surname'},
140         borrowernumber => $borrowernumber,
141         total          => sprintf( "%.2f", $total )
142     );
143     output_html_with_http_headers $input, $cookie, $template->output;
144
145 }
146 else {
147
148     my %inp;
149     my @name = $input->param;
150     for ( my $i = 0 ; $i < @name ; $i++ ) {
151         my $test = $input->param( $name[$i] );
152         if ( $test eq 'wo' ) {
153             my $temp = $name[$i];
154             $temp =~ s/payfine//;
155             $inp{ $name[$i] } = $temp;
156         }
157     }
158     my $borrowernumber;
159     while ( my ( $key, $value ) = each %inp ) {
160
161         my $accounttype = $input->param("accounttype$value");
162         $borrowernumber = $input->param("borrowernumber$value");
163         my $itemno    = $input->param("itemnumber$value");
164         my $amount    = $input->param("amount$value");
165         my $accountno = $input->param("accountno$value");
166         writeoff( $borrowernumber, $accountno, $itemno, $accounttype, $amount );
167     }
168     $borrowernumber = $input->param('borrowernumber');
169     print $input->redirect(
170         "/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
171 }
172
173 sub writeoff {
174     my ( $borrowernumber, $accountnum, $itemnum, $accounttype, $amount ) = @_;
175     my $user = $input->remote_user;
176     my $dbh  = C4::Context->dbh;
177     my $sth =
178       $dbh->prepare(
179 "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=?"
180       );
181     $sth->execute( $accountnum, $borrowernumber );
182     $sth->finish;
183     $sth = $dbh->prepare("select max(accountno) from accountlines");
184     $sth->execute;
185     my $account = $sth->fetchrow_hashref;
186     $sth->finish;
187     $account->{'max(accountno)'}++;
188     $sth = $dbh->prepare(
189 "insert into accountlines (borrowernumber,accountno,itemnumber,date,amount,description,accounttype)
190                                                 values (?,?,?,now(),?,'Writeoff','W')"
191     );
192     $sth->execute( $borrowernumber, $account->{'max(accountno)'},
193         $itemnum, $amount );
194     $sth->finish;
195     UpdateStats( $branch, 'writeoff', $amount, '', '', '',
196         $borrowernumber );
197 }