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
34 @EXPORT = qw(&recordpayment &fixaccounts &makepayment &manualinvoice
42 #here we update both the accountoffsets and the account lines
43 my ($env,$bornumber,$data)=@_;
48 my $branch=$env->{'branchcode'};
49 my $amountleft = $data;
51 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
52 # get lines with outstanding amounts to offset
53 my $query = "select * from accountlines
54 where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
56 my $sth = $dbh->prepare($query);
59 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
60 if ($accdata->{'amountoutstanding'} < $amountleft) {
62 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
64 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
67 my $thisacct = $accdata->{accountno};
68 $updquery = "update accountlines set amountoutstanding= '$newamtos'
69 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
70 my $usth = $dbh->prepare($updquery);
73 $updquery = "insert into accountoffsets
74 (borrowernumber, accountno, offsetaccount, offsetamount)
75 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
76 $usth = $dbh->prepare($updquery);
81 $updquery = "insert into accountlines
82 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
83 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
84 'Pay',0-$amountleft)";
85 my $usth = $dbh->prepare($updquery);
88 UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
94 #here we update both the accountoffsets and the account lines
95 #updated to check, if they are paying off a lost item, we return the item
96 # from their card, and put a note on the item record
97 my ($bornumber,$accountno,$amount,$user)=@_;
101 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
103 my $sel="Select * from accountlines where borrowernumber=$bornumber and
104 accountno=$accountno";
105 my $sth=$dbh->prepare($sel);
107 my $data=$sth->fetchrow_hashref;
109 my $updquery="Update accountlines set amountoutstanding=0 where
110 borrowernumber=$bornumber and accountno=$accountno";
111 $sth=$dbh->prepare($updquery);
115 $updquery = "insert into accountoffsets
116 (borrowernumber, accountno, offsetaccount, offsetamount)
117 values ($bornumber,$accountno,$nextaccntno,$newamtos)";
118 my $usth = $dbh->prepare($updquery);
122 my $payment=0-$amount;
123 $updquery = "insert into accountlines
124 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
125 values ($bornumber,$nextaccntno,now(),$payment,'Payment,thanks - $user', 'Pay',0)";
126 $usth = $dbh->prepare($updquery);
129 UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
132 #check to see what accounttype
133 if ($data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L'){
134 returnlost($bornumber,$data->{'itemnumber'});
139 my ($env,$bornumber,$dbh)=@_;
141 my $query = "select * from accountlines
142 where (borrowernumber = '$bornumber')
143 order by accountno desc";
144 my $sth = $dbh->prepare($query);
146 if (my $accdata=$sth->fetchrow_hashref){
147 $nextaccntno = $accdata->{'accountno'} + 1;
150 return($nextaccntno);
154 my ($borrowernumber,$accountno,$amount)=@_;
156 my $query="Select * from accountlines where borrowernumber=$borrowernumber
157 and accountno=$accountno";
158 my $sth=$dbh->prepare($query);
160 my $data=$sth->fetchrow_hashref;
161 my $diff=$amount-$data->{'amount'};
162 my $outstanding=$data->{'amountoutstanding'}+$diff;
164 $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
165 borrowernumber=$borrowernumber and accountno=$accountno";
166 $sth=$dbh->prepare($query);
174 my ($borrnum,$itemnum)=@_;
176 my $borrower=borrdata('',$borrnum); #from C4::Search;
177 my $upiss="Update issues set returndate=now() where
178 borrowernumber='$borrnum' and itemnumber='$itemnum' and returndate is null";
179 my $sth=$dbh->prepare($upiss);
182 my @datearr = localtime(time);
183 my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
184 my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
185 my $upitem="Update items set paidfor='Paid for by $bor $date' where itemnumber='$itemnum'";
186 $sth=$dbh->prepare($upitem);
193 my ($bornum,$itemnum,$desc,$type,$amount,$user)=@_;
198 my $accountno=getnextacctno('',$bornum,$dbh);
199 my $amountleft=$amount;
201 if ($type eq 'CS' || $type eq 'CB' || $type eq 'CW'
202 || $type eq 'CF' || $type eq 'CL'){
203 my $amount2=$amount*-1;
204 $amountleft=fixcredit(\%env,$bornum,$amount2,$itemnum,$type,$user);
209 if ($type eq 'L' && $desc eq ''){
213 $amountleft=refund('',$bornum,$amount);
216 my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
218 my $data=$sth->fetchrow_hashref;
221 $desc=$dbh->quote($desc);
222 $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
223 values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft','$data->{'itemnumber'}')";
225 $desc=$dbh->quote($desc);
226 $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
227 values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft')";
230 my $sth=$dbh->prepare($insert);
238 #here we update both the accountoffsets and the account lines
239 my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
244 my $amountleft = $data;
246 my $item=getiteminformation($env,'',$barcode);
247 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
248 my $query="Select * from accountlines where (borrowernumber='$bornumber'
249 and itemnumber='$item->{'itemnumber'}' and amountoutstanding > 0)";
251 $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
252 } elsif ($type eq 'CF'){
253 $query.=" and (accounttype = 'F' or accounttype = 'FU' or
254 accounttype='Res' or accounttype='Rent')";
255 } elsif ($type eq 'CB'){
256 $query.=" and accounttype='A'";
259 my $sth=$dbh->prepare($query);
261 $accdata=$sth->fetchrow_hashref;
263 if ($accdata->{'amountoutstanding'} < $amountleft) {
265 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
267 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
270 my $thisacct = $accdata->{accountno};
271 my $updquery = "update accountlines set amountoutstanding= '$newamtos'
272 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
273 my $usth = $dbh->prepare($updquery);
276 $updquery = "insert into accountoffsets
277 (borrowernumber, accountno, offsetaccount, offsetamount)
278 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
279 $usth = $dbh->prepare($updquery);
284 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
285 # get lines with outstanding amounts to offset
286 my $query = "select * from accountlines
287 where (borrowernumber = '$bornumber') and (amountoutstanding >0)
289 my $sth = $dbh->prepare($query);
292 # offset transactions
293 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
294 if ($accdata->{'amountoutstanding'} < $amountleft) {
296 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
298 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
301 my $thisacct = $accdata->{accountno};
302 $updquery = "update accountlines set amountoutstanding= '$newamtos'
303 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
304 my $usth = $dbh->prepare($updquery);
307 $updquery = "insert into accountoffsets
308 (borrowernumber, accountno, offsetaccount, offsetamount)
309 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
310 $usth = $dbh->prepare($updquery);
316 $env->{'branch'}=$user;
317 $type="Credit ".$type;
318 UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
325 #here we update both the accountoffsets and the account lines
326 my ($env,$bornumber,$data)=@_;
331 # my $branch=$env->{'branchcode'};
332 my $amountleft = $data *-1;
335 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
336 # get lines with outstanding amounts to offset
337 my $query = "select * from accountlines
338 where (borrowernumber = '$bornumber') and (amountoutstanding<0)
340 my $sth = $dbh->prepare($query);
344 # offset transactions
345 while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
346 if ($accdata->{'amountoutstanding'} > $amountleft) {
348 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
350 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
354 my $thisacct = $accdata->{accountno};
355 $updquery = "update accountlines set amountoutstanding= '$newamtos'
356 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
357 my $usth = $dbh->prepare($updquery);
360 $updquery = "insert into accountoffsets
361 (borrowernumber, accountno, offsetaccount, offsetamount)
362 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
363 $usth = $dbh->prepare($updquery);
371 END { } # module clean-up code here (global destructor)