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