Initial revision
[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 $amountleft = $data;
65   # begin transaction
66   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
67   # get lines with outstanding amounts to offset
68   my $query = "select * from accountlines 
69   where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
70   order by date";
71   my $sth = $dbh->prepare($query);
72   $sth->execute;
73   # offset transactions
74   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
75      if ($accdata->{'amountoutstanding'} < $amountleft) {
76         $newamtos = 0;
77         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
78      }  else {
79         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
80         $amountleft = 0;
81      }
82      my $thisacct = $accdata->{accountno};
83      $updquery = "update accountlines set amountoutstanding= '$newamtos'
84      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
85      my $usth = $dbh->prepare($updquery);
86      $usth->execute;
87      $usth->finish;
88      $updquery = "insert into accountoffsets 
89      (borrowernumber, accountno, offsetaccount,  offsetamount)
90      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
91      my $usth = $dbh->prepare($updquery);
92      $usth->execute;
93      $usth->finish;
94   }
95   # create new line
96   $updquery = "insert into accountlines 
97   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)  
98   values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
99   'Pay',0-$amountleft)";
100   my $usth = $dbh->prepare($updquery);
101   $usth->execute;
102   $usth->finish;
103   UpdateStats($env,'branch','payment',$data);
104   $sth->finish;
105   $dbh->disconnect;
106 }
107
108 sub makepayment{
109   #here we update both the accountoffsets and the account lines
110   my ($bornumber,$accountno,$amount,$user)=@_;
111   my $env;
112   my $dbh=C4Connect;
113   # begin transaction
114   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
115   my $newamtos=0;
116   my $updquery="Update accountlines set amountoutstanding=0 where
117   borrowernumber=$bornumber and accountno=$accountno";
118   my $sth=$dbh->prepare($updquery);
119   $sth->execute;
120   $sth->finish;
121 #  print $updquery;
122   $updquery = "insert into accountoffsets 
123   (borrowernumber, accountno, offsetaccount,  offsetamount)
124   values ($bornumber,$accountno,$nextaccntno,$newamtos)";
125   my $usth = $dbh->prepare($updquery);
126   $usth->execute;
127   $usth->finish;  
128   # create new line
129   my $payment=0-$amount;
130   $updquery = "insert into accountlines 
131   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)  
132   values ($bornumber,$nextaccntno,now(),$payment,'Payment,thanks - $user', 'Pay',0)";
133   my $usth = $dbh->prepare($updquery);
134   $usth->execute;
135   $usth->finish;
136   UpdateStats($env,$user,'payment',$amount);
137   $sth->finish;
138   $dbh->disconnect;
139 }
140
141 sub getnextacctno {
142   my ($env,$bornumber,$dbh)=@_;
143   my $nextaccntno = 1;
144   my $query = "select * from accountlines
145   where (borrowernumber = '$bornumber')
146   order by accountno desc";
147   my $sth = $dbh->prepare($query);
148   $sth->execute;
149   if (my $accdata=$sth->fetchrow_hashref){
150     $nextaccntno = $accdata->{'accountno'} + 1;
151   }
152   $sth->finish;
153   return($nextaccntno);
154 }
155
156 sub fixaccounts {
157   my ($borrowernumber,$accountno,$amount)=@_;
158   my $dbh=C4Connect;
159   my $query="Select * from accountlines where borrowernumber=$borrowernumber
160      and accountno=$accountno";
161   my $sth=$dbh->prepare($query);
162   $sth->execute;
163   my $data=$sth->fetchrow_hashref;
164   my $diff=$amount-$data->{'amount'};
165   my $outstanding=$data->{'amountoutstanding'}+$diff;
166   $sth->finish;
167   $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
168           borrowernumber=$borrowernumber and accountno=$accountno";
169    $sth=$dbh->prepare($query);
170 #   print $query;
171    $sth->execute;
172    $sth->finish;
173    $dbh->disconnect;
174  }
175  
176 END { }       # module clean-up code here (global destructor)