1 package C4::Accounts; #assumes C4/Accounts
3 # This module uses the CDK modules, and crashes if called from a web script
4 # Hence the existence of Accounts2
6 # This module will be deprecated when we build a new curses/slang/character
11 # Copyright 2000-2002 Katipo Communications
13 # This file is part of Koha.
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
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.
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
36 use C4::Interface::AccountsCDK;
37 use vars qw($VERSION @ISA @EXPORT);
39 # set the version for version checking
44 C4::Accounts - Functions for dealing with Koha accounts
52 The functions in this module deal with the monetary aspect of Koha,
53 including looking up and modifying the amount of money owed by a
63 @EXPORT = qw(&checkaccount &reconcileaccount &getnextacctno);
67 $owed = &checkaccount($env, $borrowernumber, $dbh);
69 Looks up the total amount of money owed by a borrower (fines, etc.).
71 C<$borrowernumber> specifies the borrower to look up.
73 C<$dbh> is a DBI::db handle for the Koha database.
81 #check accounts and list amounts owing
82 my ($env,$bornumber,$dbh)=@_;
83 my $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
84 borrowernumber=$bornumber and amountoutstanding<>0");
87 while (my $data=$sth->fetchrow_hashref){
88 $total += $data->{'sum(amountoutstanding)'};
91 # output(1,2,"borrower owes $total");
93 # # output(1,2,"borrower owes $total");
95 # reconcileaccount($env,$dbh,$bornumber,$total);
102 # XXX - POD. Need to figure out C4/Interface/AccountsCDK.pm first,
104 # FIXME - It looks as though this function really wants to be part of
105 # a curses-based script.
106 sub reconcileaccount {
107 #print put money owing give person opportunity to pay it off
108 my ($env,$dummy,$bornumber,$total)=@_;
109 my $dbh = C4::Context->dbh;
111 my $sth=$dbh->prepare("select * from borrowers
112 where borrowernumber=$bornumber");
114 my $borrower=$sth->fetchrow_hashref;
116 #get borrower information
117 $sth=$dbh->prepare("Select * from accountlines where
118 borrowernumber=$bornumber and amountoutstanding<>0 order by date");
120 #display account information
122 #&helptext('F11 quits');
123 output(20,0,"Accounts");
128 #output (1,2,"Account Info");
129 #output (1,3,"Item\tDate \tAmount\tDescription");
130 while (my $data=$sth->fetchrow_hashref){
132 my $amount=0+$data->{'amountoutstanding'};
133 my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
134 $line= $data->{'accountno'}." ".$data->{'date'}." ".$data->{'accounttype'}." ";
135 my $title = $itemdata->{'title'};
136 if (length($title) > 15 ) {$title = substr($title,0,15);}
137 $line .= $itemdata->{'barcode'}." $title ".$data->{'description'};
138 $line = fmtstr($env,$line,"L65")." ".fmtdec($env,$amount,"52");
139 push @accountlines,$line;
142 #get amount paid and update database
144 &accountsdialog($env,"Payment Entry",$borrower,\@accountlines,$total);
146 &recordpayment($env,$bornumber,$dbh,$data);
147 #Check if the borrower still owes
148 $total=&checkaccount($env,$bornumber,$dbh);
154 # FIXME - This function is never used. Then again, it's not exported,
157 #here we update both the accountoffsets and the account lines
158 my ($env,$bornumber,$dbh,$data)=@_;
162 my $amountleft = $data;
164 # my $sth = $dbh->prepare("begin");
166 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
167 # get lines with outstanding amounts to offset
168 my $query = "select * from accountlines
169 where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
171 my $sth = $dbh->prepare($query);
173 # offset transactions
174 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
175 if ($accdata->{'amountoutstanding'} < $amountleft) {
177 $amountleft -= $accdata->{'amountoutstanding'};
179 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
182 my $thisacct = $accdata->{accountno};
183 $updquery = "update accountlines set amountoutstanding= '$newamtos'
184 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
185 my $usth = $dbh->prepare($updquery);
189 $updquery = "insert into accountoffsets
190 (borrowernumber, accountno, offsetaccount, offsetamount)
191 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
192 $usth = $dbh->prepare($updquery);
198 #$updquery = "insert into accountlines (borrowernumber,
199 #accountno,date,amount,description,accounttype,amountoutstanding) values
200 #($bornumber,$nextaccntno,datetime('now'::abstime),0-$data,'Payment,thanks',
201 #'Pay',0-$amountleft)";
202 $updquery = "insert into accountlines
203 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
204 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
205 'Pay',0-$amountleft)";
206 $usth = $dbh->prepare($updquery);
209 UpdateStats($env,'branch','payment',$data)
214 $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
216 Returns the next unused account number for the patron with the given
219 C<$dbh> is a DBI::db handle to the Koha database.
224 # FIXME - Okay, so what does the above actually _mean_?
226 my ($env,$bornumber,$dbh)=@_;
229 my $query = "select max(accountno)+1 from accountlines";
230 my $sth = $dbh->prepare($query);
232 if (my $accdata=$sth->fetchrow_hashref){
233 $nextaccntno = $accdata->{'accountno'} + 1;
239 END { } # module clean-up code here (global destructor)
248 C4::Accounts2(3), DBI(3)