road to 1.3.2 : adding a biblio in MARC format.
[koha.git] / 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::Output;
29 use CGI;
30 use C4::Search;
31 use C4::Accounts2;
32 use C4::Stats;
33
34 my $input=new CGI;
35
36 #print $input->header;
37 my $bornum=$input->param('bornum');
38 if ($bornum eq ''){
39   $bornum=$input->param('bornum0');
40 }
41 #get borrower details
42 my $data=borrdata('',$bornum);
43 my $user=$input->remote_user;
44
45 #get account details
46 my %bor;
47 $bor{'borrowernumber'}=$bornum;
48
49
50 my @names=$input->param;
51 my %inp;
52 my $check=0;
53 for (my $i=0;$i<@names;$i++){
54   my$temp=$input->param($names[$i]);
55   if ($temp eq 'wo'){
56     $inp{$names[$i]}=$temp;
57     $check=1;
58   }
59   if ($temp eq 'yes'){
60     $user=~ s/Levin/C/i;
61     $user=~ s/Foxton/F/i;
62     $user=~ s/Shannon/S/i;
63     my $amount=$input->param($names[$i+4]);
64     my $bornum=$input->param($names[$i+5]);
65     my $accountno=$input->param($names[$i+6]);
66     makepayment($bornum,$accountno,$amount,$user);
67     $check=2;
68   }
69 }
70 my %env;
71     $user=~ s/Levin/C/i;
72     $user=~ s/Foxton/F/i;
73     $user=~ s/Shannon/S/i;
74
75 $env{'branchcode'}=$user;
76 my $total=$input->param('total');
77 if ($check ==0){
78   print $input->header;
79   if ($total ne ''){
80     recordpayment(\%env,$bornum,$total);
81   }
82   my ($numaccts,$accts,$total)=getboracctrecord('',\%bor);
83
84   print startpage();
85   print startmenu('member');
86   print <<printend
87   <FONT SIZE=6><em>Pay Fines for $data->{'firstname'} $data->{'surname'}</em></FONT><P>
88   <center>
89   <p>
90   <TABLE  CELLSPACING=0  CELLPADDING=5 border=1 >
91   <TR VALIGN=TOP>
92   <td  bgcolor="99cc33" background="/images/background-mem.gif" colspan=4><B>FINES & CHARGES</TD>
93   <td  bgcolor="99cc33" background="/images/background-mem.gif" colspan=4><B>AMOUNT OWING</TD>
94   </TR>
95   <form action=/cgi-bin/koha/pay.pl method=post>
96   <input type=hidden name=bornum value=$bornum>
97 printend
98 ;
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       print <<printend
104       <tr VALIGN=TOP  >
105       <TD><input type=radio name=payfine$i value=no checked>Unpaid
106       <input type=radio name=payfine$i value=yes>Pay
107       <input type=radio name=payfine$i value=wo>Writeoff
108       <input type=hidden name=itemnumber$i value=$accts->[$i]{'itemnumber'}>
109       <input type=hidden name=accounttype$i value=$accts->[$i]{'accounttype'}>
110       <input type=hidden name=amount$i value=$accts->[$i]{'amount'}>
111       <input type=hidden name=out$i value=$accts->[$i]{'amountoutstanding'}>
112       <input type=hidden name=bornum$i value=$bornum>
113       <input type=hidden name=accountno$i value=$accts->[$i]{'accountno'}>
114       </td>
115       <TD>$accts->[$i]{'description'} $accts->[$i]{'title'}</td>
116       <TD>$accts->[$i]{'accounttype'}</td>
117       <td>$accts->[$i]{'amount'}</td>
118       <TD>$accts->[$i]{'amountoutstanding'}</td>
119
120       </tr>
121 printend
122 ;
123     }
124   }
125   print <<printend
126   <tr VALIGN=TOP  >
127   <TD></td>
128   <TD colspan=2><b>Total Due</b></td>
129   <TD><b>$total</b></td>
130   </tr>
131   <tr VALIGN=TOP  >
132   <TD colspan=5 align=right>
133   <INPUT TYPE="image" name="submit"  VALUE="pay" height=42  WIDTH=187 BORDER=0 src="/images/pay-fines.gif"></td>
134   </tr>
135   </form>
136   </table>
137   <br clear=all>
138   <p> &nbsp; </p>
139
140 printend
141 ;
142   print endmenu('member');
143   print endpage();
144
145 } else {
146 #  my $quety=$input->query_string;
147 #  print $input->redirect("/cgi-bin/koha/sec/writeoff.pl?$quety");
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 $bornum;
159     while ( my ($key, $value) = each %inp){
160         #  print $key,$value;
161         my $accounttype=$input->param("accounttype$value");
162         $bornum=$input->param("bornum$value");
163         my $itemno=$input->param("itemnumber$value");
164         my $amount=$input->param("amount$value");
165         if ($accounttype eq 'Res'){
166             my $accountno=$input->param("accountno$value");
167             writeoff($bornum,$accountno,$itemno,$accounttype,$amount);
168         } else {
169             writeoff($bornum,'',$itemno,$accounttype,$amount);
170         }
171     }
172     $bornum=$input->param('bornum');
173     print $input->redirect("/cgi-bin/koha/pay.pl?bornum=$bornum");
174 }
175
176
177 sub writeoff{
178     my ($bornum,$accountnum,$itemnum,$accounttype,$amount)=@_;
179     my $user=$input->remote_user;
180     $user=~ s/Levin/C/;
181     $user=~ s/Foxton/F/;
182     $user=~ s/Shannon/S/;
183     my $dbh = C4::Context->dbh;
184     my $env;
185     my $query="Update accountlines set amountoutstanding=0 where ";
186     if ($accounttype eq 'Res'){
187         $query.="accounttype='Res' and accountno='$accountnum' and borrowernumber='$bornum'";
188     } else {
189         $query.="accounttype='$accounttype' and itemnumber='$itemnum' and borrowernumber='$bornum'";
190     }
191     my $sth=$dbh->prepare($query);
192     #  print $query;
193     $sth->execute;
194     $sth->finish;
195     $query="select max(accountno) from accountlines";
196     $sth=$dbh->prepare($query);
197     $sth->execute;
198     my $account=$sth->fetchrow_hashref;
199     $sth->finish;
200     $account->{'max(accountno)'}++;
201     $query="insert into accountlines (borrowernumber,accountno,itemnumber,date,amount,description,accounttype)
202     values ('$bornum','$account->{'max(accountno)'}','$itemnum',now(),'$amount','Writeoff','W')";
203     $sth=$dbh->prepare($query);
204     $sth->execute;
205     $sth->finish;
206 #  print $query;
207     UpdateStats($env,$user,'writeoff',$amount,'','','',$bornum);
208 }