*** empty log message ***
[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 use HTML::Template;
34
35 my $input=new CGI;
36
37 #print $input->header;
38 my $bornum=$input->param('bornum');
39 if ($bornum eq ''){
40         $bornum=$input->param('bornum0');
41 }
42 #get borrower details
43 my $data=borrdata('',$bornum);
44 my $user=$input->remote_user;
45
46 #get account details
47 my %bor;
48 $bor{'borrowernumber'}=$bornum;
49
50
51 my @names=$input->param;
52 my %inp;
53 my $check=0;
54 for (my $i=0;$i<@names;$i++){
55         my$temp=$input->param($names[$i]);
56         if ($temp eq 'wo'){
57                 $inp{$names[$i]}=$temp;
58                 $check=1;
59         }
60         if ($temp eq 'yes'){
61                 $user=~ s/Levin/C/i;
62                 $user=~ s/Foxton/F/i;
63                 $user=~ s/Shannon/S/i;
64                 my $amount=$input->param($names[$i+4]);
65                 my $bornum=$input->param($names[$i+5]);
66                 my $accountno=$input->param($names[$i+6]);
67                 makepayment($bornum,$accountno,$amount,$user);
68                 $check=2;
69         }
70 }
71 my %env;
72     $user=~ s/Levin/C/i;
73     $user=~ s/Foxton/F/i;
74     $user=~ s/Shannon/S/i;
75
76 $env{'branchcode'}=$user;
77 my $total=$input->param('total');
78 if ($check ==0){
79         my $template = gettemplate("members/pay.tmpl");
80         if ($total ne ''){
81                 recordpayment(\%env,$bornum,$total);
82         }
83         my ($numaccts,$accts,$total)=getboracctrecord('',\%bor);
84
85         my @loop_pay;
86         for (my $i=0;$i<$numaccts;$i++){
87                 if ($accts->[$i]{'amountoutstanding'} > 0){
88                         $accts->[$i]{'amount'}+=0.00;
89                         $accts->[$i]{'amountoutstanding'}+=0.00;
90                         my %line;
91                         $line{i}=$i;
92                         $line{itemnumber} = $accts->[$i]{'itemnumber'};
93                         $line{accounttype} = $accts->[$i]{'accounttype'};
94                         $line{amount} = $accts->[$i]{'amount'};
95                         $line{amountoutstanding} = $accts->[$i]{'amountoutstanding'};
96                         $line{bornum} = $bornum;
97                         $line{accountno} = $accts->[$i]{'accountno'};
98                         $line{description} = $accts->[$i]{'description'};
99                         $line{title} = $accts->[$i]{'title'};
100                         push(@loop_pay, \%line);
101                 }
102         }
103         $template->param(firstname => $data->{'firstname'},
104                                                         surname => $data->{'surname'},
105                                                         bornum => $bornum,
106                                                         loop_pay => \@loop_pay,
107                                                         total => $total);
108         print "Content-Type: text/html\n\n", $template->output;
109
110 } else {
111 #  my $quety=$input->query_string;
112 #  print $input->redirect("/cgi-bin/koha/sec/writeoff.pl?$quety");
113         my%inp;
114         my @name=$input->param;
115         for (my $i=0;$i<@name;$i++){
116                 my $test=$input->param($name[$i]);
117                 if ($test eq 'wo'){
118                 my $temp=$name[$i];
119                 $temp=~ s/payfine//;
120                 $inp{$name[$i]}=$temp;
121                 }
122         }
123         my $bornum;
124         while ( my ($key, $value) = each %inp){
125                 #  print $key,$value;
126                 my $accounttype=$input->param("accounttype$value");
127                 $bornum=$input->param("bornum$value");
128                 my $itemno=$input->param("itemnumber$value");
129                 my $amount=$input->param("amount$value");
130                 if ($accounttype eq 'Res'){
131                         my $accountno=$input->param("accountno$value");
132                         writeoff($bornum,$accountno,$itemno,$accounttype,$amount);
133                 } else {
134                         writeoff($bornum,'',$itemno,$accounttype,$amount);
135                 }
136         }
137         $bornum=$input->param('bornum');
138         print $input->redirect("/cgi-bin/koha/pay.pl?bornum=$bornum");
139 }
140
141
142 sub writeoff{
143         my ($bornum,$accountnum,$itemnum,$accounttype,$amount)=@_;
144         my $user=$input->remote_user;
145         $user=~ s/Levin/C/;
146         $user=~ s/Foxton/F/;
147         $user=~ s/Shannon/S/;
148         my $dbh = C4::Context->dbh;
149         my $env;
150         my $query="Update accountlines set amountoutstanding=0 where ";
151         if ($accounttype eq 'Res'){
152                 $query.="accounttype='Res' and accountno='$accountnum' and borrowernumber='$bornum'";
153         } else {
154                 $query.="accounttype='$accounttype' and itemnumber='$itemnum' and borrowernumber='$bornum'";
155         }
156         my $sth=$dbh->prepare($query);
157         #  print $query;
158         $sth->execute;
159         $sth->finish;
160         $query="select max(accountno) from accountlines";
161         $sth=$dbh->prepare($query);
162         $sth->execute;
163         my $account=$sth->fetchrow_hashref;
164         $sth->finish;
165         $account->{'max(accountno)'}++;
166         $query="insert into accountlines (borrowernumber,accountno,itemnumber,date,amount,description,accounttype)
167                                                 values ('$bornum','$account->{'max(accountno)'}','$itemnum',now(),'$amount','Writeoff','W')";
168         $sth=$dbh->prepare($query);
169         $sth->execute;
170         $sth->finish;
171         #  print $query;
172         UpdateStats($env,$user,'writeoff',$amount,'','','',$bornum);
173 }