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