Added a note that this module is slated to be removed
[koha.git] / C4 / Accounts.pm
1 package C4::Accounts; #assumes C4/Accounts
2
3 # 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 # FIXME - This is never used
65 sub displayaccounts{
66   my ($env)=@_;
67 }
68
69 =item checkaccount
70
71   $owed = &checkaccount($env, $borrowernumber, $dbh);
72
73 Looks up the total amount of money owed by a borrower (fines, etc.).
74
75 C<$borrowernumber> specifies the borrower to look up.
76
77 C<$dbh> is a DBI::db handle for the Koha database.
78
79 C<$env> is ignored.
80
81 =cut
82 #'
83 sub checkaccount  {
84   #take borrower number
85   #check accounts and list amounts owing
86   my ($env,$bornumber,$dbh)=@_;
87   my $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
88   borrowernumber=$bornumber and amountoutstanding<>0");
89   $sth->execute;
90   my $total=0;
91   while (my $data=$sth->fetchrow_hashref){
92     $total += $data->{'sum(amountoutstanding)'};
93   }
94   $sth->finish;
95   # output(1,2,"borrower owes $total");
96   #if ($total > 0){
97   #  # output(1,2,"borrower owes $total");
98   #  if ($total > 5){
99   #    reconcileaccount($env,$dbh,$bornumber,$total);
100   #  }
101   #}
102   #  pause();
103   return($total);
104 }
105
106 # XXX - POD. Need to figure out C4/Interface/AccountsCDK.pm first,
107 # though
108 # FIXME - It looks as though this function really wants to be part of
109 # a curses-based script.
110 sub reconcileaccount {
111   #print put money owing give person opportunity to pay it off
112   my ($env,$dummy,$bornumber,$total)=@_;
113   my $dbh = C4::Context->dbh;
114   #get borrower record
115   my $sth=$dbh->prepare("select * from borrowers
116     where borrowernumber=$bornumber");
117   $sth->execute;
118   my $borrower=$sth->fetchrow_hashref;
119   $sth->finish();
120   #get borrower information
121   $sth=$dbh->prepare("Select * from accountlines where
122   borrowernumber=$bornumber and amountoutstanding<>0 order by date");
123   $sth->execute;
124   #display account information
125   &clearscreen();
126   #&helptext('F11 quits');
127   output(20,0,"Accounts");
128   my @accountlines;
129   my $row=4;
130   my $i=0;
131   my $text;
132   #output (1,2,"Account Info");
133   #output (1,3,"Item\tDate      \tAmount\tDescription");
134   while (my $data=$sth->fetchrow_hashref){
135     my $line=$i+1;
136     my $amount=0+$data->{'amountoutstanding'};
137     my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
138     $line= $data->{'accountno'}." ".$data->{'date'}." ".$data->{'accounttype'}." ";
139     my $title = $itemdata->{'title'};
140     if (length($title) > 15 ) {$title = substr($title,0,15);}
141     $line .= $itemdata->{'barcode'}." $title ".$data->{'description'};
142     $line = fmtstr($env,$line,"L65")." ".fmtdec($env,$amount,"52");
143     push @accountlines,$line;
144     $i++;
145   }
146   #get amount paid and update database
147   my ($data,$reason)=
148     &accountsdialog($env,"Payment Entry",$borrower,\@accountlines,$total);
149   if ($data>0) {
150     &recordpayment($env,$bornumber,$dbh,$data);
151     #Check if the borrower still owes
152     $total=&checkaccount($env,$bornumber,$dbh);
153   }
154   return($total);
155
156 }
157
158 # FIXME - This function is never used. Then again, it's not exported,
159 # either.
160 sub recordpayment{
161   #here we update both the accountoffsets and the account lines
162   my ($env,$bornumber,$dbh,$data)=@_;
163   my $updquery = "";
164   my $newamtos = 0;
165   my $accdata = "";
166   my $amountleft = $data;
167   # begin transaction
168 #  my $sth = $dbh->prepare("begin");
169 #  $sth->execute;
170   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
171   # get lines with outstanding amounts to offset
172   my $query = "select * from accountlines
173   where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
174   order by date";
175   my $sth = $dbh->prepare($query);
176   $sth->execute;
177   # offset transactions
178   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
179      if ($accdata->{'amountoutstanding'} < $amountleft) {
180         $newamtos = 0;
181         $amountleft -= $accdata->{'amountoutstanding'};
182      }  else {
183         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
184         $amountleft = 0;
185      }
186      my $thisacct = $accdata->{accountno};
187      $updquery = "update accountlines set amountoutstanding= '$newamtos'
188      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
189      my $usth = $dbh->prepare($updquery);
190      $usth->execute;
191      $usth->finish;
192      $updquery = "insert into accountoffsets
193      (borrowernumber, accountno, offsetaccount,  offsetamount)
194      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
195      # FIXME - There's already a $usth in this scope.
196      my $usth = $dbh->prepare($updquery);
197 #     print $updquery
198      $usth->execute;
199      $usth->finish;
200   }
201   # create new line
202   #$updquery = "insert into accountlines (borrowernumber,
203   #accountno,date,amount,description,accounttype,amountoutstanding) values
204   #($bornumber,$nextaccntno,datetime('now'::abstime),0-$data,'Payment,thanks',
205   #'Pay',0-$amountleft)";
206   $updquery = "insert into accountlines
207   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
208   values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
209   'Pay',0-$amountleft)";
210   my $usth = $dbh->prepare($updquery);
211   $usth->execute;
212   $usth->finish;
213   UpdateStats($env,'branch','payment',$data)
214 #  $sth->finish;
215 #  $query = "commit";
216 #  $sth = $dbh->prepare;
217 #  $sth->execute;
218 #  $sth-finish;
219 }
220
221 =item getnextacctno
222
223   $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
224
225 Returns the next unused account number for the patron with the given
226 borrower number.
227
228 C<$dbh> is a DBI::db handle to the Koha database.
229
230 C<$env> is ignored.
231
232 =cut
233 # FIXME - Okay, so what does the above actually _mean_?
234 sub getnextacctno {
235   my ($env,$bornumber,$dbh)=@_;
236   my $nextaccntno = 1;
237   # FIXME - This could just be
238   #     SELECT max(accountno)+1 from accountlines;
239   my $query = "select * from accountlines
240   where (borrowernumber = '$bornumber')
241   order by accountno desc";
242   my $sth = $dbh->prepare($query);
243   $sth->execute;
244   if (my $accdata=$sth->fetchrow_hashref){
245     $nextaccntno = $accdata->{'accountno'} + 1;
246   }
247   $sth->finish;
248   return($nextaccntno);
249 }
250
251 END { }       # module clean-up code here (global destructor)
252
253 1;
254 __END__
255
256 =back
257
258 =head1 SEE ALSO
259
260 C4::Accounts2(3), DBI(3)
261
262 =cut