1 package C4::Accounts2; #asummes C4/Accounts2
3 #requires DBI.pm to be installed
11 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
13 # set the version for version checking
17 @EXPORT = qw(&recordpayment &fixaccounts &makepayment);
18 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
20 # your exported package globals go here,
21 # as well as any optionally exported functions
23 @EXPORT_OK = qw($Var1 %Hashit);
26 # non-exported package globals go here
27 use vars qw(@more $stuff);
29 # initalize package globals, first exported ones
34 # then the others (which are still accessible as $Some::Module::stuff)
38 # all file-scoped lexicals must be created before
39 # the functions below that use them.
41 # file-private lexicals go here
45 # here's a file-private function as a closure,
46 # callable as &$priv_func; it cannot be prototyped.
51 # make all your functions, whether exported or not;
58 #here we update both the accountoffsets and the account lines
59 my ($env,$bornumber,$data)=@_;
64 my $branch=$env->{'branchcode'};
65 my $amountleft = $data;
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)
72 my $sth = $dbh->prepare($query);
75 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
76 if ($accdata->{'amountoutstanding'} < $amountleft) {
78 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
80 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
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);
89 $updquery = "insert into accountoffsets
90 (borrowernumber, accountno, offsetaccount, offsetamount)
91 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
92 my $usth = $dbh->prepare($updquery);
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);
104 UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
110 #here we update both the accountoffsets and the account lines
111 my ($bornumber,$accountno,$amount,$user)=@_;
115 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
117 my $updquery="Update accountlines set amountoutstanding=0 where
118 borrowernumber=$bornumber and accountno=$accountno";
119 my $sth=$dbh->prepare($updquery);
123 $updquery = "insert into accountoffsets
124 (borrowernumber, accountno, offsetaccount, offsetamount)
125 values ($bornumber,$accountno,$nextaccntno,$newamtos)";
126 my $usth = $dbh->prepare($updquery);
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);
137 UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
143 my ($env,$bornumber,$dbh)=@_;
145 my $query = "select * from accountlines
146 where (borrowernumber = '$bornumber')
147 order by accountno desc";
148 my $sth = $dbh->prepare($query);
150 if (my $accdata=$sth->fetchrow_hashref){
151 $nextaccntno = $accdata->{'accountno'} + 1;
154 return($nextaccntno);
158 my ($borrowernumber,$accountno,$amount)=@_;
160 my $query="Select * from accountlines where borrowernumber=$borrowernumber
161 and accountno=$accountno";
162 my $sth=$dbh->prepare($query);
164 my $data=$sth->fetchrow_hashref;
165 my $diff=$amount-$data->{'amount'};
166 my $outstanding=$data->{'amountoutstanding'}+$diff;
168 $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
169 borrowernumber=$borrowernumber and accountno=$accountno";
170 $sth=$dbh->prepare($query);
177 END { } # module clean-up code here (global destructor)