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