Merged with arensb-context branch: use C4::Context->dbh instead of
[koha.git] / C4 / Accounts2.pm
1 package C4::Accounts2; #assumes C4/Accounts2
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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
11 # version.
12 #
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.
16 #
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
20
21 use strict;
22 require Exporter;
23 use DBI;
24 use C4::Context;
25 use C4::Stats;
26 use C4::Search;
27 use C4::Circulation::Circ2;
28 use vars qw($VERSION @ISA @EXPORT);
29
30 # set the version for version checking
31 $VERSION = 0.01;        # FIXME - Should probably be different from
32                         # the version for C4::Accounts
33
34 =head1 NAME
35
36 C4::Accounts - Functions for dealing with Koha accounts
37
38 =head1 SYNOPSIS
39
40   use C4::Accounts;
41
42 =head1 DESCRIPTION
43
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
46 patron.
47
48 =head1 FUNCTIONS
49
50 =over 2
51
52 =cut
53
54 @ISA = qw(Exporter);
55 @EXPORT = qw(&recordpayment &fixaccounts &makepayment &manualinvoice
56 &getnextacctno);
57
58 # FIXME - Never used
59 sub displayaccounts{
60   my ($env)=@_;
61 }
62
63 =item recordpayment
64
65   &recordpayment($env, $borrowernumber, $payment);
66
67 Record payment by a patron. C<$borrowernumber> is the patron's
68 borrower number. C<$payment> is a floating-point number, giving the
69 amount that was paid. C<$env> is a reference-to-hash;
70 C<$env-E<gt>{branchcode}> is the code of the branch where payment was
71 made.
72
73 Amounts owed are paid off oldest first. That is, if the patron has a
74 $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
75 of $1.50, then the oldest fine will be paid off in full, and $0.50
76 will be credited to the next one.
77
78 =cut
79 #'
80 sub recordpayment{
81   #here we update both the accountoffsets and the account lines
82   my ($env,$bornumber,$data)=@_;
83   my $dbh = C4::Context->dbh;
84   my $updquery = "";
85   my $newamtos = 0;
86   my $accdata = "";
87   my $branch=$env->{'branchcode'};
88   my $amountleft = $data;
89   # begin transaction
90   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
91   # get lines with outstanding amounts to offset
92   my $query = "select * from accountlines
93   where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
94   order by date";
95   my $sth = $dbh->prepare($query);
96   $sth->execute;
97   # offset transactions
98   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
99      if ($accdata->{'amountoutstanding'} < $amountleft) {
100         $newamtos = 0;
101         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
102      }  else {
103         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
104         $amountleft = 0;
105      }
106      my $thisacct = $accdata->{accountno};
107      $updquery = "update accountlines set amountoutstanding= '$newamtos'
108      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
109      my $usth = $dbh->prepare($updquery);
110      $usth->execute;
111      $usth->finish;
112      $updquery = "insert into accountoffsets
113      (borrowernumber, accountno, offsetaccount,  offsetamount)
114      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
115      $usth = $dbh->prepare($updquery);
116      $usth->execute;
117      $usth->finish;
118   }
119   # create new line
120   $updquery = "insert into accountlines
121   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
122   values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
123   'Pay',0-$amountleft)";
124   my $usth = $dbh->prepare($updquery);
125   $usth->execute;
126   $usth->finish;
127   UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
128   $sth->finish;
129 }
130
131 =item makepayment
132
133   &makepayment($borrowernumber, $acctnumber, $amount, $branchcode);
134
135 Records the fact that a patron has paid off the entire amount he or
136 she owes.
137
138 C<$borrowernumber> is the patron's borrower number. C<$acctnumber> is
139 the account that was credited. C<$amount> is the amount paid (this is
140 only used to record the payment. It is assumed to be equal to the
141 amount owed). C<$branchcode> is the code of the branch where payment
142 was made.
143
144 =cut
145 #'
146 # FIXME - I'm not at all sure about the above, because I don't
147 # understand what the acct* tables in the Koha database are for.
148 sub makepayment{
149   #here we update both the accountoffsets and the account lines
150   #updated to check, if they are paying off a lost item, we return the item
151   # from their card, and put a note on the item record
152   my ($bornumber,$accountno,$amount,$user)=@_;
153   my $env;
154   my $dbh = C4::Context->dbh;
155   # begin transaction
156   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
157   my $newamtos=0;
158   my $sel="Select * from accountlines where  borrowernumber=$bornumber and
159   accountno=$accountno";
160   my $sth=$dbh->prepare($sel);
161   $sth->execute;
162   my $data=$sth->fetchrow_hashref;
163   $sth->finish;
164   # FIXME - This prepare/execute/finish sequence could be done with
165   # $dbh->do(), no?
166   my $updquery="Update accountlines set amountoutstanding=0 where
167   borrowernumber=$bornumber and accountno=$accountno";
168   $sth=$dbh->prepare($updquery);
169   $sth->execute;
170   $sth->finish;
171 #  print $updquery;
172   $updquery = "insert into accountoffsets
173   (borrowernumber, accountno, offsetaccount,  offsetamount)
174   values ($bornumber,$accountno,$nextaccntno,$newamtos)";
175   my $usth = $dbh->prepare($updquery);
176   $usth->execute;
177   $usth->finish;
178   # create new line
179   my $payment=0-$amount;
180   $updquery = "insert into accountlines
181   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
182   values ($bornumber,$nextaccntno,now(),$payment,'Payment,thanks - $user', 'Pay',0)";
183   $usth = $dbh->prepare($updquery);
184   $usth->execute;
185   $usth->finish;
186   # FIXME - The second argument to &UpdateStats is supposed to be the
187   # branch code.
188   UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
189   $sth->finish;
190   #check to see what accounttype
191   if ($data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L'){
192     returnlost($bornumber,$data->{'itemnumber'});
193   }
194 }
195
196 =item getnextacctno
197
198   $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
199
200 Returns the next unused account number for the patron with the given
201 borrower number.
202
203 C<$dbh> is a DBI::db handle to the Koha database.
204
205 C<$env> is ignored.
206
207 =cut
208 #'
209 # FIXME - Okay, so what does the above actually _mean_?
210 sub getnextacctno {
211   my ($env,$bornumber,$dbh)=@_;
212   my $nextaccntno = 1;
213   my $query = "select * from accountlines
214   where (borrowernumber = '$bornumber')
215   order by accountno desc";
216   my $sth = $dbh->prepare($query);
217   $sth->execute;
218   if (my $accdata=$sth->fetchrow_hashref){
219     $nextaccntno = $accdata->{'accountno'} + 1;
220   }
221   $sth->finish;
222   return($nextaccntno);
223 }
224
225 =item fixaccounts
226
227   &fixaccounts($borrowernumber, $accountnumber, $amount);
228
229 =cut
230 #'
231 # FIXME - I don't understand what this function does.
232 sub fixaccounts {
233   my ($borrowernumber,$accountno,$amount)=@_;
234   my $dbh = C4::Context->dbh;
235   my $query="Select * from accountlines where borrowernumber=$borrowernumber
236      and accountno=$accountno";
237   my $sth=$dbh->prepare($query);
238   $sth->execute;
239   my $data=$sth->fetchrow_hashref;
240         # FIXME - Error-checking
241   my $diff=$amount-$data->{'amount'};
242   my $outstanding=$data->{'amountoutstanding'}+$diff;
243   $sth->finish;
244   # FIXME - Use $dbh->do();
245   $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
246           borrowernumber=$borrowernumber and accountno=$accountno";
247    $sth=$dbh->prepare($query);
248 #   print $query;
249    $sth->execute;
250    $sth->finish;
251  }
252
253 # FIXME - Never used, but not exported, either.
254 sub returnlost{
255   my ($borrnum,$itemnum)=@_;
256   my $dbh = C4::Context->dbh;
257   my $borrower=borrdata('',$borrnum); #from C4::Search;
258   my $upiss="Update issues set returndate=now() where
259   borrowernumber='$borrnum' and itemnumber='$itemnum' and returndate is null";
260   my $sth=$dbh->prepare($upiss);
261   $sth->execute;
262   $sth->finish;
263   my @datearr = localtime(time);
264   my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
265   my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
266   # FIXME - Use $dbh->do();
267   my $upitem="Update items set paidfor='Paid for by $bor $date' where itemnumber='$itemnum'";
268   $sth=$dbh->prepare($upitem);
269   $sth->execute;
270   $sth->finish;
271 }
272
273 =item manualinvoice
274
275   &manualinvoice($borrowernumber, $itemnumber, $description, $type,
276                  $amount, $user);
277
278 C<$borrowernumber> is the patron's borrower number.
279 C<$description> is a description of the transaction.
280 C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
281 or C<REF>.
282 C<$itemnumber> is the item involved, if pertinent; otherwise, it
283 should be the empty string.
284
285 =cut
286 #'
287 # FIXME - Okay, so what does this function do, really?
288 sub manualinvoice{
289   my ($bornum,$itemnum,$desc,$type,$amount,$user)=@_;
290   my $dbh = C4::Context->dbh;
291   my $insert;
292   $itemnum=~ s/ //g;
293   my %env;
294   my $accountno=getnextacctno('',$bornum,$dbh);
295   my $amountleft=$amount;
296
297   if ($type eq 'CS' || $type eq 'CB' || $type eq 'CW'
298   || $type eq 'CF' || $type eq 'CL'){
299     my $amount2=$amount*-1;     # FIXME - $amount2 = -$amount
300     $amountleft=fixcredit(\%env,$bornum,$amount2,$itemnum,$type,$user);
301   }
302   if ($type eq 'N'){
303     $desc.="New Card";
304   }
305   if ($type eq 'L' && $desc eq ''){
306     $desc="Lost Item";
307   }
308   if ($type eq 'REF'){
309     $amountleft=refund('',$bornum,$amount);
310   }
311   if ($itemnum ne ''){
312     my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
313     $sth->execute;
314     my $data=$sth->fetchrow_hashref;
315     $sth->finish;
316     $desc.=" ".$itemnum;
317     $desc=$dbh->quote($desc);
318     # FIXME - Use $dbh->do();
319     $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
320     values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft','$data->{'itemnumber'}')";
321   } else {
322       $desc=$dbh->quote($desc);
323     # FIXME - Use $dbh->do();
324     $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
325     values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft')";
326   }
327
328   my $sth=$dbh->prepare($insert);
329   $sth->execute;
330   $sth->finish;
331 }
332
333 # fixcredit
334 # $amountleft = &fixcredit($env, $bornumber, $data, $barcode, $type, $user);
335 #
336 # This function is only used internally.
337 # FIXME - Figure out what this function does, and write it down.
338 sub fixcredit{
339   #here we update both the accountoffsets and the account lines
340   my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
341   my $dbh = C4::Context->dbh;
342   my $updquery = "";
343   my $newamtos = 0;
344   my $accdata = "";
345   my $amountleft = $data;
346   if ($barcode ne ''){
347     my $item=getiteminformation($env,'',$barcode);
348     my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
349     my $query="Select * from accountlines where (borrowernumber='$bornumber'
350     and itemnumber='$item->{'itemnumber'}' and amountoutstanding > 0)";
351     if ($type eq 'CL'){
352       $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
353     } elsif ($type eq 'CF'){
354       $query.=" and (accounttype = 'F' or accounttype = 'FU' or
355       accounttype='Res' or accounttype='Rent')";
356     } elsif ($type eq 'CB'){
357       $query.=" and accounttype='A'";
358     }
359 #    print $query;
360     my $sth=$dbh->prepare($query);
361     $sth->execute;
362     $accdata=$sth->fetchrow_hashref;
363     $sth->finish;
364     if ($accdata->{'amountoutstanding'} < $amountleft) {
365         $newamtos = 0;
366         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
367      }  else {
368         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
369         $amountleft = 0;
370      }
371           my $thisacct = $accdata->{accountno};
372      my $updquery = "update accountlines set amountoutstanding= '$newamtos'
373      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
374      my $usth = $dbh->prepare($updquery);
375      $usth->execute;
376      $usth->finish;
377      $updquery = "insert into accountoffsets
378      (borrowernumber, accountno, offsetaccount,  offsetamount)
379      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
380      $usth = $dbh->prepare($updquery);
381      $usth->execute;
382      $usth->finish;
383   }
384   # begin transaction
385   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
386   # get lines with outstanding amounts to offset
387   my $query = "select * from accountlines
388   where (borrowernumber = '$bornumber') and (amountoutstanding >0)
389   order by date";
390   my $sth = $dbh->prepare($query);
391   $sth->execute;
392 #  print $query;
393   # offset transactions
394   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
395      if ($accdata->{'amountoutstanding'} < $amountleft) {
396         $newamtos = 0;
397         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
398      }  else {
399         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
400         $amountleft = 0;
401      }
402      my $thisacct = $accdata->{accountno};
403      $updquery = "update accountlines set amountoutstanding= '$newamtos'
404      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
405      my $usth = $dbh->prepare($updquery);
406      $usth->execute;
407      $usth->finish;
408      $updquery = "insert into accountoffsets
409      (borrowernumber, accountno, offsetaccount,  offsetamount)
410      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
411      $usth = $dbh->prepare($updquery);
412      $usth->execute;
413      $usth->finish;
414   }
415   $sth->finish;
416   $env->{'branch'}=$user;
417   $type="Credit ".$type;
418   UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
419   $amountleft*=-1;
420   return($amountleft);
421
422 }
423
424 # FIXME - Figure out what this function does, and write it down.
425 sub refund{
426   #here we update both the accountoffsets and the account lines
427   my ($env,$bornumber,$data)=@_;
428   my $dbh = C4::Context->dbh;
429   my $updquery = "";
430   my $newamtos = 0;
431   my $accdata = "";
432 #  my $branch=$env->{'branchcode'};
433   my $amountleft = $data *-1;
434
435   # begin transaction
436   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
437   # get lines with outstanding amounts to offset
438   my $query = "select * from accountlines
439   where (borrowernumber = '$bornumber') and (amountoutstanding<0)
440   order by date";
441   my $sth = $dbh->prepare($query);
442   $sth->execute;
443 #  print $query;
444 #  print $amountleft;
445   # offset transactions
446   while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
447      if ($accdata->{'amountoutstanding'} > $amountleft) {
448         $newamtos = 0;
449         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
450      }  else {
451         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
452         $amountleft = 0;
453      }
454 #     print $amountleft;
455      my $thisacct = $accdata->{accountno};
456      $updquery = "update accountlines set amountoutstanding= '$newamtos'
457      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
458      my $usth = $dbh->prepare($updquery);
459      $usth->execute;
460      $usth->finish;
461      $updquery = "insert into accountoffsets
462      (borrowernumber, accountno, offsetaccount,  offsetamount)
463      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
464      $usth = $dbh->prepare($updquery);
465      $usth->execute;
466      $usth->finish;
467   }
468   $sth->finish;
469   return($amountleft);
470 }
471
472 END { }       # module clean-up code here (global destructor)
473
474 1;
475 __END__
476 =back
477
478 =head1 SEE ALSO
479
480 L<DBI(3)|DBI>
481
482 =cut