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