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