fixed locating OPAC stylesheet in a "standard" install
[koha.git] / C4 / Accounts.pm
1 package C4::Accounts;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20
21 use strict;
22 use C4::Context;
23 use C4::Stats;
24 use C4::Members;
25 use C4::Items;
26 use C4::Circulation;
27
28 use vars qw($VERSION @ISA @EXPORT);
29
30 BEGIN {
31         # set the version for version checking
32         $VERSION = 3.02;
33         require Exporter;
34         @ISA    = qw(Exporter);
35         @EXPORT = qw(
36                 &recordpayment &fixaccounts &makepayment &manualinvoice
37                 &getnextacctno &reconcileaccount &getcharges &getcredits
38                 &getrefunds
39         );
40 }
41
42 =head1 NAME
43
44 C4::Accounts - Functions for dealing with Koha accounts
45
46 =head1 SYNOPSIS
47
48 use C4::Accounts;
49
50 =head1 DESCRIPTION
51
52 The functions in this module deal with the monetary aspect of Koha,
53 including looking up and modifying the amount of money owed by a
54 patron.
55
56 =head1 FUNCTIONS
57
58 =head2 recordpayment
59
60   &recordpayment($borrowernumber, $payment);
61
62 Record payment by a patron. C<$borrowernumber> is the patron's
63 borrower number. C<$payment> is a floating-point number, giving the
64 amount that was paid. 
65
66 Amounts owed are paid off oldest first. That is, if the patron has a
67 $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
68 of $1.50, then the oldest fine will be paid off in full, and $0.50
69 will be credited to the next one.
70
71 =cut
72
73 #'
74 sub recordpayment {
75
76     #here we update both the accountoffsets and the account lines
77     my ( $borrowernumber, $data ) = @_;
78     my $dbh        = C4::Context->dbh;
79     my $newamtos   = 0;
80     my $accdata    = "";
81     my $branch     = C4::Context->userenv->{'branch'};
82     my $amountleft = $data;
83
84     # begin transaction
85     my $nextaccntno = getnextacctno($borrowernumber);
86
87     # get lines with outstanding amounts to offset
88     my $sth = $dbh->prepare(
89         "SELECT * FROM accountlines
90   WHERE (borrowernumber = ?) AND (amountoutstanding<>0)
91   ORDER BY date"
92     );
93     $sth->execute($borrowernumber);
94
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         }
101         else {
102             $newamtos   = $accdata->{'amountoutstanding'} - $amountleft;
103             $amountleft = 0;
104         }
105         my $thisacct = $accdata->{accountno};
106         my $usth     = $dbh->prepare(
107             "UPDATE accountlines SET amountoutstanding= ?
108      WHERE (borrowernumber = ?) AND (accountno=?)"
109         );
110         $usth->execute( $newamtos, $borrowernumber, $thisacct );
111         $usth->finish;
112         $usth = $dbh->prepare(
113             "INSERT INTO accountoffsets
114      (borrowernumber, accountno, offsetaccount,  offsetamount)
115      VALUES (?,?,?,?)"
116         );
117         $usth->execute( $borrowernumber, $accdata->{'accountno'},
118             $nextaccntno, $newamtos );
119         $usth->finish;
120     }
121
122     # create new line
123     my $usth = $dbh->prepare(
124         "INSERT INTO accountlines
125   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
126   VALUES (?,?,now(),?,'Payment,thanks','Pay',?)"
127     );
128     $usth->execute( $borrowernumber, $nextaccntno, 0 - $data, 0 - $amountleft );
129     $usth->finish;
130     UpdateStats( $branch, 'payment', $data, '', '', '', $borrowernumber, $nextaccntno );
131     $sth->finish;
132 }
133
134 =head2 makepayment
135
136   &makepayment($borrowernumber, $acctnumber, $amount, $branchcode);
137
138 Records the fact that a patron has paid off the entire amount he or
139 she owes.
140
141 C<$borrowernumber> is the patron's borrower number. C<$acctnumber> is
142 the account that was credited. C<$amount> is the amount paid (this is
143 only used to record the payment. It is assumed to be equal to the
144 amount owed). C<$branchcode> is the code of the branch where payment
145 was made.
146
147 =cut
148
149 #'
150 # FIXME - I'm not at all sure about the above, because I don't
151 # understand what the acct* tables in the Koha database are for.
152 sub makepayment {
153
154     #here we update both the accountoffsets and the account lines
155     #updated to check, if they are paying off a lost item, we return the item
156     # from their card, and put a note on the item record
157     my ( $borrowernumber, $accountno, $amount, $user, $branch ) = @_;
158     my $dbh = C4::Context->dbh;
159
160     # begin transaction
161     my $nextaccntno = getnextacctno($borrowernumber);
162     my $newamtos    = 0;
163     my $sth =
164       $dbh->prepare(
165         "SELECT * FROM accountlines WHERE  borrowernumber=? AND accountno=?");
166     $sth->execute( $borrowernumber, $accountno );
167     my $data = $sth->fetchrow_hashref;
168     $sth->finish;
169
170     $dbh->do(
171         "UPDATE  accountlines
172         SET     amountoutstanding = 0
173         WHERE   borrowernumber = $borrowernumber
174           AND   accountno = $accountno
175         "
176     );
177
178     #  print $updquery;
179     $dbh->do( "
180         INSERT INTO     accountoffsets
181                         (borrowernumber, accountno, offsetaccount,
182                          offsetamount)
183         VALUES          ($borrowernumber, $accountno, $nextaccntno, $newamtos)
184         " );
185
186     # create new line
187     my $payment = 0 - $amount;
188     $dbh->do( "
189         INSERT INTO     accountlines
190                         (borrowernumber, accountno, date, amount,
191                          description, accounttype, amountoutstanding)
192         VALUES          ($borrowernumber, $nextaccntno, now(), $payment,
193                         'Payment,thanks - $user', 'Pay', 0)
194         " );
195
196     # FIXME - The second argument to &UpdateStats is supposed to be the
197     # branch code.
198     # UpdateStats is now being passed $accountno too. MTJ
199     UpdateStats( $user, 'payment', $amount, '', '', '', $borrowernumber,
200         $accountno );
201     $sth->finish;
202
203     #check to see what accounttype
204     if ( $data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L' ) {
205         returnlost( $borrowernumber, $data->{'itemnumber'} );
206     }
207 }
208
209 =head2 getnextacctno
210
211   $nextacct = &getnextacctno($borrowernumber);
212
213 Returns the next unused account number for the patron with the given
214 borrower number.
215
216 =cut
217
218 #'
219 # FIXME - Okay, so what does the above actually _mean_?
220 sub getnextacctno {
221     my ($borrowernumber) = @_;
222     my $nextaccntno      = 1;
223     my $dbh              = C4::Context->dbh;
224     my $sth              = $dbh->prepare(
225         "SELECT * FROM accountlines
226                                 WHERE (borrowernumber = ?)
227                                 ORDER BY accountno DESC"
228     );
229     $sth->execute($borrowernumber);
230     if ( my $accdata = $sth->fetchrow_hashref ) {
231         $nextaccntno = $accdata->{'accountno'} + 1;
232     }
233     $sth->finish;
234     return ($nextaccntno);
235 }
236
237 =head2 fixaccounts
238
239   &fixaccounts($borrowernumber, $accountnumber, $amount);
240
241 =cut
242
243 #'
244 # FIXME - I don't understand what this function does.
245 sub fixaccounts {
246     my ( $borrowernumber, $accountno, $amount ) = @_;
247     my $dbh = C4::Context->dbh;
248     my $sth = $dbh->prepare(
249         "SELECT * FROM accountlines WHERE borrowernumber=?
250      AND accountno=?"
251     );
252     $sth->execute( $borrowernumber, $accountno );
253     my $data = $sth->fetchrow_hashref;
254
255     # FIXME - Error-checking
256     my $diff        = $amount - $data->{'amount'};
257     my $outstanding = $data->{'amountoutstanding'} + $diff;
258     $sth->finish;
259
260     $dbh->do(<<EOT);
261         UPDATE  accountlines
262         SET     amount = '$amount',
263                 amountoutstanding = '$outstanding'
264         WHERE   borrowernumber = $borrowernumber
265           AND   accountno = $accountno
266 EOT
267 }
268
269 sub returnlost {
270     my ( $borrowernumber, $itemnum ) = @_;
271     MarkIssueReturned( $borrowernumber, $itemnum );
272     my $borrower = C4::Members::GetMember( $borrowernumber, 'borrowernumber' );
273     my @datearr = localtime(time);
274     my $date = ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
275     my $bor = "$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
276     ModItem({ paidfor =>  "Paid for by $bor $date" }, undef, $itemnum);
277 }
278
279 =head2 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 #'
294 # FIXME - Okay, so what does this function do, really?
295 sub manualinvoice {
296     my ( $borrowernumber, $itemnum, $desc, $type, $amount, $user ) = @_;
297     my $dbh      = C4::Context->dbh;
298     my $notifyid = 0;
299     my $insert;
300     $itemnum =~ s/ //g;
301     my $accountno  = getnextacctno($borrowernumber);
302     my $amountleft = $amount;
303
304     if (   $type eq 'CS'
305         || $type eq 'CB'
306         || $type eq 'CW'
307         || $type eq 'CF'
308         || $type eq 'CL' )
309     {
310         my $amount2 = $amount * -1;    # FIXME - $amount2 = -$amount
311         $amountleft =
312           fixcredit( $borrowernumber, $amount2, $itemnum, $type, $user );
313     }
314     if ( $type eq 'N' ) {
315         $desc .= "New Card";
316     }
317     if ( $type eq 'F' ) {
318         $desc .= "Fine";
319     }
320     if ( $type eq 'A' ) {
321         $desc .= "Account Management fee";
322     }
323     if ( $type eq 'M' ) {
324         $desc .= "Sundry";
325     }
326
327     if ( $type eq 'L' && $desc eq '' ) {
328
329         $desc = "Lost Item";
330     }
331     if ( $type eq 'REF' ) {
332         $desc .= "Cash Refund";
333         $amountleft = refund( '', $borrowernumber, $amount );
334     }
335     if (   ( $type eq 'L' )
336         or ( $type eq 'F' )
337         or ( $type eq 'A' )
338         or ( $type eq 'N' )
339         or ( $type eq 'M' ) )
340     {
341         $notifyid = 1;
342     }
343
344     if ( $itemnum ne '' ) {
345         $desc .= " " . $itemnum;
346         my $sth = $dbh->prepare(
347             "INSERT INTO  accountlines
348                         (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,notify_id)
349         VALUES (?, ?, now(), ?,?, ?,?,?,?)");
350      $sth->execute($borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$notifyid) || return $sth->errstr;
351   } else {
352     my $sth=$dbh->prepare("INSERT INTO  accountlines
353             (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding,notify_id)
354             VALUES (?, ?, now(), ?, ?, ?, ?,?)"
355         );
356         $sth->execute( $borrowernumber, $accountno, $amount, $desc, $type,
357             $amountleft, $notifyid );
358     }
359     return 0;
360 }
361
362 =head2 fixcredit
363
364  $amountleft = &fixcredit($borrowernumber, $data, $barcode, $type, $user);
365
366  This function is only used internally, not exported.
367  FIXME - Figure out what this function does, and write it down.
368
369 =cut
370
371 sub fixcredit {
372
373     #here we update both the accountoffsets and the account lines
374     my ( $borrowernumber, $data, $barcode, $type, $user ) = @_;
375     my $dbh        = C4::Context->dbh;
376     my $newamtos   = 0;
377     my $accdata    = "";
378     my $amountleft = $data;
379     if ( $barcode ne '' ) {
380         my $item        = GetBiblioFromItemNumber( '', $barcode );
381         my $nextaccntno = getnextacctno($borrowernumber);
382         my $query       = "SELECT * FROM accountlines WHERE (borrowernumber=?
383     AND itemnumber=? AND amountoutstanding > 0)";
384         if ( $type eq 'CL' ) {
385             $query .= " AND (accounttype = 'L' OR accounttype = 'Rep')";
386         }
387         elsif ( $type eq 'CF' ) {
388             $query .= " AND (accounttype = 'F' OR accounttype = 'FU' OR
389       accounttype='Res' OR accounttype='Rent')";
390         }
391         elsif ( $type eq 'CB' ) {
392             $query .= " and accounttype='A'";
393         }
394
395         #    print $query;
396         my $sth = $dbh->prepare($query);
397         $sth->execute( $borrowernumber, $item->{'itemnumber'} );
398         $accdata = $sth->fetchrow_hashref;
399         $sth->finish;
400         if ( $accdata->{'amountoutstanding'} < $amountleft ) {
401             $newamtos = 0;
402             $amountleft -= $accdata->{'amountoutstanding'};
403         }
404         else {
405             $newamtos   = $accdata->{'amountoutstanding'} - $amountleft;
406             $amountleft = 0;
407         }
408         my $thisacct = $accdata->{accountno};
409         my $usth     = $dbh->prepare(
410             "UPDATE accountlines SET amountoutstanding= ?
411      WHERE (borrowernumber = ?) AND (accountno=?)"
412         );
413         $usth->execute( $newamtos, $borrowernumber, $thisacct );
414         $usth->finish;
415         $usth = $dbh->prepare(
416             "INSERT INTO accountoffsets
417      (borrowernumber, accountno, offsetaccount,  offsetamount)
418      VALUES (?,?,?,?)"
419         );
420         $usth->execute( $borrowernumber, $accdata->{'accountno'},
421             $nextaccntno, $newamtos );
422         $usth->finish;
423     }
424
425     # begin transaction
426     my $nextaccntno = getnextacctno($borrowernumber);
427
428     # get lines with outstanding amounts to offset
429     my $sth = $dbh->prepare(
430         "SELECT * FROM accountlines
431   WHERE (borrowernumber = ?) AND (amountoutstanding >0)
432   ORDER BY date"
433     );
434     $sth->execute($borrowernumber);
435
436     #  print $query;
437     # offset transactions
438     while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft > 0 ) ) {
439         if ( $accdata->{'amountoutstanding'} < $amountleft ) {
440             $newamtos = 0;
441             $amountleft -= $accdata->{'amountoutstanding'};
442         }
443         else {
444             $newamtos   = $accdata->{'amountoutstanding'} - $amountleft;
445             $amountleft = 0;
446         }
447         my $thisacct = $accdata->{accountno};
448         my $usth     = $dbh->prepare(
449             "UPDATE accountlines SET amountoutstanding= ?
450      WHERE (borrowernumber = ?) AND (accountno=?)"
451         );
452         $usth->execute( $newamtos, $borrowernumber, $thisacct );
453         $usth->finish;
454         $usth = $dbh->prepare(
455             "INSERT INTO accountoffsets
456      (borrowernumber, accountno, offsetaccount,  offsetamount)
457      VALUE (?,?,?,?)"
458         );
459         $usth->execute( $borrowernumber, $accdata->{'accountno'},
460             $nextaccntno, $newamtos );
461         $usth->finish;
462     }
463     $sth->finish;
464     $type = "Credit " . $type;
465     UpdateStats( $user, $type, $data, $user, '', '', $borrowernumber );
466     $amountleft *= -1;
467     return ($amountleft);
468
469 }
470
471 =head2 refund
472
473 # FIXME - Figure out what this function does, and write it down.
474
475 =cut 
476
477 sub refund {
478
479     #here we update both the accountoffsets and the account lines
480     my ( $borrowernumber, $data ) = @_;
481     my $dbh        = C4::Context->dbh;
482     my $newamtos   = 0;
483     my $accdata    = "";
484     my $amountleft = $data * -1;
485
486     # begin transaction
487     my $nextaccntno = getnextacctno($borrowernumber);
488
489     # get lines with outstanding amounts to offset
490     my $sth = $dbh->prepare(
491         "SELECT * FROM accountlines
492   WHERE (borrowernumber = ?) AND (amountoutstanding<0)
493   ORDER BY date"
494     );
495     $sth->execute($borrowernumber);
496
497     #  print $amountleft;
498     # offset transactions
499     while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft < 0 ) ) {
500         if ( $accdata->{'amountoutstanding'} > $amountleft ) {
501             $newamtos = 0;
502             $amountleft -= $accdata->{'amountoutstanding'};
503         }
504         else {
505             $newamtos   = $accdata->{'amountoutstanding'} - $amountleft;
506             $amountleft = 0;
507         }
508
509         #     print $amountleft;
510         my $thisacct = $accdata->{accountno};
511         my $usth     = $dbh->prepare(
512             "UPDATE accountlines SET amountoutstanding= ?
513      WHERE (borrowernumber = ?) AND (accountno=?)"
514         );
515         $usth->execute( $newamtos, $borrowernumber, $thisacct );
516         $usth->finish;
517         $usth = $dbh->prepare(
518             "INSERT INTO accountoffsets
519      (borrowernumber, accountno, offsetaccount,  offsetamount)
520      VALUES (?,?,?,?)"
521         );
522         $usth->execute( $borrowernumber, $accdata->{'accountno'},
523             $nextaccntno, $newamtos );
524         $usth->finish;
525     }
526     $sth->finish;
527     return ($amountleft);
528 }
529
530 sub getcharges {
531         my ( $borrowerno, $timestamp, $accountno ) = @_;
532         my $dbh        = C4::Context->dbh;
533         my $timestamp2 = $timestamp - 1;
534         my $query      = "";
535         my $sth = $dbh->prepare(
536                         "SELECT * FROM accountlines WHERE borrowernumber=? AND accountno = ?"
537           );
538         $sth->execute( $borrowerno, $accountno );
539         
540     my @results;
541     while ( my $data = $sth->fetchrow_hashref ) {
542                 push @results,$data;
543         }
544     return (@results);
545 }
546
547
548 sub getcredits {
549         my ( $date, $date2 ) = @_;
550         my $dbh = C4::Context->dbh;
551         my $sth = $dbh->prepare(
552                                 "SELECT * FROM accountlines,borrowers
553       WHERE amount < 0 AND accounttype <> 'Pay' AND accountlines.borrowernumber = borrowers.borrowernumber
554           AND timestamp >=TIMESTAMP(?) AND timestamp < TIMESTAMP(?)"
555       );  
556
557     $sth->execute( $date, $date2 );                                                                                                              
558     my @results;          
559     while ( my $data = $sth->fetchrow_hashref ) {
560                 $data->{'date'} = $data->{'timestamp'};
561                 push @results,$data;
562         }
563     return (@results);
564
565
566
567 sub getrefunds {
568         my ( $date, $date2 ) = @_;
569         my $dbh = C4::Context->dbh;
570         
571         my $sth = $dbh->prepare(
572                                 "SELECT *,timestamp AS datetime                                                                                      
573                   FROM accountlines,borrowers
574                   WHERE (accounttype = 'REF'
575                                           AND accountlines.borrowernumber = borrowers.borrowernumber
576                                                           AND date  >=?  AND date  <?)"
577     );
578
579     $sth->execute( $date, $date2 );
580
581     my @results;
582     while ( my $data = $sth->fetchrow_hashref ) {
583                 push @results,$data;
584                 
585         }
586     return (@results);
587 }
588 END { }    # module clean-up code here (global destructor)
589
590 1;
591 __END__
592
593 =head1 SEE ALSO
594
595 DBI(3)
596
597 =cut
598