code cleaning : removing useless package (everything needed copied in Accounts2.pm)
[koha-ffzg.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::Members;
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( 
56 &getnextacctno);
57
58 # FIXME - Never used
59 sub displayaccounts{
60   my ($env)=@_;
61 }
62
63
64
65 =item getnextacctno
66
67   $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
68
69 Returns the next unused account number for the patron with the given
70 borrower number.
71
72 C<$dbh> is a DBI::db handle to the Koha database.
73
74 C<$env> is ignored.
75
76 =cut
77 #'
78 # FIXME - Okay, so what does the above actually _mean_?
79 sub getnextacctno {
80   my ($env,$bornumber,$dbh)=@_;
81   my $nextaccntno = 1;
82   my $sth = $dbh->prepare("select * from accountlines
83   where (borrowernumber = ?)
84   order by accountno desc");
85   $sth->execute($bornumber);
86   if (my $accdata=$sth->fetchrow_hashref){
87     $nextaccntno = $accdata->{'accountno'} + 1;
88   }
89   $sth->finish;
90   return($nextaccntno);
91 }
92
93
94 # FIXME - Never used, but not exported, either.
95 sub returnlost{
96   my ($borrnum,$itemnum)=@_;
97   my $dbh = C4::Context->dbh;
98   my $borrower=borrdata('',$borrnum);
99   my $sth=$dbh->prepare("Update issues set returndate=now() where
100   borrowernumber=? and itemnumber=? and returndate is null");
101   $sth->execute($borrnum,$itemnum);
102   $sth->finish;
103   my @datearr = localtime(time);
104   my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
105   my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
106   $sth=$dbh->prepare("Update items set paidfor=? where itemnumber=?");
107   $sth->execute("Paid for by $bor $date",$itemnum);
108   $sth->finish;
109 }
110
111
112 # fixcredit
113 # $amountleft = &fixcredit($env, $bornumber, $data, $barcode, $type, $user);
114 #
115 # This function is only used internally.
116 # FIXME - Figure out what this function does, and write it down.
117 sub fixcredit{
118   #here we update both the accountoffsets and the account lines
119   my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
120   my $dbh = C4::Context->dbh;
121   my $newamtos = 0;
122   my $accdata = "";
123   my $amountleft = $data;
124   if ($barcode ne ''){
125     my $item=getiteminformation($env,'',$barcode);
126     my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
127     my $query="Select * from accountlines where (borrowernumber=?
128     and itemnumber=? and amountoutstanding > 0)";
129     if ($type eq 'CL'){
130       $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
131     } elsif ($type eq 'CF'){
132       $query.=" and (accounttype = 'F' or accounttype = 'FU' or
133       accounttype='Res' or accounttype='Rent')";
134     } elsif ($type eq 'CB'){
135       $query.=" and accounttype='A'";
136     }
137 #    print $query;
138     my $sth=$dbh->prepare($query);
139     $sth->execute($bornumber,$item->{'itemnumber'});
140     $accdata=$sth->fetchrow_hashref;
141     $sth->finish;
142     if ($accdata->{'amountoutstanding'} < $amountleft) {
143         $newamtos = 0;
144         $amountleft -= $accdata->{'amountoutstanding'};
145      }  else {
146         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
147         $amountleft = 0;
148      }
149           my $thisacct = $accdata->{accountno};
150      my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
151      where (borrowernumber = ?) and (accountno=?)");
152      $usth->execute($newamtos,$bornumber,$thisacct);
153      $usth->finish;
154      $usth = $dbh->prepare("insert into accountoffsets
155      (borrowernumber, accountno, offsetaccount,  offsetamount)
156      values (?,?,?,?)");
157      $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
158      $usth->finish;
159   }
160   # begin transaction
161   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
162   # get lines with outstanding amounts to offset
163   my $sth = $dbh->prepare("select * from accountlines
164   where (borrowernumber = ?) and (amountoutstanding >0)
165   order by date");
166   $sth->execute($bornumber);
167 #  print $query;
168   # offset transactions
169   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
170      if ($accdata->{'amountoutstanding'} < $amountleft) {
171         $newamtos = 0;
172         $amountleft -= $accdata->{'amountoutstanding'};
173      }  else {
174         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
175         $amountleft = 0;
176      }
177      my $thisacct = $accdata->{accountno};
178      my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
179      where (borrowernumber = ?) and (accountno=?)");
180      $usth->execute($newamtos,$bornumber,$thisacct);
181      $usth->finish;
182      $usth = $dbh->prepare("insert into accountoffsets
183      (borrowernumber, accountno, offsetaccount,  offsetamount)
184      values (?,?,?,?)");
185      $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
186      $usth->finish;
187   }
188   $sth->finish;
189   $env->{'branch'}=$user;
190   $type="Credit ".$type;
191   UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
192   $amountleft*=-1;
193   return($amountleft);
194
195 }
196
197 # FIXME - Figure out what this function does, and write it down.
198 sub refund{
199   #here we update both the accountoffsets and the account lines
200   my ($env,$bornumber,$data)=@_;
201   my $dbh = C4::Context->dbh;
202   my $newamtos = 0;
203   my $accdata = "";
204 #  my $branch=$env->{'branchcode'};
205   my $amountleft = $data *-1;
206
207   # begin transaction
208   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
209   # get lines with outstanding amounts to offset
210   my $sth = $dbh->prepare("select * from accountlines
211   where (borrowernumber = ?) and (amountoutstanding<0)
212   order by date");
213   $sth->execute($bornumber);
214 #  print $amountleft;
215   # offset transactions
216   while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
217      if ($accdata->{'amountoutstanding'} > $amountleft) {
218         $newamtos = 0;
219         $amountleft -= $accdata->{'amountoutstanding'};
220      }  else {
221         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
222         $amountleft = 0;
223      }
224 #     print $amountleft;
225      my $thisacct = $accdata->{accountno};
226      my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
227      where (borrowernumber = ?) and (accountno=?)");
228      $usth->execute($newamtos,$bornumber,$thisacct);
229      $usth->finish;
230      $usth = $dbh->prepare("insert into accountoffsets
231      (borrowernumber, accountno, offsetaccount,  offsetamount)
232      values (?,?,?,?)");
233      $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
234      $usth->finish;
235   }
236   $sth->finish;
237   return($amountleft);
238 }
239
240 END { }       # module clean-up code here (global destructor)
241
242 1;
243 __END__
244
245 =back
246
247 =head1 SEE ALSO
248
249 DBI(3)
250
251 =cut
252