5 use FS::UID qw(adminsuidsetup);
6 use FS::Record qw(qsearch qsearchs);
12 my $user = shift or die &usage;
13 my $dbh = adminsuidsetup $user;
15 my $unapplied_payments_sql = <<EOF;
16 SELECT custnum FROM cust_pay WHERE paid >
17 ( ( SELECT coalesce(sum(amount),0) FROM cust_bill_pay
18 WHERE cust_pay.paynum = cust_bill_pay.paynum )
19 + ( SELECT coalesce(sum(amount),0) FROM cust_pay_refund
20 WHERE cust_pay.paynum = cust_pay_refund.paynum)
24 my $unapplied_credits_sql = <<EOF;
25 SELECT custnum FROM cust_credit WHERE cust_credit.amount >
26 ( ( SELECT coalesce(sum(cust_credit_bill.amount),0) FROM cust_credit_bill
27 WHERE cust_credit.crednum = cust_credit_bill.crednum )
28 + ( SELECT coalesce(sum(cust_Credit_refund.amount),0) FROM cust_credit_refund
29 WHERE cust_credit.crednum = cust_credit_refund.crednum)
35 my $sth = $dbh->prepare($unapplied_payments_sql) or die $dbh->errstr;
36 $sth->execute or die "unapplied payment search failed: ". $sth->errstr;
38 map { $custnum{$_->[0]} = 1 } @{ $sth->fetchall_arrayref };
40 $sth = $dbh->prepare($unapplied_credits_sql) or die $dbh->errstr;
41 $sth->execute or die "unapplied credit search failed: ". $sth->errstr;
43 map { $custnum{$_->[0]} = 1 } @{ $sth->fetchall_arrayref };
45 foreach my $custnum ( keys %custnum ) {
47 warn "processing customer $custnum\n" if $DEBUG;
49 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
50 or die "customer $custnum no longer exists!\n";
52 my $error = $cust_main->apply_payments_and_credits;
58 die "Usage:\n\n freeside-apply_payments_and_credits user\n";
63 freeside-apply_payments_and_credits - Command line interface to apply payments and credits to invoice
67 freeside-apply_payments_and_credits username
71 Finds unapplied payment and credit amounts and applies them to any outstanding
72 uncovered invoice amounts.
74 B<username> is a username added by freeside-adduser.