1 package C4::Accounts; #assumes C4/Accounts
11 use C4::Interface::AccountsCDK;
12 use vars qw($VERSION @ISA @EXPORT);
14 # set the version for version checking
18 @EXPORT = qw(&checkaccount &reconcileaccount &getnextacctno);
25 #check accounts and list amounts owing
26 my ($env,$bornumber,$dbh)=@_;
27 my $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
28 borrowernumber=$bornumber and amountoutstanding<>0");
31 while (my $data=$sth->fetchrow_hashref){
32 $total=$total+$data->{'sum(amountoutstanding)'};
35 # output(1,2,"borrower owes $total");
37 # # output(1,2,"borrower owes $total");
39 # reconcileaccount($env,$dbh,$bornumber,$total);
46 sub reconcileaccount {
47 #print put money owing give person opportunity to pay it off
48 my ($env,$dummy,$bornumber,$total)=@_;
51 my $sth=$dbh->prepare("select * from borrowers
52 where borrowernumber=$bornumber");
54 my $borrower=$sth->fetchrow_hashref;
56 #get borrower information
57 $sth=$dbh->prepare("Select * from accountlines where
58 borrowernumber=$bornumber and amountoutstanding<>0 order by date");
60 #display account information
62 #&helptext('F11 quits');
63 output(20,0,"Accounts");
68 #output (1,2,"Account Info");
69 #output (1,3,"Item\tDate \tAmount\tDescription");
70 while (my $data=$sth->fetchrow_hashref){
72 my $amount=0+$data->{'amountoutstanding'};
73 my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
74 $line= $data->{'accountno'}." ".$data->{'date'}." ".$data->{'accounttype'}." ";
75 my $title = $itemdata->{'title'};
76 if (length($title) > 15 ) {$title = substr($title,0,15);}
77 $line= $line.$itemdata->{'barcode'}." $title ".$data->{'description'};
78 $line = fmtstr($env,$line,"L65")." ".fmtdec($env,$amount,"52");
79 push @accountlines,$line;
82 #get amount paid and update database
84 &accountsdialog($env,"Payment Entry",$borrower,\@accountlines,$total);
86 &recordpayment($env,$bornumber,$dbh,$data);
87 #Check if the borrower still owes
88 $total=&checkaccount($env,$bornumber,$dbh);
96 #here we update both the accountoffsets and the account lines
97 my ($env,$bornumber,$dbh,$data)=@_;
101 my $amountleft = $data;
103 # my $sth = $dbh->prepare("begin");
105 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
106 # get lines with outstanding amounts to offset
107 my $query = "select * from accountlines
108 where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
110 my $sth = $dbh->prepare($query);
112 # offset transactions
113 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
114 if ($accdata->{'amountoutstanding'} < $amountleft) {
116 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
118 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
121 my $thisacct = $accdata->{accountno};
122 $updquery = "update accountlines set amountoutstanding= '$newamtos'
123 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
124 my $usth = $dbh->prepare($updquery);
127 $updquery = "insert into accountoffsets
128 (borrowernumber, accountno, offsetaccount, offsetamount)
129 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
130 my $usth = $dbh->prepare($updquery);
136 #$updquery = "insert into accountlines (borrowernumber,
137 #accountno,date,amount,description,accounttype,amountoutstanding) values
138 #($bornumber,$nextaccntno,datetime('now'::abstime),0-$data,'Payment,thanks',
139 #'Pay',0-$amountleft)";
140 $updquery = "insert into accountlines
141 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
142 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
143 'Pay',0-$amountleft)";
144 my $usth = $dbh->prepare($updquery);
147 UpdateStats($env,'branch','payment',$data)
150 # $sth = $dbh->prepare;
156 my ($env,$bornumber,$dbh)=@_;
158 my $query = "select * from accountlines
159 where (borrowernumber = '$bornumber')
160 order by accountno desc";
161 my $sth = $dbh->prepare($query);
163 if (my $accdata=$sth->fetchrow_hashref){
164 $nextaccntno = $accdata->{'accountno'} + 1;
167 return($nextaccntno);
170 END { } # module clean-up code here (global destructor)