From 89e3b22429fb7c216a6f4755002bf50677dd9e97 Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 4 Nov 2009 01:04:35 +0000 Subject: [PATCH] reconcile breakage from stale accounts, RT#6407 --- FS/FS/Cron/breakage.pm | 63 ++++++++++++++--- httemplate/config/config-process.cgi | 130 +++++++++++++++++++---------------- httemplate/config/config-view.cgi | 23 +++++-- 3 files changed, 143 insertions(+), 73 deletions(-) diff --git a/FS/FS/Cron/breakage.pm b/FS/FS/Cron/breakage.pm index 631266794..69758f9c5 100644 --- a/FS/FS/Cron/breakage.pm +++ b/FS/FS/Cron/breakage.pm @@ -6,7 +6,7 @@ use vars qw( @EXPORT_OK ); use FS::Conf; use FS::Record qw(qsearch); use FS::agent; -#use FS::cust_main; +use FS::cust_main; @EXPORT_OK = qw ( reconcile_breakage ); @@ -15,8 +15,7 @@ use FS::agent; # -l: debugging level sub reconcile_breakage { - return; - #nothing yet + my %opt = @_; my $conf = new FS::Conf; @@ -25,14 +24,58 @@ sub reconcile_breakage { my $days = $conf->config('breakage-days', $agent->agentnum) or next; - #find customers w/a balance older than $days (and no activity since) + my $since = int( $^T - ($days * 86400) ); - # - do a one time charge in the total amount of old unapplied payments. - # 'pkg' => 'Breakage', #or whatever. - # 'setuptax' => 'Y', - # 'classnum' => scalar($conf->config('breakage-pkg_class')), - # - use the new $cust_main->charge( 'bill_now' => 1 ) option to generate an invoice, etc. - # - apply_payments_and_credits + warn 'searching '. $agent->agent. " for customers with unapplied payments more than $days days old\n" + if $opt{'v'}; + + #find customers w/negative balance older than $days (and no activity since) + # no invoices / payments (/credits/refunds?) newer than $since + # (except antother breakage invoice???) + + my $extra_sql = ' AND 0 > '. FS::cust_main->balance_sql; + $extra_sql .= " AND ". join(' AND ', + map {" + NOT EXISTS ( SELECT 1 FROM $_ + WHERE $_.custnum = cust_main.custnum + AND _date >= $since + ) + ";} + qw( cust_bill cust_pay ) # cust_credit cust_refund ); + ); + + my @customers = qsearch({ + 'table' => 'cust_main', + 'hashref' => { 'agentnum' => $agent->agentnum, + 'payby' => { op=>'!=', value=>'COMP', }, + }, + 'extra_sql' => $extra_sql, + }); + + #and then create a "breakage" charge & invoice for them + + foreach my $cust_main ( @customers ) { + + warn 'reconciling breakage for customer '. $cust_main->custnum. + ': '. $cust_main->name. "\n" + if $opt{'v'}; + + my $error = + $cust_main->charge({ + 'amount' => sprintf('%.2f', 0 - $cust_main->balance ), + 'pkg' => 'Breakage', + 'comment' => 'breakage reconciliation', + 'classnum' => scalar($conf->config('breakage-pkg_class')), + 'setuptax' => 'Y', + 'bill_now' => 1, + }) + || $cust_main->apply_payments_and_credits; + + if ( $error ) { + warn "error charging for breakage reconciliation: $error\n"; + } + + } } diff --git a/httemplate/config/config-process.cgi b/httemplate/config/config-process.cgi index 50db40c31..788d9016e 100644 --- a/httemplate/config/config-process.cgi +++ b/httemplate/config/config-process.cgi @@ -1,3 +1,74 @@ +<% header('Configuration set') %> + + + +<%once> +#false laziness w/config-view.cgi +my %namecol = ( + 'part_svc' => 'svc', + 'part_pkg' => 'pkg', + 'pkg_class' => 'classname', +); + <%init> die "access denied\n" unless $FS::CurrentUser::CurrentUser->access_right('Configuration'); @@ -65,62 +136,3 @@ $conf->touch($_, $agentnum) foreach @touch; $conf->delete($_, $agentnum) foreach @delete; -<% header('Configuration set') %> - - diff --git a/httemplate/config/config-view.cgi b/httemplate/config/config-view.cgi index 856a2eaef..13286cf21 100644 --- a/httemplate/config/config-view.cgi +++ b/httemplate/config/config-view.cgi @@ -210,13 +210,21 @@ Click on a configuration value to change it. % } elsif ( $type =~ /^select-(part_svc|part_pkg|pkg_class)$/ ) { +% +% my $table = $1; +% my $namecol = $namecol{$table}; +% my $pkey = dbdef->table($table)->primary_key; +% % my @keys = $conf->config($i->key, $agentnum); - <% join('
', map { $_ # ': '. $svc, $pkg, whatever - } - @keys + <% join( '
', + map { + my $key = $_; + my $record = qsearchs($table, { $pkey => $key }); + $record ? "$key: ".$record->$namecol() : $key; + } @keys ) %> @@ -301,6 +309,14 @@ Click on a configuration value to change it. +<%once> +#false laziness w/config-process.cgi +my %namecol = ( + 'part_svc' => 'svc', + 'part_pkg' => 'pkg', + 'pkg_class' => 'classname', +); + <%init> die "access denied" @@ -343,6 +359,5 @@ my @all_agents = (); if ( $cgi->param('showagent') ) { @all_agents = qsearch('agent', { 'disabled' => '' } ); } -warn 'all agents: '. join('-', @all_agents); -- 2.11.0