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