1 package C4::Accounts; #asummes C4/Accounts
3 #requires DBI.pm to be installed
14 use C4::Interface::AccountsCDK;
15 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
17 # set the version for version checking
21 @EXPORT = qw(&checkaccount &reconcileaccount &getnextacctno);
22 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
24 # your exported package globals go here,
25 # as well as any optionally exported functions
27 @EXPORT_OK = qw($Var1 %Hashit);
30 # non-exported package globals go here
31 use vars qw(@more $stuff);
33 # initalize package globals, first exported ones
38 # then the others (which are still accessible as $Some::Module::stuff)
42 # all file-scoped lexicals must be created before
43 # the functions below that use them.
45 # file-private lexicals go here
49 # here's a file-private function as a closure,
50 # callable as &$priv_func; it cannot be prototyped.
55 # make all your functions, whether exported or not;
63 #check accounts and list amounts owing
64 my ($env,$bornumber,$dbh)=@_;
65 my $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
66 borrowernumber=$bornumber and amountoutstanding<>0");
69 while (my $data=$sth->fetchrow_hashref){
70 $total=$total+$data->{'sum(amountoutstanding)'};
73 # output(1,2,"borrower owes $total");
75 # # output(1,2,"borrower owes $total");
77 # reconcileaccount($env,$dbh,$bornumber,$total);
84 sub reconcileaccount {
85 #print put money owing give person opportunity to pay it off
86 my ($env,$dummy,$bornumber,$total)=@_;
89 my $sth=$dbh->prepare("select * from borrowers
90 where borrowernumber=$bornumber");
92 my $borrower=$sth->fetchrow_hashref;
94 #get borrower information
95 $sth=$dbh->prepare("Select * from accountlines where
96 borrowernumber=$bornumber and amountoutstanding<>0 order by date");
98 #display account information
100 #&helptext('F11 quits');
101 output(20,0,"Accounts");
106 #output (1,2,"Account Info");
107 #output (1,3,"Item\tDate \tAmount\tDescription");
108 while (my $data=$sth->fetchrow_hashref){
110 my $amount=0+$data->{'amountoutstanding'};
111 my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
112 $line= $data->{'accountno'}." ".$data->{'date'}." ".$data->{'accounttype'}." ";
113 my $title = $itemdata->{'title'};
114 if (length($title) > 15 ) {$title = substr($title,0,15);}
115 $line= $line.$itemdata->{'barcode'}." $title ".$data->{'description'};
116 $line = fmtstr($env,$line,"L65")." ".fmtdec($env,$amount,"52");
117 push @accountlines,$line;
120 #get amount paid and update database
122 &accountsdialog($env,"Payment Entry",$borrower,\@accountlines,$total);
124 &recordpayment($env,$bornumber,$dbh,$data);
125 #Check if the borrower still owes
126 $total=&checkaccount($env,$bornumber,$dbh);
134 #here we update both the accountoffsets and the account lines
135 my ($env,$bornumber,$dbh,$data)=@_;
139 my $amountleft = $data;
141 # my $sth = $dbh->prepare("begin");
143 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
144 # get lines with outstanding amounts to offset
145 my $query = "select * from accountlines
146 where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
148 my $sth = $dbh->prepare($query);
150 # offset transactions
151 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
152 if ($accdata->{'amountoutstanding'} < $amountleft) {
154 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
156 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
159 my $thisacct = $accdata->{accountno};
160 $updquery = "update accountlines set amountoutstanding= '$newamtos'
161 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
162 my $usth = $dbh->prepare($updquery);
165 $updquery = "insert into accountoffsets
166 (borrowernumber, accountno, offsetaccount, offsetamount)
167 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
168 my $usth = $dbh->prepare($updquery);
174 #$updquery = "insert into accountlines (borrowernumber,
175 #accountno,date,amount,description,accounttype,amountoutstanding) values
176 #($bornumber,$nextaccntno,datetime('now'::abstime),0-$data,'Payment,thanks',
177 #'Pay',0-$amountleft)";
178 $updquery = "insert into accountlines
179 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
180 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
181 'Pay',0-$amountleft)";
182 my $usth = $dbh->prepare($updquery);
185 UpdateStats($env,'branch','payment',$data)
188 # $sth = $dbh->prepare;
194 my ($env,$bornumber,$dbh)=@_;
196 my $query = "select * from accountlines
197 where (borrowernumber = '$bornumber')
198 order by accountno desc";
199 my $sth = $dbh->prepare($query);
201 if (my $accdata=$sth->fetchrow_hashref){
202 $nextaccntno = $accdata->{'accountno'} + 1;
205 return($nextaccntno);
208 END { } # module clean-up code here (global destructor)