1 package C4::Accounts2; #assumes C4/Accounts2
9 use C4::Circulation::Circ2;
10 use vars qw($VERSION @ISA @EXPORT);
12 # set the version for version checking
16 @EXPORT = qw(&recordpayment &fixaccounts &makepayment &manualinvoice
24 #here we update both the accountoffsets and the account lines
25 my ($env,$bornumber,$data)=@_;
30 my $branch=$env->{'branchcode'};
31 my $amountleft = $data;
33 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
34 # get lines with outstanding amounts to offset
35 my $query = "select * from accountlines
36 where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
38 my $sth = $dbh->prepare($query);
41 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
42 if ($accdata->{'amountoutstanding'} < $amountleft) {
44 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
46 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
49 my $thisacct = $accdata->{accountno};
50 $updquery = "update accountlines set amountoutstanding= '$newamtos'
51 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
52 my $usth = $dbh->prepare($updquery);
55 $updquery = "insert into accountoffsets
56 (borrowernumber, accountno, offsetaccount, offsetamount)
57 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
58 $usth = $dbh->prepare($updquery);
63 $updquery = "insert into accountlines
64 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
65 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
66 'Pay',0-$amountleft)";
67 my $usth = $dbh->prepare($updquery);
70 UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
76 #here we update both the accountoffsets and the account lines
77 #updated to check, if they are paying off a lost item, we return the item
78 # from their card, and put a note on the item record
79 my ($bornumber,$accountno,$amount,$user)=@_;
83 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
85 my $sel="Select * from accountlines where borrowernumber=$bornumber and
86 accountno=$accountno";
87 my $sth=$dbh->prepare($sel);
89 my $data=$sth->fetchrow_hashref;
91 my $updquery="Update accountlines set amountoutstanding=0 where
92 borrowernumber=$bornumber and accountno=$accountno";
93 $sth=$dbh->prepare($updquery);
97 $updquery = "insert into accountoffsets
98 (borrowernumber, accountno, offsetaccount, offsetamount)
99 values ($bornumber,$accountno,$nextaccntno,$newamtos)";
100 my $usth = $dbh->prepare($updquery);
104 my $payment=0-$amount;
105 $updquery = "insert into accountlines
106 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
107 values ($bornumber,$nextaccntno,now(),$payment,'Payment,thanks - $user', 'Pay',0)";
108 $usth = $dbh->prepare($updquery);
111 UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
114 #check to see what accounttype
115 if ($data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L'){
116 returnlost($bornumber,$data->{'itemnumber'});
121 my ($env,$bornumber,$dbh)=@_;
123 my $query = "select * from accountlines
124 where (borrowernumber = '$bornumber')
125 order by accountno desc";
126 my $sth = $dbh->prepare($query);
128 if (my $accdata=$sth->fetchrow_hashref){
129 $nextaccntno = $accdata->{'accountno'} + 1;
132 return($nextaccntno);
136 my ($borrowernumber,$accountno,$amount)=@_;
138 my $query="Select * from accountlines where borrowernumber=$borrowernumber
139 and accountno=$accountno";
140 my $sth=$dbh->prepare($query);
142 my $data=$sth->fetchrow_hashref;
143 my $diff=$amount-$data->{'amount'};
144 my $outstanding=$data->{'amountoutstanding'}+$diff;
146 $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
147 borrowernumber=$borrowernumber and accountno=$accountno";
148 $sth=$dbh->prepare($query);
156 my ($borrnum,$itemnum)=@_;
158 my $borrower=borrdata('',$borrnum); #from C4::Search;
159 my $upiss="Update issues set returndate=now() where
160 borrowernumber='$borrnum' and itemnumber='$itemnum' and returndate is null";
161 my $sth=$dbh->prepare($upiss);
164 my @datearr = localtime(time);
165 my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
166 my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
167 my $upitem="Update items set paidfor='Paid for by $bor $date' where itemnumber='$itemnum'";
168 $sth=$dbh->prepare($upitem);
175 my ($bornum,$itemnum,$desc,$type,$amount,$user)=@_;
180 my $accountno=getnextacctno('',$bornum,$dbh);
181 my $amountleft=$amount;
183 if ($type eq 'CS' || $type eq 'CB' || $type eq 'CW'
184 || $type eq 'CF' || $type eq 'CL'){
185 my $amount2=$amount*-1;
186 $amountleft=fixcredit(\%env,$bornum,$amount2,$itemnum,$type,$user);
191 if ($type eq 'L' && $desc eq ''){
195 $amountleft=refund('',$bornum,$amount);
198 my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
200 my $data=$sth->fetchrow_hashref;
203 $desc=$dbh->quote($desc);
204 $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
205 values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft','$data->{'itemnumber'}')";
207 $desc=$dbh->quote($desc);
208 $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
209 values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft')";
212 my $sth=$dbh->prepare($insert);
220 #here we update both the accountoffsets and the account lines
221 my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
226 my $amountleft = $data;
228 my $item=getiteminformation($env,'',$barcode);
229 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
230 my $query="Select * from accountlines where (borrowernumber='$bornumber'
231 and itemnumber='$item->{'itemnumber'}' and amountoutstanding > 0)";
233 $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
234 } elsif ($type eq 'CF'){
235 $query.=" and (accounttype = 'F' or accounttype = 'FU' or
236 accounttype='Res' or accounttype='Rent')";
237 } elsif ($type eq 'CB'){
238 $query.=" and accounttype='A'";
241 my $sth=$dbh->prepare($query);
243 $accdata=$sth->fetchrow_hashref;
245 if ($accdata->{'amountoutstanding'} < $amountleft) {
247 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
249 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
252 my $thisacct = $accdata->{accountno};
253 my $updquery = "update accountlines set amountoutstanding= '$newamtos'
254 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
255 my $usth = $dbh->prepare($updquery);
258 $updquery = "insert into accountoffsets
259 (borrowernumber, accountno, offsetaccount, offsetamount)
260 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
261 $usth = $dbh->prepare($updquery);
266 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
267 # get lines with outstanding amounts to offset
268 my $query = "select * from accountlines
269 where (borrowernumber = '$bornumber') and (amountoutstanding >0)
271 my $sth = $dbh->prepare($query);
274 # offset transactions
275 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
276 if ($accdata->{'amountoutstanding'} < $amountleft) {
278 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
280 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
283 my $thisacct = $accdata->{accountno};
284 $updquery = "update accountlines set amountoutstanding= '$newamtos'
285 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
286 my $usth = $dbh->prepare($updquery);
289 $updquery = "insert into accountoffsets
290 (borrowernumber, accountno, offsetaccount, offsetamount)
291 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
292 $usth = $dbh->prepare($updquery);
298 $env->{'branch'}=$user;
299 $type="Credit ".$type;
300 UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
307 #here we update both the accountoffsets and the account lines
308 my ($env,$bornumber,$data)=@_;
313 # my $branch=$env->{'branchcode'};
314 my $amountleft = $data *-1;
317 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
318 # get lines with outstanding amounts to offset
319 my $query = "select * from accountlines
320 where (borrowernumber = '$bornumber') and (amountoutstanding<0)
322 my $sth = $dbh->prepare($query);
326 # offset transactions
327 while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
328 if ($accdata->{'amountoutstanding'} > $amountleft) {
330 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
332 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
336 my $thisacct = $accdata->{accountno};
337 $updquery = "update accountlines set amountoutstanding= '$newamtos'
338 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
339 my $usth = $dbh->prepare($updquery);
342 $updquery = "insert into accountoffsets
343 (borrowernumber, accountno, offsetaccount, offsetamount)
344 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
345 $usth = $dbh->prepare($updquery);
353 END { } # module clean-up code here (global destructor)