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