Added an incomplete POD, as well as some FIXME comments (which, as it
[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::Database;
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=C4Connect;
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   $dbh->disconnect;
130 }
131
132 =item makepayment
133
134   &makepayment($borrowernumber, $acctnumber, $amount, $branchcode);
135
136 Records the fact that a patron has paid off the entire amount he or
137 she owes.
138
139 C<$borrowernumber> is the patron's borrower number. C<$acctnumber> is
140 the account that was credited. C<$amount> is the amount paid (this is
141 only used to record the payment. It is assumed to be equal to the
142 amount owed). C<$branchcode> is the code of the branch where payment
143 was made.
144
145 =cut
146 #'
147 # FIXME - I'm not at all sure about the above, because I don't
148 # understand what the acct* tables in the Koha database are for.
149 sub makepayment{
150   #here we update both the accountoffsets and the account lines
151   #updated to check, if they are paying off a lost item, we return the item
152   # from their card, and put a note on the item record
153   my ($bornumber,$accountno,$amount,$user)=@_;
154   my $env;
155   my $dbh=C4Connect;
156   # begin transaction
157   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
158   my $newamtos=0;
159   my $sel="Select * from accountlines where  borrowernumber=$bornumber and
160   accountno=$accountno";
161   my $sth=$dbh->prepare($sel);
162   $sth->execute;
163   my $data=$sth->fetchrow_hashref;
164   $sth->finish;
165   # FIXME - This prepare/execute/finish sequence could be done with
166   # $dbh->do(), no?
167   my $updquery="Update accountlines set amountoutstanding=0 where
168   borrowernumber=$bornumber and accountno=$accountno";
169   $sth=$dbh->prepare($updquery);
170   $sth->execute;
171   $sth->finish;
172 #  print $updquery;
173   $updquery = "insert into accountoffsets
174   (borrowernumber, accountno, offsetaccount,  offsetamount)
175   values ($bornumber,$accountno,$nextaccntno,$newamtos)";
176   my $usth = $dbh->prepare($updquery);
177   $usth->execute;
178   $usth->finish;
179   # create new line
180   my $payment=0-$amount;
181   $updquery = "insert into accountlines
182   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
183   values ($bornumber,$nextaccntno,now(),$payment,'Payment,thanks - $user', 'Pay',0)";
184   $usth = $dbh->prepare($updquery);
185   $usth->execute;
186   $usth->finish;
187   # FIXME - The second argument to &UpdateStats is supposed to be the
188   # branch code.
189   UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
190   $sth->finish;
191   $dbh->disconnect;
192   #check to see what accounttype
193   if ($data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L'){
194     returnlost($bornumber,$data->{'itemnumber'});
195   }
196 }
197
198 =item getnextacctno
199
200   $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
201
202 Returns the next unused account number for the patron with the given
203 borrower number.
204
205 C<$dbh> is a DBI::db handle to the Koha database.
206
207 C<$env> is ignored.
208
209 =cut
210 #'
211 # FIXME - Okay, so what does the above actually _mean_?
212 sub getnextacctno {
213   my ($env,$bornumber,$dbh)=@_;
214   my $nextaccntno = 1;
215   my $query = "select * from accountlines
216   where (borrowernumber = '$bornumber')
217   order by accountno desc";
218   my $sth = $dbh->prepare($query);
219   $sth->execute;
220   if (my $accdata=$sth->fetchrow_hashref){
221     $nextaccntno = $accdata->{'accountno'} + 1;
222   }
223   $sth->finish;
224   return($nextaccntno);
225 }
226
227 =item fixaccounts
228
229   &fixaccounts($borrowernumber, $accountnumber, $amount);
230
231 =cut
232 #'
233 # FIXME - I don't understand what this function does.
234 sub fixaccounts {
235   my ($borrowernumber,$accountno,$amount)=@_;
236   my $dbh=C4Connect;
237   my $query="Select * from accountlines where borrowernumber=$borrowernumber
238      and accountno=$accountno";
239   my $sth=$dbh->prepare($query);
240   $sth->execute;
241   my $data=$sth->fetchrow_hashref;
242         # FIXME - Error-checking
243   my $diff=$amount-$data->{'amount'};
244   my $outstanding=$data->{'amountoutstanding'}+$diff;
245   $sth->finish;
246   $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
247           borrowernumber=$borrowernumber and accountno=$accountno";
248    $sth=$dbh->prepare($query);
249 #   print $query;
250    $sth->execute;
251    $sth->finish;
252    $dbh->disconnect;
253  }
254
255 # FIXME - Never used, but not exported, either.
256 sub returnlost{
257   my ($borrnum,$itemnum)=@_;
258   my $dbh=C4Connect;
259   my $borrower=borrdata('',$borrnum); #from C4::Search;
260   my $upiss="Update issues set returndate=now() where
261   borrowernumber='$borrnum' and itemnumber='$itemnum' and returndate is null";
262   my $sth=$dbh->prepare($upiss);
263   $sth->execute;
264   $sth->finish;
265   my @datearr = localtime(time);
266   my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
267   my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
268   my $upitem="Update items set paidfor='Paid for by $bor $date' where itemnumber='$itemnum'";
269   $sth=$dbh->prepare($upitem);
270   $sth->execute;
271   $sth->finish;
272   $dbh->disconnect;
273 }
274
275 =item manualinvoice
276
277   &manualinvoice($borrowernumber, $itemnumber, $description, $type,
278                  $amount, $user);
279
280 C<$borrowernumber> is the patron's borrower number.
281 C<$description> is a description of the transaction.
282 C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
283 or C<REF>.
284 C<$itemnumber> is the item involved, if pertinent; otherwise, it
285 should be the empty string.
286
287 =cut
288 #'
289 # FIXME - Okay, so what does this function do, really?
290 sub manualinvoice{
291   my ($bornum,$itemnum,$desc,$type,$amount,$user)=@_;
292   my $dbh=C4Connect;
293   my $insert;
294   $itemnum=~ s/ //g;
295   my %env;
296   my $accountno=getnextacctno('',$bornum,$dbh);
297   my $amountleft=$amount;
298
299   if ($type eq 'CS' || $type eq 'CB' || $type eq 'CW'
300   || $type eq 'CF' || $type eq 'CL'){
301     my $amount2=$amount*-1;     # FIXME - $amount2 = -$amount
302     $amountleft=fixcredit(\%env,$bornum,$amount2,$itemnum,$type,$user);
303   }
304   if ($type eq 'N'){
305     $desc.="New Card";
306   }
307   if ($type eq 'L' && $desc eq ''){
308     $desc="Lost Item";
309   }
310   if ($type eq 'REF'){
311     $amountleft=refund('',$bornum,$amount);
312   }
313   if ($itemnum ne ''){
314     my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
315     $sth->execute;
316     my $data=$sth->fetchrow_hashref;
317     $sth->finish;
318     $desc.=" ".$itemnum;
319     $desc=$dbh->quote($desc);
320     $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
321     values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft','$data->{'itemnumber'}')";
322   } else {
323       $desc=$dbh->quote($desc);
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   $dbh->disconnect;
333 }
334
335 # fixcredit
336 # $amountleft = &fixcredit($env, $bornumber, $data, $barcode, $type, $user);
337 #
338 # This function is only used internally.
339 # FIXME - Figure out what this function does, and write it down.
340 sub fixcredit{
341   #here we update both the accountoffsets and the account lines
342   my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
343   my $dbh=C4Connect;
344   my $updquery = "";
345   my $newamtos = 0;
346   my $accdata = "";
347   my $amountleft = $data;
348   if ($barcode ne ''){
349     my $item=getiteminformation($env,'',$barcode);
350     my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
351     my $query="Select * from accountlines where (borrowernumber='$bornumber'
352     and itemnumber='$item->{'itemnumber'}' and amountoutstanding > 0)";
353     if ($type eq 'CL'){
354       $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
355     } elsif ($type eq 'CF'){
356       $query.=" and (accounttype = 'F' or accounttype = 'FU' or
357       accounttype='Res' or accounttype='Rent')";
358     } elsif ($type eq 'CB'){
359       $query.=" and accounttype='A'";
360     }
361 #    print $query;
362     my $sth=$dbh->prepare($query);
363     $sth->execute;
364     $accdata=$sth->fetchrow_hashref;
365     $sth->finish;
366     if ($accdata->{'amountoutstanding'} < $amountleft) {
367         $newamtos = 0;
368         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
369      }  else {
370         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
371         $amountleft = 0;
372      }
373           my $thisacct = $accdata->{accountno};
374      my $updquery = "update accountlines set amountoutstanding= '$newamtos'
375      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
376      my $usth = $dbh->prepare($updquery);
377      $usth->execute;
378      $usth->finish;
379      $updquery = "insert into accountoffsets
380      (borrowernumber, accountno, offsetaccount,  offsetamount)
381      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
382      $usth = $dbh->prepare($updquery);
383      $usth->execute;
384      $usth->finish;
385   }
386   # begin transaction
387   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
388   # get lines with outstanding amounts to offset
389   my $query = "select * from accountlines
390   where (borrowernumber = '$bornumber') and (amountoutstanding >0)
391   order by date";
392   my $sth = $dbh->prepare($query);
393   $sth->execute;
394 #  print $query;
395   # offset transactions
396   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
397      if ($accdata->{'amountoutstanding'} < $amountleft) {
398         $newamtos = 0;
399         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
400      }  else {
401         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
402         $amountleft = 0;
403      }
404      my $thisacct = $accdata->{accountno};
405      $updquery = "update accountlines set amountoutstanding= '$newamtos'
406      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
407      my $usth = $dbh->prepare($updquery);
408      $usth->execute;
409      $usth->finish;
410      $updquery = "insert into accountoffsets
411      (borrowernumber, accountno, offsetaccount,  offsetamount)
412      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
413      $usth = $dbh->prepare($updquery);
414      $usth->execute;
415      $usth->finish;
416   }
417   $sth->finish;
418   $dbh->disconnect;
419   $env->{'branch'}=$user;
420   $type="Credit ".$type;
421   UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
422   $amountleft*=-1;
423   return($amountleft);
424
425 }
426
427 # FIXME - Figure out what this function does, and write it down.
428 sub refund{
429   #here we update both the accountoffsets and the account lines
430   my ($env,$bornumber,$data)=@_;
431   my $dbh=C4Connect;
432   my $updquery = "";
433   my $newamtos = 0;
434   my $accdata = "";
435 #  my $branch=$env->{'branchcode'};
436   my $amountleft = $data *-1;
437
438   # begin transaction
439   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
440   # get lines with outstanding amounts to offset
441   my $query = "select * from accountlines
442   where (borrowernumber = '$bornumber') and (amountoutstanding<0)
443   order by date";
444   my $sth = $dbh->prepare($query);
445   $sth->execute;
446 #  print $query;
447 #  print $amountleft;
448   # offset transactions
449   while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
450      if ($accdata->{'amountoutstanding'} > $amountleft) {
451         $newamtos = 0;
452         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
453      }  else {
454         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
455         $amountleft = 0;
456      }
457 #     print $amountleft;
458      my $thisacct = $accdata->{accountno};
459      $updquery = "update accountlines set amountoutstanding= '$newamtos'
460      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
461      my $usth = $dbh->prepare($updquery);
462      $usth->execute;
463      $usth->finish;
464      $updquery = "insert into accountoffsets
465      (borrowernumber, accountno, offsetaccount,  offsetamount)
466      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
467      $usth = $dbh->prepare($updquery);
468      $usth->execute;
469      $usth->finish;
470   }
471   $sth->finish;
472   $dbh->disconnect;
473   return($amountleft);
474 }
475
476 END { }       # module clean-up code here (global destructor)
477
478 1;
479 __END__
480 =back
481
482 =head1 SEE ALSO
483
484 L<DBI(3)|DBI>
485
486 =cut