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