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