1 package C4::Accounts2; #assumes C4/Accounts2
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
27 use C4::Circulation::Circ2;
28 use vars qw($VERSION @ISA @EXPORT);
30 # set the version for version checking
31 $VERSION = 0.01; # FIXME - Should probably be different from
32 # the version for C4::Accounts
36 C4::Accounts - Functions for dealing with Koha accounts
44 The functions in this module deal with the monetary aspect of Koha,
45 including looking up and modifying the amount of money owed by a
67 $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
69 Returns the next unused account number for the patron with the given
72 C<$dbh> is a DBI::db handle to the Koha database.
78 # FIXME - Okay, so what does the above actually _mean_?
80 my ($env,$bornumber,$dbh)=@_;
82 my $sth = $dbh->prepare("select * from accountlines
83 where (borrowernumber = ?)
84 order by accountno desc");
85 $sth->execute($bornumber);
86 if (my $accdata=$sth->fetchrow_hashref){
87 $nextaccntno = $accdata->{'accountno'} + 1;
94 # FIXME - Never used, but not exported, either.
96 my ($borrnum,$itemnum)=@_;
97 my $dbh = C4::Context->dbh;
98 my $borrower=borrdata('',$borrnum);
99 my $sth=$dbh->prepare("Update issues set returndate=now() where
100 borrowernumber=? and itemnumber=? and returndate is null");
101 $sth->execute($borrnum,$itemnum);
103 my @datearr = localtime(time);
104 my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
105 my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
106 $sth=$dbh->prepare("Update items set paidfor=? where itemnumber=?");
107 $sth->execute("Paid for by $bor $date",$itemnum);
113 # $amountleft = &fixcredit($env, $bornumber, $data, $barcode, $type, $user);
115 # This function is only used internally.
116 # FIXME - Figure out what this function does, and write it down.
118 #here we update both the accountoffsets and the account lines
119 my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
120 my $dbh = C4::Context->dbh;
123 my $amountleft = $data;
125 my $item=getiteminformation($env,'',$barcode);
126 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
127 my $query="Select * from accountlines where (borrowernumber=?
128 and itemnumber=? and amountoutstanding > 0)";
130 $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
131 } elsif ($type eq 'CF'){
132 $query.=" and (accounttype = 'F' or accounttype = 'FU' or
133 accounttype='Res' or accounttype='Rent')";
134 } elsif ($type eq 'CB'){
135 $query.=" and accounttype='A'";
138 my $sth=$dbh->prepare($query);
139 $sth->execute($bornumber,$item->{'itemnumber'});
140 $accdata=$sth->fetchrow_hashref;
142 if ($accdata->{'amountoutstanding'} < $amountleft) {
144 $amountleft -= $accdata->{'amountoutstanding'};
146 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
149 my $thisacct = $accdata->{accountno};
150 my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
151 where (borrowernumber = ?) and (accountno=?)");
152 $usth->execute($newamtos,$bornumber,$thisacct);
154 $usth = $dbh->prepare("insert into accountoffsets
155 (borrowernumber, accountno, offsetaccount, offsetamount)
157 $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
161 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
162 # get lines with outstanding amounts to offset
163 my $sth = $dbh->prepare("select * from accountlines
164 where (borrowernumber = ?) and (amountoutstanding >0)
166 $sth->execute($bornumber);
168 # offset transactions
169 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
170 if ($accdata->{'amountoutstanding'} < $amountleft) {
172 $amountleft -= $accdata->{'amountoutstanding'};
174 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
177 my $thisacct = $accdata->{accountno};
178 my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
179 where (borrowernumber = ?) and (accountno=?)");
180 $usth->execute($newamtos,$bornumber,$thisacct);
182 $usth = $dbh->prepare("insert into accountoffsets
183 (borrowernumber, accountno, offsetaccount, offsetamount)
185 $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
189 $env->{'branch'}=$user;
190 $type="Credit ".$type;
191 UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
197 # FIXME - Figure out what this function does, and write it down.
199 #here we update both the accountoffsets and the account lines
200 my ($env,$bornumber,$data)=@_;
201 my $dbh = C4::Context->dbh;
204 # my $branch=$env->{'branchcode'};
205 my $amountleft = $data *-1;
208 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
209 # get lines with outstanding amounts to offset
210 my $sth = $dbh->prepare("select * from accountlines
211 where (borrowernumber = ?) and (amountoutstanding<0)
213 $sth->execute($bornumber);
215 # offset transactions
216 while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
217 if ($accdata->{'amountoutstanding'} > $amountleft) {
219 $amountleft -= $accdata->{'amountoutstanding'};
221 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
225 my $thisacct = $accdata->{accountno};
226 my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
227 where (borrowernumber = ?) and (accountno=?)");
228 $usth->execute($newamtos,$bornumber,$thisacct);
230 $usth = $dbh->prepare("insert into accountoffsets
231 (borrowernumber, accountno, offsetaccount, offsetamount)
233 $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
240 END { } # module clean-up code here (global destructor)