1 package C4::Accounts; #assumes C4/Accounts
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
29 use C4::Interface::AccountsCDK;
30 use vars qw($VERSION @ISA @EXPORT);
32 # set the version for version checking
36 @EXPORT = qw(&checkaccount &reconcileaccount &getnextacctno);
43 #check accounts and list amounts owing
44 my ($env,$bornumber,$dbh)=@_;
45 my $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
46 borrowernumber=$bornumber and amountoutstanding<>0");
49 while (my $data=$sth->fetchrow_hashref){
50 $total=$total+$data->{'sum(amountoutstanding)'};
53 # output(1,2,"borrower owes $total");
55 # # output(1,2,"borrower owes $total");
57 # reconcileaccount($env,$dbh,$bornumber,$total);
64 sub reconcileaccount {
65 #print put money owing give person opportunity to pay it off
66 my ($env,$dummy,$bornumber,$total)=@_;
69 my $sth=$dbh->prepare("select * from borrowers
70 where borrowernumber=$bornumber");
72 my $borrower=$sth->fetchrow_hashref;
74 #get borrower information
75 $sth=$dbh->prepare("Select * from accountlines where
76 borrowernumber=$bornumber and amountoutstanding<>0 order by date");
78 #display account information
80 #&helptext('F11 quits');
81 output(20,0,"Accounts");
86 #output (1,2,"Account Info");
87 #output (1,3,"Item\tDate \tAmount\tDescription");
88 while (my $data=$sth->fetchrow_hashref){
90 my $amount=0+$data->{'amountoutstanding'};
91 my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
92 $line= $data->{'accountno'}." ".$data->{'date'}." ".$data->{'accounttype'}." ";
93 my $title = $itemdata->{'title'};
94 if (length($title) > 15 ) {$title = substr($title,0,15);}
95 $line= $line.$itemdata->{'barcode'}." $title ".$data->{'description'};
96 $line = fmtstr($env,$line,"L65")." ".fmtdec($env,$amount,"52");
97 push @accountlines,$line;
100 #get amount paid and update database
102 &accountsdialog($env,"Payment Entry",$borrower,\@accountlines,$total);
104 &recordpayment($env,$bornumber,$dbh,$data);
105 #Check if the borrower still owes
106 $total=&checkaccount($env,$bornumber,$dbh);
114 #here we update both the accountoffsets and the account lines
115 my ($env,$bornumber,$dbh,$data)=@_;
119 my $amountleft = $data;
121 # my $sth = $dbh->prepare("begin");
123 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
124 # get lines with outstanding amounts to offset
125 my $query = "select * from accountlines
126 where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
128 my $sth = $dbh->prepare($query);
130 # offset transactions
131 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
132 if ($accdata->{'amountoutstanding'} < $amountleft) {
134 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
136 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
139 my $thisacct = $accdata->{accountno};
140 $updquery = "update accountlines set amountoutstanding= '$newamtos'
141 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
142 my $usth = $dbh->prepare($updquery);
145 $updquery = "insert into accountoffsets
146 (borrowernumber, accountno, offsetaccount, offsetamount)
147 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
148 my $usth = $dbh->prepare($updquery);
154 #$updquery = "insert into accountlines (borrowernumber,
155 #accountno,date,amount,description,accounttype,amountoutstanding) values
156 #($bornumber,$nextaccntno,datetime('now'::abstime),0-$data,'Payment,thanks',
157 #'Pay',0-$amountleft)";
158 $updquery = "insert into accountlines
159 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
160 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
161 'Pay',0-$amountleft)";
162 my $usth = $dbh->prepare($updquery);
165 UpdateStats($env,'branch','payment',$data)
168 # $sth = $dbh->prepare;
174 my ($env,$bornumber,$dbh)=@_;
176 my $query = "select * from accountlines
177 where (borrowernumber = '$bornumber')
178 order by accountno desc";
179 my $sth = $dbh->prepare($query);
181 if (my $accdata=$sth->fetchrow_hashref){
182 $nextaccntno = $accdata->{'accountno'} + 1;
185 return($nextaccntno);
188 END { } # module clean-up code here (global destructor)