1 package C4::Accounts2; #assumes C4/Accounts2
10 use C4::Circulation::Circ2;
11 use vars qw($VERSION @ISA @EXPORT);
13 # set the version for version checking
17 @EXPORT = qw(&recordpayment &fixaccounts &makepayment &manualinvoice
25 #here we update both the accountoffsets and the account lines
26 my ($env,$bornumber,$data)=@_;
31 my $branch=$env->{'branchcode'};
32 my $amountleft = $data;
34 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
35 # get lines with outstanding amounts to offset
36 my $query = "select * from accountlines
37 where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
39 my $sth = $dbh->prepare($query);
42 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
43 if ($accdata->{'amountoutstanding'} < $amountleft) {
45 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
47 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
50 my $thisacct = $accdata->{accountno};
51 $updquery = "update accountlines set amountoutstanding= '$newamtos'
52 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
53 my $usth = $dbh->prepare($updquery);
56 $updquery = "insert into accountoffsets
57 (borrowernumber, accountno, offsetaccount, offsetamount)
58 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
59 $usth = $dbh->prepare($updquery);
64 $updquery = "insert into accountlines
65 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
66 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
67 'Pay',0-$amountleft)";
68 my $usth = $dbh->prepare($updquery);
71 UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
77 #here we update both the accountoffsets and the account lines
78 #updated to check, if they are paying off a lost item, we return the item
79 # from their card, and put a note on the item record
80 my ($bornumber,$accountno,$amount,$user)=@_;
84 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
86 my $sel="Select * from accountlines where borrowernumber=$bornumber and
87 accountno=$accountno";
88 my $sth=$dbh->prepare($sel);
90 my $data=$sth->fetchrow_hashref;
92 my $updquery="Update accountlines set amountoutstanding=0 where
93 borrowernumber=$bornumber and accountno=$accountno";
94 $sth=$dbh->prepare($updquery);
98 $updquery = "insert into accountoffsets
99 (borrowernumber, accountno, offsetaccount, offsetamount)
100 values ($bornumber,$accountno,$nextaccntno,$newamtos)";
101 my $usth = $dbh->prepare($updquery);
105 my $payment=0-$amount;
106 $updquery = "insert into accountlines
107 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
108 values ($bornumber,$nextaccntno,now(),$payment,'Payment,thanks - $user', 'Pay',0)";
109 $usth = $dbh->prepare($updquery);
112 UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
115 #check to see what accounttype
116 if ($data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L'){
117 returnlost($bornumber,$data->{'itemnumber'});
122 my ($env,$bornumber,$dbh)=@_;
124 my $query = "select * from accountlines
125 where (borrowernumber = '$bornumber')
126 order by accountno desc";
127 my $sth = $dbh->prepare($query);
129 if (my $accdata=$sth->fetchrow_hashref){
130 $nextaccntno = $accdata->{'accountno'} + 1;
133 return($nextaccntno);
137 my ($borrowernumber,$accountno,$amount)=@_;
139 my $query="Select * from accountlines where borrowernumber=$borrowernumber
140 and accountno=$accountno";
141 my $sth=$dbh->prepare($query);
143 my $data=$sth->fetchrow_hashref;
144 my $diff=$amount-$data->{'amount'};
145 my $outstanding=$data->{'amountoutstanding'}+$diff;
147 $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
148 borrowernumber=$borrowernumber and accountno=$accountno";
149 $sth=$dbh->prepare($query);
157 my ($borrnum,$itemnum)=@_;
159 my $borrower=borrdata('',$borrnum); #from C4::Search;
160 my $upiss="Update issues set returndate=now() where
161 borrowernumber='$borrnum' and itemnumber='$itemnum' and returndate is null";
162 my $sth=$dbh->prepare($upiss);
165 my @datearr = localtime(time);
166 my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
167 my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
168 my $upitem="Update items set paidfor='Paid for by $bor $date' where itemnumber='$itemnum'";
169 $sth=$dbh->prepare($upitem);
176 my ($bornum,$itemnum,$desc,$type,$amount,$user)=@_;
181 my $accountno=getnextacctno('',$bornum,$dbh);
182 my $amountleft=$amount;
184 if ($type eq 'CS' || $type eq 'CB' || $type eq 'CW'
185 || $type eq 'CF' || $type eq 'CL'){
186 my $amount2=$amount*-1;
187 $amountleft=fixcredit(\%env,$bornum,$amount2,$itemnum,$type,$user);
192 if ($type eq 'L' && $desc eq ''){
196 $amountleft=refund('',$bornum,$amount);
199 my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
201 my $data=$sth->fetchrow_hashref;
204 $desc=$dbh->quote($desc);
205 $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
206 values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft','$data->{'itemnumber'}')";
208 $desc=$dbh->quote($desc);
209 $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
210 values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft')";
213 my $sth=$dbh->prepare($insert);
221 #here we update both the accountoffsets and the account lines
222 my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
227 my $amountleft = $data;
229 my $item=getiteminformation($env,'',$barcode);
230 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
231 my $query="Select * from accountlines where (borrowernumber='$bornumber'
232 and itemnumber='$item->{'itemnumber'}' and amountoutstanding > 0)";
234 $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
235 } elsif ($type eq 'CF'){
236 $query.=" and (accounttype = 'F' or accounttype = 'FU' or
237 accounttype='Res' or accounttype='Rent')";
238 } elsif ($type eq 'CB'){
239 $query.=" and accounttype='A'";
242 my $sth=$dbh->prepare($query);
244 $accdata=$sth->fetchrow_hashref;
246 if ($accdata->{'amountoutstanding'} < $amountleft) {
248 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
250 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
253 my $thisacct = $accdata->{accountno};
254 my $updquery = "update accountlines set amountoutstanding= '$newamtos'
255 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
256 my $usth = $dbh->prepare($updquery);
259 $updquery = "insert into accountoffsets
260 (borrowernumber, accountno, offsetaccount, offsetamount)
261 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
262 $usth = $dbh->prepare($updquery);
267 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
268 # get lines with outstanding amounts to offset
269 my $query = "select * from accountlines
270 where (borrowernumber = '$bornumber') and (amountoutstanding >0)
272 my $sth = $dbh->prepare($query);
275 # offset transactions
276 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
277 if ($accdata->{'amountoutstanding'} < $amountleft) {
279 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
281 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
284 my $thisacct = $accdata->{accountno};
285 $updquery = "update accountlines set amountoutstanding= '$newamtos'
286 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
287 my $usth = $dbh->prepare($updquery);
290 $updquery = "insert into accountoffsets
291 (borrowernumber, accountno, offsetaccount, offsetamount)
292 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
293 $usth = $dbh->prepare($updquery);
299 $env->{'branch'}=$user;
300 $type="Credit ".$type;
301 UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
308 #here we update both the accountoffsets and the account lines
309 my ($env,$bornumber,$data)=@_;
314 # my $branch=$env->{'branchcode'};
315 my $amountleft = $data *-1;
318 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
319 # get lines with outstanding amounts to offset
320 my $query = "select * from accountlines
321 where (borrowernumber = '$bornumber') and (amountoutstanding<0)
323 my $sth = $dbh->prepare($query);
327 # offset transactions
328 while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
329 if ($accdata->{'amountoutstanding'} > $amountleft) {
331 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
333 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
337 my $thisacct = $accdata->{accountno};
338 $updquery = "update accountlines set amountoutstanding= '$newamtos'
339 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
340 my $usth = $dbh->prepare($updquery);
343 $updquery = "insert into accountoffsets
344 (borrowernumber, accountno, offsetaccount, offsetamount)
345 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
346 $usth = $dbh->prepare($updquery);
354 END { } # module clean-up code here (global destructor)