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 $amountleft = $data;
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)
71 my $sth = $dbh->prepare($query);
74 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
75 if ($accdata->{'amountoutstanding'} < $amountleft) {
77 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
79 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
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);
88 $updquery = "insert into accountoffsets
89 (borrowernumber, accountno, offsetaccount, offsetamount)
90 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
91 my $usth = $dbh->prepare($updquery);
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);
103 UpdateStats($env,'branch','payment',$data,'','','',$bornumber);
109 #here we update both the accountoffsets and the account lines
110 my ($bornumber,$accountno,$amount,$user)=@_;
114 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
116 my $updquery="Update accountlines set amountoutstanding=0 where
117 borrowernumber=$bornumber and accountno=$accountno";
118 my $sth=$dbh->prepare($updquery);
122 $updquery = "insert into accountoffsets
123 (borrowernumber, accountno, offsetaccount, offsetamount)
124 values ($bornumber,$accountno,$nextaccntno,$newamtos)";
125 my $usth = $dbh->prepare($updquery);
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);
136 UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
142 my ($env,$bornumber,$dbh)=@_;
144 my $query = "select * from accountlines
145 where (borrowernumber = '$bornumber')
146 order by accountno desc";
147 my $sth = $dbh->prepare($query);
149 if (my $accdata=$sth->fetchrow_hashref){
150 $nextaccntno = $accdata->{'accountno'} + 1;
153 return($nextaccntno);
157 my ($borrowernumber,$accountno,$amount)=@_;
159 my $query="Select * from accountlines where borrowernumber=$borrowernumber
160 and accountno=$accountno";
161 my $sth=$dbh->prepare($query);
163 my $data=$sth->fetchrow_hashref;
164 my $diff=$amount-$data->{'amount'};
165 my $outstanding=$data->{'amountoutstanding'}+$diff;
167 $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
168 borrowernumber=$borrowernumber and accountno=$accountno";
169 $sth=$dbh->prepare($query);
176 END { } # module clean-up code here (global destructor)