Reworking statistics for payments and writeoffs
[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 vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
12   
13 # set the version for version checking
14 $VERSION = 0.01;
15     
16 @ISA = qw(Exporter);
17 @EXPORT = qw(&recordpayment &fixaccounts &makepayment);
18 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
19                   
20 # your exported package globals go here,
21 # as well as any optionally exported functions
22
23 @EXPORT_OK   = qw($Var1 %Hashit);
24
25
26 # non-exported package globals go here
27 use vars qw(@more $stuff);
28         
29 # initalize package globals, first exported ones
30
31 my $Var1   = '';
32 my %Hashit = ();
33                     
34 # then the others (which are still accessible as $Some::Module::stuff)
35 my $stuff  = '';
36 my @more   = ();
37         
38 # all file-scoped lexicals must be created before
39 # the functions below that use them.
40                 
41 # file-private lexicals go here
42 my $priv_var    = '';
43 my %secret_hash = ();
44                             
45 # here's a file-private function as a closure,
46 # callable as &$priv_func;  it cannot be prototyped.
47 my $priv_func = sub {
48   # stuff goes here.
49 };
50                                                     
51 # make all your functions, whether exported or not;
52
53 sub displayaccounts{
54   my ($env)=@_;
55 }
56
57 sub recordpayment{
58   #here we update both the accountoffsets and the account lines
59   my ($env,$bornumber,$data)=@_;
60   my $dbh=C4Connect;
61   my $updquery = "";
62   my $newamtos = 0;
63   my $accdata = "";
64   my $branch=$env->{'branchcode'};
65   my $amountleft = $data;
66   # begin transaction
67   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
68   # get lines with outstanding amounts to offset
69   my $query = "select * from accountlines 
70   where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
71   order by date";
72   my $sth = $dbh->prepare($query);
73   $sth->execute;
74   # offset transactions
75   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
76      if ($accdata->{'amountoutstanding'} < $amountleft) {
77         $newamtos = 0;
78         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
79      }  else {
80         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
81         $amountleft = 0;
82      }
83      my $thisacct = $accdata->{accountno};
84      $updquery = "update accountlines set amountoutstanding= '$newamtos'
85      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
86      my $usth = $dbh->prepare($updquery);
87      $usth->execute;
88      $usth->finish;
89      $updquery = "insert into accountoffsets 
90      (borrowernumber, accountno, offsetaccount,  offsetamount)
91      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
92      my $usth = $dbh->prepare($updquery);
93      $usth->execute;
94      $usth->finish;
95   }
96   # create new line
97   $updquery = "insert into accountlines 
98   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)  
99   values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
100   'Pay',0-$amountleft)";
101   my $usth = $dbh->prepare($updquery);
102   $usth->execute;
103   $usth->finish;
104   UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
105   $sth->finish;
106   $dbh->disconnect;
107 }
108
109 sub makepayment{
110   #here we update both the accountoffsets and the account lines
111   my ($bornumber,$accountno,$amount,$user)=@_;
112   my $env;
113   my $dbh=C4Connect;
114   # begin transaction
115   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
116   my $newamtos=0;
117   my $updquery="Update accountlines set amountoutstanding=0 where
118   borrowernumber=$bornumber and accountno=$accountno";
119   my $sth=$dbh->prepare($updquery);
120   $sth->execute;
121   $sth->finish;
122 #  print $updquery;
123   $updquery = "insert into accountoffsets 
124   (borrowernumber, accountno, offsetaccount,  offsetamount)
125   values ($bornumber,$accountno,$nextaccntno,$newamtos)";
126   my $usth = $dbh->prepare($updquery);
127   $usth->execute;
128   $usth->finish;  
129   # create new line
130   my $payment=0-$amount;
131   $updquery = "insert into accountlines 
132   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)  
133   values ($bornumber,$nextaccntno,now(),$payment,'Payment,thanks - $user', 'Pay',0)";
134   my $usth = $dbh->prepare($updquery);
135   $usth->execute;
136   $usth->finish;
137   UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
138   $sth->finish;
139   $dbh->disconnect;
140 }
141
142 sub getnextacctno {
143   my ($env,$bornumber,$dbh)=@_;
144   my $nextaccntno = 1;
145   my $query = "select * from accountlines
146   where (borrowernumber = '$bornumber')
147   order by accountno desc";
148   my $sth = $dbh->prepare($query);
149   $sth->execute;
150   if (my $accdata=$sth->fetchrow_hashref){
151     $nextaccntno = $accdata->{'accountno'} + 1;
152   }
153   $sth->finish;
154   return($nextaccntno);
155 }
156
157 sub fixaccounts {
158   my ($borrowernumber,$accountno,$amount)=@_;
159   my $dbh=C4Connect;
160   my $query="Select * from accountlines where borrowernumber=$borrowernumber
161      and accountno=$accountno";
162   my $sth=$dbh->prepare($query);
163   $sth->execute;
164   my $data=$sth->fetchrow_hashref;
165   my $diff=$amount-$data->{'amount'};
166   my $outstanding=$data->{'amountoutstanding'}+$diff;
167   $sth->finish;
168   $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
169           borrowernumber=$borrowernumber and accountno=$accountno";
170    $sth=$dbh->prepare($query);
171 #   print $query;
172    $sth->execute;
173    $sth->finish;
174    $dbh->disconnect;
175  }
176  
177 END { }       # module clean-up code here (global destructor)