1 package C4::Accounts2; #asummes C4/Accounts2
3 #requires DBI.pm to be installed
12 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
14 # set the version for version checking
18 @EXPORT = qw(&recordpayment &fixaccounts &makepayment &manualinvoice
20 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
22 # your exported package globals go here,
23 # as well as any optionally exported functions
25 @EXPORT_OK = qw($Var1 %Hashit);
28 # non-exported package globals go here
29 use vars qw(@more $stuff);
31 # initalize package globals, first exported ones
36 # then the others (which are still accessible as $Some::Module::stuff)
40 # all file-scoped lexicals must be created before
41 # the functions below that use them.
43 # file-private lexicals go here
47 # here's a file-private function as a closure,
48 # callable as &$priv_func; it cannot be prototyped.
53 # make all your functions, whether exported or not;
60 #here we update both the accountoffsets and the account lines
61 my ($env,$bornumber,$data)=@_;
66 my $branch=$env->{'branchcode'};
67 my $amountleft = $data;
69 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
70 # get lines with outstanding amounts to offset
71 my $query = "select * from accountlines
72 where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
74 my $sth = $dbh->prepare($query);
77 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
78 if ($accdata->{'amountoutstanding'} < $amountleft) {
80 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
82 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
85 my $thisacct = $accdata->{accountno};
86 $updquery = "update accountlines set amountoutstanding= '$newamtos'
87 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
88 my $usth = $dbh->prepare($updquery);
91 $updquery = "insert into accountoffsets
92 (borrowernumber, accountno, offsetaccount, offsetamount)
93 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
94 my $usth = $dbh->prepare($updquery);
99 $updquery = "insert into accountlines
100 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
101 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
102 'Pay',0-$amountleft)";
103 my $usth = $dbh->prepare($updquery);
106 UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
112 #here we update both the accountoffsets and the account lines
113 #updated to check, if they are paying off a lost item, we return the item
114 # from their card, and put a note on the item record
115 my ($bornumber,$accountno,$amount,$user)=@_;
119 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
121 my $sel="Select * from accountlines where borrowernumber=$bornumber and
122 accountno=$accountno";
123 my $sth=$dbh->prepare($sel);
125 my $data=$sth->fetchrow_hashref;
127 my $updquery="Update accountlines set amountoutstanding=0 where
128 borrowernumber=$bornumber and accountno=$accountno";
129 $sth=$dbh->prepare($updquery);
133 $updquery = "insert into accountoffsets
134 (borrowernumber, accountno, offsetaccount, offsetamount)
135 values ($bornumber,$accountno,$nextaccntno,$newamtos)";
136 my $usth = $dbh->prepare($updquery);
140 my $payment=0-$amount;
141 $updquery = "insert into accountlines
142 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
143 values ($bornumber,$nextaccntno,now(),$payment,'Payment,thanks - $user', 'Pay',0)";
144 my $usth = $dbh->prepare($updquery);
147 UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
150 #check to see what accounttype
151 if ($data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L'){
152 returnlost($bornumber,$data->{'itemnumber'});
157 my ($env,$bornumber,$dbh)=@_;
159 my $query = "select * from accountlines
160 where (borrowernumber = '$bornumber')
161 order by accountno desc";
162 my $sth = $dbh->prepare($query);
164 if (my $accdata=$sth->fetchrow_hashref){
165 $nextaccntno = $accdata->{'accountno'} + 1;
168 return($nextaccntno);
172 my ($borrowernumber,$accountno,$amount)=@_;
174 my $query="Select * from accountlines where borrowernumber=$borrowernumber
175 and accountno=$accountno";
176 my $sth=$dbh->prepare($query);
178 my $data=$sth->fetchrow_hashref;
179 my $diff=$amount-$data->{'amount'};
180 my $outstanding=$data->{'amountoutstanding'}+$diff;
182 $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
183 borrowernumber=$borrowernumber and accountno=$accountno";
184 $sth=$dbh->prepare($query);
192 my ($borrnum,$itemnum)=@_;
194 my $borrower=borrdata('',$borrnum); #from C4::Search;
195 my $upiss="Update issues set returndate=now() where
196 borrowernumber='$borrnum' and itemnumber='$itemnum' and returndate is null";
197 my $sth=$dbh->prepare($upiss);
200 my @datearr = localtime(time);
201 my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
202 my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
203 my $upitem="Update items set paidfor='Paid for by $bor $date' where itemnumber='$itemnum'";
204 $sth=$dbh->prepare($upitem);
211 my ($bornum,$itemnum,$desc,$type,$amount)=@_;
215 my $accountno=getnextacctno('',$bornum,$dbh);
216 my $amountleft=$amount;
218 if ($type eq 'C' || $type eq 'BAY' || $type eq 'WORK'){
219 my $amount2=$amount*-1;
220 $amountleft=fixcredit('',$bornum,$amount2);
225 if ($type eq 'L' && $desc eq ''){
229 $amountleft=refund('',$bornum,$amount);
232 my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
234 my $data=$sth->fetchrow_hashref;
237 $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
238 values ($bornum,$accountno,now(),'$amount','$desc','$type','$amountleft','$data->{'itemnumber'}')";
240 $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
241 values ($bornum,$accountno,now(),'$amount','$desc','$type','$amountleft')";
244 my $sth=$dbh->prepare($insert);
252 #here we update both the accountoffsets and the account lines
253 my ($env,$bornumber,$data)=@_;
258 # my $branch=$env->{'branchcode'};
259 my $amountleft = $data;
261 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
262 # get lines with outstanding amounts to offset
263 my $query = "select * from accountlines
264 where (borrowernumber = '$bornumber') and (amountoutstanding >0)
266 my $sth = $dbh->prepare($query);
269 # offset transactions
270 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
271 if ($accdata->{'amountoutstanding'} < $amountleft) {
273 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
275 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
278 my $thisacct = $accdata->{accountno};
279 $updquery = "update accountlines set amountoutstanding= '$newamtos'
280 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
281 my $usth = $dbh->prepare($updquery);
284 $updquery = "insert into accountoffsets
285 (borrowernumber, accountno, offsetaccount, offsetamount)
286 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
287 my $usth = $dbh->prepare($updquery);
297 #here we update both the accountoffsets and the account lines
298 my ($env,$bornumber,$data)=@_;
303 # my $branch=$env->{'branchcode'};
304 my $amountleft = $data *-1;
307 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
308 # get lines with outstanding amounts to offset
309 my $query = "select * from accountlines
310 where (borrowernumber = '$bornumber') and (amountoutstanding<0)
312 my $sth = $dbh->prepare($query);
316 # offset transactions
317 while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
318 if ($accdata->{'amountoutstanding'} > $amountleft) {
320 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
322 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
326 my $thisacct = $accdata->{accountno};
327 $updquery = "update accountlines set amountoutstanding= '$newamtos'
328 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
329 my $usth = $dbh->prepare($updquery);
332 $updquery = "insert into accountoffsets
333 (borrowernumber, accountno, offsetaccount, offsetamount)
334 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
335 my $usth = $dbh->prepare($updquery);
343 END { } # module clean-up code here (global destructor)