1 package C4::Accounts2; #asummes C4/Accounts2
3 #requires DBI.pm to be installed
12 use C4::Circulation::Circ2;
13 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
15 # set the version for version checking
19 @EXPORT = qw(&recordpayment &fixaccounts &makepayment &manualinvoice
21 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
23 # your exported package globals go here,
24 # as well as any optionally exported functions
26 @EXPORT_OK = qw($Var1 %Hashit);
29 # non-exported package globals go here
30 use vars qw(@more $stuff);
32 # initalize package globals, first exported ones
37 # then the others (which are still accessible as $Some::Module::stuff)
41 # all file-scoped lexicals must be created before
42 # the functions below that use them.
44 # file-private lexicals go here
48 # here's a file-private function as a closure,
49 # callable as &$priv_func; it cannot be prototyped.
54 # make all your functions, whether exported or not;
61 #here we update both the accountoffsets and the account lines
62 my ($env,$bornumber,$data)=@_;
67 my $branch=$env->{'branchcode'};
68 my $amountleft = $data;
70 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
71 # get lines with outstanding amounts to offset
72 my $query = "select * from accountlines
73 where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
75 my $sth = $dbh->prepare($query);
78 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
79 if ($accdata->{'amountoutstanding'} < $amountleft) {
81 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
83 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
86 my $thisacct = $accdata->{accountno};
87 $updquery = "update accountlines set amountoutstanding= '$newamtos'
88 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
89 my $usth = $dbh->prepare($updquery);
92 $updquery = "insert into accountoffsets
93 (borrowernumber, accountno, offsetaccount, offsetamount)
94 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
95 my $usth = $dbh->prepare($updquery);
100 $updquery = "insert into accountlines
101 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
102 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
103 'Pay',0-$amountleft)";
104 my $usth = $dbh->prepare($updquery);
107 UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
113 #here we update both the accountoffsets and the account lines
114 #updated to check, if they are paying off a lost item, we return the item
115 # from their card, and put a note on the item record
116 my ($bornumber,$accountno,$amount,$user)=@_;
120 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
122 my $sel="Select * from accountlines where borrowernumber=$bornumber and
123 accountno=$accountno";
124 my $sth=$dbh->prepare($sel);
126 my $data=$sth->fetchrow_hashref;
128 my $updquery="Update accountlines set amountoutstanding=0 where
129 borrowernumber=$bornumber and accountno=$accountno";
130 $sth=$dbh->prepare($updquery);
134 $updquery = "insert into accountoffsets
135 (borrowernumber, accountno, offsetaccount, offsetamount)
136 values ($bornumber,$accountno,$nextaccntno,$newamtos)";
137 my $usth = $dbh->prepare($updquery);
141 my $payment=0-$amount;
142 $updquery = "insert into accountlines
143 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
144 values ($bornumber,$nextaccntno,now(),$payment,'Payment,thanks - $user', 'Pay',0)";
145 my $usth = $dbh->prepare($updquery);
148 UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
151 #check to see what accounttype
152 if ($data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L'){
153 returnlost($bornumber,$data->{'itemnumber'});
158 my ($env,$bornumber,$dbh)=@_;
160 my $query = "select * from accountlines
161 where (borrowernumber = '$bornumber')
162 order by accountno desc";
163 my $sth = $dbh->prepare($query);
165 if (my $accdata=$sth->fetchrow_hashref){
166 $nextaccntno = $accdata->{'accountno'} + 1;
169 return($nextaccntno);
173 my ($borrowernumber,$accountno,$amount)=@_;
175 my $query="Select * from accountlines where borrowernumber=$borrowernumber
176 and accountno=$accountno";
177 my $sth=$dbh->prepare($query);
179 my $data=$sth->fetchrow_hashref;
180 my $diff=$amount-$data->{'amount'};
181 my $outstanding=$data->{'amountoutstanding'}+$diff;
183 $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
184 borrowernumber=$borrowernumber and accountno=$accountno";
185 $sth=$dbh->prepare($query);
193 my ($borrnum,$itemnum)=@_;
195 my $borrower=borrdata('',$borrnum); #from C4::Search;
196 my $upiss="Update issues set returndate=now() where
197 borrowernumber='$borrnum' and itemnumber='$itemnum' and returndate is null";
198 my $sth=$dbh->prepare($upiss);
201 my @datearr = localtime(time);
202 my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
203 my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
204 my $upitem="Update items set paidfor='Paid for by $bor $date' where itemnumber='$itemnum'";
205 $sth=$dbh->prepare($upitem);
212 my ($bornum,$itemnum,$desc,$type,$amount,$user)=@_;
217 my $accountno=getnextacctno('',$bornum,$dbh);
218 my $amountleft=$amount;
220 if ($type eq 'CS' || $type eq 'CB' || $type eq 'CW'
221 || $type eq 'CF' || $type eq 'CL'){
222 my $amount2=$amount*-1;
223 $amountleft=fixcredit(\%env,$bornum,$amount2,$itemnum,$type,$user);
228 if ($type eq 'L' && $desc eq ''){
232 $amountleft=refund('',$bornum,$amount);
235 my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
237 my $data=$sth->fetchrow_hashref;
240 $desc=$dbh->quote($desc);
241 $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding,itemnumber)
242 values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft','$data->{'itemnumber'}')";
244 $desc=$dbh->quote($desc);
245 $insert="insert into accountlines (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
246 values ($bornum,$accountno,now(),'$amount',$desc,'$type','$amountleft')";
249 my $sth=$dbh->prepare($insert);
257 #here we update both the accountoffsets and the account lines
258 my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
263 my $amountleft = $data;
265 my $item=getiteminformation($env,'',$barcode);
266 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
267 my $query="Select * from accountlines where (borrowernumber='$bornumber'
268 and itemnumber='$item->{'itemnumber'}' and amountoutstanding > 0)";
270 $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
271 } elsif ($type eq 'CF'){
272 $query.=" and (accounttype = 'F' or accounttype = 'FU' or
273 accounttype='Res' or accounttype='Rent')";
274 } elsif ($type eq 'CB'){
275 $query.=" and accounttype='A'";
278 my $sth=$dbh->prepare($query);
280 $accdata=$sth->fetchrow_hashref;
282 if ($accdata->{'amountoutstanding'} < $amountleft) {
284 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
286 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
289 my $thisacct = $accdata->{accountno};
290 my $updquery = "update accountlines set amountoutstanding= '$newamtos'
291 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
292 my $usth = $dbh->prepare($updquery);
295 $updquery = "insert into accountoffsets
296 (borrowernumber, accountno, offsetaccount, offsetamount)
297 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
298 my $usth = $dbh->prepare($updquery);
303 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
304 # get lines with outstanding amounts to offset
305 my $query = "select * from accountlines
306 where (borrowernumber = '$bornumber') and (amountoutstanding >0)
308 my $sth = $dbh->prepare($query);
311 # offset transactions
312 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
313 if ($accdata->{'amountoutstanding'} < $amountleft) {
315 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
317 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
320 my $thisacct = $accdata->{accountno};
321 $updquery = "update accountlines set amountoutstanding= '$newamtos'
322 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
323 my $usth = $dbh->prepare($updquery);
326 $updquery = "insert into accountoffsets
327 (borrowernumber, accountno, offsetaccount, offsetamount)
328 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
329 my $usth = $dbh->prepare($updquery);
335 $env->{'branch'}=$user;
336 $type="Credit ".$type;
337 UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
344 #here we update both the accountoffsets and the account lines
345 my ($env,$bornumber,$data)=@_;
350 # my $branch=$env->{'branchcode'};
351 my $amountleft = $data *-1;
354 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
355 # get lines with outstanding amounts to offset
356 my $query = "select * from accountlines
357 where (borrowernumber = '$bornumber') and (amountoutstanding<0)
359 my $sth = $dbh->prepare($query);
363 # offset transactions
364 while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
365 if ($accdata->{'amountoutstanding'} > $amountleft) {
367 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
369 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
373 my $thisacct = $accdata->{accountno};
374 $updquery = "update accountlines set amountoutstanding= '$newamtos'
375 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
376 my $usth = $dbh->prepare($updquery);
379 $updquery = "insert into accountoffsets
380 (borrowernumber, accountno, offsetaccount, offsetamount)
381 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
382 my $usth = $dbh->prepare($updquery);
390 END { } # module clean-up code here (global destructor)