Added copyright statement to all .pl and .pm files
[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::Database;
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;
32     
33 @ISA = qw(Exporter);
34 @EXPORT = qw(&recordpayment &fixaccounts &makepayment &manualinvoice
35 &getnextacctno);
36
37 sub displayaccounts{
38   my ($env)=@_;
39 }
40
41 sub recordpayment{
42   #here we update both the accountoffsets and the account lines
43   my ($env,$bornumber,$data)=@_;
44   my $dbh=C4Connect;
45   my $updquery = "";
46   my $newamtos = 0;
47   my $accdata = "";
48   my $branch=$env->{'branchcode'};
49   my $amountleft = $data;
50   # begin transaction
51   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
52   # get lines with outstanding amounts to offset
53   my $query = "select * from accountlines 
54   where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
55   order by date";
56   my $sth = $dbh->prepare($query);
57   $sth->execute;
58   # offset transactions
59   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
60      if ($accdata->{'amountoutstanding'} < $amountleft) {
61         $newamtos = 0;
62         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
63      }  else {
64         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
65         $amountleft = 0;
66      }
67      my $thisacct = $accdata->{accountno};
68      $updquery = "update accountlines set amountoutstanding= '$newamtos'
69      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
70      my $usth = $dbh->prepare($updquery);
71      $usth->execute;
72      $usth->finish;
73      $updquery = "insert into accountoffsets 
74      (borrowernumber, accountno, offsetaccount,  offsetamount)
75      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
76      $usth = $dbh->prepare($updquery);
77      $usth->execute;
78      $usth->finish;
79   }
80   # create new line
81   $updquery = "insert into accountlines 
82   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)  
83   values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
84   'Pay',0-$amountleft)";
85   my $usth = $dbh->prepare($updquery);
86   $usth->execute;
87   $usth->finish;
88   UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
89   $sth->finish;
90   $dbh->disconnect;
91 }
92
93 sub makepayment{
94   #here we update both the accountoffsets and the account lines
95   #updated to check, if they are paying off a lost item, we return the item 
96   # from their card, and put a note on the item record
97   my ($bornumber,$accountno,$amount,$user)=@_;
98   my $env;
99   my $dbh=C4Connect;
100   # begin transaction
101   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
102   my $newamtos=0;
103   my $sel="Select * from accountlines where  borrowernumber=$bornumber and
104   accountno=$accountno";
105   my $sth=$dbh->prepare($sel);
106   $sth->execute;
107   my $data=$sth->fetchrow_hashref;
108   $sth->finish;
109   my $updquery="Update accountlines set amountoutstanding=0 where
110   borrowernumber=$bornumber and accountno=$accountno";
111   $sth=$dbh->prepare($updquery);
112   $sth->execute;
113   $sth->finish;
114 #  print $updquery;
115   $updquery = "insert into accountoffsets 
116   (borrowernumber, accountno, offsetaccount,  offsetamount)
117   values ($bornumber,$accountno,$nextaccntno,$newamtos)";
118   my $usth = $dbh->prepare($updquery);
119   $usth->execute;
120   $usth->finish;  
121   # create new line
122   my $payment=0-$amount;
123   $updquery = "insert into accountlines 
124   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)  
125   values ($bornumber,$nextaccntno,now(),$payment,'Payment,thanks - $user', 'Pay',0)";
126   $usth = $dbh->prepare($updquery);
127   $usth->execute;
128   $usth->finish;
129   UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
130   $sth->finish;
131   $dbh->disconnect;
132   #check to see what accounttype
133   if ($data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L'){
134     returnlost($bornumber,$data->{'itemnumber'});
135   }
136 }
137
138 sub getnextacctno {
139   my ($env,$bornumber,$dbh)=@_;
140   my $nextaccntno = 1;
141   my $query = "select * from accountlines
142   where (borrowernumber = '$bornumber')
143   order by accountno desc";
144   my $sth = $dbh->prepare($query);
145   $sth->execute;
146   if (my $accdata=$sth->fetchrow_hashref){
147     $nextaccntno = $accdata->{'accountno'} + 1;
148   }
149   $sth->finish;
150   return($nextaccntno);
151 }
152
153 sub fixaccounts {
154   my ($borrowernumber,$accountno,$amount)=@_;
155   my $dbh=C4Connect;
156   my $query="Select * from accountlines where borrowernumber=$borrowernumber
157      and accountno=$accountno";
158   my $sth=$dbh->prepare($query);
159   $sth->execute;
160   my $data=$sth->fetchrow_hashref;
161   my $diff=$amount-$data->{'amount'};
162   my $outstanding=$data->{'amountoutstanding'}+$diff;
163   $sth->finish;
164   $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
165           borrowernumber=$borrowernumber and accountno=$accountno";
166    $sth=$dbh->prepare($query);
167 #   print $query;
168    $sth->execute;
169    $sth->finish;
170    $dbh->disconnect;
171  }
172
173 sub returnlost{
174   my ($borrnum,$itemnum)=@_;
175   my $dbh=C4Connect;
176   my $borrower=borrdata('',$borrnum); #from C4::Search;
177   my $upiss="Update issues set returndate=now() where
178   borrowernumber='$borrnum' and itemnumber='$itemnum' and returndate is null";
179   my $sth=$dbh->prepare($upiss);
180   $sth->execute;
181   $sth->finish;
182   my @datearr = localtime(time);
183   my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
184   my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
185   my $upitem="Update items set paidfor='Paid for by $bor $date' where itemnumber='$itemnum'";
186   $sth=$dbh->prepare($upitem);
187   $sth->execute;
188   $sth->finish;
189   $dbh->disconnect;
190 }
191
192 sub manualinvoice{
193   my ($bornum,$itemnum,$desc,$type,$amount,$user)=@_;
194   my $dbh=C4Connect;
195   my $insert;
196   $itemnum=~ s/ //g;
197   my %env;
198   my $accountno=getnextacctno('',$bornum,$dbh);
199   my $amountleft=$amount;
200   
201   if ($type eq 'CS' || $type eq 'CB' || $type eq 'CW'
202   || $type eq 'CF' || $type eq 'CL'){
203     my $amount2=$amount*-1;
204     $amountleft=fixcredit(\%env,$bornum,$amount2,$itemnum,$type,$user);
205   }
206   if ($type eq 'N'){
207     $desc.="New Card";
208   }
209   if ($type eq 'L' && $desc eq ''){
210     $desc="Lost Item";
211   }
212   if ($type eq 'REF'){
213     $amountleft=refund('',$bornum,$amount);    
214   }
215   if ($itemnum ne ''){
216     my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
217     $sth->execute;
218     my $data=$sth->fetchrow_hashref;
219     $sth->finish;
220     $desc.=" ".$itemnum;
221     $desc=$dbh->quote($desc);
222     $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
223     values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft','$data->{'itemnumber'}')";
224   } else {
225       $desc=$dbh->quote($desc);
226     $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
227     values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft')";
228   }
229   
230   my $sth=$dbh->prepare($insert);
231   $sth->execute;
232   $sth->finish;
233   
234   $dbh->disconnect;
235 }
236   
237 sub fixcredit{
238   #here we update both the accountoffsets and the account lines
239   my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
240   my $dbh=C4Connect;
241   my $updquery = "";
242   my $newamtos = 0;
243   my $accdata = "";
244   my $amountleft = $data;
245   if ($barcode ne ''){    
246     my $item=getiteminformation($env,'',$barcode);
247     my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
248     my $query="Select * from accountlines where (borrowernumber='$bornumber'
249     and itemnumber='$item->{'itemnumber'}' and amountoutstanding > 0)";
250     if ($type eq 'CL'){
251       $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
252     } elsif ($type eq 'CF'){
253       $query.=" and (accounttype = 'F' or accounttype = 'FU' or
254       accounttype='Res' or accounttype='Rent')";
255     } elsif ($type eq 'CB'){
256       $query.=" and accounttype='A'";
257     }
258 #    print $query;
259     my $sth=$dbh->prepare($query);
260     $sth->execute;
261     $accdata=$sth->fetchrow_hashref;
262     $sth->finish;   
263     if ($accdata->{'amountoutstanding'} < $amountleft) {
264         $newamtos = 0;
265         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
266      }  else {
267         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
268         $amountleft = 0;
269      }
270           my $thisacct = $accdata->{accountno};
271      my $updquery = "update accountlines set amountoutstanding= '$newamtos'
272      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
273      my $usth = $dbh->prepare($updquery);
274      $usth->execute;
275      $usth->finish;
276      $updquery = "insert into accountoffsets 
277      (borrowernumber, accountno, offsetaccount,  offsetamount)
278      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
279      $usth = $dbh->prepare($updquery);
280      $usth->execute;
281      $usth->finish;
282   }
283   # begin transaction
284   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
285   # get lines with outstanding amounts to offset
286   my $query = "select * from accountlines 
287   where (borrowernumber = '$bornumber') and (amountoutstanding >0)
288   order by date";
289   my $sth = $dbh->prepare($query);
290   $sth->execute;
291 #  print $query;
292   # offset transactions
293   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
294      if ($accdata->{'amountoutstanding'} < $amountleft) {
295         $newamtos = 0;
296         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
297      }  else {
298         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
299         $amountleft = 0;
300      }
301      my $thisacct = $accdata->{accountno};
302      $updquery = "update accountlines set amountoutstanding= '$newamtos'
303      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
304      my $usth = $dbh->prepare($updquery);
305      $usth->execute;
306      $usth->finish;
307      $updquery = "insert into accountoffsets 
308      (borrowernumber, accountno, offsetaccount,  offsetamount)
309      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
310      $usth = $dbh->prepare($updquery);
311      $usth->execute;
312      $usth->finish;
313   }
314   $sth->finish;
315   $dbh->disconnect;
316   $env->{'branch'}=$user;
317   $type="Credit ".$type;
318   UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
319   $amountleft*=-1;
320   return($amountleft);
321   
322 }
323
324 sub refund{
325   #here we update both the accountoffsets and the account lines
326   my ($env,$bornumber,$data)=@_;
327   my $dbh=C4Connect;
328   my $updquery = "";
329   my $newamtos = 0;
330   my $accdata = "";
331 #  my $branch=$env->{'branchcode'};
332   my $amountleft = $data *-1;
333   
334   # begin transaction
335   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
336   # get lines with outstanding amounts to offset
337   my $query = "select * from accountlines 
338   where (borrowernumber = '$bornumber') and (amountoutstanding<0)
339   order by date";
340   my $sth = $dbh->prepare($query);
341   $sth->execute;
342 #  print $query;
343 #  print $amountleft;
344   # offset transactions
345   while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
346      if ($accdata->{'amountoutstanding'} > $amountleft) {
347         $newamtos = 0;
348         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
349      }  else {
350         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
351         $amountleft = 0;
352      }
353 #     print $amountleft;
354      my $thisacct = $accdata->{accountno};
355      $updquery = "update accountlines set amountoutstanding= '$newamtos'
356      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
357      my $usth = $dbh->prepare($updquery);
358      $usth->execute;
359      $usth->finish;
360      $updquery = "insert into accountoffsets 
361      (borrowernumber, accountno, offsetaccount,  offsetamount)
362      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
363      $usth = $dbh->prepare($updquery);
364      $usth->execute;
365      $usth->finish;
366   }
367   $sth->finish;
368   $dbh->disconnect;
369   return($amountleft);
370 }
371 END { }       # module clean-up code here (global destructor)