synch'ing 2.2 and head
[koha.git] / pay.pl
1 #!/usr/bin/perl
2 # WARNING: Not enough context to figure out the correct tabstop size
3 # WARNING: Assume that this file uses 4-character tabs
4
5 # $Id$
6
7 #written 11/1/2000 by chris@katipo.oc.nz
8 #part of the koha library system, script to facilitate paying off fines
9
10
11 # Copyright 2000-2002 Katipo Communications
12 #
13 # This file is part of Koha.
14 #
15 # Koha is free software; you can redistribute it and/or modify it under the
16 # terms of the GNU General Public License as published by the Free Software
17 # Foundation; either version 2 of the License, or (at your option) any later
18 # version.
19 #
20 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
21 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License along with
25 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
26 # Suite 330, Boston, MA  02111-1307 USA
27
28 use strict;
29 use C4::Context;
30 use C4::Auth;
31 use C4::Output;
32 use CGI;
33 use C4::Search;
34 use C4::Accounts2;
35 use C4::Stats;
36 use HTML::Template;
37
38 my $input=new CGI;
39
40 #print $input->header;
41 my $bornum=$input->param('bornum');
42 if ($bornum eq ''){
43         $bornum=$input->param('bornum0');
44 }
45 #get borrower details
46 my $data=borrdata('',$bornum);
47 my $user=$input->remote_user;
48
49 #get account details
50 my %bor;
51 $bor{'borrowernumber'}=$bornum;
52
53
54 my @names=$input->param;
55 my %inp;
56 my $check=0;
57 for (my $i=0;$i<@names;$i++){
58         my $temp=$input->param($names[$i]);
59         if ($temp eq 'wo'){
60                 $inp{$names[$i]}=$temp;
61                 $check=1;
62         }
63         if ($temp eq 'yes'){
64                 $user=~ s/Levin/C/i;
65                 $user=~ s/Foxton/F/i;
66                 $user=~ s/Shannon/S/i;
67                 # FIXME : using array +4, +5, +6 is dirty. Should use arrays for each accountline
68                 my $amount=$input->param($names[$i+4]);
69                 my $bornum=$input->param($names[$i+5]);
70                 my $accountno=$input->param($names[$i+6]);
71                 makepayment($bornum,$accountno,$amount,$user);
72                 $check=2;
73         }
74 }
75 my %env;
76     $user=~ s/Levin/C/i;
77     $user=~ s/Foxton/F/i;
78     $user=~ s/Shannon/S/i;
79
80 $env{'branchcode'}=$user;
81 my $total=$input->param('total');
82 if ($check ==0){
83         my($template, $loggedinuser, $cookie)
84                 = get_template_and_user ({ template_name => "members/pay.tmpl",
85                                            query => $input,
86                                            type => "intranet",
87                                            authnotrequired => 0,
88                                            flagsrequired => {borrowers => 1},
89                                            debug => 1,
90                                          });
91         if ($total ne ''){
92                 recordpayment(\%env,$bornum,$total);
93         }
94         my ($numaccts,$accts,$total)=getboracctrecord('',\%bor);
95
96         my @loop_pay;
97         for (my $i=0;$i<$numaccts;$i++){
98                 if ($accts->[$i]{'amountoutstanding'} > 0){
99                         $accts->[$i]{'amount'}+=0.00;
100                         $accts->[$i]{'amountoutstanding'}+=0.00;
101                         my %line;
102                         $line{i}=$i;
103                         $line{itemnumber} = $accts->[$i]{'itemnumber'};
104                         $line{accounttype} = $accts->[$i]{'accounttype'};
105                         $line{amount} = sprintf("%.2f",$accts->[$i]{'amount'});
106                         $line{amountoutstanding} = sprintf("%.2f",$accts->[$i]{'amountoutstanding'});
107                         $line{bornum} = $bornum;
108                         $line{accountno} = $accts->[$i]{'accountno'};
109                         $line{description} = $accts->[$i]{'description'};
110                         $line{title} = $accts->[$i]{'title'};
111                         push(@loop_pay, \%line);
112                 }
113         }
114         $template->param(firstname => $data->{'firstname'},
115                                                         surname => $data->{'surname'},
116                                                         bornum => $bornum,
117                                                         loop_pay => \@loop_pay,
118                                                         total => sprintf("%.2f",$total));
119         print "Content-Type: text/html\n\n", $template->output;
120
121 } else {
122 #  my $quety=$input->query_string;
123 #  print $input->redirect("/cgi-bin/koha/sec/writeoff.pl?$quety");
124         my%inp;
125         my @name=$input->param;
126         for (my $i=0;$i<@name;$i++){
127                 my $test=$input->param($name[$i]);
128                 if ($test eq 'wo'){
129                 my $temp=$name[$i];
130                 $temp=~ s/payfine//;
131                 $inp{$name[$i]}=$temp;
132                 }
133         }
134         my $bornum;
135         while ( my ($key, $value) = each %inp){
136                 #  print $key,$value;
137                 my $accounttype=$input->param("accounttype$value");
138                 $bornum=$input->param("bornum$value");
139                 my $itemno=$input->param("itemnumber$value");
140                 my $amount=$input->param("amount$value");
141                 if ($accounttype eq 'Res'){
142                         my $accountno=$input->param("accountno$value");
143                         writeoff($bornum,$accountno,$itemno,$accounttype,$amount);
144                 } else {
145                         writeoff($bornum,'',$itemno,$accounttype,$amount);
146                 }
147         }
148         $bornum=$input->param('bornum');
149         print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$bornum");
150 }
151
152
153 sub writeoff{
154         my ($bornum,$accountnum,$itemnum,$accounttype,$amount)=@_;
155         my $user=$input->remote_user;
156         $user=~ s/Levin/C/;
157         $user=~ s/Foxton/F/;
158         $user=~ s/Shannon/S/;
159         my $dbh = C4::Context->dbh;
160         my $env;
161         my $sth;
162         if ($accounttype eq 'Res'){
163                 $sth=$dbh->prepare("Update accountlines set amountoutstanding=0 where accounttype='Res' and accountno=? and borrowernumber=?");
164                 $sth->execute($accountnum,$bornum);
165         } else {
166                 $sth=$dbh->prepare("Update accountlines set amountoutstanding=0 where accounttype=? and itemnumber=? and borrowernumber=?");
167                 $sth->execute($accounttype,$itemnum,$bornum);
168         }
169         $sth->finish;
170         $sth=$dbh->prepare("select max(accountno) from accountlines");
171         $sth->execute;
172         my $account=$sth->fetchrow_hashref;
173         $sth->finish;
174         $account->{'max(accountno)'}++;
175         $sth=$dbh->prepare("insert into accountlines (borrowernumber,accountno,itemnumber,date,amount,description,accounttype)
176                                                 values (?,?,?,now(),?,'Writeoff','W')");
177         $sth->execute($bornum,$account->{'max(accountno)'},$itemnum,$amount);
178         $sth->finish;
179         UpdateStats($env,$user,'writeoff',$amount,'','','',$bornum);
180 }
181
182 # Local Variables:
183 # tab-width: 4
184 # End: