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