Handle the iso8859-1 charset somewhat, so that when the po file is in
[koha.git] / C4 / Accounts.pm
1 package C4::Accounts; #assumes C4/Accounts
2
3 # FIXME: This module uses the CDK modules, and crashes if called from a web script
4 # Hence the existence of Accounts2
5 #
6 # This module will be deprecated when we build a new curses/slang/character
7 # based interface.
8
9 # $Id$
10
11 # Copyright 2000-2002 Katipo Communications
12 #
13 # This file is part of Koha.
14 #
15 # Koha is free software; you can redistribute it and/or modify it under the
16 # terms of the GNU General Public License as published by the Free Software
17 # Foundation; either version 2 of the License, or (at your option) any later
18 # version.
19 #
20 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
21 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License along with
25 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
26 # Suite 330, Boston, MA  02111-1307 USA
27
28 use strict;
29 require Exporter;
30 use DBI;
31 use C4::Context;
32 use C4::Format;
33 use C4::Search;
34 use C4::Stats;
35 #use C4::InterfaceCDK;
36 #use C4::Interface::AccountsCDK;
37 use vars qw($VERSION @ISA @EXPORT);
38
39 # set the version for version checking
40 $VERSION = 0.01;
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 =over 2
59
60 =cut
61
62 @ISA = qw(Exporter);
63 @EXPORT = qw(&checkaccount &reconcileaccount &getnextacctno);
64
65 =item checkaccount
66
67   $owed = &checkaccount($env, $borrowernumber, $dbh);
68
69 Looks up the total amount of money owed by a borrower (fines, etc.).
70
71 C<$borrowernumber> specifies the borrower to look up.
72
73 C<$dbh> is a DBI::db handle for the Koha database.
74
75 C<$env> is ignored.
76
77 =cut
78 #'
79 sub checkaccount  {
80   #take borrower number
81   #check accounts and list amounts owing
82   my ($env,$bornumber,$dbh)=@_;
83   my $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
84   borrowernumber=$bornumber and amountoutstanding<>0");
85   $sth->execute;
86   my $total=0;
87   while (my $data=$sth->fetchrow_hashref){
88     $total += $data->{'sum(amountoutstanding)'};
89   }
90   $sth->finish;
91   # output(1,2,"borrower owes $total");
92   #if ($total > 0){
93   #  # output(1,2,"borrower owes $total");
94   #  if ($total > 5){
95   #    reconcileaccount($env,$dbh,$bornumber,$total);
96   #  }
97   #}
98   #  pause();
99   return($total);
100 }
101
102 # XXX - POD. Need to figure out C4/Interface/AccountsCDK.pm first,
103 # though
104 # FIXME - It looks as though this function really wants to be part of
105 # a curses-based script.
106 sub reconcileaccount {
107   #print put money owing give person opportunity to pay it off
108   my ($env,$dummy,$bornumber,$total)=@_;
109   my $dbh = C4::Context->dbh;
110   #get borrower record
111   my $sth=$dbh->prepare("select * from borrowers
112     where borrowernumber=$bornumber");
113   $sth->execute;
114   my $borrower=$sth->fetchrow_hashref;
115   $sth->finish();
116   #get borrower information
117   $sth=$dbh->prepare("Select * from accountlines where
118   borrowernumber=$bornumber and amountoutstanding<>0 order by date");
119   $sth->execute;
120   #display account information
121   &clearscreen();
122   #&helptext('F11 quits');
123   output(20,0,"Accounts");
124   my @accountlines;
125   my $row=4;
126   my $i=0;
127   my $text;
128   #output (1,2,"Account Info");
129   #output (1,3,"Item\tDate      \tAmount\tDescription");
130   while (my $data=$sth->fetchrow_hashref){
131     my $line=$i+1;
132     my $amount=0+$data->{'amountoutstanding'};
133     my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
134     $line= $data->{'accountno'}." ".$data->{'date'}." ".$data->{'accounttype'}." ";
135     my $title = $itemdata->{'title'};
136     if (length($title) > 15 ) {$title = substr($title,0,15);}
137     $line .= $itemdata->{'barcode'}." $title ".$data->{'description'};
138     $line = fmtstr($env,$line,"L65")." ".fmtdec($env,$amount,"52");
139     push @accountlines,$line;
140     $i++;
141   }
142   #get amount paid and update database
143   my ($data,$reason)=
144     &accountsdialog($env,"Payment Entry",$borrower,\@accountlines,$total);
145   if ($data>0) {
146     &recordpayment($env,$bornumber,$dbh,$data);
147     #Check if the borrower still owes
148     $total=&checkaccount($env,$bornumber,$dbh);
149   }
150   return($total);
151
152 }
153
154 # FIXME - This function is never used. Then again, it's not exported,
155 # either.
156 sub recordpayment{
157   #here we update both the accountoffsets and the account lines
158   my ($env,$bornumber,$dbh,$data)=@_;
159   my $newamtos = 0;
160   my $accdata = "";
161   my $amountleft = $data;
162   # begin transaction
163 #  my $sth = $dbh->prepare("begin");
164 #  $sth->execute;
165   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
166   # get lines with outstanding amounts to offset
167   my $sth = $dbh->prepare("select * from accountlines
168   where (borrowernumber = ?) and (amountoutstanding<>0)
169   order by date");
170   $sth->execute($bornumber);
171   # offset transactions
172   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
173      if ($accdata->{'amountoutstanding'} < $amountleft) {
174         $newamtos = 0;
175         $amountleft -= $accdata->{'amountoutstanding'};
176      }  else {
177         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
178         $amountleft = 0;
179      }
180      my $thisacct = $accdata->{accountno};
181      my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
182      where (borrowernumber = ?) and (accountno=?)");
183      $usth->execute($newamtos,$bornumber,$thisacct);
184      $usth->finish;
185      
186      $usth = $dbh->prepare("insert into accountoffsets
187      (borrowernumber, accountno, offsetaccount,  offsetamount)
188      values (?,?,?,?)");
189 #     print $updquery
190      $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
191      $usth->finish;
192   }
193   # create new line
194   #$updquery = "insert into accountlines (borrowernumber,
195   #accountno,date,amount,description,accounttype,amountoutstanding) values
196   #($bornumber,$nextaccntno,datetime('now'::abstime),0-$data,'Payment,thanks',
197   #'Pay',0-$amountleft)";
198   my $usth = $dbh->prepare("insert into accountlines
199   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
200   values (?,?,now(),?,?,'Payment,thanks','Pay')");
201   $usth->execute($bornumber,$nextaccntno,0-$data,0-$amountleft);
202   $usth->finish;
203   UpdateStats($env,'branch','payment',$data)
204 }
205
206 =item getnextacctno
207
208   $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
209
210 Returns the next unused account number for the patron with the given
211 borrower number.
212
213 C<$dbh> is a DBI::db handle to the Koha database.
214
215 C<$env> is ignored.
216
217 =cut
218 # FIXME - Okay, so what does the above actually _mean_?
219 sub getnextacctno {
220   my ($env,$bornumber,$dbh)=@_;
221   my $nextaccntno = 1;
222   
223   my $sth = $dbh->prepare("select max(accountno)+1 from accountlines");
224   $sth->execute;
225   if (my $accdata=$sth->fetchrow_hashref){
226     $nextaccntno = $accdata->{'accountno'} + 1;
227   }
228   $sth->finish;
229   return$nextaccntno;
230 }
231
232 END { }       # module clean-up code here (global destructor)
233
234 1;
235 __END__
236
237 =back
238
239 =head1 SEE ALSO
240
241 C4::Accounts2(3), DBI(3)
242
243 =cut