New scripts for translation into Chinese and other languages where English
[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 #FIXME to use ? before uncommenting
305 #     my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
306 #     $sth->execute;
307 #     my $data=$sth->fetchrow_hashref;
308 #     $sth->finish;
309     $desc.=" ".$itemnum;
310     my $sth=$dbh->prepare("INSERT INTO  accountlines
311                         (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber)
312         VALUES (?, ?, now(), ?,?, ?,?,?)");
313 #     $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft, $data->{'itemnumber'});
314      $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft, $itemnum);
315   } else {
316     $desc=$dbh->quote($desc);
317     my $sth=$dbh->prepare("INSERT INTO  accountlines
318                         (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding)
319                         VALUES (?, ?, now(), ?, ?, ?, ?)");
320     $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft);
321   }
322 }
323
324 # fixcredit
325 # $amountleft = &fixcredit($env, $bornumber, $data, $barcode, $type, $user);
326 #
327 # This function is only used internally.
328 # FIXME - Figure out what this function does, and write it down.
329 sub fixcredit{
330   #here we update both the accountoffsets and the account lines
331   my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
332   my $dbh = C4::Context->dbh;
333   my $newamtos = 0;
334   my $accdata = "";
335   my $amountleft = $data;
336   if ($barcode ne ''){
337     my $item=getiteminformation($env,'',$barcode);
338     my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
339     my $query="Select * from accountlines where (borrowernumber=?
340     and itemnumber=? and amountoutstanding > 0)";
341     if ($type eq 'CL'){
342       $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
343     } elsif ($type eq 'CF'){
344       $query.=" and (accounttype = 'F' or accounttype = 'FU' or
345       accounttype='Res' or accounttype='Rent')";
346     } elsif ($type eq 'CB'){
347       $query.=" and accounttype='A'";
348     }
349 #    print $query;
350     my $sth=$dbh->prepare($query);
351     $sth->execute($bornumber,$item->{'itemnumber'});
352     $accdata=$sth->fetchrow_hashref;
353     $sth->finish;
354     if ($accdata->{'amountoutstanding'} < $amountleft) {
355         $newamtos = 0;
356         $amountleft -= $accdata->{'amountoutstanding'};
357      }  else {
358         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
359         $amountleft = 0;
360      }
361           my $thisacct = $accdata->{accountno};
362      my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
363      where (borrowernumber = ?) and (accountno=?)");
364      $usth->execute($newamtos,$bornumber,$thisacct);
365      $usth->finish;
366      $usth = $dbh->prepare("insert into accountoffsets
367      (borrowernumber, accountno, offsetaccount,  offsetamount)
368      values (?,?,?,?)");
369      $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
370      $usth->finish;
371   }
372   # begin transaction
373   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
374   # get lines with outstanding amounts to offset
375   my $sth = $dbh->prepare("select * from accountlines
376   where (borrowernumber = ?) and (amountoutstanding >0)
377   order by date");
378   $sth->execute($bornumber);
379 #  print $query;
380   # offset transactions
381   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
382      if ($accdata->{'amountoutstanding'} < $amountleft) {
383         $newamtos = 0;
384         $amountleft -= $accdata->{'amountoutstanding'};
385      }  else {
386         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
387         $amountleft = 0;
388      }
389      my $thisacct = $accdata->{accountno};
390      my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
391      where (borrowernumber = ?) and (accountno=?)");
392      $usth->execute($newamtos,$bornumber,$thisacct);
393      $usth->finish;
394      $usth = $dbh->prepare("insert into accountoffsets
395      (borrowernumber, accountno, offsetaccount,  offsetamount)
396      values (?,?,?,?)");
397      $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
398      $usth->finish;
399   }
400   $sth->finish;
401   $env->{'branch'}=$user;
402   $type="Credit ".$type;
403   UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
404   $amountleft*=-1;
405   return($amountleft);
406
407 }
408
409 # FIXME - Figure out what this function does, and write it down.
410 sub refund{
411   #here we update both the accountoffsets and the account lines
412   my ($env,$bornumber,$data)=@_;
413   my $dbh = C4::Context->dbh;
414   my $newamtos = 0;
415   my $accdata = "";
416 #  my $branch=$env->{'branchcode'};
417   my $amountleft = $data *-1;
418
419   # begin transaction
420   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
421   # get lines with outstanding amounts to offset
422   my $sth = $dbh->prepare("select * from accountlines
423   where (borrowernumber = ?) and (amountoutstanding<0)
424   order by date");
425   $sth->execute($bornumber);
426 #  print $amountleft;
427   # offset transactions
428   while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
429      if ($accdata->{'amountoutstanding'} > $amountleft) {
430         $newamtos = 0;
431         $amountleft -= $accdata->{'amountoutstanding'};
432      }  else {
433         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
434         $amountleft = 0;
435      }
436 #     print $amountleft;
437      my $thisacct = $accdata->{accountno};
438      my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
439      where (borrowernumber = ?) and (accountno=?)");
440      $usth->execute($newamtos,$bornumber,$thisacct);
441      $usth->finish;
442      $usth = $dbh->prepare("insert into accountoffsets
443      (borrowernumber, accountno, offsetaccount,  offsetamount)
444      values (?,?,?,?)");
445      $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
446      $usth->finish;
447   }
448   $sth->finish;
449   return($amountleft);
450 }
451
452 END { }       # module clean-up code here (global destructor)
453
454 1;
455 __END__
456
457 =back
458
459 =head1 SEE ALSO
460
461 DBI(3)
462
463 =cut