Adding KohaAdminEmailAddress to template
[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                 my $amount=$input->param($names[$i+4]);
68                 my $bornum=$input->param($names[$i+5]);
69                 my $accountno=$input->param($names[$i+6]);
70                 makepayment($bornum,$accountno,$amount,$user);
71                 $check=2;
72         }
73 }
74 my %env;
75     $user=~ s/Levin/C/i;
76     $user=~ s/Foxton/F/i;
77     $user=~ s/Shannon/S/i;
78
79 $env{'branchcode'}=$user;
80 my $total=$input->param('total');
81 if ($check ==0){
82         my($template, $loggedinuser, $cookie)
83                 = get_template_and_user ({ template_name => "members/pay.tmpl",
84                                            query => $input,
85                                            type => "intranet",
86                                            authnotrequired => 0,
87                                            flagsrequired => {borrowers => 1},
88                                            debug => 1,
89                                          });
90         if ($total ne ''){
91                 recordpayment(\%env,$bornum,$total);
92         }
93         my ($numaccts,$accts,$total)=getboracctrecord('',\%bor);
94
95         my @loop_pay;
96         for (my $i=0;$i<$numaccts;$i++){
97                 if ($accts->[$i]{'amountoutstanding'} > 0){
98                         $accts->[$i]{'amount'}+=0.00;
99                         $accts->[$i]{'amountoutstanding'}+=0.00;
100                         my %line;
101                         $line{i}=$i;
102                         $line{itemnumber} = $accts->[$i]{'itemnumber'};
103                         $line{accounttype} = $accts->[$i]{'accounttype'};
104                         $line{amount} = $accts->[$i]{'amount'};
105                         $line{amountoutstanding} = $accts->[$i]{'amountoutstanding'};
106                         $line{bornum} = $bornum;
107                         $line{accountno} = $accts->[$i]{'accountno'};
108                         $line{description} = $accts->[$i]{'description'};
109                         $line{title} = $accts->[$i]{'title'};
110                         push(@loop_pay, \%line);
111                 }
112         }
113         $template->param(firstname => $data->{'firstname'},
114                                                         surname => $data->{'surname'},
115                                                         bornum => $bornum,
116                                                         loop_pay => \@loop_pay,
117                                                         total => $total);
118         print "Content-Type: text/html\n\n", $template->output;
119
120 } else {
121 #  my $quety=$input->query_string;
122 #  print $input->redirect("/cgi-bin/koha/sec/writeoff.pl?$quety");
123         my%inp;
124         my @name=$input->param;
125         for (my $i=0;$i<@name;$i++){
126                 my $test=$input->param($name[$i]);
127                 if ($test eq 'wo'){
128                 my $temp=$name[$i];
129                 $temp=~ s/payfine//;
130                 $inp{$name[$i]}=$temp;
131                 }
132         }
133         my $bornum;
134         while ( my ($key, $value) = each %inp){
135                 #  print $key,$value;
136                 my $accounttype=$input->param("accounttype$value");
137                 $bornum=$input->param("bornum$value");
138                 my $itemno=$input->param("itemnumber$value");
139                 my $amount=$input->param("amount$value");
140                 if ($accounttype eq 'Res'){
141                         my $accountno=$input->param("accountno$value");
142                         writeoff($bornum,$accountno,$itemno,$accounttype,$amount);
143                 } else {
144                         writeoff($bornum,'',$itemno,$accounttype,$amount);
145                 }
146         }
147         $bornum=$input->param('bornum');
148         print $input->redirect("/cgi-bin/koha/pay.pl?bornum=$bornum");
149 }
150
151
152 sub writeoff{
153         my ($bornum,$accountnum,$itemnum,$accounttype,$amount)=@_;
154         my $user=$input->remote_user;
155         $user=~ s/Levin/C/;
156         $user=~ s/Foxton/F/;
157         $user=~ s/Shannon/S/;
158         my $dbh = C4::Context->dbh;
159         my $env;
160         my $sth;
161         if ($accounttype eq 'Res'){
162                 $sth=$dbh->prepare("Update accountlines set amountoutstanding=0 where accounttype='Res' and accountno=? and borrowernumber=?");
163                 $sth->execute($accountnum,$bornum);
164         } else {
165                 $sth=$dbh->prepare("Update accountlines set amountoutstanding=0 where accounttype=? and itemnumber=? and borrowernumber=?");
166                 $sth->execute($accounttype,$itemnum,$bornum);
167         }
168         $sth->finish;
169         $sth=$dbh->prepare("select max(accountno) from accountlines");
170         $sth->execute;
171         my $account=$sth->fetchrow_hashref;
172         $sth->finish;
173         $account->{'max(accountno)'}++;
174         $sth=$dbh->prepare("insert into accountlines (borrowernumber,accountno,itemnumber,date,amount,description,accounttype)
175                                                 values (?,?,?,now(),?,'Writeoff','W')");
176         $sth->execute($bornum,$account->{'max(accountno)'},$itemnum,$amount);
177         $sth->finish;
178         UpdateStats($env,$user,'writeoff',$amount,'','','',$bornum);
179 }
180
181 # Local Variables:
182 # tab-width: 4
183 # End: