1 package FS::Cron::breakage;
5 use vars qw( @EXPORT_OK );
7 use FS::Record qw(qsearch);
11 @EXPORT_OK = qw ( reconcile_breakage );
14 # -v: enable debugging
17 sub reconcile_breakage {
20 my $conf = new FS::Conf;
22 foreach my $agent (qsearch('agent', {})) {
24 my $days = $conf->config('breakage-days', $agent->agentnum)
27 my $since = int( $^T - ($days * 86400) );
29 warn 'searching '. $agent->agent. " for customers with unapplied payments more than $days days old\n"
32 #find customers w/negative balance older than $days (and no activity since)
33 # and no activity (invoices/payments/credits/refunds) newer than $since
34 # (XXX except antother breakage invoice???)
37 ' AND 0 > '. FS::cust_main->balance_sql.
38 ' AND '. join(' AND ', map {
41 WHERE $_.custnum = cust_main.custnum
44 } qw( cust_bill cust_pay cust_credit cust_refund )
47 my @customers = qsearch({
48 'table' => 'cust_main',
49 'hashref' => { 'agentnum' => $agent->agentnum,
50 'payby' => { op=>'!=', value=>'COMP', },
52 'extra_sql' => $extra_sql,
55 #and then create a "breakage" charge & invoice for them
57 foreach my $cust_main ( @customers ) {
59 warn 'reconciling breakage for customer '. $cust_main->custnum.
60 ': '. $cust_main->name. "\n"
65 'amount' => sprintf('%.2f', 0 - $cust_main->balance ),
67 'comment' => 'breakage reconciliation',
68 'classnum' => scalar($conf->config('breakage-pkg_class')),
72 || $cust_main->apply_payments_and_credits;
75 warn "error charging for breakage reconciliation: $error\n";